… | |
… | |
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 | |
|
|
37 | #include "ev.h" |
36 | #include "event.h" |
38 | #include "event.h" |
37 | |
39 | |
38 | #ifdef EV_MULTIPLICITY |
40 | #if EV_MULTIPLICITY |
39 | # define dLOOPev struct ev_loop *loop = ev->ev_base->loop |
41 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
40 | # define dLOOPbase struct ev_loop *loop = base->loop |
42 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
41 | #else |
43 | #else |
42 | # define dLOOPev |
44 | # define dLOOPev |
43 | # define dLOOPbase |
45 | # define dLOOPbase |
44 | #endif |
46 | #endif |
45 | |
47 | |
|
|
48 | /* never accessed, will always be cast from/to ev_loop */ |
46 | struct event_base |
49 | struct event_base |
47 | { |
50 | { |
48 | #ifdef EV_MULTIPLICITY |
|
|
49 | struct ev_loop *loop; |
|
|
50 | #endif |
|
|
51 | int dummy; |
51 | int dummy; |
52 | }; |
52 | }; |
53 | |
53 | |
54 | static struct event_base x_base, *x_cur; |
54 | static struct event_base *x_cur; |
55 | |
55 | |
56 | static void |
56 | static void |
57 | tv_set (struct timeval *tv, ev_tstamp at) |
57 | tv_set (struct timeval *tv, ev_tstamp at) |
58 | { |
58 | { |
59 | tv->tv_sec = (long)at; |
59 | tv->tv_sec = (long)at; |
… | |
… | |
81 | return "libev"; |
81 | return "libev"; |
82 | } |
82 | } |
83 | |
83 | |
84 | void *event_init (void) |
84 | void *event_init (void) |
85 | { |
85 | { |
86 | if (!x_cur && ev_init (0)) |
86 | #if EV_MULTIPLICITY |
87 | return x_cur = &x_base; |
87 | if (x_cur) |
|
|
88 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
|
|
89 | else |
|
|
90 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
|
|
91 | #else |
|
|
92 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
88 | |
93 | |
|
|
94 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
|
|
95 | #endif |
|
|
96 | |
89 | return 0; |
97 | return x_cur; |
90 | } |
98 | } |
91 | |
99 | |
92 | void event_base_free (struct event_base *base) |
100 | void event_base_free (struct event_base *base) |
93 | { |
101 | { |
94 | dLOOPbase; |
102 | dLOOPbase; |
95 | |
103 | |
96 | /* nop */ |
104 | #if EV_MULTIPLICITY |
|
|
105 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
|
|
106 | ev_loop_destroy (loop); |
|
|
107 | #endif |
97 | } |
108 | } |
98 | |
109 | |
99 | int event_dispatch (void) |
110 | int event_dispatch (void) |
100 | { |
111 | { |
101 | return event_base_dispatch (x_cur); |
112 | return event_base_dispatch (x_cur); |
… | |
… | |
137 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
148 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
138 | { |
149 | { |
139 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
150 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
140 | |
151 | |
141 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
152 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
142 | ev_io_stop (w); |
153 | ev_io_stop (EV_A_ w); |
143 | |
154 | |
144 | x_cb (ev, revents); |
155 | x_cb (ev, revents); |
145 | } |
156 | } |
146 | |
157 | |
147 | static void |
158 | static void |
148 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
149 | { |
160 | { |
150 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
161 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
162 | |
|
|
163 | event_del (ev); |
|
|
164 | |
|
|
165 | x_cb (ev, revents); |
151 | } |
166 | } |
152 | |
167 | |
153 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
168 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
154 | { |
169 | { |
155 | if (events & EV_SIGNAL) |
170 | if (events & EV_SIGNAL) |
… | |
… | |
157 | else |
172 | else |
158 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
159 | |
174 | |
160 | ev_watcher_init (&ev->to, x_cb_to); |
175 | ev_watcher_init (&ev->to, x_cb_to); |
161 | |
176 | |
162 | ev->ev_base = x_cur; |
177 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
163 | ev->ev_fd = fd; |
178 | ev->ev_fd = fd; |
164 | ev->ev_events = events; |
179 | ev->ev_events = events; |
165 | ev->ev_pri = 0; |
180 | ev->ev_pri = 0; |
166 | ev->ev_callback = cb; |
181 | ev->ev_callback = cb; |
167 | ev->ev_arg = arg; |
182 | ev->ev_arg = arg; |
… | |
… | |
230 | short revents = 0; |
245 | short revents = 0; |
231 | |
246 | |
232 | if (ev->ev_events & EV_SIGNAL) |
247 | if (ev->ev_events & EV_SIGNAL) |
233 | { |
248 | { |
234 | /* sig */ |
249 | /* sig */ |
235 | if (ev->iosig.sig.pending) |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
236 | revents |= EV_SIGNAL; |
251 | revents |= EV_SIGNAL; |
237 | } |
252 | } |
238 | else |
253 | else |
239 | { |
254 | { |
240 | /* io */ |
255 | /* io */ |
241 | if (ev->iosig.io.pending) |
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
242 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
243 | } |
258 | } |
244 | |
259 | |
245 | if (ev->to.pending) |
260 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
246 | { |
261 | { |
247 | revents |= EV_TIMEOUT; |
262 | revents |= EV_TIMEOUT; |
248 | |
263 | |
249 | if (tv) |
264 | if (tv) |
250 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
265 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
285 | { |
300 | { |
286 | return event_base_loop (base, 0); |
301 | return event_base_loop (base, 0); |
287 | } |
302 | } |
288 | |
303 | |
289 | static void |
304 | static void |
290 | x_loopexit_cb (EV_P_ int revents, void *arg) |
305 | x_loopexit_cb (int revents, void *base) |
291 | { |
306 | { |
|
|
307 | dLOOPbase; |
|
|
308 | |
292 | ev_unloop (EV_A_ 2); |
309 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
293 | } |
310 | } |
294 | |
311 | |
295 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
312 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
296 | { |
313 | { |
297 | dLOOPbase; |
314 | dLOOPbase; |
… | |
… | |
335 | return 0; |
352 | return 0; |
336 | } |
353 | } |
337 | |
354 | |
338 | int event_base_priority_init (struct event_base *base, int npri) |
355 | int event_base_priority_init (struct event_base *base, int npri) |
339 | { |
356 | { |
340 | dLOOPbase; |
357 | /*dLOOPbase;*/ |
341 | |
358 | |
342 | return 0; |
359 | return 0; |
343 | } |
360 | } |
344 | |
361 | |