… | |
… | |
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 |
… | |
… | |
85 | { |
88 | { |
86 | #if EV_MULTIPLICITY |
89 | #if EV_MULTIPLICITY |
87 | if (x_cur) |
90 | if (x_cur) |
88 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
91 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
89 | else |
92 | else |
90 | x_cur = 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 | |
100 | void event_base_free (struct event_base *base) |
103 | void event_base_free (struct event_base *base) |
101 | { |
104 | { |
102 | dLOOPbase; |
105 | dLOOPbase; |
103 | |
106 | |
104 | #if EV_MULTIPLICITY |
107 | #if EV_MULTIPLICITY |
|
|
108 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
105 | ev_loop_delete (loop); |
109 | ev_loop_destroy (loop); |
106 | #endif |
110 | #endif |
107 | } |
111 | } |
108 | |
112 | |
109 | int event_dispatch (void) |
113 | int event_dispatch (void) |
110 | { |
114 | { |
… | |
… | |
138 | } |
142 | } |
139 | |
143 | |
140 | static void |
144 | static void |
141 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
145 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
142 | { |
146 | { |
143 | 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); |
144 | } |
153 | } |
145 | |
154 | |
146 | static void |
155 | static void |
147 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
156 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
148 | { |
157 | { |
149 | 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)); |
150 | |
159 | |
|
|
160 | if (revents & EV_ERROR) |
|
|
161 | event_del (ev); |
151 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
162 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
152 | ev_io_stop (EV_A_ w); |
163 | ev_io_stop (EV_A_ w); |
153 | |
164 | |
154 | x_cb (ev, revents); |
165 | x_cb (ev, revents); |
155 | } |
166 | } |
156 | |
167 | |
157 | static void |
168 | static void |
158 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
169 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
159 | { |
170 | { |
160 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
171 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
172 | |
|
|
173 | event_del (ev); |
|
|
174 | |
|
|
175 | x_cb (ev, revents); |
161 | } |
176 | } |
162 | |
177 | |
163 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
178 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
164 | { |
179 | { |
165 | if (!ev->initialised) |
|
|
166 | { |
|
|
167 | ev->initialised = 1; |
|
|
168 | |
|
|
169 | if (events & EV_SIGNAL) |
180 | if (events & EV_SIGNAL) |
170 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
181 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
171 | else |
182 | else |
172 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
183 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
173 | |
184 | |
174 | ev_watcher_init (&ev->to, x_cb_to); |
185 | ev_watcher_init (&ev->to, x_cb_to); |
175 | } |
|
|
176 | |
186 | |
177 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
187 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
178 | ev->ev_fd = fd; |
188 | ev->ev_fd = fd; |
179 | ev->ev_events = events; |
189 | ev->ev_events = events; |
180 | ev->ev_pri = 0; |
190 | ev->ev_pri = 0; |
… | |
… | |
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 | if (res & EV_TIMEOUT) |
|
|
254 | ev_feed_event (&ev->to, res & EV_TIMEOUT); |
|
|
255 | |
|
|
256 | if (res & EV_SIGNAL) |
|
|
257 | ev_feed_event (&ev->iosig.sig, res & EV_SIGNAL); |
|
|
258 | |
|
|
259 | if (res & (EV_READ | EV_WRITE)) |
|
|
260 | ev_feed_event (&ev->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->iosig.sig.pending) |
272 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
251 | revents |= EV_SIGNAL; |
273 | revents |= EV_SIGNAL; |
252 | } |
274 | } |
253 | else |
275 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
254 | { |
276 | { |
255 | /* io */ |
277 | /* io */ |
256 | if (ev->iosig.io.pending) |
278 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
279 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
258 | } |
280 | } |
259 | |
281 | |
260 | if (ev->to.pending) |
282 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
261 | { |
283 | { |
262 | revents |= EV_TIMEOUT; |
284 | revents |= EV_TIMEOUT; |
263 | |
285 | |
264 | if (tv) |
286 | if (tv) |
265 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
287 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
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; |
… | |
… | |
352 | return 0; |
374 | return 0; |
353 | } |
375 | } |
354 | |
376 | |
355 | int event_base_priority_init (struct event_base *base, int npri) |
377 | int event_base_priority_init (struct event_base *base, int npri) |
356 | { |
378 | { |
357 | dLOOPbase; |
379 | /*dLOOPbase;*/ |
358 | |
380 | |
359 | return 0; |
381 | return 0; |
360 | } |
382 | } |
361 | |
383 | |