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

@@ -20,8 +20,6 @@
#include "Map.h"
#include "Metric.h"
#include <mutex>
class MapUpdateRequest
{
private:
@@ -48,9 +46,7 @@ class MapUpdateRequest
void MapUpdater::activate(size_t num_threads)
{
for (size_t i = 0; i < num_threads; ++i)
{
_workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
}
_workerThreads.emplace_back(&MapUpdater::WorkerThread, this);
}
void MapUpdater::deactivate()
@@ -62,38 +58,33 @@ void MapUpdater::deactivate()
_queue.Cancel();
for (auto& thread : _workerThreads)
{
thread.join();
}
}
void MapUpdater::wait()
{
std::unique_lock<std::mutex> lock(_lock);
std::unique_lock lock(_lock);
while (pending_requests > 0)
_condition.wait(lock);
lock.unlock();
_condition.wait(lock, [&] { return pending_requests == 0; });
}
void MapUpdater::schedule_update(Map& map, uint32 diff)
{
std::lock_guard<std::mutex> lock(_lock);
std::scoped_lock lock(_lock);
++pending_requests;
_queue.Push(new MapUpdateRequest(map, *this, diff));
}
bool MapUpdater::activated()
bool MapUpdater::activated() const
{
return _workerThreads.size() > 0;
return !_workerThreads.empty();
}
void MapUpdater::update_finished()
{
std::lock_guard<std::mutex> lock(_lock);
std::scoped_lock lock(_lock);
--pending_requests;
@@ -107,7 +98,7 @@ void MapUpdater::WorkerThread()
WorldDatabase.WarnAboutSyncQueries(true);
HotfixDatabase.WarnAboutSyncQueries(true);
while (1)
while (true)
{
MapUpdateRequest* request = nullptr;