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.
This commit is contained in:
Karrz1
2026-11-19 22:11:57 +00:00
parent 25fc7ca3a6
commit e871b44915
2 changed files with 21 additions and 0 deletions
+16
View File
@@ -114,12 +114,24 @@
#include "WorldTickStats.h"
#include <zlib.h>
#include <filesystem>
#include <thread>
TC_GAME_API std::atomic<bool> World::m_stopEvent(false);
TC_GAME_API uint8 World::m_ExitCode = SHUTDOWN_EXIT_CODE;
TC_GAME_API std::atomic<uint32> World::m_worldLoopCounter(0);
namespace
{
// Set at startup and on every world tick; read by World::IsInWorldThread from any thread.
std::atomic<std::thread::id> 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()
+5
View File
@@ -603,6 +603,11 @@ class TC_GAME_API World
static std::atomic<uint32> 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<WorldSocket> sock, uint64 connectToKey);