-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer.c
57 lines (41 loc) · 1016 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
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timerfd.h>
#include "timer.h"
int
prepare_timer_fd(const int timeout) {
struct itimerspec tv;
int ret, fd;
fd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC | TFD_NONBLOCK);
if (fd == -1) {
perror("E: Cannot create timer");
exit(1);
}
memset(&tv, 0, sizeof tv);
tv.it_interval.tv_sec = timeout;
tv.it_value.tv_sec = timeout;
ret = timerfd_settime(fd, 0, &tv, NULL);
if (ret == -1) {
perror("E: Cannot set timer");
exit(1);
}
return fd;
}
unsigned long
update_timestamp(struct timespec * now) {
struct timespec old, tmp;
unsigned long ret;
old.tv_sec = now->tv_sec;
old.tv_nsec = now->tv_nsec;
clock_gettime(CLOCK_REALTIME, now);
tmp.tv_sec = now->tv_sec - old.tv_sec;
tmp.tv_nsec = now->tv_nsec - old.tv_nsec;
if (tmp.tv_nsec < 0) {
tmp.tv_sec -= 1;
tmp.tv_nsec += 1000000000;
}
ret = tmp.tv_sec * 1000000 + tmp.tv_nsec / 1000;
return ret;
}