1 | /* |
1 | /* |
2 | * libevent compatibility layer |
2 | * libevent compatibility layer |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2012 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * modification, are permitted provided that the following conditions are |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * met: |
|
|
10 | * |
9 | * |
11 | * * Redistributions of source code must retain the above copyright |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
11 | * this list of conditions and the following disclaimer. |
|
|
12 | * |
|
|
13 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
14 | * notice, this list of conditions and the following disclaimer in the |
|
|
15 | * documentation and/or other materials provided with the distribution. |
13 | * |
16 | * |
14 | * * Redistributions in binary form must reproduce the above |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
15 | * copyright notice, this list of conditions and the following |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
16 | * disclaimer in the documentation and/or other materials provided |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
17 | * with the distribution. |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
18 | * |
|
|
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
25 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
27 | * |
|
|
28 | * Alternatively, the contents of this file may be used under the terms of |
|
|
29 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
30 | * in which case the provisions of the GPL are applicable instead of |
|
|
31 | * the above. If you wish to allow the use of your version of this file |
|
|
32 | * only under the terms of the GPL and not to allow others to use your |
|
|
33 | * version of this file under the BSD license, indicate your decision |
|
|
34 | * by deleting the provisions above and replace them with the notice |
|
|
35 | * and other provisions required by the GPL. If you do not delete the |
|
|
36 | * provisions above, a recipient may use your version of this file under |
|
|
37 | * either the BSD or the GPL. |
30 | */ |
38 | */ |
31 | |
39 | |
32 | #include <stddef.h> |
40 | #include <stddef.h> |
33 | #include <stdlib.h> |
41 | #include <stdlib.h> |
34 | #include <sys/time.h> |
42 | #include <assert.h> |
35 | |
43 | |
|
|
44 | #ifdef EV_EVENT_H |
|
|
45 | # include EV_EVENT_H |
|
|
46 | #else |
36 | #include "event.h" |
47 | # include "event.h" |
|
|
48 | #endif |
37 | |
49 | |
|
|
50 | #if EV_MULTIPLICITY |
|
|
51 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
|
|
52 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
|
|
53 | #else |
|
|
54 | # define dLOOPev |
|
|
55 | # define dLOOPbase |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | /* never accessed, will always be cast from/to ev_loop */ |
38 | struct event_base |
59 | struct event_base |
39 | { |
60 | { |
40 | int dummy; |
61 | int dummy; |
41 | }; |
62 | }; |
42 | |
63 | |
43 | static int x_actives; |
|
|
44 | |
|
|
45 | static struct event_base x_base, *x_cur; |
64 | static struct event_base *ev_x_cur; |
46 | |
|
|
47 | static void |
|
|
48 | tv_set (struct timeval *tv, ev_tstamp at) |
|
|
49 | { |
|
|
50 | tv->tv_sec = (long)at; |
|
|
51 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
|
|
52 | } |
|
|
53 | |
65 | |
54 | static ev_tstamp |
66 | static ev_tstamp |
55 | tv_get (struct timeval *tv) |
67 | ev_tv_get (struct timeval *tv) |
56 | { |
68 | { |
57 | if (tv) |
69 | if (tv) |
|
|
70 | { |
58 | return tv->tv_sec + tv->tv_usec * 1e-6; |
71 | ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6; |
|
|
72 | return after ? after : 1e-6; |
|
|
73 | } |
59 | else |
74 | else |
60 | return -1.; |
75 | return -1.; |
61 | } |
76 | } |
62 | |
77 | |
63 | #define EVENT_VERSION(a,b) # a "." # b |
78 | #define EVENT_STRINGIFY(s) # s |
|
|
79 | #define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b) |
64 | |
80 | |
|
|
81 | const char * |
65 | const char *event_get_version (void) |
82 | event_get_version (void) |
66 | { |
83 | { |
|
|
84 | /* returns ABI, not API or library, version */ |
67 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
85 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
68 | } |
86 | } |
69 | |
87 | |
|
|
88 | const char * |
70 | const char *event_get_method (void) |
89 | event_get_method (void) |
71 | { |
90 | { |
72 | return "libev"; |
91 | return "libev"; |
73 | } |
92 | } |
74 | |
93 | |
75 | void *event_init (void) |
94 | void *event_init (void) |
76 | { |
95 | { |
77 | if (ev_init (0)) |
96 | #if EV_MULTIPLICITY |
78 | return x_cur = &x_base; |
97 | if (ev_x_cur) |
|
|
98 | ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
|
|
99 | else |
|
|
100 | ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
|
|
101 | #else |
|
|
102 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
79 | |
103 | |
|
|
104 | ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
|
|
105 | #endif |
|
|
106 | |
|
|
107 | return ev_x_cur; |
|
|
108 | } |
|
|
109 | |
|
|
110 | const char * |
|
|
111 | event_base_get_method (const struct event_base *base) |
|
|
112 | { |
|
|
113 | return "libev"; |
|
|
114 | } |
|
|
115 | |
|
|
116 | struct event_base * |
|
|
117 | event_base_new (void) |
|
|
118 | { |
|
|
119 | #if EV_MULTIPLICITY |
|
|
120 | return (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
|
|
121 | #else |
|
|
122 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY")); |
80 | return 0; |
123 | return NULL; |
|
|
124 | #endif |
81 | } |
125 | } |
82 | |
126 | |
83 | void event_base_free (struct event_base *base) |
127 | void event_base_free (struct event_base *base) |
84 | { |
128 | { |
|
|
129 | dLOOPbase; |
|
|
130 | |
|
|
131 | #if EV_MULTIPLICITY |
|
|
132 | if (!ev_is_default_loop (loop)) |
|
|
133 | ev_loop_destroy (loop); |
|
|
134 | #endif |
|
|
135 | } |
|
|
136 | |
|
|
137 | int event_dispatch (void) |
|
|
138 | { |
|
|
139 | return event_base_dispatch (ev_x_cur); |
|
|
140 | } |
|
|
141 | |
|
|
142 | #ifdef EV_STANDALONE |
|
|
143 | void event_set_log_callback (event_log_cb cb) |
|
|
144 | { |
85 | /* nop */ |
145 | /* nop */ |
86 | } |
146 | } |
87 | |
147 | #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 | |
148 | |
98 | int event_loop (int flags) |
149 | int event_loop (int flags) |
99 | { |
150 | { |
100 | return event_base_loop (x_cur, flags); |
151 | return event_base_loop (ev_x_cur, flags); |
101 | } |
152 | } |
102 | |
153 | |
103 | int event_loopexit (struct timeval *tv) |
154 | int event_loopexit (struct timeval *tv) |
104 | { |
155 | { |
105 | event_base_loopexit (x_cur, tv); |
156 | return event_base_loopexit (ev_x_cur, tv); |
106 | } |
157 | } |
107 | |
158 | |
|
|
159 | event_callback_fn event_get_callback |
|
|
160 | (const struct event *ev) |
|
|
161 | { |
|
|
162 | return ev->ev_callback; |
|
|
163 | } |
|
|
164 | |
108 | static void |
165 | static void |
109 | x_cb (struct event *ev, int revents) |
166 | ev_x_cb (struct event *ev, int revents) |
110 | { |
167 | { |
|
|
168 | revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL; |
|
|
169 | |
|
|
170 | ev->ev_res = revents; |
|
|
171 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
|
|
172 | } |
|
|
173 | |
|
|
174 | static void |
|
|
175 | ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
|
|
176 | { |
|
|
177 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
|
|
178 | |
|
|
179 | if (revents & EV_ERROR) |
|
|
180 | event_del (ev); |
|
|
181 | |
|
|
182 | ev_x_cb (ev, revents); |
|
|
183 | } |
|
|
184 | |
|
|
185 | static void |
|
|
186 | ev_x_cb_io (EV_P_ struct ev_io *w, int revents) |
|
|
187 | { |
|
|
188 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
|
|
189 | |
|
|
190 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
|
|
191 | event_del (ev); |
|
|
192 | |
|
|
193 | ev_x_cb (ev, revents); |
|
|
194 | } |
|
|
195 | |
|
|
196 | static void |
|
|
197 | ev_x_cb_to (EV_P_ struct ev_timer *w, int revents) |
|
|
198 | { |
|
|
199 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
200 | |
|
|
201 | event_del (ev); |
|
|
202 | |
|
|
203 | ev_x_cb (ev, revents); |
|
|
204 | } |
|
|
205 | |
|
|
206 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
|
|
207 | { |
111 | if (ev->ev_events & EV_SIGNAL) |
208 | if (events & EV_SIGNAL) |
112 | { |
209 | ev_init (&ev->iosig.sig, ev_x_cb_sig); |
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 |
210 | 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; |
|
|
131 | |
|
|
132 | if (revents & EV_TIMEOUT) |
|
|
133 | --x_actives; |
|
|
134 | |
|
|
135 | ev->ev_res = revents; |
|
|
136 | ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); |
|
|
137 | } |
|
|
138 | |
|
|
139 | 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) |
|
|
153 | { |
|
|
154 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents); |
|
|
155 | } |
|
|
156 | |
|
|
157 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
|
|
158 | { |
|
|
159 | if (events & EV_SIGNAL) |
|
|
160 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
|
|
161 | else |
|
|
162 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
211 | ev_init (&ev->iosig.io, ev_x_cb_io); |
163 | |
212 | |
164 | ev_watcher_init (&ev->to, x_cb_to); |
213 | ev_init (&ev->to, ev_x_cb_to); |
165 | |
214 | |
166 | ev->ev_base = x_cur; |
215 | ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */ |
167 | ev->ev_fd = fd; |
216 | ev->ev_fd = fd; |
168 | ev->ev_events = events; |
217 | ev->ev_events = events; |
169 | ev->ev_pri = 0; |
218 | ev->ev_pri = 0; |
170 | ev->ev_callback = cb; |
219 | ev->ev_callback = cb; |
171 | ev->ev_arg = arg; |
220 | ev->ev_arg = arg; |
172 | ev->ev_res = 0; |
221 | ev->ev_res = 0; |
|
|
222 | ev->ev_flags = EVLIST_INIT; |
173 | } |
223 | } |
174 | |
224 | |
175 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
225 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
176 | { |
226 | { |
177 | event_base_once (x_cur, fd, events, cb, arg, tv); |
227 | return event_base_once (ev_x_cur, fd, events, cb, arg, tv); |
178 | } |
228 | } |
179 | |
229 | |
180 | int event_add (struct event *ev, struct timeval *tv) |
230 | int event_add (struct event *ev, struct timeval *tv) |
181 | { |
231 | { |
182 | /* disable all watchers */ |
232 | dLOOPev; |
183 | event_del (ev); |
|
|
184 | |
233 | |
185 | if (ev->ev_events & EV_SIGNAL) |
234 | if (ev->ev_events & EV_SIGNAL) |
186 | { |
235 | { |
|
|
236 | if (!ev_is_active (&ev->iosig.sig)) |
|
|
237 | { |
187 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
238 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
188 | ev_signal_start (&ev->iosig.sig); |
239 | ev_signal_start (EV_A_ &ev->iosig.sig); |
189 | ++x_actives; |
240 | |
|
|
241 | ev->ev_flags |= EVLIST_SIGNAL; |
|
|
242 | } |
190 | } |
243 | } |
191 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
244 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
192 | { |
245 | { |
|
|
246 | if (!ev_is_active (&ev->iosig.io)) |
|
|
247 | { |
193 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
248 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
194 | ev_io_start (&ev->iosig.io); |
249 | ev_io_start (EV_A_ &ev->iosig.io); |
195 | ++x_actives; |
250 | |
|
|
251 | ev->ev_flags |= EVLIST_INSERTED; |
|
|
252 | } |
196 | } |
253 | } |
197 | |
254 | |
198 | if (tv) |
255 | if (tv) |
199 | { |
256 | { |
200 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
257 | ev->to.repeat = ev_tv_get (tv); |
|
|
258 | ev_timer_again (EV_A_ &ev->to); |
|
|
259 | ev->ev_flags |= EVLIST_TIMEOUT; |
|
|
260 | } |
|
|
261 | else |
|
|
262 | { |
201 | ev_timer_start (&ev->to); |
263 | ev_timer_stop (EV_A_ &ev->to); |
202 | ++x_actives; |
264 | ev->ev_flags &= ~EVLIST_TIMEOUT; |
203 | } |
265 | } |
|
|
266 | |
|
|
267 | ev->ev_flags |= EVLIST_ACTIVE; |
204 | |
268 | |
205 | return 0; |
269 | return 0; |
206 | } |
270 | } |
207 | |
271 | |
208 | int event_del (struct event *ev) |
272 | int event_del (struct event *ev) |
209 | { |
273 | { |
|
|
274 | dLOOPev; |
|
|
275 | |
210 | if (ev->ev_events & EV_SIGNAL) |
276 | if (ev->ev_events & EV_SIGNAL) |
|
|
277 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
|
|
278 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
279 | ev_io_stop (EV_A_ &ev->iosig.io); |
|
|
280 | |
|
|
281 | if (ev_is_active (&ev->to)) |
|
|
282 | ev_timer_stop (EV_A_ &ev->to); |
|
|
283 | |
|
|
284 | ev->ev_flags = EVLIST_INIT; |
|
|
285 | |
|
|
286 | return 0; |
|
|
287 | } |
|
|
288 | |
|
|
289 | void event_active (struct event *ev, int res, short ncalls) |
|
|
290 | { |
|
|
291 | dLOOPev; |
|
|
292 | |
|
|
293 | if (res & EV_TIMEOUT) |
|
|
294 | ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT); |
|
|
295 | |
|
|
296 | if (res & EV_SIGNAL) |
|
|
297 | ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL); |
|
|
298 | |
|
|
299 | if (res & (EV_READ | EV_WRITE)) |
|
|
300 | ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE)); |
|
|
301 | } |
|
|
302 | |
|
|
303 | int event_pending (struct event *ev, short events, struct timeval *tv) |
|
|
304 | { |
|
|
305 | short revents = 0; |
|
|
306 | dLOOPev; |
|
|
307 | |
|
|
308 | if (ev->ev_events & EV_SIGNAL) |
211 | { |
309 | { |
212 | /* sig */ |
310 | /* sig */ |
213 | if (ev_is_active (&ev->iosig.sig)) |
311 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
|
|
312 | revents |= EV_SIGNAL; |
|
|
313 | } |
|
|
314 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
315 | { |
|
|
316 | /* io */ |
|
|
317 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
|
|
318 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
|
|
319 | } |
|
|
320 | |
|
|
321 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
|
|
322 | { |
|
|
323 | revents |= EV_TIMEOUT; |
|
|
324 | |
|
|
325 | if (tv) |
214 | { |
326 | { |
215 | ev_signal_stop (&ev->iosig.sig); |
327 | ev_tstamp at = ev_now (EV_A); |
216 | --x_actives; |
328 | |
|
|
329 | tv->tv_sec = (long)at; |
|
|
330 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
217 | } |
331 | } |
218 | } |
332 | } |
219 | else |
|
|
220 | { |
|
|
221 | /* io */ |
|
|
222 | if (ev_is_active (&ev->iosig.io)) |
|
|
223 | { |
|
|
224 | ev_io_stop (&ev->iosig.io); |
|
|
225 | --x_actives; |
|
|
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | if (ev_is_active (&ev->to)) |
|
|
230 | { |
|
|
231 | ev_timer_stop (&ev->to); |
|
|
232 | --x_actives; |
|
|
233 | } |
|
|
234 | |
|
|
235 | return 0; |
|
|
236 | } |
|
|
237 | |
|
|
238 | int event_pending (struct event *ev, short events, struct timeval *tv) |
|
|
239 | { |
|
|
240 | short revents; |
|
|
241 | |
|
|
242 | if (ev->ev_events & EV_SIGNAL) |
|
|
243 | { |
|
|
244 | /* sig */ |
|
|
245 | if (ev->iosig.sig.pending) |
|
|
246 | revents |= EV_SIGNAL; |
|
|
247 | } |
|
|
248 | else |
|
|
249 | { |
|
|
250 | /* io */ |
|
|
251 | if (ev->iosig.io.pending) |
|
|
252 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
|
|
253 | } |
|
|
254 | |
|
|
255 | if (ev->to.pending) |
|
|
256 | { |
|
|
257 | revents |= EV_TIMEOUT; |
|
|
258 | |
|
|
259 | if (tv) |
|
|
260 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
|
|
261 | } |
|
|
262 | |
333 | |
263 | return events & revents; |
334 | return events & revents; |
264 | } |
335 | } |
265 | |
336 | |
266 | int event_priority_init (int npri) |
337 | int event_priority_init (int npri) |
267 | { |
338 | { |
268 | return event_base_priority_init (x_cur, npri); |
339 | return event_base_priority_init (ev_x_cur, npri); |
269 | } |
340 | } |
270 | |
341 | |
271 | int event_priority_set (struct event *ev, int pri) |
342 | int event_priority_set (struct event *ev, int pri) |
272 | { |
343 | { |
273 | ev->ev_pri = pri; |
344 | ev->ev_pri = pri; |
… | |
… | |
282 | return 0; |
353 | return 0; |
283 | } |
354 | } |
284 | |
355 | |
285 | int event_base_loop (struct event_base *base, int flags) |
356 | int event_base_loop (struct event_base *base, int flags) |
286 | { |
357 | { |
287 | do |
358 | dLOOPbase; |
288 | { |
|
|
289 | ev_loop (flags | EVLOOP_ONESHOT); |
|
|
290 | } |
|
|
291 | while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done); |
|
|
292 | |
359 | |
293 | return 0; |
360 | return !ev_run (EV_A_ flags); |
294 | } |
361 | } |
295 | |
362 | |
296 | int event_base_dispatch (struct event_base *base) |
363 | int event_base_dispatch (struct event_base *base) |
297 | { |
364 | { |
298 | return event_base_loop (base, 0); |
365 | return event_base_loop (base, 0); |
299 | } |
366 | } |
300 | |
367 | |
301 | static void |
368 | static void |
302 | x_loopexit_cb (int revents, void *arg) |
369 | ev_x_loopexit_cb (int revents, void *base) |
303 | { |
370 | { |
304 | ev_loop_done = 2; |
371 | dLOOPbase; |
|
|
372 | |
|
|
373 | ev_break (EV_A_ EVBREAK_ONE); |
305 | } |
374 | } |
306 | |
375 | |
307 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
376 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
308 | { |
377 | { |
309 | ev_tstamp after = tv_get (tv); |
378 | ev_tstamp after = ev_tv_get (tv); |
|
|
379 | dLOOPbase; |
310 | |
380 | |
311 | ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
381 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base); |
312 | } |
|
|
313 | |
382 | |
|
|
383 | return 0; |
|
|
384 | } |
|
|
385 | |
314 | struct x_once |
386 | struct ev_x_once |
315 | { |
387 | { |
316 | int fd; |
388 | int fd; |
317 | void (*cb)(int, short, void *); |
389 | void (*cb)(int, short, void *); |
318 | void *arg; |
390 | void *arg; |
319 | }; |
391 | }; |
320 | |
392 | |
321 | static void |
393 | static void |
322 | x_once_cb (int revents, void *arg) |
394 | ev_x_once_cb (int revents, void *arg) |
323 | { |
395 | { |
324 | struct x_once *once = arg; |
396 | struct ev_x_once *once = (struct ev_x_once *)arg; |
325 | |
397 | |
326 | once->cb (once->fd, revents, once->arg); |
398 | once->cb (once->fd, (short)revents, once->arg); |
327 | free (once); |
399 | free (once); |
328 | } |
400 | } |
329 | |
401 | |
330 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
402 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
331 | { |
403 | { |
332 | struct x_once *once = malloc (sizeof (struct x_once)); |
404 | struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once)); |
|
|
405 | dLOOPbase; |
333 | |
406 | |
334 | if (!once) |
407 | if (!once) |
335 | return -1; |
408 | return -1; |
336 | |
409 | |
337 | once->fd = fd; |
410 | once->fd = fd; |
338 | once->cb = cb; |
411 | once->cb = cb; |
339 | once->arg = arg; |
412 | once->arg = arg; |
340 | |
413 | |
341 | ev_once (fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
414 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once); |
342 | |
415 | |
343 | return 0; |
416 | return 0; |
344 | } |
417 | } |
345 | |
418 | |
346 | int event_base_priority_init (struct event_base *base, int npri) |
419 | int event_base_priority_init (struct event_base *base, int npri) |
347 | { |
420 | { |
348 | return 0; |
421 | /*dLOOPbase;*/ |
349 | } |
|
|
350 | |
422 | |
|
|
423 | return 0; |
|
|
424 | } |
|
|
425 | |