… | |
… | |
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> |
36 | |
35 | |
|
|
36 | #ifndef WIN32 |
|
|
37 | # include <sys/time.h> |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | #include "ev.h" |
37 | #include "event.h" |
41 | #include "event.h" |
38 | |
42 | |
39 | #if EV_MULTIPLICITY |
43 | #if EV_MULTIPLICITY |
40 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
44 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
41 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
45 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
… | |
… | |
84 | { |
88 | { |
85 | #if EV_MULTIPLICITY |
89 | #if EV_MULTIPLICITY |
86 | if (x_cur) |
90 | if (x_cur) |
87 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
91 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
88 | else |
92 | else |
89 | x_cur = ev_default_loop (EVMETHOD_AUTO); |
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
90 | #else |
94 | #else |
91 | 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)); |
92 | |
96 | |
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
97 | x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
94 | #endif |
98 | #endif |
95 | |
99 | |
96 | return x_cur; |
100 | return x_cur; |
97 | } |
101 | } |
98 | |
102 | |
99 | void event_base_free (struct event_base *base) |
103 | void event_base_free (struct event_base *base) |
100 | { |
104 | { |
101 | dLOOPbase; |
105 | dLOOPbase; |
102 | |
106 | |
103 | #if EV_MULTIPLICITY |
107 | #if EV_MULTIPLICITY |
|
|
108 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
104 | ev_loop_delete (loop); |
109 | ev_loop_destroy (loop); |
105 | #endif |
110 | #endif |
106 | } |
111 | } |
107 | |
112 | |
108 | int event_dispatch (void) |
113 | int event_dispatch (void) |
109 | { |
114 | { |
… | |
… | |
137 | } |
142 | } |
138 | |
143 | |
139 | static void |
144 | static void |
140 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
145 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
141 | { |
146 | { |
142 | 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); |
143 | } |
153 | } |
144 | |
154 | |
145 | static void |
155 | static void |
146 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
156 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
147 | { |
157 | { |
148 | 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)); |
149 | |
159 | |
|
|
160 | if (revents & EV_ERROR) |
|
|
161 | event_del (ev); |
150 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
162 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
151 | ev_io_stop (EV_A_ w); |
163 | ev_io_stop (EV_A_ w); |
152 | |
164 | |
153 | x_cb (ev, revents); |
165 | x_cb (ev, revents); |
154 | } |
166 | } |
155 | |
167 | |
156 | static void |
168 | static void |
157 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
169 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
158 | { |
170 | { |
159 | 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); |
160 | } |
176 | } |
161 | |
177 | |
162 | 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) |
163 | { |
179 | { |
164 | if (!ev->initialised) |
|
|
165 | { |
|
|
166 | ev->initialised = 1; |
|
|
167 | |
|
|
168 | if (events & EV_SIGNAL) |
180 | if (events & EV_SIGNAL) |
169 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
181 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
170 | else |
182 | else |
171 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
183 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
172 | |
184 | |
173 | ev_watcher_init (&ev->to, x_cb_to); |
185 | ev_watcher_init (&ev->to, x_cb_to); |
174 | } |
|
|
175 | |
186 | |
176 | 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 */ |
177 | ev->ev_fd = fd; |
188 | ev->ev_fd = fd; |
178 | ev->ev_events = events; |
189 | ev->ev_events = events; |
179 | ev->ev_pri = 0; |
190 | ev->ev_pri = 0; |
… | |
… | |
222 | { |
233 | { |
223 | /* sig */ |
234 | /* sig */ |
224 | if (ev_is_active (&ev->iosig.sig)) |
235 | if (ev_is_active (&ev->iosig.sig)) |
225 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
236 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
226 | } |
237 | } |
227 | else |
238 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
228 | { |
239 | { |
229 | /* io */ |
240 | /* io */ |
230 | if (ev_is_active (&ev->iosig.io)) |
241 | if (ev_is_active (&ev->iosig.io)) |
231 | ev_io_stop (EV_A_ &ev->iosig.io); |
242 | ev_io_stop (EV_A_ &ev->iosig.io); |
232 | } |
243 | } |
… | |
… | |
235 | ev_timer_stop (EV_A_ &ev->to); |
246 | ev_timer_stop (EV_A_ &ev->to); |
236 | |
247 | |
237 | return 0; |
248 | return 0; |
238 | } |
249 | } |
239 | |
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 | |
240 | int event_pending (struct event *ev, short events, struct timeval *tv) |
265 | int event_pending (struct event *ev, short events, struct timeval *tv) |
241 | { |
266 | { |
|
|
267 | short revents = 0; |
242 | dLOOPev; |
268 | dLOOPev; |
243 | |
269 | |
244 | short revents = 0; |
|
|
245 | |
270 | |
246 | if (ev->ev_events & EV_SIGNAL) |
271 | if (ev->ev_events & EV_SIGNAL) |
247 | { |
272 | { |
248 | /* sig */ |
273 | /* sig */ |
249 | if (ev->iosig.sig.pending) |
274 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
250 | revents |= EV_SIGNAL; |
275 | revents |= EV_SIGNAL; |
251 | } |
276 | } |
252 | else |
277 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
253 | { |
278 | { |
254 | /* io */ |
279 | /* io */ |
255 | if (ev->iosig.io.pending) |
280 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
256 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
281 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | } |
282 | } |
258 | |
283 | |
259 | if (ev->to.pending) |
284 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
260 | { |
285 | { |
261 | revents |= EV_TIMEOUT; |
286 | revents |= EV_TIMEOUT; |
262 | |
287 | |
263 | if (tv) |
288 | if (tv) |
264 | 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 :) */ |
… | |
… | |
308 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
333 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
309 | } |
334 | } |
310 | |
335 | |
311 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
336 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
312 | { |
337 | { |
313 | dLOOPbase; |
|
|
314 | ev_tstamp after = tv_get (tv); |
338 | ev_tstamp after = tv_get (tv); |
|
|
339 | dLOOPbase; |
315 | |
340 | |
316 | 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); |
317 | |
342 | |
318 | return -1; |
343 | return -1; |
319 | } |
344 | } |
… | |
… | |
326 | }; |
351 | }; |
327 | |
352 | |
328 | static void |
353 | static void |
329 | x_once_cb (int revents, void *arg) |
354 | x_once_cb (int revents, void *arg) |
330 | { |
355 | { |
331 | struct x_once *once = arg; |
356 | struct x_once *once = (struct x_once *)arg; |
332 | |
357 | |
333 | once->cb (once->fd, revents, once->arg); |
358 | once->cb (once->fd, revents, once->arg); |
334 | free (once); |
359 | free (once); |
335 | } |
360 | } |
336 | |
361 | |
337 | 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) |
338 | { |
363 | { |
339 | dLOOPbase; |
|
|
340 | struct x_once *once = malloc (sizeof (struct x_once)); |
364 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
365 | dLOOPbase; |
341 | |
366 | |
342 | if (!once) |
367 | if (!once) |
343 | return -1; |
368 | return -1; |
344 | |
369 | |
345 | once->fd = fd; |
370 | once->fd = fd; |
… | |
… | |
351 | return 0; |
376 | return 0; |
352 | } |
377 | } |
353 | |
378 | |
354 | int event_base_priority_init (struct event_base *base, int npri) |
379 | int event_base_priority_init (struct event_base *base, int npri) |
355 | { |
380 | { |
356 | dLOOPbase; |
381 | /*dLOOPbase;*/ |
357 | |
382 | |
358 | return 0; |
383 | return 0; |
359 | } |
384 | } |
360 | |
385 | |