Core/Misc: Use std::scoped_lock instead of unique_lock where possible (and old lock_guard)

This commit is contained in:
Shauren
2025-11-30 14:25:32 +01:00
parent d3f2aee245
commit 90be8fafb3
23 changed files with 92 additions and 106 deletions

View File

@@ -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())
{