-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTimer.cpp
233 lines (187 loc) · 4.7 KB
/
Timer.cpp
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
#include "Timer.h"
#include <stdexcept>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
struct Timer::TimerImpl
{
HANDLE timerQueue;
HANDLE timerQueueTimer;
};
#elif __linux__
#include <unistd.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <string.h>
#include <thread>
struct Timer::TimerImpl
{
int timerfd;
int epollfd;
std::thread poller;
};
#else
#error "Not implemented"
#endif
Timer::Timer(const Interval& timer_period, const TimerCallback& timer_callback, bool periodic)
{
this->period = timer_period;
this->callback = timer_callback;
this->isPeriodic = periodic;
this->impl = new Timer::TimerImpl;
if (!this->InitializeImpl())
throw std::runtime_error("Failed to initialize timer!");
}
Timer::~Timer()
{
this->isRunning = false;
if (this->impl)
{
CleanupImpl();
delete impl;
}
}
Interval Timer::GetPeriod() const
{
return this->period;
}
void Timer::SetPeriod(const Interval& new_period)
{
this->period = new_period;
}
TimerCallback Timer::GetCallback() const
{
return this->callback;
}
void Timer::SetCallback(const TimerCallback& new_callback)
{
this->callback = new_callback;
}
bool Timer::IsPeriodic() const
{
return this->isPeriodic;
}
void Timer::SetPeriodic(bool periodic)
{
this->isPeriodic = periodic;
}
bool Timer::IsRunning() const
{
return this->isRunning;
}
#ifdef _WIN32
static void CALLBACK WinTimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
if (!lpParameter)
return;
Timer *t = (Timer *) lpParameter;
if (t->GetCallback())
t->GetCallback()();
}
bool Timer::InitializeImpl()
{
impl->timerQueue = CreateTimerQueue();
return impl->timerQueue != INVALID_HANDLE_VALUE;
}
void Timer::CleanupImpl()
{
if (impl->timerQueueTimer != INVALID_HANDLE_VALUE)
DeleteTimerQueueTimer(impl->timerQueue, impl->timerQueueTimer, INVALID_HANDLE_VALUE);
if (impl->timerQueue != INVALID_HANDLE_VALUE)
DeleteTimerQueueEx(impl->timerQueue, INVALID_HANDLE_VALUE);
}
void Timer::Start()
{
DWORD period = (this->isPeriodic) ? static_cast<DWORD>(this->GetPeriod().count()) : 0;
BOOL success = CreateTimerQueueTimer(
&impl->timerQueueTimer,
impl->timerQueue,
&WinTimerCallback,
this,
0,
period,
0
);
if (!success)
throw std::runtime_error("Failed to start timer!");
this->isRunning = true;
}
void Timer::Stop()
{
this->isRunning = false;
this->CleanupImpl();
this->InitializeImpl();
}
#elif __linux__
constexpr int EPOLL_EVENT_SIZE = 1;
bool Timer::InitializeImpl()
{
epoll_event event;
impl->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (impl->timerfd == -1)
return false;
impl->epollfd = epoll_create(EPOLL_EVENT_SIZE);
if (impl->epollfd == -1)
{
close(impl->timerfd);
return false;
}
event.events = EPOLLIN;
event.data.fd = impl->timerfd;
if (epoll_ctl(impl->epollfd, EPOLL_CTL_ADD, impl->timerfd, &event) == -1)
{
close(impl->epollfd);
close(impl->timerfd);
return false;
}
return true;
}
void Timer::CleanupImpl()
{
close(impl->timerfd);
close(impl->epollfd);
impl->poller.join();
}
void Timer::Start()
{
struct itimerspec interval;
auto timeval = this->GetPeriod().count(); // millis
// convert val to (seconds, nanoseconds)
memset(&interval, 0, sizeof(struct itimerspec));
interval.it_value.tv_sec = static_cast<int>(timeval) / 1000;
interval.it_value.tv_nsec = 1000000 * (static_cast<int>(timeval) % 1000);
if (this->isPeriodic)
{
interval.it_interval.tv_sec = interval.it_value.tv_sec;
interval.it_interval.tv_nsec = interval.it_value.tv_nsec;
}
if (timerfd_settime(impl->timerfd, 0, &interval, nullptr) == -1)
{
close(impl->timerfd);
close(impl->epollfd);
throw std::runtime_error("Failed to start timer!");
}
this->isRunning = true;
// start poll thread
impl->poller = std::thread([&]()
{
epoll_event events[EPOLL_EVENT_SIZE];
while (this->isRunning)
{
int num__events = epoll_wait(impl->epollfd, events, EPOLL_EVENT_SIZE, 1);
if (num__events == -1)
continue;
size_t s = 0;
int i = read(impl->timerfd, &s, sizeof(size_t));
if (i != -1 && this->GetCallback())
this->GetCallback()();
}
});
}
void Timer::Stop()
{
this->isRunning = false;
if (impl->poller.joinable())
impl->poller.join();
}
#endif