forked from graeme-hattan/qwt-example
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCppTimer.h
162 lines (151 loc) · 4.04 KB
/
CppTimer.h
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
#ifndef __CPP_TIMER_H_
#define __CPP_TIMER_H_
/**
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* (C) 2020-2024, Bernd Porr <mail@bernporr.me.uk>
*
* This is inspired by the timer_create man page.
**/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <thread>
#include <sys/timerfd.h>
/**
* Enumeration of CppTimer types
**/
enum cppTimerType_t
{
PERIODIC,
ONESHOT
};
/**
* Timer class which repeatedly fires. It's wrapper around the
* POSIX per-process timer.
**/
class CppTimer
{
public:
/**
* Starts the timer. The timer fires first after
* the specified time in nanoseconds and then at
* that interval in PERIODIC mode. In ONESHOT mode
* the timer fires once after the specified time in
* nanoseconds.
* @param nanosecs Time in nanoseconds
* @param type Either PERIODIC or ONESHOT
**/
virtual void startns(long nanosecs, cppTimerType_t type = PERIODIC) {
if (running) return;
fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (fd < 0)
throw("Could not start timer");
switch (type)
{
case (PERIODIC):
//starts after specified period of nanoseconds
its.it_value.tv_sec = nanosecs / 1000000000;
its.it_value.tv_nsec = nanosecs % 1000000000;
its.it_interval.tv_sec = nanosecs / 1000000000;
its.it_interval.tv_nsec = nanosecs % 1000000000;
break;
case (ONESHOT):
//fires once after specified period of nanoseconds
its.it_value.tv_sec = nanosecs / 1000000000;
its.it_value.tv_nsec = nanosecs % 1000000000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
break;
}
if (timerfd_settime(fd, 0, &its, NULL) == -1)
throw("Could not start timer");
uthread = std::thread(&CppTimer::worker,this);
}
/**
* Starts the timer. The timer fires first after
* the specified time in milliseconds and then at
* that interval in PERIODIC mode. In ONESHOT mode
* the timer fires once after the specified time in
* milliseconds.
* @param millisecs Time in milliseconds
* @param type Either PERIODIC or ONESHOT
**/
virtual void startms(long millisecs, cppTimerType_t type = PERIODIC) {
if (running) return;
fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (fd < 0)
throw("Could not start timer");
switch (type)
{
case (PERIODIC):
//starts after specified period of milliseconds
its.it_value.tv_sec = millisecs / 1000;
its.it_value.tv_nsec = (millisecs % 1000) * 1000000;
its.it_interval.tv_sec = millisecs / 1000;
its.it_interval.tv_nsec = (millisecs % 1000) * 1000000;
break;
case (ONESHOT):
//fires once after specified period of milliseconds
its.it_value.tv_sec = millisecs / 1000;
its.it_value.tv_nsec = (millisecs % 1000) * 1000000;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
break;
}
if (timerfd_settime(fd, 0, &its, NULL) == -1)
throw("Could not start timer");
uthread = std::thread(&CppTimer::worker,this);
}
/**
* Stops the timer by disarming it. It can be re-started
* with start().
**/
virtual void stop() {
if (!running) return;
running = false;
uthread.join();
}
/**
* Destructor disarms the timer, deletes it and
* disconnect the signal handler.
**/
virtual ~CppTimer() {
stop();
}
protected:
/**
* Abstract function which needs to be implemented by the children.
* This is called every time the timer fires.
**/
virtual void timerEvent() = 0;
private:
int fd = 0;
struct itimerspec its;
bool running = false;
std::thread uthread;
void worker() {
running = true;
while (running) {
uint64_t exp;
const long int s = read(fd, &exp, sizeof(uint64_t));
if (s != sizeof(uint64_t) ) {
running = false;
return;
}
timerEvent();
}
// disarm
struct itimerspec itsnew;
itsnew.it_value.tv_sec = 0;
itsnew.it_value.tv_nsec = 0;
itsnew.it_interval.tv_sec = 0;
itsnew.it_interval.tv_nsec = 0;
timerfd_settime(fd, 0, &itsnew, &its);
close(fd);
fd = -1;
}
};
#endif