mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: Use std::scoped_lock instead of unique_lock where possible (and old lock_guard)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user