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