-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblTimer.hpp
501 lines (300 loc) · 13.2 KB
/
blTimer.hpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#ifndef BL_TIMER_HPP
#define BL_TIMER_HPP
//-------------------------------------------------------------------
// FILE: blTimer.hpp
// CLASS: blTimer
// BASE CLASS: None
//
// PURPOSE: A timer that runs on a parallel thread and invokes
// multiple user provided callbacks every user's specified
// period seconds
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes and libs needed for this file and sub-files
//-------------------------------------------------------------------
#include <thread>
#include <functional>
#include <atomic>
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blTimerAPI namespace
//-------------------------------------------------------------------
namespace blTimerAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class blTimer
{
public: // Constructors and destructor
// Default constructor
blTimer();
// Destructor
virtual ~blTimer();
// Eliminate copy construction and
// assignment operator
blTimer(blTimer const&) = delete;
blTimer& operator=(blTimer const&) = delete;
public: // Public functions
// Functions used to set/get the timer
// period in seconds
void setPeriod(const double& periodExpressedInSeconds);
void setPeriod(const std::chrono::duration<double>& period);
const std::chrono::duration<double>& getPeriod()const;
// Functions used to set/get the timer's
// total duration
void setDuration(const double& timerDurationInSeconds);
void setDuration(const std::chrono::duration<double>& duration);
const std::chrono::duration<double>& getDuration()const;
// Functions used to set/get the maximum
// number of times that the timer fires
void setMaximumNumberOfTimesTheTimerFires(const int& maximumNumberOfTimesTheTimerFires);
const int& getMaximumNumberOfTimesTheTimerFires()const;
// Functions used to get the current
// time/firings while the thread is
// running
const int& getCurrentNumberOfTimesTheTimerFired()const;
const std::chrono::high_resolution_clock::time_point& getTimeTheThreadStartedRunning()const;
// Functions used to stop the thread and
// either join the calling thead or just
// be detached
void stopAndJoin();
void stopAndDetach();
void join();
void detach();
// pause function will signal the
// thread to pause its execution to
// later be restarted again
void pause();
// Functions used ot start the
// parallel thread
template<typename blFunctor,
typename...blFunctorArguments>
bool start(blFunctor functor,
blFunctorArguments... arguments);
protected: // Protected functions
// Run function will call the registered
// callbacks as the thread is being executed
// this function takes as an argument a functor
// with functor arguments and will call that
// functor with passed arguments repeatedly
// every time the timer hits
template<typename blFunctor,
typename...blFunctorArguments>
void run(blFunctor functor,
blFunctorArguments... arguments);
private: // Private variables
// The atomic variable used to signal
// the thread to stop its execution
std::atomic<bool> m_stop;
// The atomic variable used to "pause"
// the thread, meaning that if the
// pause variable is true, the thread
// does not execute its function
std::atomic<bool> m_pause;
// The maximum number of times the
// timer fires
int m_maximumNumberOfTimesTheTimerFires;
// The number of times the timer
// has fired
int m_currentNumberOfTimesTheTimerHasFired;
// The timer period in seconds
std::chrono::duration<double> m_period;
// The total duration of the timer
// after which the timer will be stopped
std::chrono::duration<double> m_duration;
// Time point at which timer was started
std::chrono::high_resolution_clock::time_point m_timeTheThreadStartedRunning;
// The variable used to spawn parallel
// therads
std::thread m_thread;
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Default constructor
//-------------------------------------------------------------------
inline blTimer::blTimer()
{
// Default all values
m_stop = true;
m_pause = false;
m_maximumNumberOfTimesTheTimerFires = -1;
m_currentNumberOfTimesTheTimerHasFired = 0;
m_period = std::chrono::duration< double,std::ratio<1,1> >(0);
m_duration = std::chrono::duration<double>::zero();
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
inline blTimer::~blTimer()
{
stopAndJoin();
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Set/get functions
//-------------------------------------------------------------------
inline void blTimer::setPeriod(const double& periodExpressedInSeconds)
{
m_period = std::chrono::duration< double,std::ratio<1,1> >(periodExpressedInSeconds);
}
inline void blTimer::setPeriod(const std::chrono::duration<double>& period)
{
m_period = period;
}
inline void blTimer::setDuration(const double& timerDurationInSeconds)
{
m_duration = std::chrono::duration< double,std::ratio<1,1> >(timerDurationInSeconds);
}
inline void blTimer::setDuration(const std::chrono::duration<double>& duration)
{
m_duration = duration;
}
inline void blTimer::setMaximumNumberOfTimesTheTimerFires(const int& maximumNumberOfTimesTheTimerFires)
{
m_maximumNumberOfTimesTheTimerFires = maximumNumberOfTimesTheTimerFires;
}
inline const std::chrono::duration<double>& blTimer::getPeriod()const
{
return m_period;
}
inline const std::chrono::duration<double>& blTimer::getDuration()const
{
return m_duration;
}
inline const int& blTimer::getMaximumNumberOfTimesTheTimerFires()const
{
return m_maximumNumberOfTimesTheTimerFires;
}
inline const int& blTimer::getCurrentNumberOfTimesTheTimerFired()const
{
return m_currentNumberOfTimesTheTimerHasFired;
}
inline const std::chrono::high_resolution_clock::time_point& blTimer::getTimeTheThreadStartedRunning()const
{
return m_timeTheThreadStartedRunning;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to stop the thread and either join the
// calling thead or just be detached
//-------------------------------------------------------------------
inline void blTimer::stopAndJoin()
{
m_stop = true;
if(m_thread.joinable() == true)
m_thread.join();
}
inline void blTimer::stopAndDetach()
{
m_stop = true;
if(m_thread.joinable() == true)
m_thread.detach();
}
inline void blTimer::join()
{
if(m_thread.joinable())
m_thread.join();
}
inline void blTimer::detach()
{
if(m_thread.joinable())
m_thread.detach();
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blTimer::pause()
{
m_pause = true;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blFunctor,
typename...blFunctorArguments>
inline bool blTimer::start(blFunctor functor,blFunctorArguments... arguments)
{
// We want this timer to only spin off
// one thread, so we first check if the
// timer had already started
if(m_stop == true && m_thread.joinable() == false)
{
// This means the timer had been
// stopped or not started yet, and
// that the previous timer thread
// has been detached or joined
m_stop = false;
m_pause = false;
m_thread = std::thread(&blTimer::run<blFunctor,blFunctorArguments...>,std::ref(*this),functor,arguments...);
return true;
}
else
{
// This means the thread had already
// started and not finished yet
return false;
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blFunctor,
typename...blFunctorArguments>
inline void blTimer::run(blFunctor functor,blFunctorArguments... arguments)
{
// The first thing we do is get
// the current time so that the
// timer can fire at specified
// intervals and we can keep track
// of when the timer was started
m_timeTheThreadStartedRunning = std::chrono::high_resolution_clock::now();
auto lastTimeOfExecution = m_timeTheThreadStartedRunning;
auto currentTime = m_timeTheThreadStartedRunning;
// We also zero out the number of
// times the timer has fired so far
m_currentNumberOfTimesTheTimerHasFired = 0;
// We then enter a loop that will keep
// going until the user calls the stop
// function or until the timer has fired
// the maximum number of allowed times
while(m_stop == false &&
(m_currentNumberOfTimesTheTimerHasFired < m_maximumNumberOfTimesTheTimerFires || m_maximumNumberOfTimesTheTimerFires < 0))
{
// Get the current time
currentTime = std::chrono::high_resolution_clock::now();
// If enough time has passed (more than
// the specified timer period), and the
// thread has not been paused, then we
// execute the thread callback function
if(m_pause == false && (currentTime - lastTimeOfExecution) > m_period)
{
std::invoke(functor,arguments...);
// Let's not forget to save the current
// time as the last time the timer executed
lastTimeOfExecution = currentTime;
// We also check if the total timer
// duration has passed only if the
// specified timer duration is greater
// than zero, otherwise we keep going
// until the user stops the timer
if( ((currentTime - m_timeTheThreadStartedRunning) > m_duration) && m_duration > std::chrono::duration<double>::zero() )
{
// In this case the timer has passed
// the specified duration and the specified
// duration was valid, so we stop the timer
m_stop = true;
}
// Let's increase the number of times the
// timer has fired so far
++m_currentNumberOfTimesTheTimerHasFired;
}
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of namespace
}
//-------------------------------------------------------------------
#endif // BL_TIMER_HPP