… | |
… | |
30 | */ |
30 | */ |
31 | |
31 | |
32 | #include <stddef.h> |
32 | #include <stddef.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <sys/time.h> |
34 | #include <sys/time.h> |
|
|
35 | #include <assert.h> |
35 | |
36 | |
|
|
37 | #include "ev.h" |
36 | #include "event.h" |
38 | #include "event.h" |
37 | |
39 | |
|
|
40 | #if EV_MULTIPLICITY |
|
|
41 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
|
|
42 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
|
|
43 | #else |
38 | #define dLOOPev |
44 | # define dLOOPev |
39 | #define dLOOPbase |
45 | # define dLOOPbase |
|
|
46 | #endif |
40 | |
47 | |
|
|
48 | /* never accessed, will always be cast from/to ev_loop */ |
41 | struct event_base |
49 | struct event_base |
42 | { |
50 | { |
43 | int dummy; |
51 | int dummy; |
44 | }; |
52 | }; |
45 | |
53 | |
46 | static struct event_base x_base, *x_cur; |
54 | static struct event_base *x_cur; |
47 | |
55 | |
48 | static void |
56 | static void |
49 | tv_set (struct timeval *tv, ev_tstamp at) |
57 | tv_set (struct timeval *tv, ev_tstamp at) |
50 | { |
58 | { |
51 | tv->tv_sec = (long)at; |
59 | tv->tv_sec = (long)at; |
… | |
… | |
73 | return "libev"; |
81 | return "libev"; |
74 | } |
82 | } |
75 | |
83 | |
76 | void *event_init (void) |
84 | void *event_init (void) |
77 | { |
85 | { |
78 | if (!x_cur && ev_init (0)) |
86 | #if EV_MULTIPLICITY |
79 | return x_cur = &x_base; |
87 | if (x_cur) |
|
|
88 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
|
|
89 | else |
|
|
90 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
|
|
91 | #else |
|
|
92 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
80 | |
93 | |
|
|
94 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
|
|
95 | #endif |
|
|
96 | |
81 | return 0; |
97 | return x_cur; |
82 | } |
98 | } |
83 | |
99 | |
84 | void event_base_free (struct event_base *base) |
100 | void event_base_free (struct event_base *base) |
85 | { |
101 | { |
86 | /* nop */ |
102 | dLOOPbase; |
|
|
103 | |
|
|
104 | #if EV_MULTIPLICITY |
|
|
105 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
|
|
106 | ev_loop_destroy (loop); |
|
|
107 | #endif |
87 | } |
108 | } |
88 | |
109 | |
89 | int event_dispatch (void) |
110 | int event_dispatch (void) |
90 | { |
111 | { |
91 | return event_base_dispatch (x_cur); |
112 | return event_base_dispatch (x_cur); |
… | |
… | |
127 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
148 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
128 | { |
149 | { |
129 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
150 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
130 | |
151 | |
131 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
152 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
132 | ev_io_stop (w); |
153 | ev_io_stop (EV_A_ w); |
133 | |
154 | |
134 | x_cb (ev, revents); |
155 | x_cb (ev, revents); |
135 | } |
156 | } |
136 | |
157 | |
137 | static void |
158 | static void |
138 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
139 | { |
160 | { |
140 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
161 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
162 | |
|
|
163 | event_del (ev); |
|
|
164 | |
|
|
165 | x_cb (ev, revents); |
141 | } |
166 | } |
142 | |
167 | |
143 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
168 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
144 | { |
169 | { |
145 | if (events & EV_SIGNAL) |
170 | if (events & EV_SIGNAL) |
… | |
… | |
147 | else |
172 | else |
148 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
149 | |
174 | |
150 | ev_watcher_init (&ev->to, x_cb_to); |
175 | ev_watcher_init (&ev->to, x_cb_to); |
151 | |
176 | |
152 | ev->ev_base = x_cur; |
177 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
153 | ev->ev_fd = fd; |
178 | ev->ev_fd = fd; |
154 | ev->ev_events = events; |
179 | ev->ev_events = events; |
155 | ev->ev_pri = 0; |
180 | ev->ev_pri = 0; |
156 | ev->ev_callback = cb; |
181 | ev->ev_callback = cb; |
157 | ev->ev_arg = arg; |
182 | ev->ev_arg = arg; |
… | |
… | |
220 | short revents = 0; |
245 | short revents = 0; |
221 | |
246 | |
222 | if (ev->ev_events & EV_SIGNAL) |
247 | if (ev->ev_events & EV_SIGNAL) |
223 | { |
248 | { |
224 | /* sig */ |
249 | /* sig */ |
225 | if (ev->iosig.sig.pending) |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
226 | revents |= EV_SIGNAL; |
251 | revents |= EV_SIGNAL; |
227 | } |
252 | } |
228 | else |
253 | else |
229 | { |
254 | { |
230 | /* io */ |
255 | /* io */ |
231 | if (ev->iosig.io.pending) |
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
232 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
233 | } |
258 | } |
234 | |
259 | |
235 | if (ev->to.pending) |
260 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
236 | { |
261 | { |
237 | revents |= EV_TIMEOUT; |
262 | revents |= EV_TIMEOUT; |
238 | |
263 | |
239 | if (tv) |
264 | if (tv) |
240 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
265 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
263 | } |
288 | } |
264 | |
289 | |
265 | int event_base_loop (struct event_base *base, int flags) |
290 | int event_base_loop (struct event_base *base, int flags) |
266 | { |
291 | { |
267 | dLOOPbase; |
292 | dLOOPbase; |
268 | ev_loop (EV_A_ flags | EVLOOP_ONESHOT); |
293 | |
|
|
294 | ev_loop (EV_A_ flags); |
269 | |
295 | |
270 | return 0; |
296 | return 0; |
271 | } |
297 | } |
272 | |
298 | |
273 | int event_base_dispatch (struct event_base *base) |
299 | int event_base_dispatch (struct event_base *base) |
274 | { |
300 | { |
275 | return event_base_loop (base, 0); |
301 | return event_base_loop (base, 0); |
276 | } |
302 | } |
277 | |
303 | |
278 | static void |
304 | static void |
279 | x_loopexit_cb (EV_P_ int revents, void *arg) |
305 | x_loopexit_cb (int revents, void *base) |
280 | { |
306 | { |
|
|
307 | dLOOPbase; |
|
|
308 | |
281 | ev_unloop (EV_A_ 2); |
309 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
282 | } |
310 | } |
283 | |
311 | |
284 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
312 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
285 | { |
313 | { |
286 | dLOOPbase; |
314 | dLOOPbase; |
… | |
… | |
324 | return 0; |
352 | return 0; |
325 | } |
353 | } |
326 | |
354 | |
327 | int event_base_priority_init (struct event_base *base, int npri) |
355 | int event_base_priority_init (struct event_base *base, int npri) |
328 | { |
356 | { |
329 | dLOOPbase; |
357 | /*dLOOPbase;*/ |
330 | |
358 | |
331 | return 0; |
359 | return 0; |
332 | } |
360 | } |
333 | |
361 | |