diff options
Diffstat (limited to 'src/common/Threading')
| -rw-r--r-- | src/common/Threading/LockedQueue.h | 18 | ||||
| -rw-r--r-- | src/common/Threading/ProducerConsumerQueue.h | 21 |
2 files changed, 18 insertions, 21 deletions
diff --git a/src/common/Threading/LockedQueue.h b/src/common/Threading/LockedQueue.h index 25666db9358..4a97da0c726 100644 --- a/src/common/Threading/LockedQueue.h +++ b/src/common/Threading/LockedQueue.h @@ -39,14 +39,14 @@ public: //! Adds an item to the queue. void add(T const& item) { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); _queue.push_back(item); } //! Adds an item to the queue. void add(T&& item) { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); _queue.push_back(std::move(item)); } @@ -54,14 +54,14 @@ public: template<std::input_iterator Iterator> void readd(Iterator begin, Iterator end) { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); _queue.insert(_queue.begin(), begin, end); } //! Gets the next result in the queue, if any. bool next(T& result) { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); if (_queue.empty()) return false; @@ -73,7 +73,7 @@ public: template<class Checker> bool next(T& result, Checker& check) { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); if (_queue.empty()) return false; @@ -89,28 +89,28 @@ public: //! Cancels the queue. void cancel() { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); _canceled = true; } //! Checks if the queue is cancelled. bool cancelled() { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); return _canceled; } ///! Calls pop_front of the queue void pop_front() { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); _queue.pop_front(); } ///! Checks if we're empty or not with locks held bool empty() { - std::lock_guard<std::mutex> lock(_lock); + std::scoped_lock lock(_lock); return _queue.empty(); } }; diff --git a/src/common/Threading/ProducerConsumerQueue.h b/src/common/Threading/ProducerConsumerQueue.h index 04c2017c1f7..7efa24cb78c 100644 --- a/src/common/Threading/ProducerConsumerQueue.h +++ b/src/common/Threading/ProducerConsumerQueue.h @@ -18,10 +18,10 @@ #ifndef TRINITY_PRODUCER_CONSUMER_QUEUE_H #define TRINITY_PRODUCER_CONSUMER_QUEUE_H +#include <atomic> #include <condition_variable> #include <mutex> #include <queue> -#include <atomic> #include <type_traits> template <typename T> @@ -39,7 +39,7 @@ public: void Push(T const& value) { - std::lock_guard<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); _queue.push(value); _condition.notify_one(); @@ -47,7 +47,7 @@ public: void Push(T&& value) { - std::lock_guard<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); _queue.push(std::move(value)); _condition.notify_one(); @@ -55,21 +55,21 @@ public: bool Empty() const { - std::lock_guard<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); return _queue.empty(); } size_t Size() const { - std::lock_guard<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); return _queue.size(); } bool Pop(T& value) { - std::lock_guard<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); if (_queue.empty() || _shutdown) return false; @@ -83,12 +83,9 @@ public: void WaitAndPop(T& value) { - std::unique_lock<std::mutex> lock(_queueLock); + std::unique_lock lock(_queueLock); - // we could be using .wait(lock, predicate) overload here but it is broken - // https://connect.microsoft.com/VisualStudio/feedback/details/1098841 - while (_queue.empty() && !_shutdown) - _condition.wait(lock); + _condition.wait(lock, [&] { return !_queue.empty() || _shutdown; }); if (_queue.empty() || _shutdown) return; @@ -100,7 +97,7 @@ public: void Cancel() { - std::unique_lock<std::mutex> lock(_queueLock); + std::scoped_lock lock(_queueLock); while (!_queue.empty()) { |
