… | |
… | |
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 | |
… | |
… | |
156 | } |
159 | } |
157 | |
160 | |
158 | static void |
161 | static void |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
162 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
160 | { |
163 | { |
161 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
164 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
165 | |
|
|
166 | event_del (ev); |
|
|
167 | |
|
|
168 | x_cb (ev, revents); |
162 | } |
169 | } |
163 | |
170 | |
164 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
171 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
165 | { |
172 | { |
166 | if (!ev->initialised) |
|
|
167 | { |
|
|
168 | ev->initialised = 1; |
|
|
169 | |
|
|
170 | if (events & EV_SIGNAL) |
173 | if (events & EV_SIGNAL) |
171 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
174 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
172 | else |
175 | else |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
176 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
174 | |
177 | |
175 | ev_watcher_init (&ev->to, x_cb_to); |
178 | ev_watcher_init (&ev->to, x_cb_to); |
176 | } |
|
|
177 | |
179 | |
178 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
180 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
179 | ev->ev_fd = fd; |
181 | ev->ev_fd = fd; |
180 | ev->ev_events = events; |
182 | ev->ev_events = events; |
181 | ev->ev_pri = 0; |
183 | ev->ev_pri = 0; |
… | |
… | |
224 | { |
226 | { |
225 | /* sig */ |
227 | /* sig */ |
226 | if (ev_is_active (&ev->iosig.sig)) |
228 | if (ev_is_active (&ev->iosig.sig)) |
227 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
229 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
228 | } |
230 | } |
229 | else |
231 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
230 | { |
232 | { |
231 | /* io */ |
233 | /* io */ |
232 | if (ev_is_active (&ev->iosig.io)) |
234 | if (ev_is_active (&ev->iosig.io)) |
233 | ev_io_stop (EV_A_ &ev->iosig.io); |
235 | ev_io_stop (EV_A_ &ev->iosig.io); |
234 | } |
236 | } |
… | |
… | |
239 | return 0; |
241 | return 0; |
240 | } |
242 | } |
241 | |
243 | |
242 | int event_pending (struct event *ev, short events, struct timeval *tv) |
244 | int event_pending (struct event *ev, short events, struct timeval *tv) |
243 | { |
245 | { |
|
|
246 | short revents = 0; |
244 | dLOOPev; |
247 | dLOOPev; |
245 | |
248 | |
246 | short revents = 0; |
|
|
247 | |
249 | |
248 | if (ev->ev_events & EV_SIGNAL) |
250 | if (ev->ev_events & EV_SIGNAL) |
249 | { |
251 | { |
250 | /* sig */ |
252 | /* sig */ |
251 | if (ev->iosig.sig.pending) |
253 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
252 | revents |= EV_SIGNAL; |
254 | revents |= EV_SIGNAL; |
253 | } |
255 | } |
254 | else |
256 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
255 | { |
257 | { |
256 | /* io */ |
258 | /* io */ |
257 | if (ev->iosig.io.pending) |
259 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
258 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
260 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
259 | } |
261 | } |
260 | |
262 | |
261 | if (ev->to.pending) |
263 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
262 | { |
264 | { |
263 | revents |= EV_TIMEOUT; |
265 | revents |= EV_TIMEOUT; |
264 | |
266 | |
265 | if (tv) |
267 | if (tv) |
266 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
268 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
310 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
312 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
311 | } |
313 | } |
312 | |
314 | |
313 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
315 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
314 | { |
316 | { |
315 | dLOOPbase; |
|
|
316 | ev_tstamp after = tv_get (tv); |
317 | ev_tstamp after = tv_get (tv); |
|
|
318 | dLOOPbase; |
317 | |
319 | |
318 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
320 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
319 | |
321 | |
320 | return -1; |
322 | return -1; |
321 | } |
323 | } |
… | |
… | |
328 | }; |
330 | }; |
329 | |
331 | |
330 | static void |
332 | static void |
331 | x_once_cb (int revents, void *arg) |
333 | x_once_cb (int revents, void *arg) |
332 | { |
334 | { |
333 | struct x_once *once = arg; |
335 | struct x_once *once = (struct x_once *)arg; |
334 | |
336 | |
335 | once->cb (once->fd, revents, once->arg); |
337 | once->cb (once->fd, revents, once->arg); |
336 | free (once); |
338 | free (once); |
337 | } |
339 | } |
338 | |
340 | |
339 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
341 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
340 | { |
342 | { |
341 | dLOOPbase; |
|
|
342 | struct x_once *once = malloc (sizeof (struct x_once)); |
343 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
344 | dLOOPbase; |
343 | |
345 | |
344 | if (!once) |
346 | if (!once) |
345 | return -1; |
347 | return -1; |
346 | |
348 | |
347 | once->fd = fd; |
349 | once->fd = fd; |