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