-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtick.h
More file actions
379 lines (322 loc) · 13.3 KB
/
tick.h
File metadata and controls
379 lines (322 loc) · 13.3 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#pragma once
/*
Service TickService
Implementation of a class with a timer and the ability to connect timer tick handlers,
You can set different intervals for the timer and specific identifiers for each timer.
Available to set invoked UI timer and async
Implemented a helper class CTickHandlerImpl that monitors timers and simplifies working with them
Example:
struct Test : ::ext::tick::TickSubscriber
{
Test()
{
TickSubscriber::SubscribeTimer(std::chrono::minutes(5));
}
// ITickHandler
private:
/// <summary>Called when the timer ticks</summary>
/// <param name="tickParam">Parameter passed during subscription.</param>
void OnTick(::ext::tick::TickParam) noexcept override
{
... // execute text each 5 minutes
}
};
*/
#include <algorithm>
#include <chrono>
#include <map>
#include <mutex>
#include <optional>
#ifdef __AFX_H__
#include <windows.h>
#include <WinUser.h>
#endif // __AFX_H__
#include <ext/core/check.h>
#include <ext/core/singleton.h>
#include <ext/scope/defer.h>
#include <ext/thread/invoker.h>
#include <ext/thread/thread.h>
#include <ext/scope/auto_setter.h>
namespace ext::tick {
// tick parameter, passed to the tick handler
typedef LONG_PTR TickParam;
// type of clock used in the service
typedef std::chrono::steady_clock tick_clock;
// Interface for tick handlers, see TickSubscriber
struct ITickHandler
{
virtual ~ITickHandler() = default;
/// <summary>Called when the timer ticks</summary>
/// <param name="tickParam">Parameter passed during subscription.</param>
virtual void OnTick(TickParam tickParam) noexcept = 0;
};
// Tick service implementation, creates Invoker and Async timers for sending information about ticks to subscribers
// You can set the tick interval (it will work with kDefTickInterval precision)
// see TickSubscriber
class TickService
{
friend ext::Singleton<TickService>;
public:
TickService() = default;
// default tick interval
inline static const auto kDefTickInterval = std::chrono::milliseconds(200);
public:
/// <summary>Add tick handler, OnTick will be called asynhroniously.</summary>
/// <param name="handler">Handler pointer.</param>
/// <param name="tickInterval">Tick interval for this parameter.</param>
/// <param name="tickParam">Parameter passed to the handler on tick, allows you to identify the timer
/// or pass information to the handler</param>
void SubscribeAsync(ITickHandler* handler, tick_clock::duration tickInterval = kDefTickInterval, TickParam tickParam = 0)
{
m_asyncTimer.AddHandlerTimer(handler, std::move(tickInterval), std::move(tickParam));
}
/// <summary>Remove asynchronious tick handler</summary>
/// <param name="handler">Handler pointer.</param>
/// <param name="tickParam">Tick parameter, if null - delete all handler timers.</param>
void UnsubscribeAsync(ITickHandler* handler, const std::optional<TickParam>& tickParam = std::nullopt)
{
m_asyncTimer.RemoveHandler(handler, tickParam);
}
#ifdef __AFX_H__
/// <summary>Add tick handler, OnTick must be called from Invoker thread.</summary>
/// <param name="handler">Handler pointer.</param>
/// <param name="tickInterval">Tick interval for this parameter.</param>
/// <param name="tickParam">Parameter passed to the handler on tick, allows you to identify the timer
/// or pass information to the handler</param>
void SubscribeInvoked(ITickHandler* handler, tick_clock::duration tickInterval = kDefTickInterval, TickParam tickParam = 0)
{
m_invokedTimer.AddHandlerTimer(handler, std::move(tickInterval), std::move(tickParam));
}
/// <summary>Remove invoked tick handler</summary>
/// <param name="handler">Handler pointer.</param>
/// <param name="tickParam">Tick parameter, if null - delete all handler timers.</param>
void UnsubscribeInvoked(ITickHandler* handler, const std::optional<TickParam>& tickParam = std::nullopt)
{
m_invokedTimer.RemoveHandler(handler, tickParam);
}
#endif // __AFX_H__
/// <summary>Checking if this handler has a timer with the given parameter</summary>
/// <param name="handler">Handler pointer.</param>
/// <param name="tickParam">Tick parameter.</param>
[[nodiscard]] bool IsTimerExist(ITickHandler* handler, const TickParam& tickParam)
{
return
#ifdef __AFX_H__
m_invokedTimer.IsHandlerExist(handler, tickParam) ||
#endif // __AFX_H__
m_asyncTimer.IsHandlerExist(handler, tickParam);
}
private:
struct Timer
{
[[nodiscard]] bool IsHandlerExist(ITickHandler* handler, const TickParam& tickParam)
{
std::scoped_lock lock(m_handlersMutex);
return FindTickHandler(handler, tickParam) != m_handlers.end();
}
void AddHandlerTimer(ITickHandler* handler, tick_clock::duration&& tickInterval, TickParam&& tickParam)
{
std::scoped_lock lock(m_handlersMutex);
if (auto it = FindTickHandler(handler, tickParam); it != m_handlers.end())
{
it->second.tickInterval = std::move(tickInterval);
return;
}
m_handlers.emplace(std::make_pair(handler, TickHandlerInfo(std::move(tickParam), std::move(tickInterval))));
m_handlersChanged = true;
CheckTimerNecessary();
}
void RemoveHandler(ITickHandler* handler, const std::optional<TickParam>& tickParam)
{
std::scoped_lock lock(m_handlersMutex);
const auto previousCount = m_handlers.size();
if (tickParam.has_value())
{
const auto handlers = m_handlers.equal_range(handler);
for (auto it = handlers.first; it != handlers.second;)
{
if (it->second.tickParam == tickParam)
it = m_handlers.erase(it);
else
++it;
}
}
else
m_handlers.erase(handler);
m_handlersChanged = previousCount != m_handlers.size();
if (m_handlersChanged)
CheckTimerNecessary();
}
public:
void OnTickTimer()
{
bool changed = false;
m_handlersMutex.lock();
EXT_DEFER(m_handlersMutex.unlock());
for (size_t index = 0; index < m_handlers.size();)
{
auto handler = std::next(m_handlers.begin(), index);
if (tick_clock::now() - handler->second.lastTickTime >= handler->second.tickInterval)
{
const TickParam tickParam = handler->second.tickParam;
ITickHandler* handlerPointer = handler->first;
ext::scope::AutoSet changedAutoSet(m_handlersChanged, false, false);
// While tick subscriptions may happened
{
m_handlersMutex.unlock();
EXT_DEFER(m_handlersMutex.lock());
handlerPointer->OnTick(tickParam);
}
if (m_handlersChanged)
{
changed = true;
handler = FindTickHandler(handlerPointer, tickParam);
if (handler != m_handlers.end())
handler->second.lastTickTime = tick_clock::now();
}
else
handler->second.lastTickTime = tick_clock::now();
}
++index;
}
if (changed)
CheckTimerNecessary();
}
protected:
struct TickHandlerInfo
{
TickParam tickParam = 0;
tick_clock::duration tickInterval = kDefTickInterval;
tick_clock::time_point lastTickTime = tick_clock::now();
TickHandlerInfo(TickParam&& param, tick_clock::duration&& interval) : tickParam(param), tickInterval(interval)
{}
};
typedef std::multimap<ITickHandler*, TickHandlerInfo> TickHandlersMap;
TickHandlersMap::iterator FindTickHandler(ITickHandler* handler, const TickParam& tickParam)
{
EXT_ASSERT(!m_handlersMutex.try_lock()) << "Mutex must be locked";
const auto handlers = m_handlers.equal_range(handler);
for (auto it = handlers.first; it != handlers.second; ++it)
{
if (it->second.tickParam == tickParam)
return it;
}
return m_handlers.end();
}
virtual void StartTimer() = 0;
virtual void StopTimer() = 0;
private:
void CheckTimerNecessary()
{
// if handlers exist - timer should work
if (m_handlers.empty() == m_timerWorks)
{
m_timerWorks ? StopTimer() : StartTimer();
m_timerWorks = !m_timerWorks;
}
}
protected:
std::atomic_bool m_handlersChanged = false;
std::atomic_bool m_timerWorks = false;
std::mutex m_handlersMutex;
std::multimap<ITickHandler*, TickHandlerInfo> m_handlers;
};
#ifdef __AFX_H__
struct InvokedTimer : Timer
{
~InvokedTimer() { if (m_timerWorks) StopTimer(); }
private:
void StartTimer() override
{
EXT_ASSERT(!m_timerId.has_value());
ext::InvokeMethod([timerId = &m_timerId]()
{
timerId->emplace(::SetTimer(nullptr, NULL, (UINT)kDefTickInterval.count(),
[](HWND, UINT, UINT_PTR, DWORD)
{
get_singleton<TickService>().m_invokedTimer.OnTickTimer();
}));
});
EXT_ASSERT(m_timerId.value_or(0) != 0) << "Timer must be set";
}
void StopTimer() override
{
if (m_timerId.has_value())
{
::KillTimer(nullptr, *m_timerId);
m_timerId.reset();
}
}
private:
std::optional<UINT_PTR> m_timerId;
} m_invokedTimer;
#endif // __AFX_H__
struct AsyncTimer : Timer
{
~AsyncTimer() { if (m_timerWorks) StopTimer(); }
private:
void StartTimer() override
{
EXT_ASSERT(!m_tickThread.joinable());
m_tickThread.run([&]()
{
try
{
while (!m_tickThread.interrupted())
{
OnTickTimer();
ext::this_thread::interruptible_sleep_for(kDefTickInterval);
}
}
catch (const ext::thread::thread_interrupted& /*interrupted*/)
{}
});
}
void StopTimer() override
{
EXT_ASSERT(m_tickThread.joinable());
m_tickThread.interrupt_and_join();
}
private:
ext::thread m_tickThread;
} m_asyncTimer;
};
// Implementation of the base class of the tick handler, simplify tick subscription
struct TickSubscriber : public ITickHandler
{
TickSubscriber() = default;
virtual ~TickSubscriber()
{
UnsubscribeTimer();
#ifdef __AFX_H__
UnsubscribeInvokedTimer();
#endif // __AFX_H__
}
/// <summary>Add tick handler, tick function will be called async.</summary>
/// <param name="tickInterval">Tick interval for this parameter.</param>
/// <param name="tickParam">Parameter passed to the handler on tick, allows you to identify the timer
/// or pass information to the handler</param>
void SubscribeTimer(tick_clock::duration tickInterval = TickService::kDefTickInterval, const TickParam& tickParam = 0)
{ get_singleton<TickService>().SubscribeAsync(this, tickInterval, tickParam); }
/// <summary>Remove async tick timer</summary>
/// <param name="tickParam">Tick parameter, if null - delete all handler timers.</param>
void UnsubscribeTimer(const std::optional<TickParam>& tickParam = std::nullopt)
{ get_singleton<TickService>().UnsubscribeAsync(this, tickParam); }
#ifdef __AFX_H__
/// <summary>Add tick timer, tick function will be called in main UI thread.</summary>
/// <param name="tickInterval">Tick interval for this parameter.</param>
/// <param name="tickParam">Parameter passed to the handler on tick, allows you to identify the timer
/// or pass information to the handler</param>
void SubscribeInvokedTimer(tick_clock::duration tickInterval = TickService::kDefTickInterval, const TickParam& tickParam = 0)
{ get_singleton<TickService>().SubscribeInvoked(this, tickInterval, tickParam); }
/// <summary>Remove main UI thread tick timer</summary>
/// <param name="tickParam">Tick parameter, if null - delete all handler timers</param>
void UnsubscribeInvokedTimer(const std::optional<TickParam>& tickParam = std::nullopt)
{ get_singleton<TickService>().UnsubscribeInvoked(this, tickParam); }
#endif // __AFX_H__
/// <summary>Checking if this handler has a timer with the given parameter.</summary>
/// <param name="tickParam">Tick parameter.</param>
[[nodiscard]] bool IsTimerExist(const TickParam& tickParam)
{ return get_singleton<TickService>().IsTimerExist(this, tickParam); }
};
} // namespace ext::tick