… | |
… | |
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); |
… | |
… | |
137 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
151 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
138 | { |
152 | { |
139 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
153 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
140 | |
154 | |
141 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
155 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
142 | ev_io_stop (w); |
156 | ev_io_stop (EV_A_ w); |
143 | |
157 | |
144 | x_cb (ev, revents); |
158 | x_cb (ev, revents); |
145 | } |
159 | } |
146 | |
160 | |
147 | static void |
161 | static void |
148 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
162 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
149 | { |
163 | { |
150 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
164 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
165 | |
|
|
166 | event_del (ev); |
|
|
167 | |
|
|
168 | x_cb (ev, revents); |
151 | } |
169 | } |
152 | |
170 | |
153 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
171 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
154 | { |
172 | { |
155 | if (events & EV_SIGNAL) |
173 | if (events & EV_SIGNAL) |
… | |
… | |
157 | else |
175 | else |
158 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
176 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
159 | |
177 | |
160 | ev_watcher_init (&ev->to, x_cb_to); |
178 | ev_watcher_init (&ev->to, x_cb_to); |
161 | |
179 | |
162 | ev->ev_base = x_cur; |
180 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
163 | ev->ev_fd = fd; |
181 | ev->ev_fd = fd; |
164 | ev->ev_events = events; |
182 | ev->ev_events = events; |
165 | ev->ev_pri = 0; |
183 | ev->ev_pri = 0; |
166 | ev->ev_callback = cb; |
184 | ev->ev_callback = cb; |
167 | ev->ev_arg = arg; |
185 | ev->ev_arg = arg; |
… | |
… | |
208 | { |
226 | { |
209 | /* sig */ |
227 | /* sig */ |
210 | if (ev_is_active (&ev->iosig.sig)) |
228 | if (ev_is_active (&ev->iosig.sig)) |
211 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
229 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
212 | } |
230 | } |
213 | else |
231 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
214 | { |
232 | { |
215 | /* io */ |
233 | /* io */ |
216 | if (ev_is_active (&ev->iosig.io)) |
234 | if (ev_is_active (&ev->iosig.io)) |
217 | ev_io_stop (EV_A_ &ev->iosig.io); |
235 | ev_io_stop (EV_A_ &ev->iosig.io); |
218 | } |
236 | } |
… | |
… | |
223 | return 0; |
241 | return 0; |
224 | } |
242 | } |
225 | |
243 | |
226 | int event_pending (struct event *ev, short events, struct timeval *tv) |
244 | int event_pending (struct event *ev, short events, struct timeval *tv) |
227 | { |
245 | { |
|
|
246 | short revents = 0; |
228 | dLOOPev; |
247 | dLOOPev; |
229 | |
248 | |
230 | short revents = 0; |
|
|
231 | |
249 | |
232 | if (ev->ev_events & EV_SIGNAL) |
250 | if (ev->ev_events & EV_SIGNAL) |
233 | { |
251 | { |
234 | /* sig */ |
252 | /* sig */ |
235 | if (ev->iosig.sig.pending) |
253 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
236 | revents |= EV_SIGNAL; |
254 | revents |= EV_SIGNAL; |
237 | } |
255 | } |
238 | else |
256 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
239 | { |
257 | { |
240 | /* io */ |
258 | /* io */ |
241 | if (ev->iosig.io.pending) |
259 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
242 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
260 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
243 | } |
261 | } |
244 | |
262 | |
245 | if (ev->to.pending) |
263 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
246 | { |
264 | { |
247 | revents |= EV_TIMEOUT; |
265 | revents |= EV_TIMEOUT; |
248 | |
266 | |
249 | if (tv) |
267 | if (tv) |
250 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
268 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
285 | { |
303 | { |
286 | return event_base_loop (base, 0); |
304 | return event_base_loop (base, 0); |
287 | } |
305 | } |
288 | |
306 | |
289 | static void |
307 | static void |
290 | x_loopexit_cb (EV_P_ int revents, void *arg) |
308 | x_loopexit_cb (int revents, void *base) |
291 | { |
309 | { |
|
|
310 | dLOOPbase; |
|
|
311 | |
292 | ev_unloop (EV_A_ 2); |
312 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
293 | } |
313 | } |
294 | |
314 | |
295 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
315 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
296 | { |
316 | { |
297 | dLOOPbase; |
|
|
298 | ev_tstamp after = tv_get (tv); |
317 | ev_tstamp after = tv_get (tv); |
|
|
318 | dLOOPbase; |
299 | |
319 | |
300 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
320 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
301 | |
321 | |
302 | return -1; |
322 | return -1; |
303 | } |
323 | } |
… | |
… | |
318 | free (once); |
338 | free (once); |
319 | } |
339 | } |
320 | |
340 | |
321 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
341 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
322 | { |
342 | { |
323 | dLOOPbase; |
|
|
324 | struct x_once *once = malloc (sizeof (struct x_once)); |
343 | struct x_once *once = malloc (sizeof (struct x_once)); |
|
|
344 | dLOOPbase; |
325 | |
345 | |
326 | if (!once) |
346 | if (!once) |
327 | return -1; |
347 | return -1; |
328 | |
348 | |
329 | once->fd = fd; |
349 | once->fd = fd; |
… | |
… | |
335 | return 0; |
355 | return 0; |
336 | } |
356 | } |
337 | |
357 | |
338 | int event_base_priority_init (struct event_base *base, int npri) |
358 | int event_base_priority_init (struct event_base *base, int npri) |
339 | { |
359 | { |
340 | dLOOPbase; |
360 | /*dLOOPbase;*/ |
341 | |
361 | |
342 | return 0; |
362 | return 0; |
343 | } |
363 | } |
344 | |
364 | |