… | |
… | |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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> |
|
|
35 | #include <assert.h> |
34 | #include <assert.h> |
|
|
35 | |
|
|
36 | #ifndef WIN32 |
|
|
37 | # include <sys/time.h> |
|
|
38 | #endif |
36 | |
39 | |
37 | #include "ev.h" |
40 | #include "ev.h" |
38 | #include "event.h" |
41 | #include "event.h" |
39 | |
42 | |
40 | #if EV_MULTIPLICITY |
43 | #if EV_MULTIPLICITY |
… | |
… | |
89 | else |
92 | else |
90 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
91 | #else |
94 | #else |
92 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
95 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
93 | |
96 | |
94 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
97 | x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
95 | #endif |
98 | #endif |
96 | |
99 | |
97 | return x_cur; |
100 | return x_cur; |
98 | } |
101 | } |
99 | |
102 | |
… | |
… | |
139 | } |
142 | } |
140 | |
143 | |
141 | static void |
144 | static void |
142 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
145 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
143 | { |
146 | { |
144 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents); |
147 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
|
|
148 | |
|
|
149 | if (revents & EV_ERROR) |
|
|
150 | event_del (ev); |
|
|
151 | |
|
|
152 | x_cb (ev, revents); |
145 | } |
153 | } |
146 | |
154 | |
147 | static void |
155 | static void |
148 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
156 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
149 | { |
157 | { |
150 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
151 | |
159 | |
|
|
160 | if (revents & EV_ERROR) |
|
|
161 | event_del (ev); |
152 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
162 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
153 | ev_io_stop (EV_A_ w); |
163 | ev_io_stop (EV_A_ w); |
154 | |
164 | |
155 | x_cb (ev, revents); |
165 | x_cb (ev, revents); |
156 | } |
166 | } |
157 | |
167 | |
… | |
… | |
236 | ev_timer_stop (EV_A_ &ev->to); |
246 | ev_timer_stop (EV_A_ &ev->to); |
237 | |
247 | |
238 | return 0; |
248 | return 0; |
239 | } |
249 | } |
240 | |
250 | |
|
|
251 | void event_active (struct event *ev, int res, short ncalls) |
|
|
252 | { |
|
|
253 | if (res & EV_TIMEOUT) |
|
|
254 | ev_feed_event (&iosig.to, res & EV_TIMEOUT); |
|
|
255 | |
|
|
256 | if (res & EV_SIGNAL) |
|
|
257 | ev_feed_event (&iosig.sig, res & EV_SIGNAL); |
|
|
258 | |
|
|
259 | if (res & (EV_READ | EV_WRITE)) |
|
|
260 | ev_feed_event (&iosig.io, res & (EV_READ | EV_WRITE)); |
|
|
261 | } |
|
|
262 | |
241 | int event_pending (struct event *ev, short events, struct timeval *tv) |
263 | int event_pending (struct event *ev, short events, struct timeval *tv) |
242 | { |
264 | { |
|
|
265 | short revents = 0; |
243 | dLOOPev; |
266 | dLOOPev; |
244 | |
267 | |
245 | short revents = 0; |
|
|
246 | |
268 | |
247 | if (ev->ev_events & EV_SIGNAL) |
269 | if (ev->ev_events & EV_SIGNAL) |
248 | { |
270 | { |
249 | /* sig */ |
271 | /* sig */ |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
272 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
… | |
… | |
309 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
331 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
310 | } |
332 | } |
311 | |
333 | |
312 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
334 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
313 | { |
335 | { |
314 | dLOOPbase; |
|
|
315 | ev_tstamp after = tv_get (tv); |
336 | ev_tstamp after = tv_get (tv); |
|
|
337 | dLOOPbase; |
316 | |
338 | |
317 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
339 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
318 | |
340 | |
319 | return -1; |
341 | return -1; |
320 | } |
342 | } |
… | |
… | |
327 | }; |
349 | }; |
328 | |
350 | |
329 | static void |
351 | static void |
330 | x_once_cb (int revents, void *arg) |
352 | x_once_cb (int revents, void *arg) |
331 | { |
353 | { |
332 | struct x_once *once = arg; |
354 | struct x_once *once = (struct x_once *)arg; |
333 | |
355 | |
334 | once->cb (once->fd, revents, once->arg); |
356 | once->cb (once->fd, revents, once->arg); |
335 | free (once); |
357 | free (once); |
336 | } |
358 | } |
337 | |
359 | |
338 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
360 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
339 | { |
361 | { |
340 | dLOOPbase; |
|
|
341 | struct x_once *once = malloc (sizeof (struct x_once)); |
362 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
363 | dLOOPbase; |
342 | |
364 | |
343 | if (!once) |
365 | if (!once) |
344 | return -1; |
366 | return -1; |
345 | |
367 | |
346 | once->fd = fd; |
368 | once->fd = fd; |