TrinityCore
Loading...
Searching...
No Matches
MapUpdater.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "MapUpdater.h"
19#include "DatabaseEnv.h"
20#include "Map.h"
21#include "Metric.h"
22
23#include <mutex>
24
26{
27 private:
28
32
33 public:
34
36 : m_map(m), m_updater(u), m_diff(d)
37 {
38 }
39
40 void call()
41 {
42 TC_METRIC_TIMER("map_update_time_diff", TC_METRIC_TAG("map_id", std::to_string(m_map.GetId())));
45 }
46};
47
48void MapUpdater::activate(size_t num_threads)
49{
50 for (size_t i = 0; i < num_threads; ++i)
51 {
52 _workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
53 }
54}
55
57{
58 _cancelationToken = true;
59
60 wait();
61
62 _queue.Cancel();
63
64 for (auto& thread : _workerThreads)
65 {
66 thread.join();
67 }
68}
69
71{
72 std::unique_lock<std::mutex> lock(_lock);
73
74 while (pending_requests > 0)
75 _condition.wait(lock);
76
77 lock.unlock();
78}
79
81{
82 std::lock_guard<std::mutex> lock(_lock);
83
85
86 _queue.Push(new MapUpdateRequest(map, *this, diff));
87}
88
90{
91 return _workerThreads.size() > 0;
92}
93
95{
96 std::lock_guard<std::mutex> lock(_lock);
97
99
100 _condition.notify_all();
101}
102
104{
105 LoginDatabase.WarnAboutSyncQueries(true);
106 CharacterDatabase.WarnAboutSyncQueries(true);
107 WorldDatabase.WarnAboutSyncQueries(true);
108
109 while (true)
110 {
111 MapUpdateRequest* request = nullptr;
112
113 _queue.WaitAndPop(request);
114
116 return;
117
118 request->call();
119
120 delete request;
121 }
122}
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint32_t uint32
Definition Define.h:133
#define TC_METRIC_TAG(name, value)
Definition Metric.h:169
#define TC_METRIC_TIMER(category,...)
Definition Metric.h:212
MapUpdateRequest(Map &m, MapUpdater &u, uint32 d)
MapUpdater & m_updater
std::vector< std::thread > _workerThreads
Definition MapUpdater.h:53
std::mutex _lock
Definition MapUpdater.h:56
void schedule_update(Map &map, uint32 diff)
void activate(size_t num_threads)
void WorkerThread()
void wait()
size_t pending_requests
Definition MapUpdater.h:58
std::atomic< bool > _cancelationToken
Definition MapUpdater.h:54
bool activated()
ProducerConsumerQueue< MapUpdateRequest * > _queue
Definition MapUpdater.h:51
void deactivate()
void update_finished()
friend class MapUpdateRequest
Definition MapUpdater.h:37
std::condition_variable _condition
Definition MapUpdater.h:57
Definition Map.h:281
virtual void Update(uint32)
Definition Map.cpp:762
uint32 GetId() const
Definition Map.cpp:4216
void Push(T const &value)