Files
ForeverCore/src/server/game/Maps/MapUpdater.cpp
T

125 lines
2.6 KiB
C++
Raw Normal View History

2014-06-08 15:30:57 +02:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2015-04-04 13:11:32 +02:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2014-06-08 15:30:57 +02:00
2009-12-17 07:34:00 +01:00
#include "MapUpdater.h"
#include "DatabaseEnv.h"
2009-12-17 07:34:00 +01:00
#include "Map.h"
#include "Metric.h"
2009-12-17 07:34:00 +01:00
2015-07-25 17:34:41 +02:00
#include <mutex>
2009-12-17 07:34:00 +01:00
class MapUpdateRequest
2009-12-17 07:34:00 +01:00
{
private:
2009-12-17 07:34:00 +01:00
Map& m_map;
MapUpdater& m_updater;
uint32 m_diff;
2009-12-17 07:34:00 +01:00
public:
MapUpdateRequest(Map& m, MapUpdater& u, uint32 d)
: m_map(m), m_updater(u), m_diff(d)
{
}
void call()
{
TC_METRIC_TIMER("map_update_time_diff", TC_METRIC_TAG("map_id", std::to_string(m_map.GetId())));
m_map.Update (m_diff);
2012-03-29 13:42:04 +08:00
m_updater.update_finished();
}
2009-12-17 07:34:00 +01:00
};
void MapUpdater::activate(size_t num_threads)
2009-12-17 07:34:00 +01:00
{
for (size_t i = 0; i < num_threads; ++i)
{
_workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
}
2009-12-17 07:34:00 +01:00
}
void MapUpdater::deactivate()
2009-12-17 07:34:00 +01:00
{
_cancelationToken = true;
wait();
2009-12-17 07:34:00 +01:00
_queue.Cancel();
for (auto& thread : _workerThreads)
{
thread.join();
}
2009-12-17 07:34:00 +01:00
}
void MapUpdater::wait()
2009-12-17 07:34:00 +01:00
{
2014-06-08 15:30:57 +02:00
std::unique_lock<std::mutex> lock(_lock);
2009-12-17 07:34:00 +01:00
while (pending_requests > 0)
2014-06-08 15:30:57 +02:00
_condition.wait(lock);
lock.unlock();
2009-12-17 07:34:00 +01:00
}
void MapUpdater::schedule_update(Map& map, uint32 diff)
2009-12-17 07:34:00 +01:00
{
2014-06-08 15:30:57 +02:00
std::lock_guard<std::mutex> lock(_lock);
2009-12-17 07:34:00 +01:00
++pending_requests;
2009-12-17 07:34:00 +01:00
_queue.Push(new MapUpdateRequest(map, *this, diff));
2009-12-17 07:34:00 +01:00
}
bool MapUpdater::activated()
{
return _workerThreads.size() > 0;
2009-12-17 07:34:00 +01:00
}
void MapUpdater::update_finished()
{
2014-06-08 15:30:57 +02:00
std::lock_guard<std::mutex> lock(_lock);
2009-12-17 07:34:00 +01:00
--pending_requests;
2009-12-17 07:34:00 +01:00
2014-06-08 15:30:57 +02:00
_condition.notify_all();
2009-12-17 07:34:00 +01:00
}
void MapUpdater::WorkerThread()
{
LoginDatabase.WarnAboutSyncQueries(true);
CharacterDatabase.WarnAboutSyncQueries(true);
WorldDatabase.WarnAboutSyncQueries(true);
HotfixDatabase.WarnAboutSyncQueries(true);
while (1)
{
MapUpdateRequest* request = nullptr;
_queue.WaitAndPop(request);
if (_cancelationToken)
return;
request->call();
delete request;
}
}