Skip to content

Commit

Permalink
Rework of main timer in WorldRunnable.cpp, smoother updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymphome authored and killerwife committed Sep 18, 2021
1 parent fec3721 commit 281549c
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/mangosd/WorldRunnable.cpp
Expand Up @@ -42,33 +42,32 @@ void WorldRunnable::run()
WorldDatabase.ThreadStart(); // let thread do safe mySQL requests (one connection call enough)
sWorld.InitResultQueue();

uint32 realCurrTime = 0;
uint32 realPrevTime = WorldTimer::tick();

uint32 prevSleepTime = 0; // used for balanced full tick time length near WORLD_SLEEP_CONST
uint32 diffTick = WorldTimer::tick(); // initialize world timer vars
uint32 diffTime = 0; // used to compute real time elapsed in World::Update()
uint32 overCounter = 0; // count overtime loops

///- While we have not World::m_stopEvent, update the world
while (!World::IsStopped())
{
++World::m_worldLoopCounter;
realCurrTime = WorldTimer::getMSTime();

uint32 diff = WorldTimer::tick();

sWorld.Update(diff);
realPrevTime = realCurrTime;
diffTick = WorldTimer::tick();
sWorld.Update(diffTick);
diffTime = WorldTimer::getMSTime() - WorldTimer::tickTime();

// diff (D0) include time of previous sleep (d0) + tick time (t0)
// we want that next d1 + t1 == WORLD_SLEEP_CONST
// we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
// d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
if (diff <= WORLD_SLEEP_CONST + prevSleepTime)
// we have to wait WORLD_SLEEP_CONST max between loops
// don't wait if over
if (diffTime < WORLD_SLEEP_CONST)
{
prevSleepTime = WORLD_SLEEP_CONST + prevSleepTime - diff;
MaNGOS::Thread::Sleep(prevSleepTime);
MaNGOS::Thread::Sleep(WORLD_SLEEP_CONST - diffTime);
}
#ifdef MANGOS_DEBUG
else
prevSleepTime = 0;
{
++overCounter;
sLog.outString("WorldRunnable:run Long loop #%d : %dms (total : %d loop(s), %.3f%%)", World::m_worldLoopCounter, diffTime, overCounter, (float)(100*overCounter) / (float)World::m_worldLoopCounter);
}
#endif

#ifdef _WIN32
if (m_ServiceStatus == 0) World::StopNow(SHUTDOWN_EXIT_CODE);
Expand Down

0 comments on commit 281549c

Please sign in to comment.