From 203bc3afa0d234177959ebdc896a401bdcf2da4c Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 20 Dec 2022 01:03:07 +0100 Subject: Core/Threading: Modernize ProducerConsumerQueue a bit to fix GCC build Closes #28607 (cherry picked from commit 85d5f4bc0683d99cfaab244a8f0355b463f93267) --- src/common/Threading/ProducerConsumerQueue.h | 36 +++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/common/Threading/ProducerConsumerQueue.h b/src/common/Threading/ProducerConsumerQueue.h index 778f434e9de..04c2017c1f7 100644 --- a/src/common/Threading/ProducerConsumerQueue.h +++ b/src/common/Threading/ProducerConsumerQueue.h @@ -15,8 +15,8 @@ * with this program. If not, see . */ -#ifndef _PCQ_H -#define _PCQ_H +#ifndef TRINITY_PRODUCER_CONSUMER_QUEUE_H +#define TRINITY_PRODUCER_CONSUMER_QUEUE_H #include #include @@ -28,16 +28,24 @@ template class ProducerConsumerQueue { private: - std::mutex _queueLock; + mutable std::mutex _queueLock; std::queue _queue; std::condition_variable _condition; std::atomic _shutdown; public: - ProducerConsumerQueue() : _shutdown(false) { } + ProducerConsumerQueue() : _shutdown(false) { } - void Push(const T& value) + void Push(T const& value) + { + std::lock_guard lock(_queueLock); + _queue.push(value); + + _condition.notify_one(); + } + + void Push(T&& value) { std::lock_guard lock(_queueLock); _queue.push(std::move(value)); @@ -45,7 +53,7 @@ public: _condition.notify_one(); } - bool Empty() + bool Empty() const { std::lock_guard lock(_queueLock); @@ -54,6 +62,8 @@ public: size_t Size() const { + std::lock_guard lock(_queueLock); + return _queue.size(); } @@ -64,7 +74,7 @@ public: if (_queue.empty() || _shutdown) return false; - value = _queue.front(); + value = std::move(_queue.front()); _queue.pop(); @@ -96,7 +106,8 @@ public: { T& value = _queue.front(); - DeleteQueuedObject(value); + if constexpr (std::is_pointer_v) + delete value; _queue.pop(); } @@ -105,13 +116,6 @@ public: _condition.notify_all(); } - -private: - template - typename std::enable_if::value>::type DeleteQueuedObject(E& obj) { delete obj; } - - template - typename std::enable_if::value>::type DeleteQueuedObject(E const& /*packet*/) { } }; -#endif +#endif // TRINITY_PRODUCER_CONSUMER_QUEUE_H -- cgit v1.2.3