Blob


1 /*
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
26 #include "compat.h"
28 #include <sys/time.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
35 #include <poll.h>
37 #include "ev.h"
39 int fired_a;
40 int fired_b;
41 int fired_c;
43 struct timeval tv_a = { 0, 250000 };
44 struct timeval tv_b = { 0, 300000 };
45 struct timeval tv_c = { 0, 350000 };
47 unsigned long tout_a;
48 unsigned long tout_b;
49 unsigned long tout_c;
51 static void
52 pipe_ev(int fd, int ev, void *data)
53 {
54 warn("shouldn't have happened!");
55 abort();
56 }
58 static void
59 timeout_cb(int fd, int ev, void *data)
60 {
61 int *d = data;
63 assert(fd == -1);
64 *d = 1;
65 }
67 static void
68 timeout_cancel_b(int fd, int ev, void *data)
69 {
70 timeout_cb(fd, ev, data);
71 ev_timer_cancel(tout_b);
72 }
74 static void
75 timeout_quit(int fd, int ev, void *data)
76 {
77 timeout_cb(fd, ev, data);
78 ev_break();
79 }
81 int
82 main(void)
83 {
84 int p[2];
86 alarm(2); /* safety net */
88 /* the ev subsystem needs at least a file descriptor */
89 if (pipe(p) == -1)
90 err(1, "pipe");
92 if (ev_init() == -1)
93 err(1, "ev_init");
95 if (ev_add(p[0], POLLIN, pipe_ev, NULL) == -1)
96 err(1, "ev_add");
98 if ((tout_c = ev_timer(&tv_c, timeout_quit, &fired_c)) == 0 ||
99 (tout_b = ev_timer(&tv_b, timeout_cb, &fired_b)) == 0 ||
100 (tout_a = ev_timer(&tv_a, timeout_cancel_b, &fired_a)) == 0)
101 err(1, "ev_timer");
103 ev_loop();
105 if (fired_a && !fired_b && fired_c)
106 return 0;
108 errx(1, "events fired not as expected: a:%d b:%d c:%d",
109 fired_a, fired_b, fired_c);