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
24{
25 private:
26
30
31 public:
32
34 : m_map(m), m_updater(u), m_diff(d)
35 {
36 }
37
38 void call()
39 {
40 TC_METRIC_TIMER("map_update_time_diff", TC_METRIC_TAG("map_id", std::to_string(m_map.GetId())));
43 }
44};
45
46void MapUpdater::activate(size_t num_threads)
47{
48 for (size_t i = 0; i < num_threads; ++i)
49 _workerThreads.emplace_back(&MapUpdater::WorkerThread, this);
50}
51
53{
54 _cancelationToken = true;
55
56 wait();
57
58 _queue.Cancel();
59
60 for (auto& thread : _workerThreads)
61 thread.join();
62}
63
65{
66 std::unique_lock lock(_lock);
67
68 _condition.wait(lock, [&] { return pending_requests == 0; });
69}
70
72{
73 std::scoped_lock lock(_lock);
74
76
77 _queue.Push(new MapUpdateRequest(map, *this, diff));
78}
79
81{
82 return !_workerThreads.empty();
83}
84
86{
87 std::scoped_lock lock(_lock);
88
90
91 _condition.notify_all();
92}
93
95{
96 LoginDatabase.WarnAboutSyncQueries(true);
97 CharacterDatabase.WarnAboutSyncQueries(true);
98 WorldDatabase.WarnAboutSyncQueries(true);
99 HotfixDatabase.WarnAboutSyncQueries(true);
100
101 while (true)
102 {
103 MapUpdateRequest* request = nullptr;
104
105 _queue.WaitAndPop(request);
106
108 return;
109
110 request->call();
111
112 delete request;
113 }
114}
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
DatabaseWorkerPool< HotfixDatabaseConnection > HotfixDatabase
Accessor to the hotfix database.
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint32_t uint32
Definition Define.h:154
#define TC_METRIC_TAG(name, value)
Definition Metric.h:187
#define TC_METRIC_TIMER(category,...)
Definition Metric.h:221
MapUpdateRequest(Map &m, MapUpdater &u, uint32 d)
MapUpdater & m_updater
std::vector< std::thread > _workerThreads
Definition MapUpdater.h:54
std::mutex _lock
Definition MapUpdater.h:57
void schedule_update(Map &map, uint32 diff)
void activate(size_t num_threads)
void WorkerThread()
void wait()
bool activated() const
size_t pending_requests
Definition MapUpdater.h:59
std::atomic< bool > _cancelationToken
Definition MapUpdater.h:55
ProducerConsumerQueue< MapUpdateRequest * > _queue
Definition MapUpdater.h:52
void deactivate()
void update_finished()
friend class MapUpdateRequest
Definition MapUpdater.h:38
std::condition_variable _condition
Definition MapUpdater.h:58
Definition Map.h:225
virtual void Update(uint32)
Definition Map.cpp:653
uint32 GetId() const
Definition Map.cpp:3257
void Push(T const &value)