… | |
… | |
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 <assert.h> |
|
|
35 | |
|
|
36 | #ifndef WIN32 |
34 | #include <sys/time.h> |
37 | # include <sys/time.h> |
|
|
38 | #endif |
35 | |
39 | |
|
|
40 | #ifdef EV_EVENT_H |
|
|
41 | # include EV_EVENT_H |
|
|
42 | #else |
36 | #include "event.h" |
43 | # include "event.h" |
|
|
44 | #endif |
37 | |
45 | |
|
|
46 | #if EV_MULTIPLICITY |
|
|
47 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
|
|
48 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
|
|
49 | #else |
|
|
50 | # define dLOOPev |
|
|
51 | # define dLOOPbase |
|
|
52 | #endif |
|
|
53 | |
|
|
54 | /* never accessed, will always be cast from/to ev_loop */ |
38 | struct event_base |
55 | struct event_base |
39 | { |
56 | { |
40 | int dummy; |
57 | int dummy; |
41 | }; |
58 | }; |
42 | |
59 | |
43 | static int x_actives; |
|
|
44 | |
|
|
45 | static struct event_base x_base, *x_cur; |
60 | static struct event_base *x_cur; |
46 | |
61 | |
47 | static void |
62 | static void |
48 | tv_set (struct timeval *tv, ev_tstamp at) |
63 | tv_set (struct timeval *tv, ev_tstamp at) |
49 | { |
64 | { |
50 | tv->tv_sec = (long)at; |
65 | tv->tv_sec = (long)at; |
… | |
… | |
72 | return "libev"; |
87 | return "libev"; |
73 | } |
88 | } |
74 | |
89 | |
75 | void *event_init (void) |
90 | void *event_init (void) |
76 | { |
91 | { |
77 | if (ev_init (0)) |
92 | #if EV_MULTIPLICITY |
78 | return x_cur = &x_base; |
93 | if (x_cur) |
|
|
94 | x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
|
|
95 | else |
|
|
96 | x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
|
|
97 | #else |
|
|
98 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
79 | |
99 | |
|
|
100 | x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
|
|
101 | #endif |
|
|
102 | |
80 | return 0; |
103 | return x_cur; |
81 | } |
104 | } |
82 | |
105 | |
83 | void event_base_free (struct event_base *base) |
106 | void event_base_free (struct event_base *base) |
84 | { |
107 | { |
|
|
108 | dLOOPbase; |
|
|
109 | |
|
|
110 | #if EV_MULTIPLICITY |
|
|
111 | if (ev_default_loop (EVFLAG_AUTO) != loop) |
|
|
112 | ev_loop_destroy (loop); |
|
|
113 | #endif |
|
|
114 | } |
|
|
115 | |
|
|
116 | int event_dispatch (void) |
|
|
117 | { |
|
|
118 | return event_base_dispatch (x_cur); |
|
|
119 | } |
|
|
120 | |
|
|
121 | #ifdef EV_STANDALONE |
|
|
122 | void event_set_log_callback (event_log_cb cb) |
|
|
123 | { |
85 | /* nop */ |
124 | /* nop */ |
86 | } |
125 | } |
87 | |
126 | #endif |
88 | int event_dispatch (void) |
|
|
89 | { |
|
|
90 | return event_base_dispatch (x_cur); |
|
|
91 | } |
|
|
92 | |
|
|
93 | void event_set_log_callback (event_log_cb cb) |
|
|
94 | { |
|
|
95 | /* nop */ |
|
|
96 | } |
|
|
97 | |
127 | |
98 | int event_loop (int flags) |
128 | int event_loop (int flags) |
99 | { |
129 | { |
100 | return event_base_loop (x_cur, flags); |
130 | return event_base_loop (x_cur, flags); |
101 | } |
131 | } |
102 | |
132 | |
103 | int event_loopexit (struct timeval *tv) |
133 | int event_loopexit (struct timeval *tv) |
104 | { |
134 | { |
105 | event_base_loopexit (x_cur, tv); |
135 | return event_base_loopexit (x_cur, tv); |
106 | } |
136 | } |
107 | |
137 | |
108 | static void |
138 | static void |
109 | x_cb (struct event *ev, int revents) |
139 | x_cb (struct event *ev, int revents) |
110 | { |
140 | { |
111 | if (ev_is_active (&ev->sig)) |
|
|
112 | { |
|
|
113 | ev_signal_stop (&ev->sig); |
|
|
114 | --x_actives; |
|
|
115 | } |
|
|
116 | |
|
|
117 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->io)) |
|
|
118 | { |
|
|
119 | ev_io_stop (&ev->io); |
|
|
120 | --x_actives; |
|
|
121 | } |
|
|
122 | |
|
|
123 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
141 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
124 | |
142 | |
125 | if (revents & EV_TIMEOUT) |
|
|
126 | --x_actives; |
|
|
127 | |
|
|
128 | ev->ev_res = revents; |
143 | ev->ev_res = revents; |
129 | ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); |
144 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
130 | } |
145 | } |
131 | |
146 | |
132 | static void |
147 | static void |
133 | x_cb_io (struct ev_io *w, int revents) |
|
|
134 | { |
|
|
135 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, io)), revents); |
|
|
136 | } |
|
|
137 | |
|
|
138 | static void |
|
|
139 | x_cb_to (struct ev_timer *w, int revents) |
|
|
140 | { |
|
|
141 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
|
|
142 | } |
|
|
143 | |
|
|
144 | static void |
|
|
145 | x_cb_sig (struct ev_signal *w, int revents) |
148 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
146 | { |
149 | { |
|
|
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); |
|
|
156 | } |
|
|
157 | |
|
|
158 | static void |
|
|
159 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
|
|
160 | { |
147 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, sig)), revents); |
161 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
|
|
162 | |
|
|
163 | if (revents & EV_ERROR) |
|
|
164 | event_del (ev); |
|
|
165 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
|
|
166 | { |
|
|
167 | ev_io_stop (EV_A_ w); |
|
|
168 | ev->ev_flags &= ~EVLIST_INSERTED; |
|
|
169 | } |
|
|
170 | |
|
|
171 | x_cb (ev, revents); |
|
|
172 | } |
|
|
173 | |
|
|
174 | static void |
|
|
175 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
|
|
176 | { |
|
|
177 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
178 | |
|
|
179 | event_del (ev); |
|
|
180 | |
|
|
181 | x_cb (ev, revents); |
148 | } |
182 | } |
149 | |
183 | |
150 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
184 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
151 | { |
185 | { |
|
|
186 | if (events & EV_SIGNAL) |
|
|
187 | ev_init (&ev->iosig.sig, x_cb_sig); |
|
|
188 | else |
152 | ev_watcher_init (&ev->io, x_cb_io); |
189 | ev_init (&ev->iosig.io, x_cb_io); |
|
|
190 | |
153 | ev_watcher_init (&ev->to, x_cb_to); |
191 | ev_init (&ev->to, x_cb_to); |
154 | ev_watcher_init (&ev->sig, x_cb_sig); |
|
|
155 | |
192 | |
156 | ev->ev_base = x_cur; |
193 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
157 | ev->ev_fd = fd; |
194 | ev->ev_fd = fd; |
158 | ev->ev_events = events; |
195 | ev->ev_events = events; |
159 | ev->ev_pri = 0; |
196 | ev->ev_pri = 0; |
160 | ev->ev_callback = cb; |
197 | ev->ev_callback = cb; |
161 | ev->ev_arg = arg; |
198 | ev->ev_arg = arg; |
162 | ev->ev_res = 0; |
199 | ev->ev_res = 0; |
|
|
200 | ev->ev_flags = EVLIST_INIT; |
163 | } |
201 | } |
164 | |
202 | |
165 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
203 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
166 | { |
204 | { |
167 | event_base_once (x_cur, fd, events, cb, arg, tv); |
205 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
168 | } |
206 | } |
169 | |
207 | |
170 | int event_add (struct event *ev, struct timeval *tv) |
208 | int event_add (struct event *ev, struct timeval *tv) |
171 | { |
209 | { |
|
|
210 | dLOOPev; |
|
|
211 | |
|
|
212 | if (ev->ev_events & EV_SIGNAL) |
|
|
213 | { |
|
|
214 | if (!ev_is_active (&ev->iosig.sig)) |
|
|
215 | { |
|
|
216 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
|
|
217 | ev_signal_start (EV_A_ &ev->iosig.sig); |
|
|
218 | |
|
|
219 | ev->ev_flags |= EVLIST_SIGNAL; |
|
|
220 | } |
|
|
221 | } |
|
|
222 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
223 | { |
|
|
224 | if (!ev_is_active (&ev->iosig.io)) |
|
|
225 | { |
|
|
226 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
|
|
227 | ev_io_start (EV_A_ &ev->iosig.io); |
|
|
228 | |
|
|
229 | ev->ev_flags |= EVLIST_INSERTED; |
|
|
230 | } |
|
|
231 | } |
|
|
232 | |
172 | if (tv) |
233 | if (tv) |
173 | { |
234 | { |
|
|
235 | ev->to.repeat = tv_get (tv); |
|
|
236 | ev_timer_again (EV_A_ &ev->to); |
|
|
237 | ev->ev_flags |= EVLIST_TIMEOUT; |
|
|
238 | } |
|
|
239 | else |
174 | if (ev_is_active (&ev->to)) |
240 | if (ev_is_active (&ev->to)) |
175 | { |
|
|
176 | ev_timer_stop (&ev->to); |
241 | ev_timer_stop (EV_A_ &ev->to); |
177 | --x_actives; |
|
|
178 | } |
|
|
179 | |
242 | |
180 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
243 | ev->ev_flags |= EVLIST_ACTIVE; |
181 | ev_timer_start (&ev->to); |
|
|
182 | ++x_actives; |
|
|
183 | } |
|
|
184 | |
244 | |
185 | if (ev->ev_events & (EV_READ|EV_WRITE)) |
245 | return 0; |
186 | { |
246 | } |
187 | if (ev_is_active (&ev->io)) |
|
|
188 | { |
|
|
189 | ev_io_stop (&ev->io); |
|
|
190 | --x_actives; |
|
|
191 | } |
|
|
192 | |
247 | |
193 | ev_io_set (&ev->io, ev->ev_fd, ev->ev_events); |
248 | int event_del (struct event *ev) |
194 | ev_io_start (&ev->io); |
249 | { |
195 | ++x_actives; |
250 | dLOOPev; |
196 | } |
|
|
197 | |
251 | |
198 | if (ev->ev_events & EV_SIGNAL) |
252 | if (ev->ev_events & EV_SIGNAL) |
199 | { |
253 | { |
|
|
254 | /* sig */ |
|
|
255 | if (ev_is_active (&ev->iosig.sig)) |
|
|
256 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
|
|
257 | } |
|
|
258 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
259 | { |
|
|
260 | /* io */ |
200 | if (ev_is_active (&ev->sig)) |
261 | if (ev_is_active (&ev->iosig.io)) |
201 | { |
|
|
202 | ev_signal_stop (&ev->sig); |
|
|
203 | --x_actives; |
|
|
204 | } |
|
|
205 | |
|
|
206 | ev_signal_set (&ev->sig, ev->ev_fd); |
|
|
207 | ev_signal_start (&ev->sig); |
|
|
208 | ++x_actives; |
|
|
209 | } |
|
|
210 | |
|
|
211 | return 0; |
|
|
212 | } |
|
|
213 | |
|
|
214 | int event_del (struct event *ev) |
|
|
215 | { |
|
|
216 | if (ev_is_active (&ev->io)) |
|
|
217 | { |
|
|
218 | ev_io_stop (&ev->io); |
262 | ev_io_stop (EV_A_ &ev->iosig.io); |
219 | --x_actives; |
|
|
220 | } |
263 | } |
221 | |
264 | |
222 | if (ev_is_active (&ev->to)) |
265 | if (ev_is_active (&ev->to)) |
223 | { |
|
|
224 | ev_timer_stop (&ev->to); |
266 | ev_timer_stop (EV_A_ &ev->to); |
225 | --x_actives; |
|
|
226 | } |
|
|
227 | |
267 | |
228 | if (ev_is_active (&ev->sig)) |
268 | ev->ev_flags = EVLIST_INIT; |
229 | { |
|
|
230 | ev_signal_stop (&ev->sig); |
|
|
231 | --x_actives; |
|
|
232 | } |
|
|
233 | |
269 | |
234 | return 0; |
270 | return 0; |
235 | } |
271 | } |
236 | |
272 | |
237 | void event_active (struct event *ev, int fd, short events) |
273 | void event_active (struct event *ev, int res, short ncalls) |
238 | { |
274 | { |
239 | /* unclear what this does, likely internal-use */ |
275 | dLOOPev; |
|
|
276 | |
|
|
277 | if (res & EV_TIMEOUT) |
|
|
278 | ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT); |
|
|
279 | |
|
|
280 | if (res & EV_SIGNAL) |
|
|
281 | ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL); |
|
|
282 | |
|
|
283 | if (res & (EV_READ | EV_WRITE)) |
|
|
284 | ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE)); |
240 | } |
285 | } |
241 | |
286 | |
242 | int event_pending (struct event *ev, short events, struct timeval *tv) |
287 | int event_pending (struct event *ev, short events, struct timeval *tv) |
243 | { |
288 | { |
244 | short revents; |
289 | short revents = 0; |
|
|
290 | dLOOPev; |
245 | |
291 | |
246 | if (ev->io.pending) |
292 | |
|
|
293 | if (ev->ev_events & EV_SIGNAL) |
|
|
294 | { |
|
|
295 | /* sig */ |
|
|
296 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
|
|
297 | revents |= EV_SIGNAL; |
|
|
298 | } |
|
|
299 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
300 | { |
|
|
301 | /* io */ |
|
|
302 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
247 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
303 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
|
|
304 | } |
248 | |
305 | |
249 | if (ev->to.pending) |
306 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
250 | { |
307 | { |
251 | revents |= EV_TIMEOUT; |
308 | revents |= EV_TIMEOUT; |
252 | |
309 | |
253 | if (tv) |
310 | if (tv) |
254 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
311 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
255 | } |
312 | } |
256 | |
|
|
257 | if (ev->sig.pending) |
|
|
258 | revents |= EV_SIGNAL; |
|
|
259 | |
313 | |
260 | return events & revents; |
314 | return events & revents; |
261 | } |
315 | } |
262 | |
316 | |
263 | int event_priority_init (int npri) |
317 | int event_priority_init (int npri) |
… | |
… | |
279 | return 0; |
333 | return 0; |
280 | } |
334 | } |
281 | |
335 | |
282 | int event_base_loop (struct event_base *base, int flags) |
336 | int event_base_loop (struct event_base *base, int flags) |
283 | { |
337 | { |
284 | do |
338 | dLOOPbase; |
285 | { |
339 | |
286 | ev_loop (flags | EVLOOP_ONESHOT); |
340 | ev_loop (EV_A_ flags); |
287 | } |
|
|
288 | while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done); |
|
|
289 | |
341 | |
290 | return 0; |
342 | return 0; |
291 | } |
343 | } |
292 | |
344 | |
293 | int event_base_dispatch (struct event_base *base) |
345 | int event_base_dispatch (struct event_base *base) |
294 | { |
346 | { |
295 | return event_base_loop (base, 0); |
347 | return event_base_loop (base, 0); |
296 | } |
348 | } |
297 | |
349 | |
298 | static void |
350 | static void |
299 | x_loopexit_cb (int revents, void *arg) |
351 | x_loopexit_cb (int revents, void *base) |
300 | { |
352 | { |
301 | ev_loop_done = 2; |
353 | dLOOPbase; |
|
|
354 | |
|
|
355 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
302 | } |
356 | } |
303 | |
357 | |
304 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
358 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
305 | { |
359 | { |
306 | ev_tstamp after = tv_get (tv); |
360 | ev_tstamp after = tv_get (tv); |
|
|
361 | dLOOPbase; |
307 | |
362 | |
308 | ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
363 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
|
|
364 | |
|
|
365 | return -1; |
309 | } |
366 | } |
310 | |
367 | |
311 | struct x_once |
368 | struct x_once |
312 | { |
369 | { |
313 | int fd; |
370 | int fd; |
… | |
… | |
316 | }; |
373 | }; |
317 | |
374 | |
318 | static void |
375 | static void |
319 | x_once_cb (int revents, void *arg) |
376 | x_once_cb (int revents, void *arg) |
320 | { |
377 | { |
321 | struct x_once *once = arg; |
378 | struct x_once *once = (struct x_once *)arg; |
322 | |
379 | |
323 | once->cb (once->fd, revents, once->arg); |
380 | once->cb (once->fd, (short)revents, once->arg); |
324 | free (once); |
381 | free (once); |
325 | } |
382 | } |
326 | |
383 | |
327 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
384 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
328 | { |
385 | { |
329 | struct x_once *once = malloc (sizeof (struct x_once)); |
386 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
387 | dLOOPbase; |
330 | |
388 | |
331 | if (!once) |
389 | if (!once) |
332 | return -1; |
390 | return -1; |
333 | |
391 | |
334 | once->fd = fd; |
392 | once->fd = fd; |
335 | once->cb = cb; |
393 | once->cb = cb; |
336 | once->arg = arg; |
394 | once->arg = arg; |
337 | |
395 | |
338 | ev_once (fd, events & (EV_READ|EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
396 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
339 | |
397 | |
340 | return 0; |
398 | return 0; |
341 | } |
399 | } |
342 | |
400 | |
343 | int event_base_priority_init (struct event_base *base, int npri) |
401 | int event_base_priority_init (struct event_base *base, int npri) |
344 | { |
402 | { |
345 | return 0; |
403 | /*dLOOPbase;*/ |
346 | } |
|
|
347 | |
404 | |
|
|
405 | return 0; |
|
|
406 | } |
|
|
407 | |