… | |
… | |
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 | |
… | |
… | |
223 | { |
233 | { |
224 | /* sig */ |
234 | /* sig */ |
225 | if (ev_is_active (&ev->iosig.sig)) |
235 | if (ev_is_active (&ev->iosig.sig)) |
226 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
236 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
227 | } |
237 | } |
228 | else |
238 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
229 | { |
239 | { |
230 | /* io */ |
240 | /* io */ |
231 | if (ev_is_active (&ev->iosig.io)) |
241 | if (ev_is_active (&ev->iosig.io)) |
232 | ev_io_stop (EV_A_ &ev->iosig.io); |
242 | ev_io_stop (EV_A_ &ev->iosig.io); |
233 | } |
243 | } |
… | |
… | |
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 | dLOOPev; |
|
|
254 | |
|
|
255 | if (res & EV_TIMEOUT) |
|
|
256 | ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT); |
|
|
257 | |
|
|
258 | if (res & EV_SIGNAL) |
|
|
259 | ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL); |
|
|
260 | |
|
|
261 | if (res & (EV_READ | EV_WRITE)) |
|
|
262 | ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE)); |
|
|
263 | } |
|
|
264 | |
241 | int event_pending (struct event *ev, short events, struct timeval *tv) |
265 | int event_pending (struct event *ev, short events, struct timeval *tv) |
242 | { |
266 | { |
|
|
267 | short revents = 0; |
243 | dLOOPev; |
268 | dLOOPev; |
244 | |
269 | |
245 | short revents = 0; |
|
|
246 | |
270 | |
247 | if (ev->ev_events & EV_SIGNAL) |
271 | if (ev->ev_events & EV_SIGNAL) |
248 | { |
272 | { |
249 | /* sig */ |
273 | /* sig */ |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
274 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
251 | revents |= EV_SIGNAL; |
275 | revents |= EV_SIGNAL; |
252 | } |
276 | } |
253 | else |
277 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
254 | { |
278 | { |
255 | /* io */ |
279 | /* io */ |
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
280 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
281 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
258 | } |
282 | } |
… | |
… | |
309 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
333 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
310 | } |
334 | } |
311 | |
335 | |
312 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
336 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
313 | { |
337 | { |
314 | dLOOPbase; |
|
|
315 | ev_tstamp after = tv_get (tv); |
338 | ev_tstamp after = tv_get (tv); |
|
|
339 | dLOOPbase; |
316 | |
340 | |
317 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
341 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
318 | |
342 | |
319 | return -1; |
343 | return -1; |
320 | } |
344 | } |
… | |
… | |
327 | }; |
351 | }; |
328 | |
352 | |
329 | static void |
353 | static void |
330 | x_once_cb (int revents, void *arg) |
354 | x_once_cb (int revents, void *arg) |
331 | { |
355 | { |
332 | struct x_once *once = arg; |
356 | struct x_once *once = (struct x_once *)arg; |
333 | |
357 | |
334 | once->cb (once->fd, revents, once->arg); |
358 | once->cb (once->fd, revents, once->arg); |
335 | free (once); |
359 | free (once); |
336 | } |
360 | } |
337 | |
361 | |
338 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
362 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
339 | { |
363 | { |
340 | dLOOPbase; |
|
|
341 | struct x_once *once = malloc (sizeof (struct x_once)); |
364 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
365 | dLOOPbase; |
342 | |
366 | |
343 | if (!once) |
367 | if (!once) |
344 | return -1; |
368 | return -1; |
345 | |
369 | |
346 | once->fd = fd; |
370 | once->fd = fd; |