-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjv_timer_main.c
executable file
·74 lines (54 loc) · 1.42 KB
/
jv_timer_main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <jv_timer.h>
#include <sys/time.h> /* gettimeofday */
#include <unistd.h> /* usleep */
extern volatile jv_msec_t jv_current_msec;
static jv_msec_t jv_time_update(void) {
time_t sec;
jv_uint_t msec;
struct timeval tv;
(void)gettimeofday(&tv, NULL);
sec = tv.tv_sec;
msec = tv.tv_usec / 1000;
jv_current_msec = (jv_msec_t)sec * 1000 + msec;
return jv_current_msec;
}
void timer_test1(void *arg) {
printf("timer_test1()\n");
}
void timer_test2(void *arg) {
printf("timer_test2()\n");
}
void timer_test3(void *arg) {
printf("timer_test3()\n");
usleep(100);
}
void sig_int(int signo) {
puts("Exit");
exit(0);
}
int main(int argc, char *argv[]) {
jv_timer_t *timer;
signal(SIGINT, sig_int);
timer = jv_timer_create();
jv_time_update();
if (!jv_timer_add(timer, 0, 1 * 1000L, timer_test1, NULL)) {
printf("tmr_create(test1) failed\n");
exit(1);
}
if (!jv_timer_add(timer, 2 * 1000L, 0, timer_test2, NULL)) {
printf("tmr_create(test2) failed\n");
exit(1);
}
if (!jv_timer_add(timer, 0, 3 * 1000L, timer_test3, NULL)) {
printf("tmr_create(test3) failed\n");
exit(1);
}
while (1) {
printf("jv_timer_mstimeout:%lu\n", jv_timer_mstimeout(timer));
jv_time_update();
jv_timer_run(timer);
usleep(1000 * 1000L);
}
jv_timer_destroy(timer);
return 0;
}