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