Core/Maps: Use atomic wait instead of condition variable and mutex for map updater

Signed-off-by: luis <[email protected]>
This commit is contained in:
Shauren
2026-11-12 17:41:26 -03:00
committed by luis
parent 8ec47f76ab
commit 1271cc39c8
2 changed files with 14 additions and 16 deletions
+12 -11
View File
@@ -43,7 +43,7 @@ class MapUpdateRequest
}
};
MapUpdater::MapUpdater() : pending_requests(0)
MapUpdater::MapUpdater() : _pendingRequests(0)
{
}
@@ -67,16 +67,20 @@ void MapUpdater::deactivate()
void MapUpdater::wait()
{
std::unique_lock lock(_lock);
for (;;)
{
std::size_t pendingRequests = _pendingRequests.load(std::memory_order::acquire);
if (!pendingRequests)
break;
_condition.wait(lock, [&] { return pending_requests == 0; });
// wait for change
_pendingRequests.wait(pendingRequests, std::memory_order::relaxed);
}
}
void MapUpdater::schedule_update(Map& map, uint32 diff)
{
std::scoped_lock lock(_lock);
++pending_requests;
++_pendingRequests;
_queue.Push(std::make_unique<MapUpdateRequest>(map, *this, diff));
}
@@ -88,11 +92,8 @@ bool MapUpdater::activated() const
void MapUpdater::update_finished()
{
std::scoped_lock lock(_lock);
--pending_requests;
_condition.notify_all();
if (_pendingRequests.fetch_sub(1, std::memory_order::release) == 1)
_pendingRequests.notify_all();
}
void MapUpdater::WorkerThread()
+2 -5
View File
@@ -20,8 +20,7 @@
#include "Define.h"
#include "ProducerConsumerQueue.h"
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <memory>
#include <thread>
#include <vector>
@@ -54,9 +53,7 @@ class TC_GAME_API MapUpdater
std::vector<std::thread> _workerThreads;
std::mutex _lock;
std::condition_variable _condition;
size_t pending_requests;
std::atomic<std::size_t> _pendingRequests;
void update_finished();