]> git.saurik.com Git - apple/launchd.git/blob - launchd/testing/EVFILT_TIMER.c
launchd-152.tar.gz
[apple/launchd.git] / launchd / testing / EVFILT_TIMER.c
1 #include <sys/event.h>
2 #include <sys/time.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <assert.h>
9
10 static int kq = -1;
11
12 time_t
13 now_plus_n(int n)
14 {
15 time_t later = time(NULL);
16
17 return later + n;
18 }
19
20 void
21 add_abs_timer(int n)
22 {
23 struct kevent kev;
24
25 EV_SET(&kev, n * 100000, EVFILT_TIMER, EV_ADD, NOTE_SECONDS | NOTE_ABSOLUTE, now_plus_n(n), (void *)n);
26
27 assert(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
28 }
29
30 int
31 main(void)
32 {
33 struct kevent kev;
34
35 assert((kq = kqueue()) != -1);
36
37 add_abs_timer(2);
38 add_abs_timer(3);
39 add_abs_timer(4);
40 add_abs_timer(5);
41 add_abs_timer(6);
42
43 for (;;) {
44 assert(kevent(kq, NULL, 0, &kev, 1, NULL) == 1);
45 fprintf(stdout, "kev.ident == %ld kev.udata == %p\n", kev.ident, kev.udata);
46 add_abs_timer((int)kev.udata);
47 }
48
49 exit(EXIT_SUCCESS);
50 }