… | |
… | |
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) |
|
|
134 | { |
|
|
135 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, io)), revents); |
|
|
136 | } |
|
|
137 | |
|
|
138 | static void |
|
|
139 | x_cb_to (struct ev_timer *w, int revents) |
|
|
140 | { |
|
|
141 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
|
|
142 | } |
|
|
143 | |
|
|
144 | static void |
|
|
145 | x_cb_sig (struct ev_signal *w, int revents) |
142 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
146 | { |
143 | { |
147 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, sig)), revents); |
144 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents); |
|
|
145 | } |
|
|
146 | |
|
|
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 |
|
|
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
|
|
160 | { |
|
|
161 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
162 | |
|
|
163 | event_del (ev); |
|
|
164 | |
|
|
165 | x_cb (ev, revents); |
148 | } |
166 | } |
149 | |
167 | |
150 | 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) |
151 | { |
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); |
|
|
155 | |
176 | |
156 | ev->ev_base = x_cur; |
177 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
157 | ev->ev_fd = fd; |
178 | ev->ev_fd = fd; |
158 | ev->ev_events = events; |
179 | ev->ev_events = events; |
159 | ev->ev_pri = 0; |
180 | ev->ev_pri = 0; |
160 | ev->ev_callback = cb; |
181 | ev->ev_callback = cb; |
161 | ev->ev_arg = arg; |
182 | ev->ev_arg = arg; |
162 | ev->ev_res = 0; |
183 | ev->ev_res = 0; |
163 | } |
184 | } |
164 | |
185 | |
165 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
186 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
166 | { |
187 | { |
167 | event_base_once (x_cur, fd, events, cb, arg, tv); |
188 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
168 | } |
189 | } |
169 | |
190 | |
170 | int event_add (struct event *ev, struct timeval *tv) |
191 | int event_add (struct event *ev, struct timeval *tv) |
171 | { |
192 | { |
|
|
193 | dLOOPev; |
|
|
194 | |
|
|
195 | /* disable all watchers */ |
|
|
196 | event_del (ev); |
|
|
197 | |
|
|
198 | if (ev->ev_events & EV_SIGNAL) |
|
|
199 | { |
|
|
200 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
|
|
201 | ev_signal_start (EV_A_ &ev->iosig.sig); |
|
|
202 | } |
|
|
203 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
204 | { |
|
|
205 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
|
|
206 | ev_io_start (EV_A_ &ev->iosig.io); |
|
|
207 | } |
|
|
208 | |
172 | if (tv) |
209 | if (tv) |
173 | { |
210 | { |
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.); |
211 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
181 | ev_timer_start (&ev->to); |
212 | ev_timer_start (EV_A_ &ev->to); |
182 | ++x_actives; |
|
|
183 | } |
|
|
184 | |
|
|
185 | if (ev->ev_events & (EV_READ|EV_WRITE)) |
|
|
186 | { |
213 | } |
187 | if (ev_is_active (&ev->io)) |
|
|
188 | { |
|
|
189 | ev_io_stop (&ev->io); |
|
|
190 | --x_actives; |
|
|
191 | } |
|
|
192 | |
214 | |
193 | ev_io_set (&ev->io, ev->ev_fd, ev->ev_events); |
215 | return 0; |
194 | ev_io_start (&ev->io); |
216 | } |
195 | ++x_actives; |
217 | |
196 | } |
218 | int event_del (struct event *ev) |
|
|
219 | { |
|
|
220 | dLOOPev; |
197 | |
221 | |
198 | if (ev->ev_events & EV_SIGNAL) |
222 | if (ev->ev_events & EV_SIGNAL) |
199 | { |
223 | { |
|
|
224 | /* sig */ |
|
|
225 | if (ev_is_active (&ev->iosig.sig)) |
|
|
226 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
|
|
227 | } |
|
|
228 | else |
|
|
229 | { |
|
|
230 | /* io */ |
200 | if (ev_is_active (&ev->sig)) |
231 | 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); |
232 | ev_io_stop (EV_A_ &ev->iosig.io); |
219 | --x_actives; |
|
|
220 | } |
233 | } |
221 | |
234 | |
222 | if (ev_is_active (&ev->to)) |
235 | if (ev_is_active (&ev->to)) |
223 | { |
|
|
224 | ev_timer_stop (&ev->to); |
236 | ev_timer_stop (EV_A_ &ev->to); |
225 | --x_actives; |
|
|
226 | } |
|
|
227 | |
237 | |
228 | if (ev_is_active (&ev->sig)) |
|
|
229 | { |
|
|
230 | ev_signal_stop (&ev->sig); |
|
|
231 | --x_actives; |
|
|
232 | } |
|
|
233 | |
|
|
234 | return 0; |
238 | 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 | } |
239 | } |
241 | |
240 | |
242 | int event_pending (struct event *ev, short events, struct timeval *tv) |
241 | int event_pending (struct event *ev, short events, struct timeval *tv) |
243 | { |
242 | { |
|
|
243 | dLOOPev; |
|
|
244 | |
244 | short revents; |
245 | short revents = 0; |
245 | |
246 | |
246 | if (ev->io.pending) |
247 | if (ev->ev_events & EV_SIGNAL) |
|
|
248 | { |
|
|
249 | /* sig */ |
|
|
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
|
|
251 | revents |= EV_SIGNAL; |
|
|
252 | } |
|
|
253 | else |
|
|
254 | { |
|
|
255 | /* io */ |
|
|
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
247 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
|
|
258 | } |
248 | |
259 | |
249 | if (ev->to.pending) |
260 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
250 | { |
261 | { |
251 | revents |= EV_TIMEOUT; |
262 | revents |= EV_TIMEOUT; |
252 | |
263 | |
253 | if (tv) |
264 | if (tv) |
254 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
265 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
255 | } |
266 | } |
256 | |
|
|
257 | if (ev->sig.pending) |
|
|
258 | revents |= EV_SIGNAL; |
|
|
259 | |
267 | |
260 | return events & revents; |
268 | return events & revents; |
261 | } |
269 | } |
262 | |
270 | |
263 | int event_priority_init (int npri) |
271 | int event_priority_init (int npri) |
… | |
… | |
279 | return 0; |
287 | return 0; |
280 | } |
288 | } |
281 | |
289 | |
282 | int event_base_loop (struct event_base *base, int flags) |
290 | int event_base_loop (struct event_base *base, int flags) |
283 | { |
291 | { |
284 | do |
292 | dLOOPbase; |
285 | { |
293 | |
286 | ev_loop (flags | EVLOOP_ONESHOT); |
294 | ev_loop (EV_A_ flags); |
287 | } |
|
|
288 | while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done); |
|
|
289 | |
295 | |
290 | return 0; |
296 | return 0; |
291 | } |
297 | } |
292 | |
298 | |
293 | int event_base_dispatch (struct event_base *base) |
299 | int event_base_dispatch (struct event_base *base) |
294 | { |
300 | { |
295 | return event_base_loop (base, 0); |
301 | return event_base_loop (base, 0); |
296 | } |
302 | } |
297 | |
303 | |
298 | static void |
304 | static void |
299 | x_loopexit_cb (int revents, void *arg) |
305 | x_loopexit_cb (int revents, void *base) |
300 | { |
306 | { |
301 | ev_loop_done = 2; |
307 | dLOOPbase; |
|
|
308 | |
|
|
309 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
302 | } |
310 | } |
303 | |
311 | |
304 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
312 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
305 | { |
313 | { |
|
|
314 | dLOOPbase; |
306 | ev_tstamp after = tv_get (tv); |
315 | ev_tstamp after = tv_get (tv); |
307 | |
316 | |
308 | ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
317 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
|
|
318 | |
|
|
319 | return -1; |
309 | } |
320 | } |
310 | |
321 | |
311 | struct x_once |
322 | struct x_once |
312 | { |
323 | { |
313 | int fd; |
324 | int fd; |
… | |
… | |
324 | free (once); |
335 | free (once); |
325 | } |
336 | } |
326 | |
337 | |
327 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
338 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
328 | { |
339 | { |
|
|
340 | dLOOPbase; |
329 | struct x_once *once = malloc (sizeof (struct x_once)); |
341 | struct x_once *once = malloc (sizeof (struct x_once)); |
330 | |
342 | |
331 | if (!once) |
343 | if (!once) |
332 | return -1; |
344 | return -1; |
333 | |
345 | |
334 | once->fd = fd; |
346 | once->fd = fd; |
335 | once->cb = cb; |
347 | once->cb = cb; |
336 | once->arg = arg; |
348 | once->arg = arg; |
337 | |
349 | |
338 | ev_once (fd, events & (EV_READ|EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
350 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
339 | |
351 | |
340 | return 0; |
352 | return 0; |
341 | } |
353 | } |
342 | |
354 | |
343 | int event_base_priority_init (struct event_base *base, int npri) |
355 | int event_base_priority_init (struct event_base *base, int npri) |
344 | { |
356 | { |
345 | return 0; |
357 | /*dLOOPbase;*/ |
346 | } |
|
|
347 | |
358 | |
|
|
359 | return 0; |
|
|
360 | } |
|
|
361 | |