… | |
… | |
30 | */ |
30 | */ |
31 | |
31 | |
32 | #include <stddef.h> |
32 | #include <stddef.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <sys/time.h> |
34 | #include <sys/time.h> |
|
|
35 | #include <assert.h> |
35 | |
36 | |
36 | #include "event.h" |
37 | #include "event.h" |
37 | |
38 | |
|
|
39 | #if EV_MULTIPLICITY |
|
|
40 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
|
|
41 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
|
|
42 | #else |
38 | #define dLOOPev |
43 | # define dLOOPev |
39 | #define dLOOPbase |
44 | # define dLOOPbase |
|
|
45 | #endif |
40 | |
46 | |
|
|
47 | /* never accessed, will always be cast from/to ev_loop */ |
41 | struct event_base |
48 | struct event_base |
42 | { |
49 | { |
43 | int dummy; |
50 | int dummy; |
44 | }; |
51 | }; |
45 | |
52 | |
46 | static struct event_base x_base, *x_cur; |
53 | static struct event_base *x_cur; |
47 | |
54 | |
48 | static void |
55 | static void |
49 | tv_set (struct timeval *tv, ev_tstamp at) |
56 | tv_set (struct timeval *tv, ev_tstamp at) |
50 | { |
57 | { |
51 | tv->tv_sec = (long)at; |
58 | tv->tv_sec = (long)at; |
… | |
… | |
73 | return "libev"; |
80 | return "libev"; |
74 | } |
81 | } |
75 | |
82 | |
76 | void *event_init (void) |
83 | void *event_init (void) |
77 | { |
84 | { |
78 | if (!x_cur && ev_init (0)) |
85 | #if EV_MULTIPLICITY |
79 | return x_cur = &x_base; |
86 | if (x_cur) |
|
|
87 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
|
|
88 | else |
|
|
89 | x_cur = ev_default_loop (EVMETHOD_AUTO); |
|
|
90 | #else |
|
|
91 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
80 | |
92 | |
|
|
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
|
|
94 | #endif |
|
|
95 | |
81 | return 0; |
96 | return x_cur; |
82 | } |
97 | } |
83 | |
98 | |
84 | void event_base_free (struct event_base *base) |
99 | void event_base_free (struct event_base *base) |
85 | { |
100 | { |
86 | /* nop */ |
101 | dLOOPbase; |
|
|
102 | |
|
|
103 | #if EV_MULTIPLICITY |
|
|
104 | ev_loop_delete (loop); |
|
|
105 | #endif |
87 | } |
106 | } |
88 | |
107 | |
89 | int event_dispatch (void) |
108 | int event_dispatch (void) |
90 | { |
109 | { |
91 | return event_base_dispatch (x_cur); |
110 | return event_base_dispatch (x_cur); |
… | |
… | |
127 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
146 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
128 | { |
147 | { |
129 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
148 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
130 | |
149 | |
131 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
150 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
132 | ev_io_stop (w); |
151 | ev_io_stop (EV_A_ w); |
133 | |
152 | |
134 | x_cb (ev, revents); |
153 | x_cb (ev, revents); |
135 | } |
154 | } |
136 | |
155 | |
137 | static void |
156 | static void |
… | |
… | |
140 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
159 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
141 | } |
160 | } |
142 | |
161 | |
143 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
162 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
144 | { |
163 | { |
|
|
164 | if (!ev->initialised) |
|
|
165 | { |
|
|
166 | ev->initialised = 1; |
|
|
167 | |
145 | if (events & EV_SIGNAL) |
168 | if (events & EV_SIGNAL) |
146 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
169 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
147 | else |
170 | else |
148 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
171 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
149 | |
172 | |
150 | ev_watcher_init (&ev->to, x_cb_to); |
173 | ev_watcher_init (&ev->to, x_cb_to); |
|
|
174 | } |
151 | |
175 | |
152 | ev->ev_base = x_cur; |
176 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
153 | ev->ev_fd = fd; |
177 | ev->ev_fd = fd; |
154 | ev->ev_events = events; |
178 | ev->ev_events = events; |
155 | ev->ev_pri = 0; |
179 | ev->ev_pri = 0; |
156 | ev->ev_callback = cb; |
180 | ev->ev_callback = cb; |
157 | ev->ev_arg = arg; |
181 | ev->ev_arg = arg; |
… | |
… | |
263 | } |
287 | } |
264 | |
288 | |
265 | int event_base_loop (struct event_base *base, int flags) |
289 | int event_base_loop (struct event_base *base, int flags) |
266 | { |
290 | { |
267 | dLOOPbase; |
291 | dLOOPbase; |
268 | ev_loop (EV_A_ flags | EVLOOP_ONESHOT); |
292 | |
|
|
293 | ev_loop (EV_A_ flags); |
269 | |
294 | |
270 | return 0; |
295 | return 0; |
271 | } |
296 | } |
272 | |
297 | |
273 | int event_base_dispatch (struct event_base *base) |
298 | int event_base_dispatch (struct event_base *base) |
274 | { |
299 | { |
275 | return event_base_loop (base, 0); |
300 | return event_base_loop (base, 0); |
276 | } |
301 | } |
277 | |
302 | |
278 | static void |
303 | static void |
279 | x_loopexit_cb (EV_P_ int revents, void *arg) |
304 | x_loopexit_cb (int revents, void *base) |
280 | { |
305 | { |
|
|
306 | dLOOPbase; |
|
|
307 | |
281 | ev_unloop (EV_A_ 2); |
308 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
282 | } |
309 | } |
283 | |
310 | |
284 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
311 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
285 | { |
312 | { |
286 | dLOOPbase; |
313 | dLOOPbase; |