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