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