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