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