mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-15 18:10:26 +00:00
feat(Core/MapUpdate): switch from ACE_Method_Request to PCQ (#3459)
This commit is contained in:
@@ -1,88 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
|
||||
* Copyright (C) 2008-2020 TrinityCore <http://www.trinitycore.org/>
|
||||
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
||||
*/
|
||||
|
||||
#include "MapUpdater.h"
|
||||
#include "DelayExecutor.h"
|
||||
#include "Map.h"
|
||||
#include "DatabaseEnv.h"
|
||||
#include "LFGMgr.h"
|
||||
#include "AvgDiffTracker.h"
|
||||
|
||||
#include <ace/Guard_T.h>
|
||||
#include <ace/Method_Request.h>
|
||||
|
||||
class WDBThreadStartReq1 : public ACE_Method_Request
|
||||
class UpdateRequest
|
||||
{
|
||||
public:
|
||||
UpdateRequest() = default;
|
||||
virtual ~UpdateRequest() = default;
|
||||
|
||||
WDBThreadStartReq1()
|
||||
{
|
||||
}
|
||||
|
||||
virtual int call()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
virtual void call() = 0;
|
||||
};
|
||||
|
||||
class WDBThreadEndReq1 : public ACE_Method_Request
|
||||
class MapUpdateRequest : public UpdateRequest
|
||||
{
|
||||
public:
|
||||
|
||||
WDBThreadEndReq1()
|
||||
{
|
||||
}
|
||||
|
||||
virtual int call()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
class MapUpdateRequest : public ACE_Method_Request
|
||||
{
|
||||
private:
|
||||
|
||||
Map& m_map;
|
||||
MapUpdater& m_updater;
|
||||
uint32 m_diff;
|
||||
uint32 s_diff;
|
||||
|
||||
public:
|
||||
|
||||
MapUpdateRequest(Map& m, MapUpdater& u, uint32 d, uint32 sd)
|
||||
: m_map(m), m_updater(u), m_diff(d), s_diff(sd)
|
||||
{
|
||||
}
|
||||
|
||||
virtual int call()
|
||||
void call() override
|
||||
{
|
||||
m_map.Update (m_diff, s_diff);
|
||||
m_map.Update(m_diff, s_diff);
|
||||
m_updater.update_finished();
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
class LFGUpdateRequest : public ACE_Method_Request
|
||||
{
|
||||
private:
|
||||
|
||||
Map& m_map;
|
||||
MapUpdater& m_updater;
|
||||
uint32 m_diff;
|
||||
uint32 s_diff;
|
||||
};
|
||||
|
||||
class LFGUpdateRequest : public UpdateRequest
|
||||
{
|
||||
public:
|
||||
LFGUpdateRequest(MapUpdater& u, uint32 d) : m_updater(u), m_diff(d) {}
|
||||
|
||||
virtual int call()
|
||||
void call()
|
||||
{
|
||||
uint32 startTime = getMSTime();
|
||||
sLFGMgr->Update(m_diff, 1);
|
||||
uint32 totalTime = getMSTimeDiff(startTime, getMSTime());
|
||||
lfgDiffTracker.Update(totalTime);
|
||||
m_updater.update_finished();
|
||||
return 0;
|
||||
}
|
||||
private:
|
||||
MapUpdater& m_updater;
|
||||
uint32 m_diff;
|
||||
};
|
||||
|
||||
MapUpdater::MapUpdater():
|
||||
m_executor(), m_mutex(), m_condition(m_mutex), pending_requests(0)
|
||||
MapUpdater::MapUpdater(): pending_requests(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -91,80 +65,82 @@ MapUpdater::~MapUpdater()
|
||||
deactivate();
|
||||
}
|
||||
|
||||
int MapUpdater::activate(size_t num_threads)
|
||||
void MapUpdater::activate(size_t num_threads)
|
||||
{
|
||||
return m_executor.start((int)num_threads, new WDBThreadStartReq1, new WDBThreadEndReq1);
|
||||
for (size_t i = 0; i < num_threads; ++i)
|
||||
{
|
||||
_workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
|
||||
}
|
||||
}
|
||||
|
||||
int MapUpdater::deactivate()
|
||||
void MapUpdater::deactivate()
|
||||
{
|
||||
_cancelationToken = true;
|
||||
|
||||
wait();
|
||||
|
||||
return m_executor.deactivate();
|
||||
_queue.Cancel();
|
||||
|
||||
for (auto& thread : _workerThreads)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
int MapUpdater::wait()
|
||||
void MapUpdater::wait()
|
||||
{
|
||||
ACORE_GUARD(ACE_Thread_Mutex, m_mutex);
|
||||
std::unique_lock<std::mutex> guard(_lock);
|
||||
|
||||
while (pending_requests > 0)
|
||||
m_condition.wait();
|
||||
_condition.wait(guard);
|
||||
|
||||
return 0;
|
||||
guard.unlock();
|
||||
}
|
||||
|
||||
int MapUpdater::schedule_update(Map& map, uint32 diff, uint32 s_diff)
|
||||
void MapUpdater::schedule_update(Map& map, uint32 diff, uint32 s_diff)
|
||||
{
|
||||
ACORE_GUARD(ACE_Thread_Mutex, m_mutex);
|
||||
std::lock_guard<std::mutex> guard(_lock);
|
||||
|
||||
++pending_requests;
|
||||
|
||||
if (m_executor.execute(new MapUpdateRequest(map, *this, diff, s_diff)) == -1)
|
||||
{
|
||||
ACE_DEBUG((LM_ERROR, ACE_TEXT("(%t) \n"), ACE_TEXT("Failed to schedule Map Update")));
|
||||
|
||||
--pending_requests;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
_queue.Push(new MapUpdateRequest(map, *this, diff, s_diff));
|
||||
}
|
||||
|
||||
int MapUpdater::schedule_lfg_update(uint32 diff)
|
||||
void MapUpdater::schedule_lfg_update(uint32 diff)
|
||||
{
|
||||
ACORE_GUARD(ACE_Thread_Mutex, m_mutex);
|
||||
std::lock_guard<std::mutex> guard(_lock);
|
||||
|
||||
++pending_requests;
|
||||
|
||||
if (m_executor.execute(new LFGUpdateRequest(*this, diff)) == -1)
|
||||
{
|
||||
ACE_DEBUG((LM_ERROR, ACE_TEXT("(%t) \n"), ACE_TEXT("Failed to schedule LFG Update")));
|
||||
|
||||
--pending_requests;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
_queue.Push(new LFGUpdateRequest(*this, diff));
|
||||
}
|
||||
|
||||
bool MapUpdater::activated()
|
||||
{
|
||||
return m_executor.activated();
|
||||
return _workerThreads.size() > 0;
|
||||
}
|
||||
|
||||
void MapUpdater::update_finished()
|
||||
{
|
||||
ACORE_GUARD(ACE_Thread_Mutex, m_mutex);
|
||||
|
||||
if (pending_requests == 0)
|
||||
{
|
||||
ACE_ERROR((LM_ERROR, ACE_TEXT("(%t)\n"), ACE_TEXT("MapUpdater::update_finished BUG, report to devs")));
|
||||
sLog->outMisc("WOOT! pending_requests == 0 before decrement!");
|
||||
m_condition.broadcast();
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
|
||||
--pending_requests;
|
||||
|
||||
m_condition.broadcast();
|
||||
_condition.notify_all();
|
||||
}
|
||||
|
||||
void MapUpdater::WorkerThread()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
UpdateRequest* request = nullptr;
|
||||
|
||||
_queue.WaitAndPop(request);
|
||||
if (_cancelationToken)
|
||||
return;
|
||||
|
||||
request->call();
|
||||
|
||||
delete request;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user