… | |
… | |
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 (EVMETHOD_AUTO); |
|
|
95 | else |
|
|
96 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_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 (EVMETHOD_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 (EVMETHOD_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 | { |
|
|
141 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
|
|
142 | |
|
|
143 | ev->ev_res = revents; |
|
|
144 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
|
|
145 | } |
|
|
146 | |
|
|
147 | static void |
|
|
148 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
|
|
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 | { |
|
|
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 | ev_io_stop (EV_A_ w); |
|
|
167 | |
|
|
168 | x_cb (ev, revents); |
|
|
169 | } |
|
|
170 | |
|
|
171 | static void |
|
|
172 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
|
|
173 | { |
|
|
174 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
175 | |
|
|
176 | event_del (ev); |
|
|
177 | |
|
|
178 | x_cb (ev, revents); |
|
|
179 | } |
|
|
180 | |
|
|
181 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
|
|
182 | { |
111 | if (ev->ev_events & EV_SIGNAL) |
183 | if (events & EV_SIGNAL) |
112 | { |
184 | ev_init (&ev->iosig.sig, x_cb_sig); |
113 | /* sig */ |
|
|
114 | if (ev_is_active (&ev->iosig.sig)) |
|
|
115 | { |
|
|
116 | ev_signal_stop (&ev->iosig.sig); |
|
|
117 | --x_actives; |
|
|
118 | } |
|
|
119 | } |
|
|
120 | else |
185 | else |
121 | { |
|
|
122 | /* io */ |
|
|
123 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->iosig.io)) |
|
|
124 | { |
|
|
125 | ev_io_stop (&ev->iosig.io); |
|
|
126 | --x_actives; |
|
|
127 | } |
|
|
128 | } |
|
|
129 | |
|
|
130 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
|
|
131 | |
|
|
132 | if (revents & EV_TIMEOUT) |
|
|
133 | --x_actives; |
|
|
134 | |
|
|
135 | ev->ev_res = revents; |
|
|
136 | ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); |
|
|
137 | } |
|
|
138 | |
|
|
139 | static void |
|
|
140 | x_cb_io (struct ev_io *w, int revents) |
|
|
141 | { |
|
|
142 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.io)), revents); |
|
|
143 | } |
|
|
144 | |
|
|
145 | static void |
|
|
146 | x_cb_to (struct ev_timer *w, int revents) |
|
|
147 | { |
|
|
148 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
|
|
149 | } |
|
|
150 | |
|
|
151 | static void |
|
|
152 | x_cb_sig (struct ev_signal *w, int revents) |
|
|
153 | { |
|
|
154 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents); |
|
|
155 | } |
|
|
156 | |
|
|
157 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
|
|
158 | { |
|
|
159 | if (events & EV_SIGNAL) |
|
|
160 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
|
|
161 | else |
|
|
162 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
186 | ev_init (&ev->iosig.io, x_cb_io); |
163 | |
187 | |
164 | ev_watcher_init (&ev->to, x_cb_to); |
188 | ev_init (&ev->to, x_cb_to); |
165 | |
189 | |
166 | ev->ev_base = x_cur; |
190 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
167 | ev->ev_fd = fd; |
191 | ev->ev_fd = fd; |
168 | ev->ev_events = events; |
192 | ev->ev_events = events; |
169 | ev->ev_pri = 0; |
193 | ev->ev_pri = 0; |
170 | ev->ev_callback = cb; |
194 | ev->ev_callback = cb; |
171 | ev->ev_arg = arg; |
195 | ev->ev_arg = arg; |
172 | ev->ev_res = 0; |
196 | ev->ev_res = 0; |
173 | } |
197 | } |
174 | |
198 | |
175 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
199 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
176 | { |
200 | { |
177 | event_base_once (x_cur, fd, events, cb, arg, tv); |
201 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
178 | } |
202 | } |
179 | |
203 | |
180 | int event_add (struct event *ev, struct timeval *tv) |
204 | int event_add (struct event *ev, struct timeval *tv) |
181 | { |
205 | { |
|
|
206 | dLOOPev; |
|
|
207 | |
182 | /* disable all watchers */ |
208 | /* disable all watchers */ |
183 | event_del (ev); |
209 | event_del (ev); |
184 | |
210 | |
185 | if (ev->ev_events & EV_SIGNAL) |
211 | if (ev->ev_events & EV_SIGNAL) |
186 | { |
212 | { |
187 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
213 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
188 | ev_signal_start (&ev->iosig.sig); |
214 | ev_signal_start (EV_A_ &ev->iosig.sig); |
189 | ++x_actives; |
|
|
190 | } |
215 | } |
191 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
216 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
192 | { |
217 | { |
193 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
218 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
194 | ev_io_start (&ev->iosig.io); |
219 | ev_io_start (EV_A_ &ev->iosig.io); |
195 | ++x_actives; |
|
|
196 | } |
220 | } |
197 | |
221 | |
198 | if (tv) |
222 | if (tv) |
199 | { |
223 | { |
200 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
224 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
201 | ev_timer_start (&ev->to); |
225 | ev_timer_start (EV_A_ &ev->to); |
202 | ++x_actives; |
|
|
203 | } |
226 | } |
204 | |
227 | |
205 | return 0; |
228 | return 0; |
206 | } |
229 | } |
207 | |
230 | |
208 | int event_del (struct event *ev) |
231 | int event_del (struct event *ev) |
209 | { |
232 | { |
|
|
233 | dLOOPev; |
|
|
234 | |
210 | if (ev->ev_events & EV_SIGNAL) |
235 | if (ev->ev_events & EV_SIGNAL) |
211 | { |
236 | { |
212 | /* sig */ |
237 | /* sig */ |
213 | if (ev_is_active (&ev->iosig.sig)) |
238 | if (ev_is_active (&ev->iosig.sig)) |
214 | { |
|
|
215 | ev_signal_stop (&ev->iosig.sig); |
239 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
216 | --x_actives; |
|
|
217 | } |
|
|
218 | } |
240 | } |
219 | else |
241 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
220 | { |
242 | { |
221 | /* io */ |
243 | /* io */ |
222 | if (ev_is_active (&ev->iosig.io)) |
244 | if (ev_is_active (&ev->iosig.io)) |
223 | { |
|
|
224 | ev_io_stop (&ev->iosig.io); |
245 | ev_io_stop (EV_A_ &ev->iosig.io); |
225 | --x_actives; |
|
|
226 | } |
|
|
227 | } |
246 | } |
228 | |
247 | |
229 | if (ev_is_active (&ev->to)) |
248 | if (ev_is_active (&ev->to)) |
230 | { |
|
|
231 | ev_timer_stop (&ev->to); |
249 | ev_timer_stop (EV_A_ &ev->to); |
232 | --x_actives; |
|
|
233 | } |
|
|
234 | |
250 | |
235 | return 0; |
251 | return 0; |
|
|
252 | } |
|
|
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)); |
236 | } |
266 | } |
237 | |
267 | |
238 | int event_pending (struct event *ev, short events, struct timeval *tv) |
268 | int event_pending (struct event *ev, short events, struct timeval *tv) |
239 | { |
269 | { |
240 | short revents; |
270 | short revents = 0; |
|
|
271 | dLOOPev; |
|
|
272 | |
241 | |
273 | |
242 | if (ev->ev_events & EV_SIGNAL) |
274 | if (ev->ev_events & EV_SIGNAL) |
243 | { |
275 | { |
244 | /* sig */ |
276 | /* sig */ |
245 | if (ev->iosig.sig.pending) |
277 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
246 | revents |= EV_SIGNAL; |
278 | revents |= EV_SIGNAL; |
247 | } |
279 | } |
248 | else |
280 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
249 | { |
281 | { |
250 | /* io */ |
282 | /* io */ |
251 | if (ev->iosig.io.pending) |
283 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
252 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
284 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
253 | } |
285 | } |
254 | |
286 | |
255 | if (ev->to.pending) |
287 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
256 | { |
288 | { |
257 | revents |= EV_TIMEOUT; |
289 | revents |= EV_TIMEOUT; |
258 | |
290 | |
259 | if (tv) |
291 | if (tv) |
260 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
292 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
261 | } |
293 | } |
262 | |
294 | |
263 | return events & revents; |
295 | return events & revents; |
264 | } |
296 | } |
265 | |
297 | |
… | |
… | |
282 | return 0; |
314 | return 0; |
283 | } |
315 | } |
284 | |
316 | |
285 | int event_base_loop (struct event_base *base, int flags) |
317 | int event_base_loop (struct event_base *base, int flags) |
286 | { |
318 | { |
287 | do |
319 | dLOOPbase; |
288 | { |
320 | |
289 | ev_loop (flags | EVLOOP_ONESHOT); |
321 | ev_loop (EV_A_ flags); |
290 | } |
|
|
291 | while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done); |
|
|
292 | |
322 | |
293 | return 0; |
323 | return 0; |
294 | } |
324 | } |
295 | |
325 | |
296 | int event_base_dispatch (struct event_base *base) |
326 | int event_base_dispatch (struct event_base *base) |
297 | { |
327 | { |
298 | return event_base_loop (base, 0); |
328 | return event_base_loop (base, 0); |
299 | } |
329 | } |
300 | |
330 | |
301 | static void |
331 | static void |
302 | x_loopexit_cb (int revents, void *arg) |
332 | x_loopexit_cb (int revents, void *base) |
303 | { |
333 | { |
304 | ev_loop_done = 2; |
334 | dLOOPbase; |
|
|
335 | |
|
|
336 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
305 | } |
337 | } |
306 | |
338 | |
307 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
339 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
308 | { |
340 | { |
309 | ev_tstamp after = tv_get (tv); |
341 | ev_tstamp after = tv_get (tv); |
|
|
342 | dLOOPbase; |
310 | |
343 | |
311 | ev_once (-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); |
|
|
345 | |
|
|
346 | return -1; |
312 | } |
347 | } |
313 | |
348 | |
314 | struct x_once |
349 | struct x_once |
315 | { |
350 | { |
316 | int fd; |
351 | int fd; |
… | |
… | |
319 | }; |
354 | }; |
320 | |
355 | |
321 | static void |
356 | static void |
322 | x_once_cb (int revents, void *arg) |
357 | x_once_cb (int revents, void *arg) |
323 | { |
358 | { |
324 | struct x_once *once = arg; |
359 | struct x_once *once = (struct x_once *)arg; |
325 | |
360 | |
326 | once->cb (once->fd, revents, once->arg); |
361 | once->cb (once->fd, (short)revents, once->arg); |
327 | free (once); |
362 | free (once); |
328 | } |
363 | } |
329 | |
364 | |
330 | 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) |
331 | { |
366 | { |
332 | struct x_once *once = malloc (sizeof (struct x_once)); |
367 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
368 | dLOOPbase; |
333 | |
369 | |
334 | if (!once) |
370 | if (!once) |
335 | return -1; |
371 | return -1; |
336 | |
372 | |
337 | once->fd = fd; |
373 | once->fd = fd; |
338 | once->cb = cb; |
374 | once->cb = cb; |
339 | once->arg = arg; |
375 | once->arg = arg; |
340 | |
376 | |
341 | ev_once (fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
377 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
342 | |
378 | |
343 | return 0; |
379 | return 0; |
344 | } |
380 | } |
345 | |
381 | |
346 | int event_base_priority_init (struct event_base *base, int npri) |
382 | int event_base_priority_init (struct event_base *base, int npri) |
347 | { |
383 | { |
348 | return 0; |
384 | /*dLOOPbase;*/ |
349 | } |
|
|
350 | |
385 | |
|
|
386 | return 0; |
|
|
387 | } |
|
|
388 | |