… | |
… | |
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 | |
38 | #ifdef EV_MULTIPLICITY |
43 | #if EV_MULTIPLICITY |
39 | # define dLOOPev struct ev_loop *loop = ev->ev_base->loop |
44 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
40 | # define dLOOPbase struct ev_loop *loop = base->loop |
45 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
41 | #else |
46 | #else |
42 | # define dLOOPev |
47 | # define dLOOPev |
43 | # define dLOOPbase |
48 | # define dLOOPbase |
44 | #endif |
49 | #endif |
45 | |
50 | |
|
|
51 | /* never accessed, will always be cast from/to ev_loop */ |
46 | struct event_base |
52 | struct event_base |
47 | { |
53 | { |
48 | #ifdef EV_MULTIPLICITY |
|
|
49 | struct ev_loop *loop; |
|
|
50 | #endif |
|
|
51 | int dummy; |
54 | int dummy; |
52 | }; |
55 | }; |
53 | |
56 | |
54 | static struct event_base x_base, *x_cur; |
57 | static struct event_base *x_cur; |
55 | |
58 | |
56 | static void |
59 | static void |
57 | tv_set (struct timeval *tv, ev_tstamp at) |
60 | tv_set (struct timeval *tv, ev_tstamp at) |
58 | { |
61 | { |
59 | tv->tv_sec = (long)at; |
62 | tv->tv_sec = (long)at; |
… | |
… | |
81 | return "libev"; |
84 | return "libev"; |
82 | } |
85 | } |
83 | |
86 | |
84 | void *event_init (void) |
87 | void *event_init (void) |
85 | { |
88 | { |
86 | if (!x_cur && ev_init (0)) |
89 | #if EV_MULTIPLICITY |
87 | 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)); |
88 | |
96 | |
|
|
97 | x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
|
|
98 | #endif |
|
|
99 | |
89 | return 0; |
100 | return x_cur; |
90 | } |
101 | } |
91 | |
102 | |
92 | void event_base_free (struct event_base *base) |
103 | void event_base_free (struct event_base *base) |
93 | { |
104 | { |
94 | dLOOPbase; |
105 | dLOOPbase; |
95 | |
106 | |
96 | /* nop */ |
107 | #if EV_MULTIPLICITY |
|
|
108 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
|
|
109 | ev_loop_destroy (loop); |
|
|
110 | #endif |
97 | } |
111 | } |
98 | |
112 | |
99 | int event_dispatch (void) |
113 | int event_dispatch (void) |
100 | { |
114 | { |
101 | return event_base_dispatch (x_cur); |
115 | return event_base_dispatch (x_cur); |
… | |
… | |
128 | } |
142 | } |
129 | |
143 | |
130 | static void |
144 | static void |
131 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
145 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
132 | { |
146 | { |
133 | 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); |
134 | } |
153 | } |
135 | |
154 | |
136 | static void |
155 | static void |
137 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
156 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
138 | { |
157 | { |
139 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
140 | |
159 | |
|
|
160 | if (revents & EV_ERROR) |
|
|
161 | event_del (ev); |
141 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
162 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
142 | ev_io_stop (w); |
163 | ev_io_stop (EV_A_ w); |
143 | |
164 | |
144 | x_cb (ev, revents); |
165 | x_cb (ev, revents); |
145 | } |
166 | } |
146 | |
167 | |
147 | static void |
168 | static void |
148 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
169 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
149 | { |
170 | { |
150 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
171 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
172 | |
|
|
173 | event_del (ev); |
|
|
174 | |
|
|
175 | x_cb (ev, revents); |
151 | } |
176 | } |
152 | |
177 | |
153 | 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) |
154 | { |
179 | { |
155 | if (events & EV_SIGNAL) |
180 | if (events & EV_SIGNAL) |
… | |
… | |
157 | else |
182 | else |
158 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
183 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
159 | |
184 | |
160 | ev_watcher_init (&ev->to, x_cb_to); |
185 | ev_watcher_init (&ev->to, x_cb_to); |
161 | |
186 | |
162 | ev->ev_base = x_cur; |
187 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
163 | ev->ev_fd = fd; |
188 | ev->ev_fd = fd; |
164 | ev->ev_events = events; |
189 | ev->ev_events = events; |
165 | ev->ev_pri = 0; |
190 | ev->ev_pri = 0; |
166 | ev->ev_callback = cb; |
191 | ev->ev_callback = cb; |
167 | ev->ev_arg = arg; |
192 | ev->ev_arg = arg; |
… | |
… | |
208 | { |
233 | { |
209 | /* sig */ |
234 | /* sig */ |
210 | if (ev_is_active (&ev->iosig.sig)) |
235 | if (ev_is_active (&ev->iosig.sig)) |
211 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
236 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
212 | } |
237 | } |
213 | else |
238 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
214 | { |
239 | { |
215 | /* io */ |
240 | /* io */ |
216 | if (ev_is_active (&ev->iosig.io)) |
241 | if (ev_is_active (&ev->iosig.io)) |
217 | ev_io_stop (EV_A_ &ev->iosig.io); |
242 | ev_io_stop (EV_A_ &ev->iosig.io); |
218 | } |
243 | } |
… | |
… | |
223 | return 0; |
248 | return 0; |
224 | } |
249 | } |
225 | |
250 | |
226 | int event_pending (struct event *ev, short events, struct timeval *tv) |
251 | int event_pending (struct event *ev, short events, struct timeval *tv) |
227 | { |
252 | { |
|
|
253 | short revents = 0; |
228 | dLOOPev; |
254 | dLOOPev; |
229 | |
255 | |
230 | short revents = 0; |
|
|
231 | |
256 | |
232 | if (ev->ev_events & EV_SIGNAL) |
257 | if (ev->ev_events & EV_SIGNAL) |
233 | { |
258 | { |
234 | /* sig */ |
259 | /* sig */ |
235 | if (ev->iosig.sig.pending) |
260 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
236 | revents |= EV_SIGNAL; |
261 | revents |= EV_SIGNAL; |
237 | } |
262 | } |
238 | else |
263 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
239 | { |
264 | { |
240 | /* io */ |
265 | /* io */ |
241 | if (ev->iosig.io.pending) |
266 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
242 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
267 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
243 | } |
268 | } |
244 | |
269 | |
245 | if (ev->to.pending) |
270 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
246 | { |
271 | { |
247 | revents |= EV_TIMEOUT; |
272 | revents |= EV_TIMEOUT; |
248 | |
273 | |
249 | if (tv) |
274 | if (tv) |
250 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
275 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
285 | { |
310 | { |
286 | return event_base_loop (base, 0); |
311 | return event_base_loop (base, 0); |
287 | } |
312 | } |
288 | |
313 | |
289 | static void |
314 | static void |
290 | x_loopexit_cb (EV_P_ int revents, void *arg) |
315 | x_loopexit_cb (int revents, void *base) |
291 | { |
316 | { |
|
|
317 | dLOOPbase; |
|
|
318 | |
292 | ev_unloop (EV_A_ 2); |
319 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
293 | } |
320 | } |
294 | |
321 | |
295 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
322 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
296 | { |
323 | { |
297 | dLOOPbase; |
|
|
298 | ev_tstamp after = tv_get (tv); |
324 | ev_tstamp after = tv_get (tv); |
|
|
325 | dLOOPbase; |
299 | |
326 | |
300 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
327 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
301 | |
328 | |
302 | return -1; |
329 | return -1; |
303 | } |
330 | } |
… | |
… | |
310 | }; |
337 | }; |
311 | |
338 | |
312 | static void |
339 | static void |
313 | x_once_cb (int revents, void *arg) |
340 | x_once_cb (int revents, void *arg) |
314 | { |
341 | { |
315 | struct x_once *once = arg; |
342 | struct x_once *once = (struct x_once *)arg; |
316 | |
343 | |
317 | once->cb (once->fd, revents, once->arg); |
344 | once->cb (once->fd, revents, once->arg); |
318 | free (once); |
345 | free (once); |
319 | } |
346 | } |
320 | |
347 | |
321 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
348 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
322 | { |
349 | { |
323 | dLOOPbase; |
|
|
324 | struct x_once *once = malloc (sizeof (struct x_once)); |
350 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
|
|
351 | dLOOPbase; |
325 | |
352 | |
326 | if (!once) |
353 | if (!once) |
327 | return -1; |
354 | return -1; |
328 | |
355 | |
329 | once->fd = fd; |
356 | once->fd = fd; |
… | |
… | |
335 | return 0; |
362 | return 0; |
336 | } |
363 | } |
337 | |
364 | |
338 | int event_base_priority_init (struct event_base *base, int npri) |
365 | int event_base_priority_init (struct event_base *base, int npri) |
339 | { |
366 | { |
340 | dLOOPbase; |
367 | /*dLOOPbase;*/ |
341 | |
368 | |
342 | return 0; |
369 | return 0; |
343 | } |
370 | } |
344 | |
371 | |