-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
24 lines (21 loc) · 844 Bytes
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <signal.h> // for SIGEV_THREAD, sigevent
#include <stdbool.h> // for bool
#include <time.h> // for timer_create, timer_settime
void tick_create(void (*callback)(sigval_t), unsigned sec, unsigned nsec, bool once)
{
struct sigevent timerEvent = {
.sigev_notify = SIGEV_THREAD,
.sigev_notify_function = callback,
.sigev_notify_attributes = NULL,
};
struct itimerspec timerPeriod = {
.it_value.tv_sec = (once) ? sec : 0,
.it_value.tv_nsec = (once) ? nsec
: 1, // If !once we want the timer to fire immediately
.it_interval.tv_sec = (once) ? 0 : sec,
.it_interval.tv_nsec = (once) ? 0 : nsec,
};
timer_t timer;
timer_create(CLOCK_MONOTONIC, &timerEvent, &timer);
timer_settime(timer, 0, &timerPeriod, NULL);
}