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