1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* 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/>.
*/
#include "MapUpdater.h"
#include "DatabaseEnv.h"
#include "LFGMgr.h"
#include "Log.h"
#include "Map.h"
#include "MapMgr.h"
#include "Metric.h"
class UpdateRequest
{
public:
UpdateRequest() = default;
virtual ~UpdateRequest() = default;
virtual void call() = 0;
};
class MapUpdateRequest : public UpdateRequest
{
public:
MapUpdateRequest(Map& m, MapUpdater& u, uint32 d, uint32 sd)
: m_map(m), m_updater(u), m_diff(d), s_diff(sd)
{
}
void call() override
{
METRIC_TIMER("map_update_time_diff", METRIC_TAG("map_id", std::to_string(m_map.GetId())));
m_map.Update(m_diff, s_diff);
m_updater.update_finished();
}
private:
Map& m_map;
MapUpdater& m_updater;
uint32 m_diff;
uint32 s_diff;
};
class MapPreloadRequest : public UpdateRequest
{
public:
MapPreloadRequest(uint32 mapId, MapUpdater& updater)
: _mapId(mapId), _updater(updater)
{
}
void call() override
{
Map* map = sMapMgr->CreateBaseMap(_mapId);
LOG_INFO("server.loading", ">> Loading All Grids For Map {} ({})", map->GetId(), map->GetMapName());
map->LoadAllGrids();
_updater.update_finished();
}
private:
uint32 _mapId;
MapUpdater& _updater;
};
class LFGUpdateRequest : public UpdateRequest
{
public:
LFGUpdateRequest(MapUpdater& u, uint32 d) : m_updater(u), m_diff(d) {}
void call() override
{
sLFGMgr->Update(m_diff, 1);
m_updater.update_finished();
}
private:
MapUpdater& m_updater;
uint32 m_diff;
};
MapUpdater::MapUpdater() : pending_requests(0), _cancelationToken(false)
{
}
void MapUpdater::activate(std::size_t num_threads)
{
_workerThreads.reserve(num_threads);
for (std::size_t i = 0; i < num_threads; ++i)
{
_workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
}
}
void MapUpdater::deactivate()
{
_cancelationToken = true;
wait(); // This is where we wait for tasks to complete
_queue.Cancel(); // Cancel the queue to prevent further task processing
// Join all worker threads
for (auto& thread : _workerThreads)
{
if (thread.joinable())
{
thread.join();
}
}
}
void MapUpdater::wait()
{
std::unique_lock<std::mutex> guard(_lock); // Guard lock for safe waiting
// Wait until there are no pending requests
_condition.wait(guard, [this] {
return pending_requests.load(std::memory_order_acquire) == 0;
});
}
void MapUpdater::schedule_task(UpdateRequest* request)
{
// Atomic increment for pending_requests
pending_requests.fetch_add(1, std::memory_order_release);
_queue.Push(request);
}
void MapUpdater::schedule_update(Map& map, uint32 diff, uint32 s_diff)
{
schedule_task(new MapUpdateRequest(map, *this, diff, s_diff));
}
void MapUpdater::schedule_map_preload(uint32 mapid)
{
schedule_task(new MapPreloadRequest(mapid, *this));
}
void MapUpdater::schedule_lfg_update(uint32 diff)
{
schedule_task(new LFGUpdateRequest(*this, diff));
}
bool MapUpdater::activated()
{
return !_workerThreads.empty();
}
void MapUpdater::update_finished()
{
// Atomic decrement for pending_requests
if (pending_requests.fetch_sub(1, std::memory_order_acquire) == 1)
{
// Only notify when pending_requests becomes 0 (i.e., all tasks are finished)
std::lock_guard<std::mutex> lock(_lock); // Lock only for condition variable notification
_condition.notify_all(); // Notify waiting threads that all requests are complete
}
}
void MapUpdater::WorkerThread()
{
LoginDatabase.WarnAboutSyncQueries(true);
CharacterDatabase.WarnAboutSyncQueries(true);
WorldDatabase.WarnAboutSyncQueries(true);
while (!_cancelationToken)
{
UpdateRequest* request = nullptr;
_queue.WaitAndPop(request); // Wait for and pop a request from the queue
if (!_cancelationToken && request)
{
request->call(); // Execute the request
delete request; // Clean up after processing
}
}
}
|