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);