AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
MapUpdater Class Reference

#include "MapUpdater.h"

Public Member Functions

 MapUpdater ()
 
 ~MapUpdater ()=default
 
void schedule_update (Map &map, uint32 diff, uint32 s_diff)
 
void schedule_lfg_update (uint32 diff)
 
void wait ()
 
void activate (std::size_t num_threads)
 
void deactivate ()
 
bool activated ()
 
void update_finished ()
 

Private Member Functions

void WorkerThread ()
 

Private Attributes

ProducerConsumerQueue< UpdateRequest * > _queue
 
std::vector< std::thread > _workerThreads
 
std::atomic< bool > _cancelationToken
 
std::mutex _lock
 
std::condition_variable _condition
 
std::size_t pending_requests
 

Detailed Description

Constructor & Destructor Documentation

◆ MapUpdater()

MapUpdater::MapUpdater ( )
71{
72}
std::size_t pending_requests
Definition: MapUpdater.h:54

◆ ~MapUpdater()

MapUpdater::~MapUpdater ( )
default

Member Function Documentation

◆ activate()

void MapUpdater::activate ( std::size_t  num_threads)
75{
76 _workerThreads.reserve(num_threads);
77 for (std::size_t i = 0; i < num_threads; ++i)
78 {
79 _workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
80 }
81}
std::vector< std::thread > _workerThreads
Definition: MapUpdater.h:49
void WorkerThread()
Definition: MapUpdater.cpp:142

References _workerThreads, and WorkerThread().

Referenced by MapMgr::Initialize().

◆ activated()

bool MapUpdater::activated ( )
129{
130 return _workerThreads.size() > 0;
131}

References _workerThreads.

Referenced by MapMgr::UnloadAll(), and MapMgr::Update().

◆ deactivate()

void MapUpdater::deactivate ( )
84{
85 _cancelationToken = true;
86
87 wait();
88
89 _queue.Cancel();
90
91 for (auto& thread : _workerThreads)
92 {
93 if (thread.joinable())
94 {
95 thread.join();
96 }
97 }
98}
void Cancel()
Definition: PCQueue.h:93
void wait()
Definition: MapUpdater.cpp:100
std::atomic< bool > _cancelationToken
Definition: MapUpdater.h:50
ProducerConsumerQueue< UpdateRequest * > _queue
Definition: MapUpdater.h:47

References _cancelationToken, _queue, _workerThreads, ProducerConsumerQueue< T >::Cancel(), and wait().

Referenced by MapMgr::UnloadAll().

◆ schedule_lfg_update()

void MapUpdater::schedule_lfg_update ( uint32  diff)
120{
121 std::lock_guard<std::mutex> guard(_lock);
122
124
125 _queue.Push(new LFGUpdateRequest(*this, diff));
126}
void Push(const T &value)
Definition: PCQueue.h:36
Definition: MapUpdater.cpp:56
std::mutex _lock
Definition: MapUpdater.h:52

References _lock, _queue, pending_requests, and ProducerConsumerQueue< T >::Push().

Referenced by MapMgr::Update().

◆ schedule_update()

void MapUpdater::schedule_update ( Map map,
uint32  diff,
uint32  s_diff 
)
111{
112 std::lock_guard<std::mutex> guard(_lock);
113
115
116 _queue.Push(new MapUpdateRequest(map, *this, diff, s_diff));
117}
Definition: MapUpdater.cpp:34

References _lock, _queue, pending_requests, and ProducerConsumerQueue< T >::Push().

Referenced by MapMgr::Update().

◆ update_finished()

void MapUpdater::update_finished ( )
134{
135 std::lock_guard<std::mutex> lock(_lock);
136
138
139 _condition.notify_all();
140}
std::condition_variable _condition
Definition: MapUpdater.h:53

References _condition, _lock, and pending_requests.

Referenced by MapUpdateRequest::call(), and LFGUpdateRequest::call().

◆ wait()

void MapUpdater::wait ( )
101{
102 std::unique_lock<std::mutex> guard(_lock);
103
104 while (pending_requests > 0)
105 _condition.wait(guard);
106
107 guard.unlock();
108}

References _condition, _lock, and pending_requests.

Referenced by deactivate(), and MapMgr::Update().

◆ WorkerThread()

void MapUpdater::WorkerThread ( )
private
143{
144 LoginDatabase.WarnAboutSyncQueries(true);
145 CharacterDatabase.WarnAboutSyncQueries(true);
146 WorldDatabase.WarnAboutSyncQueries(true);
147
148 while (1)
149 {
150 UpdateRequest* request = nullptr;
151
152 _queue.WaitAndPop(request);
154 return;
155
156 request->call();
157
158 delete request;
159 }
160}
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
void WaitAndPop(T &value)
Definition: PCQueue.h:72
Definition: MapUpdater.cpp:25
virtual void call()=0

References _cancelationToken, _queue, UpdateRequest::call(), CharacterDatabase, LoginDatabase, ProducerConsumerQueue< T >::WaitAndPop(), and WorldDatabase.

Referenced by activate().

Member Data Documentation

◆ _cancelationToken

std::atomic<bool> MapUpdater::_cancelationToken
private

Referenced by deactivate(), and WorkerThread().

◆ _condition

std::condition_variable MapUpdater::_condition
private

Referenced by update_finished(), and wait().

◆ _lock

std::mutex MapUpdater::_lock
private

◆ _queue

◆ _workerThreads

std::vector<std::thread> MapUpdater::_workerThreads
private

Referenced by activate(), activated(), and deactivate().

◆ pending_requests

std::size_t MapUpdater::pending_requests
private