Skip to content

Commit

Permalink
Guard async packets processing behind mutex.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratkosrb committed Sep 28, 2024
1 parent b829db5 commit d1f0c11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/game/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,16 +1941,16 @@ void World::DetectDBCLang()
// Only processes packets while session update, the messager, and cli commands processing are NOT running
void World::ProcessAsyncPackets()
{
while (!IsStopped())
do
{
do
{
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::lock_guard<std::mutex> lock(m_asyncPacketsMutex);

if (IsStopped())
return;
if (IsStopped())
return;

} while (!m_canProcessAsyncPackets);
if (!m_canProcessAsyncPackets)
continue;

for (auto const& itr : m_sessions)
{
Expand All @@ -1963,7 +1963,7 @@ void World::ProcessAsyncPackets()
if (!m_canProcessAsyncPackets)
break;
}
}
} while (!IsStopped());
}

// Update the World !
Expand Down Expand Up @@ -1998,16 +1998,21 @@ void World::Update(uint32 diff)
sAuctionMgr.Update();
}

GetMessager().Execute(this);
{
m_canProcessAsyncPackets = false;
std::lock_guard<std::mutex> lock(m_asyncPacketsMutex);

GetMessager().Execute(this);

// <li> Handle session updates
uint32 updateSessionsTime = WorldTimer::getMSTime();
UpdateSessions(diff);
updateSessionsTime = WorldTimer::getMSTimeDiffToNow(updateSessionsTime);
if (getConfig(CONFIG_UINT32_PERFLOG_SLOW_SESSIONS_UPDATE) && updateSessionsTime > getConfig(CONFIG_UINT32_PERFLOG_SLOW_SESSIONS_UPDATE))
sLog.Out(LOG_PERFORMANCE, LOG_LVL_MINIMAL, "Update sessions: %ums", updateSessionsTime);
// <li> Handle session updates
uint32 updateSessionsTime = WorldTimer::getMSTime();
UpdateSessions(diff);
updateSessionsTime = WorldTimer::getMSTimeDiffToNow(updateSessionsTime);
if (getConfig(CONFIG_UINT32_PERFLOG_SLOW_SESSIONS_UPDATE) && updateSessionsTime > getConfig(CONFIG_UINT32_PERFLOG_SLOW_SESSIONS_UPDATE))
sLog.Out(LOG_PERFORMANCE, LOG_LVL_MINIMAL, "Update sessions: %ums", updateSessionsTime);

m_canProcessAsyncPackets = true;
m_canProcessAsyncPackets = true;
}

// <li> Update uptime table
if (m_timers[WUPDATE_UPTIME].Passed())
Expand Down Expand Up @@ -2117,10 +2122,15 @@ void World::Update(uint32 diff)
// Update ban list if necessary
sAccountMgr.Update(diff);

m_canProcessAsyncPackets = false;
{
m_canProcessAsyncPackets = false;
std::lock_guard<std::mutex> lock(m_asyncPacketsMutex);

// And last, but not least handle the issued cli commands
ProcessCliCommands();
// And last, but not least handle the issued cli commands
ProcessCliCommands();

m_canProcessAsyncPackets = true;
}

//cleanup unused GridMap objects as well as VMaps
if (getConfig(CONFIG_BOOL_CLEANUP_TERRAIN))
Expand Down
1 change: 1 addition & 0 deletions src/game/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ class World

// This thread handles packets while the world sessions update is not running
std::unique_ptr<std::thread> m_asyncPacketsThread;
std::mutex m_asyncPacketsMutex;
bool m_canProcessAsyncPackets;
void ProcessAsyncPackets();

Expand Down

0 comments on commit d1f0c11

Please sign in to comment.