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