… | |
… | |
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 | |
158 | static void |
168 | static void |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
169 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
160 | { |
170 | { |
161 | 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); |
162 | } |
176 | } |
163 | |
177 | |
164 | 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) |
165 | { |
179 | { |
166 | if (!ev->initialised) |
|
|
167 | { |
|
|
168 | ev->initialised = 1; |
|
|
169 | |
|
|
170 | if (events & EV_SIGNAL) |
180 | if (events & EV_SIGNAL) |
171 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
181 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
172 | else |
182 | else |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
183 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
174 | |
184 | |
175 | ev_watcher_init (&ev->to, x_cb_to); |
185 | ev_watcher_init (&ev->to, x_cb_to); |
176 | } |
|
|
177 | |
186 | |
178 | 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 */ |
179 | ev->ev_fd = fd; |
188 | ev->ev_fd = fd; |
180 | ev->ev_events = events; |
189 | ev->ev_events = events; |
181 | ev->ev_pri = 0; |
190 | ev->ev_pri = 0; |
… | |
… | |
224 | { |
233 | { |
225 | /* sig */ |
234 | /* sig */ |
226 | if (ev_is_active (&ev->iosig.sig)) |
235 | if (ev_is_active (&ev->iosig.sig)) |
227 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
236 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
228 | } |
237 | } |
229 | else |
238 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
230 | { |
239 | { |
231 | /* io */ |
240 | /* io */ |
232 | if (ev_is_active (&ev->iosig.io)) |
241 | if (ev_is_active (&ev->iosig.io)) |
233 | ev_io_stop (EV_A_ &ev->iosig.io); |
242 | ev_io_stop (EV_A_ &ev->iosig.io); |
234 | } |
243 | } |
… | |
… | |
237 | ev_timer_stop (EV_A_ &ev->to); |
246 | ev_timer_stop (EV_A_ &ev->to); |
238 | |
247 | |
239 | return 0; |
248 | return 0; |
240 | } |
249 | } |
241 | |
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 | |
242 | int event_pending (struct event *ev, short events, struct timeval *tv) |
265 | int event_pending (struct event *ev, short events, struct timeval *tv) |
243 | { |
266 | { |
|
|
267 | short revents = 0; |
244 | dLOOPev; |
268 | dLOOPev; |
245 | |
269 | |
246 | short revents = 0; |
|
|
247 | |
270 | |
248 | if (ev->ev_events & EV_SIGNAL) |
271 | if (ev->ev_events & EV_SIGNAL) |
249 | { |
272 | { |
250 | /* sig */ |
273 | /* sig */ |
251 | if (ev->iosig.sig.pending) |
274 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
252 | revents |= EV_SIGNAL; |
275 | revents |= EV_SIGNAL; |
253 | } |
276 | } |
254 | else |
277 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
255 | { |
278 | { |
256 | /* io */ |
279 | /* io */ |
257 | if (ev->iosig.io.pending) |
280 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
258 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
281 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
259 | } |
282 | } |
260 | |
283 | |
261 | if (ev->to.pending) |
284 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
262 | { |
285 | { |
263 | revents |= EV_TIMEOUT; |
286 | revents |= EV_TIMEOUT; |
264 | |
287 | |
265 | if (tv) |
288 | if (tv) |
266 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
289 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
310 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
333 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
311 | } |
334 | } |
312 | |
335 | |
313 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
336 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
314 | { |
337 | { |
315 | dLOOPbase; |
|
|
316 | ev_tstamp after = tv_get (tv); |
338 | ev_tstamp after = tv_get (tv); |
|
|
339 | dLOOPbase; |
317 | |
340 | |
318 | 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); |
319 | |
342 | |
320 | return -1; |
343 | return -1; |
321 | } |
344 | } |
… | |
… | |
328 | }; |
351 | }; |
329 | |
352 | |
330 | static void |
353 | static void |
331 | x_once_cb (int revents, void *arg) |
354 | x_once_cb (int revents, void *arg) |
332 | { |
355 | { |
333 | struct x_once *once = arg; |
356 | struct x_once *once = (struct x_once *)arg; |
334 | |
357 | |
335 | once->cb (once->fd, revents, once->arg); |
358 | once->cb (once->fd, revents, once->arg); |
336 | free (once); |
359 | free (once); |
337 | } |
360 | } |
338 | |
361 | |
339 | 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) |
340 | { |
363 | { |
341 | dLOOPbase; |
|
|
342 | struct x_once *once = malloc (sizeof (struct x_once)); |
364 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
365 | dLOOPbase; |
343 | |
366 | |
344 | if (!once) |
367 | if (!once) |
345 | return -1; |
368 | return -1; |
346 | |
369 | |
347 | once->fd = fd; |
370 | once->fd = fd; |