Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
MapUpdater.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "DatabaseEnv.h"
7#include "DelayExecutor.h"
8#include "Log.h"
9#include "Map.h"
10#include "MapUpdater.h"
11#include "RuntimeMetrics.h"
12#include "Util.h"
13
15{
16public:
18
19 virtual int call() { return 0; }
20};
21
23{
24public:
26
27 virtual int call() { return 0; }
28};
29
31{
32private:
36
37public:
39 : m_map(m), m_updater(u), m_diff(d) { }
40
41 virtual int call()
42 {
43 m_map.Update(m_diff);
44 m_updater.update_finished();
45 return 0;
46 }
47};
48
51
56
57int MapUpdater::activate(size_t num_threads)
58{
59 return m_executor.start((int)num_threads, std::unique_ptr<DelayTask>(new WDBThreadStartReq1), std::unique_ptr<DelayTask>(new WDBThreadEndReq1));
60}
61
63{
64 wait();
65 return m_executor.deactivate();
66}
67
69{
70 uint32 const waitStart = getMSTime();
71 bool waited = false;
72 std::unique_lock<std::mutex> ulock(Lock);
73
74 while (pending_requests > 0)
75 {
76 waited = true;
77 condition.wait(ulock);
78 }
79
80 ulock.unlock();
81
82 if (waited)
84
85 return 0;
86}
87
89{
90 std::lock_guard<std::mutex> guard(Lock);
91
94
95 if (m_executor.execute(std::unique_ptr<DelayTask>(new MapUpdateRequest(map, *this, diff))) == -1)
96 {
97 SF_LOG_ERROR("misc", "Failed to schedule Map Update");
98
101 return -1;
102 }
103
104 return 0;
105}
106
108{
109 return m_executor.activated();
110}
111
113{
114 std::lock_guard<std::mutex> guard(Lock);
115
116 if (pending_requests == 0)
117 {
118 SF_LOG_ERROR("misc", "MapUpdater::update_finished BUG, report to devs");
119 return;
120 }
121
124
125 condition.notify_all();
126}
std::uint32_t uint32
Definition Define.h:77
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
uint32 getMSTime()
Definition Timer.h:12
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition Timer.h:17
Definition Map.h:238
MapUpdateRequest(Map &m, MapUpdater &u, uint32 d)
virtual int call()
MapUpdater & m_updater
int schedule_update(Map &map, uint32 diff)
virtual ~MapUpdater()
int activate(size_t num_threads)
std::condition_variable condition
Definition MapUpdater.h:35
size_t pending_requests
Definition MapUpdater.h:36
bool activated()
std::mutex Lock
Definition MapUpdater.h:34
void update_finished()
friend class MapUpdateRequest
Definition MapUpdater.h:22
DelayExecutor m_executor
Definition MapUpdater.h:33
int deactivate()
void RecordMapUpdateScheduleFailed(uint32 pendingRequests)
void RecordMapUpdateScheduled(uint32 pendingRequests)
void RecordMapUpdateCompleted(uint32 pendingRequests)
virtual int call()
virtual int call()
RuntimeMetrics & GetRuntimeMetrics()