From e871b449151673483c28c947ca095bac8a80149e Mon Sep 17 00:00:00 2001 From: Karrz1 <127596890+Karrz1@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:45:14 +0000 Subject: [PATCH] World: check for code running on the world thread Modules that start their own work (bots) need to know whether they may touch world-thread-only state now or must queue it for the next world tick. The world thread id is recorded at startup and on each world tick and compared against the caller. No behaviour change. --- src/server/game/World/World.cpp | 16 ++++++++++++++++ src/server/game/World/World.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index d61ab97b45..e3de2e1d1b 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -114,12 +114,24 @@ #include "WorldTickStats.h" #include #include +#include TC_GAME_API std::atomic World::m_stopEvent(false); TC_GAME_API uint8 World::m_ExitCode = SHUTDOWN_EXIT_CODE; TC_GAME_API std::atomic World::m_worldLoopCounter(0); +namespace +{ + // Set at startup and on every world tick; read by World::IsInWorldThread from any thread. + std::atomic WorldThreadId{}; +} + +bool World::IsInWorldThread() +{ + return WorldThreadId.load(std::memory_order_relaxed) == std::this_thread::get_id(); +} + struct PersistentWorldVariable { std::string Id; @@ -1338,6 +1350,8 @@ void World::LoadConfigSettings(bool reload) /// Initialize the World bool World::SetInitialWorldSettings() { + WorldThreadId.store(std::this_thread::get_id(), std::memory_order_relaxed); + sLog->SetRealmId(sRealmList->GetCurrentRealmId().Realm); ///- Server startup begin @@ -2274,6 +2288,8 @@ namespace /// Update the World ! void World::Update(uint32 diff) { + WorldThreadId.store(std::this_thread::get_id(), std::memory_order_relaxed); + TC_METRIC_TIMER("world_update_time_total"); bool const tickStats = Trinity::WorldTickStats::Enabled(); Trinity::WorldTickStats::Clock::time_point const tickStart = tickStats ? Trinity::WorldTickStats::Clock::now() diff --git a/src/server/game/World/World.h b/src/server/game/World/World.h index 26f663552b..d55225d0b7 100644 --- a/src/server/game/World/World.h +++ b/src/server/game/World/World.h @@ -603,6 +603,11 @@ class TC_GAME_API World static std::atomic m_worldLoopCounter; + // True when called on the thread that runs startup and World::Update (the world thread), + // false on map update threads, network threads and before startup. Lets module code check + // that it may touch world-thread-only state, or queue the work for the next world tick. + static bool IsInWorldThread(); + WorldSession* FindSession(uint32 id) const; void AddSession(WorldSession* s); void AddInstanceSocket(std::weak_ptr sock, uint64 connectToKey);