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 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 <assert.h> |
42 | #include <assert.h> |
35 | |
43 | |
36 | #ifndef WIN32 |
44 | #ifdef EV_EVENT_H |
37 | # include <sys/time.h> |
45 | # include EV_EVENT_H |
38 | #endif |
46 | #else |
39 | |
|
|
40 | #include "ev.h" |
|
|
41 | #include "event.h" |
47 | # include "event.h" |
|
|
48 | #endif |
42 | |
49 | |
43 | #if EV_MULTIPLICITY |
50 | #if EV_MULTIPLICITY |
44 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
51 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
45 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
52 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
46 | #else |
53 | #else |
… | |
… | |
52 | struct event_base |
59 | struct event_base |
53 | { |
60 | { |
54 | int dummy; |
61 | int dummy; |
55 | }; |
62 | }; |
56 | |
63 | |
57 | static struct event_base *x_cur; |
64 | static struct event_base *ev_x_cur; |
58 | |
|
|
59 | static void |
|
|
60 | tv_set (struct timeval *tv, ev_tstamp at) |
|
|
61 | { |
|
|
62 | tv->tv_sec = (long)at; |
|
|
63 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
|
|
64 | } |
|
|
65 | |
65 | |
66 | static ev_tstamp |
66 | static ev_tstamp |
67 | tv_get (struct timeval *tv) |
67 | ev_tv_get (struct timeval *tv) |
68 | { |
68 | { |
69 | if (tv) |
69 | if (tv) |
|
|
70 | { |
70 | 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 | } |
71 | else |
74 | else |
72 | return -1.; |
75 | return -1.; |
73 | } |
76 | } |
74 | |
77 | |
75 | #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) |
76 | |
80 | |
77 | const char *event_get_version (void) |
81 | const char *event_get_version (void) |
78 | { |
82 | { |
|
|
83 | /* returns ABI, not API or library, version */ |
79 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
84 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
80 | } |
85 | } |
81 | |
86 | |
82 | const char *event_get_method (void) |
87 | const char *event_get_method (void) |
83 | { |
88 | { |
… | |
… | |
85 | } |
90 | } |
86 | |
91 | |
87 | void *event_init (void) |
92 | void *event_init (void) |
88 | { |
93 | { |
89 | #if EV_MULTIPLICITY |
94 | #if EV_MULTIPLICITY |
90 | if (x_cur) |
95 | if (ev_x_cur) |
91 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
96 | ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
92 | else |
97 | else |
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
98 | ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
94 | #else |
99 | #else |
95 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
100 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
96 | |
101 | |
97 | x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
102 | ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
98 | #endif |
103 | #endif |
99 | |
104 | |
100 | return x_cur; |
105 | return ev_x_cur; |
101 | } |
106 | } |
102 | |
107 | |
103 | void event_base_free (struct event_base *base) |
108 | void event_base_free (struct event_base *base) |
104 | { |
109 | { |
105 | dLOOPbase; |
110 | dLOOPbase; |
106 | |
111 | |
107 | #if EV_MULTIPLICITY |
112 | #if EV_MULTIPLICITY |
108 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
113 | if (ev_default_loop (EVFLAG_AUTO) != loop) |
109 | ev_loop_destroy (loop); |
114 | ev_loop_destroy (loop); |
110 | #endif |
115 | #endif |
111 | } |
116 | } |
112 | |
117 | |
113 | int event_dispatch (void) |
118 | int event_dispatch (void) |
114 | { |
119 | { |
115 | return event_base_dispatch (x_cur); |
120 | return event_base_dispatch (ev_x_cur); |
116 | } |
121 | } |
117 | |
122 | |
118 | #ifdef EV_STANDALONE |
123 | #ifdef EV_STANDALONE |
119 | void event_set_log_callback (event_log_cb cb) |
124 | void event_set_log_callback (event_log_cb cb) |
120 | { |
125 | { |
… | |
… | |
122 | } |
127 | } |
123 | #endif |
128 | #endif |
124 | |
129 | |
125 | int event_loop (int flags) |
130 | int event_loop (int flags) |
126 | { |
131 | { |
127 | return event_base_loop (x_cur, flags); |
132 | return event_base_loop (ev_x_cur, flags); |
128 | } |
133 | } |
129 | |
134 | |
130 | int event_loopexit (struct timeval *tv) |
135 | int event_loopexit (struct timeval *tv) |
131 | { |
136 | { |
132 | return event_base_loopexit (x_cur, tv); |
137 | return event_base_loopexit (ev_x_cur, tv); |
133 | } |
138 | } |
134 | |
139 | |
135 | static void |
140 | static void |
136 | x_cb (struct event *ev, int revents) |
141 | ev_x_cb (struct event *ev, int revents) |
137 | { |
142 | { |
138 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
143 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
139 | |
144 | |
140 | ev->ev_res = revents; |
145 | ev->ev_res = revents; |
141 | ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); |
146 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
142 | } |
147 | } |
143 | |
148 | |
144 | static void |
149 | static void |
145 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
150 | ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
146 | { |
151 | { |
147 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
152 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
148 | |
153 | |
149 | if (revents & EV_ERROR) |
154 | if (revents & EV_ERROR) |
150 | event_del (ev); |
155 | event_del (ev); |
151 | |
156 | |
152 | x_cb (ev, revents); |
157 | ev_x_cb (ev, revents); |
153 | } |
158 | } |
154 | |
159 | |
155 | static void |
160 | static void |
156 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
161 | ev_x_cb_io (EV_P_ struct ev_io *w, int revents) |
157 | { |
162 | { |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
163 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
159 | |
164 | |
160 | if (revents & EV_ERROR) |
165 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
161 | event_del (ev); |
166 | event_del (ev); |
162 | else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
|
|
163 | ev_io_stop (EV_A_ w); |
|
|
164 | |
167 | |
165 | x_cb (ev, revents); |
168 | ev_x_cb (ev, revents); |
166 | } |
169 | } |
167 | |
170 | |
168 | static void |
171 | static void |
169 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
172 | ev_x_cb_to (EV_P_ struct ev_timer *w, int revents) |
170 | { |
173 | { |
171 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
174 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
172 | |
175 | |
173 | event_del (ev); |
176 | event_del (ev); |
174 | |
177 | |
175 | x_cb (ev, revents); |
178 | ev_x_cb (ev, revents); |
176 | } |
179 | } |
177 | |
180 | |
178 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
181 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
179 | { |
182 | { |
180 | if (events & EV_SIGNAL) |
183 | if (events & EV_SIGNAL) |
181 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
184 | ev_init (&ev->iosig.sig, ev_x_cb_sig); |
182 | else |
185 | else |
183 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
186 | ev_init (&ev->iosig.io, ev_x_cb_io); |
184 | |
187 | |
185 | ev_watcher_init (&ev->to, x_cb_to); |
188 | ev_init (&ev->to, ev_x_cb_to); |
186 | |
189 | |
187 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
190 | ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */ |
188 | ev->ev_fd = fd; |
191 | ev->ev_fd = fd; |
189 | ev->ev_events = events; |
192 | ev->ev_events = events; |
190 | ev->ev_pri = 0; |
193 | ev->ev_pri = 0; |
191 | ev->ev_callback = cb; |
194 | ev->ev_callback = cb; |
192 | ev->ev_arg = arg; |
195 | ev->ev_arg = arg; |
193 | ev->ev_res = 0; |
196 | ev->ev_res = 0; |
|
|
197 | ev->ev_flags = EVLIST_INIT; |
194 | } |
198 | } |
195 | |
199 | |
196 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
200 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
197 | { |
201 | { |
198 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
202 | return event_base_once (ev_x_cur, fd, events, cb, arg, tv); |
199 | } |
203 | } |
200 | |
204 | |
201 | int event_add (struct event *ev, struct timeval *tv) |
205 | int event_add (struct event *ev, struct timeval *tv) |
202 | { |
206 | { |
203 | dLOOPev; |
207 | dLOOPev; |
204 | |
208 | |
205 | /* disable all watchers */ |
|
|
206 | event_del (ev); |
|
|
207 | |
|
|
208 | if (ev->ev_events & EV_SIGNAL) |
209 | if (ev->ev_events & EV_SIGNAL) |
209 | { |
210 | { |
|
|
211 | if (!ev_is_active (&ev->iosig.sig)) |
|
|
212 | { |
210 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
213 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
211 | ev_signal_start (EV_A_ &ev->iosig.sig); |
214 | ev_signal_start (EV_A_ &ev->iosig.sig); |
|
|
215 | |
|
|
216 | ev->ev_flags |= EVLIST_SIGNAL; |
|
|
217 | } |
212 | } |
218 | } |
213 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
219 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
214 | { |
220 | { |
|
|
221 | if (!ev_is_active (&ev->iosig.io)) |
|
|
222 | { |
215 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
223 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
216 | ev_io_start (EV_A_ &ev->iosig.io); |
224 | ev_io_start (EV_A_ &ev->iosig.io); |
|
|
225 | |
|
|
226 | ev->ev_flags |= EVLIST_INSERTED; |
|
|
227 | } |
217 | } |
228 | } |
218 | |
229 | |
219 | if (tv) |
230 | if (tv) |
220 | { |
231 | { |
221 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
232 | ev->to.repeat = ev_tv_get (tv); |
|
|
233 | ev_timer_again (EV_A_ &ev->to); |
|
|
234 | ev->ev_flags |= EVLIST_TIMEOUT; |
|
|
235 | } |
|
|
236 | else |
|
|
237 | { |
222 | ev_timer_start (EV_A_ &ev->to); |
238 | ev_timer_stop (EV_A_ &ev->to); |
|
|
239 | ev->ev_flags &= ~EVLIST_TIMEOUT; |
223 | } |
240 | } |
|
|
241 | |
|
|
242 | ev->ev_flags |= EVLIST_ACTIVE; |
224 | |
243 | |
225 | return 0; |
244 | return 0; |
226 | } |
245 | } |
227 | |
246 | |
228 | int event_del (struct event *ev) |
247 | int event_del (struct event *ev) |
229 | { |
248 | { |
230 | dLOOPev; |
249 | dLOOPev; |
231 | |
250 | |
232 | if (ev->ev_events & EV_SIGNAL) |
251 | if (ev->ev_events & EV_SIGNAL) |
233 | { |
|
|
234 | /* sig */ |
|
|
235 | if (ev_is_active (&ev->iosig.sig)) |
|
|
236 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
252 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
237 | } |
|
|
238 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
253 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
239 | { |
|
|
240 | /* io */ |
|
|
241 | if (ev_is_active (&ev->iosig.io)) |
|
|
242 | ev_io_stop (EV_A_ &ev->iosig.io); |
254 | ev_io_stop (EV_A_ &ev->iosig.io); |
243 | } |
|
|
244 | |
255 | |
245 | if (ev_is_active (&ev->to)) |
256 | if (ev_is_active (&ev->to)) |
246 | ev_timer_stop (EV_A_ &ev->to); |
257 | ev_timer_stop (EV_A_ &ev->to); |
247 | |
258 | |
|
|
259 | ev->ev_flags = EVLIST_INIT; |
|
|
260 | |
248 | return 0; |
261 | return 0; |
|
|
262 | } |
|
|
263 | |
|
|
264 | void event_active (struct event *ev, int res, short ncalls) |
|
|
265 | { |
|
|
266 | dLOOPev; |
|
|
267 | |
|
|
268 | if (res & EV_TIMEOUT) |
|
|
269 | ev_feed_event (EV_A_ &ev->to, res & EV_TIMEOUT); |
|
|
270 | |
|
|
271 | if (res & EV_SIGNAL) |
|
|
272 | ev_feed_event (EV_A_ &ev->iosig.sig, res & EV_SIGNAL); |
|
|
273 | |
|
|
274 | if (res & (EV_READ | EV_WRITE)) |
|
|
275 | ev_feed_event (EV_A_ &ev->iosig.io, res & (EV_READ | EV_WRITE)); |
249 | } |
276 | } |
250 | |
277 | |
251 | int event_pending (struct event *ev, short events, struct timeval *tv) |
278 | int event_pending (struct event *ev, short events, struct timeval *tv) |
252 | { |
279 | { |
253 | short revents = 0; |
280 | short revents = 0; |
254 | dLOOPev; |
281 | dLOOPev; |
255 | |
|
|
256 | |
282 | |
257 | if (ev->ev_events & EV_SIGNAL) |
283 | if (ev->ev_events & EV_SIGNAL) |
258 | { |
284 | { |
259 | /* sig */ |
285 | /* sig */ |
260 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
286 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
… | |
… | |
270 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
296 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
271 | { |
297 | { |
272 | revents |= EV_TIMEOUT; |
298 | revents |= EV_TIMEOUT; |
273 | |
299 | |
274 | if (tv) |
300 | if (tv) |
275 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
301 | EV_TV_SET (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
276 | } |
302 | } |
277 | |
303 | |
278 | return events & revents; |
304 | return events & revents; |
279 | } |
305 | } |
280 | |
306 | |
281 | int event_priority_init (int npri) |
307 | int event_priority_init (int npri) |
282 | { |
308 | { |
283 | return event_base_priority_init (x_cur, npri); |
309 | return event_base_priority_init (ev_x_cur, npri); |
284 | } |
310 | } |
285 | |
311 | |
286 | int event_priority_set (struct event *ev, int pri) |
312 | int event_priority_set (struct event *ev, int pri) |
287 | { |
313 | { |
288 | ev->ev_pri = pri; |
314 | ev->ev_pri = pri; |
… | |
… | |
310 | { |
336 | { |
311 | return event_base_loop (base, 0); |
337 | return event_base_loop (base, 0); |
312 | } |
338 | } |
313 | |
339 | |
314 | static void |
340 | static void |
315 | x_loopexit_cb (int revents, void *base) |
341 | ev_x_loopexit_cb (int revents, void *base) |
316 | { |
342 | { |
317 | dLOOPbase; |
343 | dLOOPbase; |
318 | |
344 | |
319 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
345 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
320 | } |
346 | } |
321 | |
347 | |
322 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
348 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
323 | { |
349 | { |
324 | ev_tstamp after = tv_get (tv); |
350 | ev_tstamp after = ev_tv_get (tv); |
325 | dLOOPbase; |
351 | dLOOPbase; |
326 | |
352 | |
327 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
353 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base); |
328 | |
354 | |
329 | return -1; |
355 | return 0; |
330 | } |
356 | } |
331 | |
357 | |
332 | struct x_once |
358 | struct ev_x_once |
333 | { |
359 | { |
334 | int fd; |
360 | int fd; |
335 | void (*cb)(int, short, void *); |
361 | void (*cb)(int, short, void *); |
336 | void *arg; |
362 | void *arg; |
337 | }; |
363 | }; |
338 | |
364 | |
339 | static void |
365 | static void |
340 | x_once_cb (int revents, void *arg) |
366 | ev_x_once_cb (int revents, void *arg) |
341 | { |
367 | { |
342 | struct x_once *once = (struct x_once *)arg; |
368 | struct ev_x_once *once = (struct ev_x_once *)arg; |
343 | |
369 | |
344 | once->cb (once->fd, revents, once->arg); |
370 | once->cb (once->fd, (short)revents, once->arg); |
345 | free (once); |
371 | free (once); |
346 | } |
372 | } |
347 | |
373 | |
348 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
374 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
349 | { |
375 | { |
350 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
376 | struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once)); |
351 | dLOOPbase; |
377 | dLOOPbase; |
352 | |
378 | |
353 | if (!once) |
379 | if (!once) |
354 | return -1; |
380 | return -1; |
355 | |
381 | |
356 | once->fd = fd; |
382 | once->fd = fd; |
357 | once->cb = cb; |
383 | once->cb = cb; |
358 | once->arg = arg; |
384 | once->arg = arg; |
359 | |
385 | |
360 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
386 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once); |
361 | |
387 | |
362 | return 0; |
388 | return 0; |
363 | } |
389 | } |
364 | |
390 | |
365 | int event_base_priority_init (struct event_base *base, int npri) |
391 | int event_base_priority_init (struct event_base *base, int npri) |