forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htimer_test.c
60 lines (49 loc) · 1.75 KB
/
htimer_test.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
#include "hloop.h"
#include "hbase.h"
void on_timer(htimer_t* timer) {
printf("time=%llus on_timer\n", LLU(hloop_now(hevent_loop(timer))));
}
// test htimer_add
void on_timer_add(htimer_t* timer) {
printf("time=%llus on_timer_add\n", LLU(hloop_now(hevent_loop(timer))));
htimer_add(hevent_loop(timer), on_timer_add, 1000, 1);
}
// test htimer_del
void on_timer_del(htimer_t* timer) {
printf("time=%llus on_timer_del\n", LLU(hloop_now(hevent_loop(timer))));
htimer_del(timer);
}
// test htimer_reset
void on_timer_reset(htimer_t* timer) {
printf("time=%llus on_timer_reset\n", LLU(hloop_now(hevent_loop(timer))));
htimer_reset((htimer_t*)hevent_userdata(timer), 0);
}
// test hloop_stop
void on_timer_quit(htimer_t* timer) {
printf("time=%llus on_timer_quit\n", LLU(hloop_now(hevent_loop(timer))));
hloop_stop(hevent_loop(timer));
}
// test cron
void cron_hourly(htimer_t* timer) {
time_t tt = time(NULL);
printf("time=%llus cron_hourly: %s\n", LLU(hloop_now(hevent_loop(timer))), ctime(&tt));
}
int main() {
HV_MEMCHECK;
hloop_t* loop = hloop_new(0);
htimer_add(loop, on_timer_add, 1000, 1);
htimer_add(loop, on_timer_del, 1000, 10);
htimer_t* reseted = htimer_add(loop, on_timer, 5000, 1);
htimer_t* reset = htimer_add(loop, on_timer_reset, 1000, 5);
hevent_set_userdata(reset, reseted);
// cron_hourly next triggered in one minute
int minute = time(NULL)%3600/60;
htimer_add_period(loop, cron_hourly, minute+1, -1, -1, -1, -1, INFINITE);
// quit application after 1 min
htimer_add(loop, on_timer_quit, 60000, 1);
printf("time=%llus begin\n", LLU(hloop_now(loop)));
hloop_run(loop);
printf("time=%llus stop\n", LLU(hloop_now(loop)));
hloop_free(&loop);
return 0;
}