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