diff options
author | Shauren <shauren.trinity@gmail.com> | 2022-12-20 01:03:07 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-08-12 14:24:29 +0200 |
commit | 203bc3afa0d234177959ebdc896a401bdcf2da4c (patch) | |
tree | 52cb3d9f8cd378037485134ce37b1c0eb6bf310a | |
parent | b7287e85e4bc8acb2b95271ece9dd8a5b93873cd (diff) |
Core/Threading: Modernize ProducerConsumerQueue a bit to fix GCC build
Closes #28607
(cherry picked from commit 85d5f4bc0683d99cfaab244a8f0355b463f93267)
-rw-r--r-- | src/common/Threading/ProducerConsumerQueue.h | 36 |
1 files changed, 20 insertions, 16 deletions
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 <http://www.gnu.org/licenses/>. */ -#ifndef _PCQ_H -#define _PCQ_H +#ifndef TRINITY_PRODUCER_CONSUMER_QUEUE_H +#define TRINITY_PRODUCER_CONSUMER_QUEUE_H #include <condition_variable> #include <mutex> @@ -28,16 +28,24 @@ template <typename T> class ProducerConsumerQueue { private: - std::mutex _queueLock; + mutable std::mutex _queueLock; std::queue<T> _queue; std::condition_variable _condition; std::atomic<bool> _shutdown; public: - ProducerConsumerQueue<T>() : _shutdown(false) { } + ProducerConsumerQueue() : _shutdown(false) { } - void Push(const T& value) + void Push(T const& value) + { + std::lock_guard<std::mutex> lock(_queueLock); + _queue.push(value); + + _condition.notify_one(); + } + + void Push(T&& value) { std::lock_guard<std::mutex> lock(_queueLock); _queue.push(std::move(value)); @@ -45,7 +53,7 @@ public: _condition.notify_one(); } - bool Empty() + bool Empty() const { std::lock_guard<std::mutex> lock(_queueLock); @@ -54,6 +62,8 @@ public: size_t Size() const { + std::lock_guard<std::mutex> 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<T>) + delete value; _queue.pop(); } @@ -105,13 +116,6 @@ public: _condition.notify_all(); } - -private: - template<typename E = T> - typename std::enable_if<std::is_pointer<E>::value>::type DeleteQueuedObject(E& obj) { delete obj; } - - template<typename E = T> - typename std::enable_if<!std::is_pointer<E>::value>::type DeleteQueuedObject(E const& /*packet*/) { } }; -#endif +#endif // TRINITY_PRODUCER_CONSUMER_QUEUE_H |