From b92bbedce7866bf3760863604e1af1e8e42db24a Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Fri, 8 Dec 2017 21:11:03 -0800 Subject: [PATCH] replace std::dequeue with UMPMCQueue in UnboundedBlockingQueue Summary: As above. Thanks magedm for giving us this beautiful piece of equipment! Reviewed By: magedm Differential Revision: D6488661 fbshipit-source-id: 95aa9646ca1ea937bb1d055e9baa037896c3161e --- .../task_queue/UnboundedBlockingQueue.h | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/folly/executors/task_queue/UnboundedBlockingQueue.h b/folly/executors/task_queue/UnboundedBlockingQueue.h index 8fc83915..95fa94c6 100644 --- a/folly/executors/task_queue/UnboundedBlockingQueue.h +++ b/folly/executors/task_queue/UnboundedBlockingQueue.h @@ -17,48 +17,37 @@ #pragma once #include +#include #include #include -#include namespace folly { -// Warning: this is effectively just a std::deque wrapped in a single mutex -// We are aiming to add a more performant concurrent unbounded queue in the -// future, but this class is available if you must have an unbounded queue -// and can tolerate any contention. template class UnboundedBlockingQueue : public BlockingQueue { public: virtual ~UnboundedBlockingQueue() {} void add(T item) override { - queue_.wlock()->push(std::move(item)); + queue_.enqueue(std::move(item)); sem_.post(); } T take() override { - while (true) { - { - auto ulockedQueue = queue_.ulock(); - if (!ulockedQueue->empty()) { - auto wlockedQueue = ulockedQueue.moveFromUpgradeToWrite(); - T item = std::move(wlockedQueue->front()); - wlockedQueue->pop(); - return item; - } - } + T item; + while (!queue_.try_dequeue(item)) { sem_.wait(); } + return item; } size_t size() override { - return queue_.rlock()->size(); + return queue_.size(); } private: LifoSem sem_; - Synchronized> queue_; + UMPMCQueue queue_; }; } // namespace folly -- 2.34.1