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