… | |
… | |
63 | struct event_base |
63 | struct event_base |
64 | { |
64 | { |
65 | int dummy; |
65 | int dummy; |
66 | }; |
66 | }; |
67 | |
67 | |
68 | static struct event_base *x_cur; |
68 | static struct event_base *ev_x_cur; |
69 | |
69 | |
70 | static void |
70 | static void |
71 | tv_set (struct timeval *tv, ev_tstamp at) |
71 | ev_tv_set (struct timeval *tv, ev_tstamp at) |
72 | { |
72 | { |
73 | tv->tv_sec = (long)at; |
73 | tv->tv_sec = (long)at; |
74 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
74 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
75 | } |
75 | } |
76 | |
76 | |
77 | static ev_tstamp |
77 | static ev_tstamp |
78 | tv_get (struct timeval *tv) |
78 | ev_tv_get (struct timeval *tv) |
79 | { |
79 | { |
80 | if (tv) |
80 | if (tv) |
81 | return tv->tv_sec + tv->tv_usec * 1e-6; |
81 | return tv->tv_sec + tv->tv_usec * 1e-6; |
82 | else |
82 | else |
83 | return -1.; |
83 | return -1.; |
… | |
… | |
96 | } |
96 | } |
97 | |
97 | |
98 | void *event_init (void) |
98 | void *event_init (void) |
99 | { |
99 | { |
100 | #if EV_MULTIPLICITY |
100 | #if EV_MULTIPLICITY |
101 | if (x_cur) |
101 | if (ev_x_cur) |
102 | x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
102 | ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
103 | else |
103 | else |
104 | x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
104 | ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
105 | #else |
105 | #else |
106 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
106 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
107 | |
107 | |
108 | x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
108 | ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
109 | #endif |
109 | #endif |
110 | |
110 | |
111 | return x_cur; |
111 | return ev_x_cur; |
112 | } |
112 | } |
113 | |
113 | |
114 | void event_base_free (struct event_base *base) |
114 | void event_base_free (struct event_base *base) |
115 | { |
115 | { |
116 | dLOOPbase; |
116 | dLOOPbase; |
… | |
… | |
121 | #endif |
121 | #endif |
122 | } |
122 | } |
123 | |
123 | |
124 | int event_dispatch (void) |
124 | int event_dispatch (void) |
125 | { |
125 | { |
126 | return event_base_dispatch (x_cur); |
126 | return event_base_dispatch (ev_x_cur); |
127 | } |
127 | } |
128 | |
128 | |
129 | #ifdef EV_STANDALONE |
129 | #ifdef EV_STANDALONE |
130 | void event_set_log_callback (event_log_cb cb) |
130 | void event_set_log_callback (event_log_cb cb) |
131 | { |
131 | { |
… | |
… | |
133 | } |
133 | } |
134 | #endif |
134 | #endif |
135 | |
135 | |
136 | int event_loop (int flags) |
136 | int event_loop (int flags) |
137 | { |
137 | { |
138 | return event_base_loop (x_cur, flags); |
138 | return event_base_loop (ev_x_cur, flags); |
139 | } |
139 | } |
140 | |
140 | |
141 | int event_loopexit (struct timeval *tv) |
141 | int event_loopexit (struct timeval *tv) |
142 | { |
142 | { |
143 | return event_base_loopexit (x_cur, tv); |
143 | return event_base_loopexit (ev_x_cur, tv); |
144 | } |
144 | } |
145 | |
145 | |
146 | static void |
146 | static void |
147 | x_cb (struct event *ev, int revents) |
147 | ev_x_cb (struct event *ev, int revents) |
148 | { |
148 | { |
149 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
149 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
150 | |
150 | |
151 | ev->ev_res = revents; |
151 | ev->ev_res = revents; |
152 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
152 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
153 | } |
153 | } |
154 | |
154 | |
155 | static void |
155 | static void |
156 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
156 | ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
157 | { |
157 | { |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
159 | |
159 | |
160 | if (revents & EV_ERROR) |
160 | if (revents & EV_ERROR) |
161 | event_del (ev); |
161 | event_del (ev); |
162 | |
162 | |
163 | x_cb (ev, revents); |
163 | ev_x_cb (ev, revents); |
164 | } |
164 | } |
165 | |
165 | |
166 | static void |
166 | static void |
167 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
167 | ev_x_cb_io (EV_P_ struct ev_io *w, int revents) |
168 | { |
168 | { |
169 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
169 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
170 | |
170 | |
171 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
171 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
172 | event_del (ev); |
172 | event_del (ev); |
173 | |
173 | |
174 | x_cb (ev, revents); |
174 | ev_x_cb (ev, revents); |
175 | } |
175 | } |
176 | |
176 | |
177 | static void |
177 | static void |
178 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
178 | ev_x_cb_to (EV_P_ struct ev_timer *w, int revents) |
179 | { |
179 | { |
180 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
180 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
181 | |
181 | |
182 | event_del (ev); |
182 | event_del (ev); |
183 | |
183 | |
184 | x_cb (ev, revents); |
184 | ev_x_cb (ev, revents); |
185 | } |
185 | } |
186 | |
186 | |
187 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
187 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
188 | { |
188 | { |
189 | if (events & EV_SIGNAL) |
189 | if (events & EV_SIGNAL) |
190 | ev_init (&ev->iosig.sig, x_cb_sig); |
190 | ev_init (&ev->iosig.sig, ev_x_cb_sig); |
191 | else |
191 | else |
192 | ev_init (&ev->iosig.io, x_cb_io); |
192 | ev_init (&ev->iosig.io, ev_x_cb_io); |
193 | |
193 | |
194 | ev_init (&ev->to, x_cb_to); |
194 | ev_init (&ev->to, ev_x_cb_to); |
195 | |
195 | |
196 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
196 | ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */ |
197 | ev->ev_fd = fd; |
197 | ev->ev_fd = fd; |
198 | ev->ev_events = events; |
198 | ev->ev_events = events; |
199 | ev->ev_pri = 0; |
199 | ev->ev_pri = 0; |
200 | ev->ev_callback = cb; |
200 | ev->ev_callback = cb; |
201 | ev->ev_arg = arg; |
201 | ev->ev_arg = arg; |
… | |
… | |
203 | ev->ev_flags = EVLIST_INIT; |
203 | ev->ev_flags = EVLIST_INIT; |
204 | } |
204 | } |
205 | |
205 | |
206 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
206 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
207 | { |
207 | { |
208 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
208 | return event_base_once (ev_x_cur, fd, events, cb, arg, tv); |
209 | } |
209 | } |
210 | |
210 | |
211 | int event_add (struct event *ev, struct timeval *tv) |
211 | int event_add (struct event *ev, struct timeval *tv) |
212 | { |
212 | { |
213 | dLOOPev; |
213 | dLOOPev; |
… | |
… | |
233 | } |
233 | } |
234 | } |
234 | } |
235 | |
235 | |
236 | if (tv) |
236 | if (tv) |
237 | { |
237 | { |
238 | ev->to.repeat = tv_get (tv); |
238 | ev->to.repeat = ev_tv_get (tv); |
239 | ev_timer_again (EV_A_ &ev->to); |
239 | ev_timer_again (EV_A_ &ev->to); |
240 | ev->ev_flags |= EVLIST_TIMEOUT; |
240 | ev->ev_flags |= EVLIST_TIMEOUT; |
241 | } |
241 | } |
242 | else |
242 | else |
243 | { |
243 | { |
… | |
… | |
302 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
302 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
303 | { |
303 | { |
304 | revents |= EV_TIMEOUT; |
304 | revents |= EV_TIMEOUT; |
305 | |
305 | |
306 | if (tv) |
306 | if (tv) |
307 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
307 | ev_tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
308 | } |
308 | } |
309 | |
309 | |
310 | return events & revents; |
310 | return events & revents; |
311 | } |
311 | } |
312 | |
312 | |
313 | int event_priority_init (int npri) |
313 | int event_priority_init (int npri) |
314 | { |
314 | { |
315 | return event_base_priority_init (x_cur, npri); |
315 | return event_base_priority_init (ev_x_cur, npri); |
316 | } |
316 | } |
317 | |
317 | |
318 | int event_priority_set (struct event *ev, int pri) |
318 | int event_priority_set (struct event *ev, int pri) |
319 | { |
319 | { |
320 | ev->ev_pri = pri; |
320 | ev->ev_pri = pri; |
… | |
… | |
342 | { |
342 | { |
343 | return event_base_loop (base, 0); |
343 | return event_base_loop (base, 0); |
344 | } |
344 | } |
345 | |
345 | |
346 | static void |
346 | static void |
347 | x_loopexit_cb (int revents, void *base) |
347 | ev_x_loopexit_cb (int revents, void *base) |
348 | { |
348 | { |
349 | dLOOPbase; |
349 | dLOOPbase; |
350 | |
350 | |
351 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
351 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
352 | } |
352 | } |
353 | |
353 | |
354 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
354 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
355 | { |
355 | { |
356 | ev_tstamp after = tv_get (tv); |
356 | ev_tstamp after = ev_tv_get (tv); |
357 | dLOOPbase; |
357 | dLOOPbase; |
358 | |
358 | |
359 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
359 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base); |
360 | |
360 | |
361 | return 0; |
361 | return 0; |
362 | } |
362 | } |
363 | |
363 | |
364 | struct x_once |
364 | struct ev_x_once |
365 | { |
365 | { |
366 | int fd; |
366 | int fd; |
367 | void (*cb)(int, short, void *); |
367 | void (*cb)(int, short, void *); |
368 | void *arg; |
368 | void *arg; |
369 | }; |
369 | }; |
370 | |
370 | |
371 | static void |
371 | static void |
372 | x_once_cb (int revents, void *arg) |
372 | ev_x_once_cb (int revents, void *arg) |
373 | { |
373 | { |
374 | struct x_once *once = (struct x_once *)arg; |
374 | struct ev_x_once *once = (struct ev_x_once *)arg; |
375 | |
375 | |
376 | once->cb (once->fd, (short)revents, once->arg); |
376 | once->cb (once->fd, (short)revents, once->arg); |
377 | free (once); |
377 | free (once); |
378 | } |
378 | } |
379 | |
379 | |
380 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
380 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
381 | { |
381 | { |
382 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
382 | struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once)); |
383 | dLOOPbase; |
383 | dLOOPbase; |
384 | |
384 | |
385 | if (!once) |
385 | if (!once) |
386 | return -1; |
386 | return -1; |
387 | |
387 | |
388 | once->fd = fd; |
388 | once->fd = fd; |
389 | once->cb = cb; |
389 | once->cb = cb; |
390 | once->arg = arg; |
390 | once->arg = arg; |
391 | |
391 | |
392 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
392 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once); |
393 | |
393 | |
394 | return 0; |
394 | return 0; |
395 | } |
395 | } |
396 | |
396 | |
397 | int event_base_priority_init (struct event_base *base, int npri) |
397 | int event_base_priority_init (struct event_base *base, int npri) |