-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.h
340 lines (275 loc) · 7.08 KB
/
timer.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
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
/** ffsys: timer
2020, Simon Zolin */
/*
fftimer_create
fftimer_close
fftimer_start
fftimer_stop
fftimer_consume
*/
#pragma once
#include <ffsys/queue.h>
#ifdef FF_WIN
/*
A timer must be started within a separate thread, which sleeps in an alertable state to process timer events.
From this thread a message to kernel queue is sent.
Note that GetQueuedCompletionStatusEx(alertable=1) can't be used for the reliable timer,
because it doesn't enter an alertable state if there's a finished I/O completion task.
timer thread: SleepEx(alertable=1) etc. -> timer proc -> ffkq_post()
main thread: ffkq_wait() -> timer handler
*/
#include <ffsys/thread.h>
#include <ffsys/std.h>
#include <ffsys/error.h>
#include <ffbase/atomic.h>
typedef struct _fftimer {
HANDLE htmr;
ffkq kq;
void *data;
int period_ms;
ffuint ctl;
int result;
ffthread thd;
HANDLE evt;
HANDLE evt_result;
} _fftimer;
typedef _fftimer* fftimer;
#define FFTIMER_NULL NULL
/** Timer function: called by OS while we are sleeping in alertable state */
static void __stdcall _fftimer_onfire(LPVOID arg, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
{
(void)dwTimerLowValue; (void)dwTimerHighValue;
_fftimer *tmr = (_fftimer*)arg;
ffkq_postevent post = ffkq_post_attach(tmr->kq, tmr->data);
int r = ffkq_post(post, tmr->data);
(void)r;
FF_ASSERT(r == 0);
}
enum {
_FFTIMER_EXIT,
_FFTIMER_START,
_FFTIMER_STOP,
};
/**
. Wait until a command is received
* _FFTIMER_EXIT: exit thread
* _FFTIMER_START: Arm the timer and sleep in an alertable state to execute timer callback function
Set result code in tmr->result field
* _FFTIMER_STOP: Stop the timer
Set result code in tmr->result field
*/
static int __stdcall _fftimer_thread(void *param)
{
_fftimer *tmr = (_fftimer*)param;
for (;;) {
int r = WaitForSingleObjectEx(tmr->evt, INFINITE, /*alertable*/ 1);
if (r == WAIT_IO_COMPLETION) {
} else if (r == WAIT_OBJECT_0) {
// execute the command and set the result
ffuint cmd = FFINT_READONCE(tmr->ctl);
if (cmd == _FFTIMER_EXIT)
break;
r = 0;
switch (cmd) {
case _FFTIMER_START: {
ffint64 due_ns100 = (ffint64)tmr->period_ms * 1000 * -10;
ffuint period_ms = (tmr->period_ms >= 0) ? tmr->period_ms : 0;
if (!SetWaitableTimer(tmr->htmr, (LARGE_INTEGER*)&due_ns100, period_ms, &_fftimer_onfire, tmr, /*fResume*/ 1))
r = GetLastError();
break;
}
case _FFTIMER_STOP:
if (!CancelWaitableTimer(tmr->htmr))
r = GetLastError();
break;
}
FFINT_WRITEONCE(tmr->result, r);
ffcpu_fence_release();
if (!SetEvent(tmr->evt_result)) { // wake up main thread
FF_ASSERT(0);
}
} else {
FF_ASSERT(0);
break;
}
}
return 0;
}
/** Send command to a timer thread and get the result */
static int _fftimer_cmd(fftimer tmr, ffuint cmd)
{
FFINT_WRITEONCE(tmr->ctl, cmd);
ffcpu_fence_release();
if (!SetEvent(tmr->evt)) // wake up timer thread
return -1;
if (cmd == _FFTIMER_EXIT)
return 0;
int r = WaitForSingleObject(tmr->evt_result, INFINITE);
if (r != WAIT_OBJECT_0)
return -1;
r = FFINT_READONCE(tmr->result);
SetLastError(r);
return r;
}
static inline void fftimer_close(fftimer tmr, ffkq kq)
{
(void)kq;
if (tmr == NULL)
return;
CloseHandle(tmr->htmr);
if (tmr->thd != FFTHREAD_NULL) {
_fftimer_cmd(tmr, _FFTIMER_EXIT);
ffthread_join(tmr->thd, -1, NULL);
}
CloseHandle(tmr->evt);
CloseHandle(tmr->evt_result);
ffmem_free(tmr);
}
static inline fftimer fftimer_create(int flags)
{
(void)flags;
_fftimer *tmr = ffmem_new(_fftimer);
if (tmr == NULL)
return FFTIMER_NULL;
if (NULL == (tmr->htmr = CreateWaitableTimer(NULL, 0, NULL)))
goto err;
if (NULL == (tmr->evt = CreateEvent(NULL, 0, 0, NULL)))
goto err;
if (NULL == (tmr->evt_result = CreateEvent(NULL, 0, 0, NULL)))
goto err;
if (FFTHREAD_NULL == (tmr->thd = ffthread_create(&_fftimer_thread, tmr, 4 * 1024)))
goto err;
return tmr;
err:
fftimer_close(tmr, NULL);
return FFTIMER_NULL;
}
static inline int fftimer_start(fftimer tmr, ffkq kq, void *data, int period_ms)
{
tmr->kq = kq;
tmr->data = data;
tmr->period_ms = period_ms;
return _fftimer_cmd(tmr, _FFTIMER_START);
}
static inline int fftimer_stop(fftimer tmr, ffkq kq)
{
(void)kq;
return _fftimer_cmd(tmr, _FFTIMER_STOP);
}
static inline void fftimer_consume(fftimer tmr)
{
(void)tmr;
}
#elif defined FF_LINUX
#include <sys/timerfd.h>
#include <errno.h>
typedef int fftimer;
#define FFTIMER_NULL (-1)
static inline fftimer fftimer_create(int flags)
{
return timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | flags);
}
static inline void fftimer_close(fftimer tmr, ffkq kq)
{
(void)kq;
close(tmr);
}
static inline int fftimer_start(fftimer tmr, ffkq kq, void *data, int period_ms)
{
struct epoll_event e;
e.events = EPOLLIN | EPOLLET;
e.data.ptr = data;
if (0 != epoll_ctl(kq, EPOLL_CTL_ADD, tmr, &e)
&& errno != EEXIST)
return -1;
int neg = 0;
if (period_ms < 0) {
neg = 1;
period_ms = -period_ms;
}
struct itimerspec its;
its.it_value.tv_sec = period_ms / 1000;
its.it_value.tv_nsec = (period_ms % 1000) * 1000000;
if (neg)
its.it_interval.tv_sec = its.it_interval.tv_nsec = 0;
else
its.it_interval = its.it_value;
return timerfd_settime(tmr, 0, &its, NULL);
}
static inline int fftimer_stop(fftimer tmr, ffkq kq)
{
(void)kq;
const struct itimerspec ts = {};
return timerfd_settime(tmr, 0, &ts, NULL);
}
static inline void fftimer_consume(fftimer tmr)
{
ffint64 val;
read(tmr, &val, sizeof(ffint64));
}
#else // BSD/macOS:
#include <sys/event.h>
#include <stdlib.h>
typedef ffssize fftimer;
#define FFTIMER_NULL (-1)
static inline fftimer fftimer_create(int flags)
{
(void)flags;
void *tmr = ffmem_alloc(1);
if (tmr == NULL)
return FFTIMER_NULL;
*(ffbyte*)tmr = 0;
return (ffssize)tmr;
}
static inline void fftimer_close(fftimer tmr, ffkq kq)
{
if ((ffssize)tmr == FFTIMER_NULL)
return;
if (*(ffbyte*)tmr == 1) {
struct kevent kev;
EV_SET(&kev, tmr, EVFILT_TIMER, EV_DELETE, 0, 0, (void*)-1);
kevent(kq, &kev, 1, NULL, 0, NULL);
}
ffmem_free((void*)tmr);
}
static inline int fftimer_start(fftimer tmr, ffkq kq, void *data, int period_ms)
{
int f = 0;
if (period_ms < 0) {
period_ms = -period_ms;
f = EV_ONESHOT;
}
struct kevent kev;
EV_SET(&kev, tmr, EVFILT_TIMER, EV_ADD | EV_ENABLE | f, 0, period_ms, data);
if (0 != kevent(kq, &kev, 1, NULL, 0, NULL))
return -1;
*(ffbyte*)tmr = 1;
return 0;
}
static inline int fftimer_stop(fftimer tmr, ffkq kq)
{
struct kevent kev;
EV_SET(&kev, tmr, EVFILT_TIMER, EV_DELETE, 0, 0, (void*)-1);
if (0 != kevent(kq, &kev, 1, NULL, 0, NULL))
return -1;
*(ffbyte*)tmr = 0;
return 0;
}
static inline void fftimer_consume(fftimer tmr)
{
(void)tmr;
}
#endif
/** Create timer
Return FFTIMER_NULL on error */
static fftimer fftimer_create(int flags);
/** Destroy timer */
static void fftimer_close(fftimer tmr, ffkq kq);
/** Start timer
period_ms: interval
<0: fire just once after -period_ms interval */
static int fftimer_start(fftimer tmr, ffkq kq, void *data, int period_ms);
/** Stop a timer */
static int fftimer_stop(fftimer tmr, ffkq kq);
/** Consume timer data: must be called on each timer signal */
static void fftimer_consume(fftimer tmr);