1 |
root |
1.1 |
/* |
2 |
|
|
* libevent compatibility layer |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions are |
9 |
|
|
* met: |
10 |
|
|
* |
11 |
|
|
* * Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* |
14 |
|
|
* * Redistributions in binary form must reproduce the above |
15 |
|
|
* copyright notice, this list of conditions and the following |
16 |
|
|
* disclaimer in the documentation and/or other materials provided |
17 |
|
|
* with the distribution. |
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 |
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 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
#include <stddef.h> |
33 |
|
|
#include <stdlib.h> |
34 |
root |
1.10 |
#include <assert.h> |
35 |
root |
1.1 |
|
36 |
root |
1.18 |
#ifndef WIN32 |
37 |
|
|
# include <sys/time.h> |
38 |
|
|
#endif |
39 |
|
|
|
40 |
root |
1.12 |
#include "ev.h" |
41 |
|
|
#include "event.h" |
42 |
root |
1.1 |
|
43 |
root |
1.9 |
#if EV_MULTIPLICITY |
44 |
|
|
# define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
45 |
|
|
# define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
46 |
root |
1.8 |
#else |
47 |
|
|
# define dLOOPev |
48 |
|
|
# define dLOOPbase |
49 |
|
|
#endif |
50 |
root |
1.7 |
|
51 |
root |
1.9 |
/* never accessed, will always be cast from/to ev_loop */ |
52 |
root |
1.1 |
struct event_base |
53 |
|
|
{ |
54 |
|
|
int dummy; |
55 |
|
|
}; |
56 |
|
|
|
57 |
root |
1.10 |
static struct event_base *x_cur; |
58 |
root |
1.1 |
|
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 |
|
|
|
66 |
|
|
static ev_tstamp |
67 |
|
|
tv_get (struct timeval *tv) |
68 |
|
|
{ |
69 |
|
|
if (tv) |
70 |
|
|
return tv->tv_sec + tv->tv_usec * 1e-6; |
71 |
|
|
else |
72 |
|
|
return -1.; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
#define EVENT_VERSION(a,b) # a "." # b |
76 |
|
|
|
77 |
|
|
const char *event_get_version (void) |
78 |
|
|
{ |
79 |
|
|
return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
const char *event_get_method (void) |
83 |
|
|
{ |
84 |
|
|
return "libev"; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
void *event_init (void) |
88 |
|
|
{ |
89 |
root |
1.9 |
#if EV_MULTIPLICITY |
90 |
root |
1.10 |
if (x_cur) |
91 |
|
|
x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
92 |
|
|
else |
93 |
root |
1.13 |
x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
94 |
root |
1.9 |
#else |
95 |
root |
1.10 |
assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
96 |
|
|
|
97 |
root |
1.17 |
x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
98 |
root |
1.9 |
#endif |
99 |
root |
1.1 |
|
100 |
root |
1.9 |
return x_cur; |
101 |
root |
1.1 |
} |
102 |
|
|
|
103 |
|
|
void event_base_free (struct event_base *base) |
104 |
|
|
{ |
105 |
root |
1.8 |
dLOOPbase; |
106 |
|
|
|
107 |
root |
1.9 |
#if EV_MULTIPLICITY |
108 |
root |
1.13 |
if (ev_default_loop (EVMETHOD_AUTO) != loop) |
109 |
|
|
ev_loop_destroy (loop); |
110 |
root |
1.9 |
#endif |
111 |
root |
1.1 |
} |
112 |
|
|
|
113 |
|
|
int event_dispatch (void) |
114 |
|
|
{ |
115 |
|
|
return event_base_dispatch (x_cur); |
116 |
|
|
} |
117 |
|
|
|
118 |
root |
1.12 |
#ifdef EV_STANDALONE |
119 |
root |
1.1 |
void event_set_log_callback (event_log_cb cb) |
120 |
|
|
{ |
121 |
|
|
/* nop */ |
122 |
|
|
} |
123 |
root |
1.5 |
#endif |
124 |
root |
1.1 |
|
125 |
|
|
int event_loop (int flags) |
126 |
|
|
{ |
127 |
|
|
return event_base_loop (x_cur, flags); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
int event_loopexit (struct timeval *tv) |
131 |
|
|
{ |
132 |
root |
1.5 |
return event_base_loopexit (x_cur, tv); |
133 |
root |
1.1 |
} |
134 |
|
|
|
135 |
|
|
static void |
136 |
|
|
x_cb (struct event *ev, int revents) |
137 |
|
|
{ |
138 |
|
|
revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
139 |
|
|
|
140 |
|
|
ev->ev_res = revents; |
141 |
|
|
ev->ev_callback (ev->ev_fd, revents, ev->ev_arg); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
static void |
145 |
root |
1.7 |
x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
146 |
root |
1.1 |
{ |
147 |
root |
1.20 |
struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
148 |
|
|
|
149 |
|
|
if (revents & EV_ERROR) |
150 |
|
|
event_del (ev); |
151 |
|
|
|
152 |
|
|
x_cb (ev, revents); |
153 |
root |
1.1 |
} |
154 |
|
|
|
155 |
|
|
static void |
156 |
root |
1.7 |
x_cb_io (EV_P_ struct ev_io *w, int revents) |
157 |
root |
1.1 |
{ |
158 |
root |
1.7 |
struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
159 |
|
|
|
160 |
root |
1.20 |
if (revents & EV_ERROR) |
161 |
|
|
event_del (ev); |
162 |
|
|
else if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
163 |
root |
1.9 |
ev_io_stop (EV_A_ w); |
164 |
root |
1.7 |
|
165 |
|
|
x_cb (ev, revents); |
166 |
root |
1.1 |
} |
167 |
|
|
|
168 |
|
|
static void |
169 |
root |
1.7 |
x_cb_to (EV_P_ struct ev_timer *w, int revents) |
170 |
root |
1.1 |
{ |
171 |
root |
1.14 |
struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
172 |
|
|
|
173 |
|
|
event_del (ev); |
174 |
|
|
|
175 |
|
|
x_cb (ev, revents); |
176 |
root |
1.1 |
} |
177 |
|
|
|
178 |
|
|
void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
179 |
|
|
{ |
180 |
root |
1.14 |
if (events & EV_SIGNAL) |
181 |
|
|
ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
182 |
|
|
else |
183 |
|
|
ev_watcher_init (&ev->iosig.io, x_cb_io); |
184 |
root |
1.4 |
|
185 |
root |
1.14 |
ev_watcher_init (&ev->to, x_cb_to); |
186 |
root |
1.1 |
|
187 |
root |
1.9 |
ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
188 |
root |
1.1 |
ev->ev_fd = fd; |
189 |
|
|
ev->ev_events = events; |
190 |
|
|
ev->ev_pri = 0; |
191 |
|
|
ev->ev_callback = cb; |
192 |
|
|
ev->ev_arg = arg; |
193 |
|
|
ev->ev_res = 0; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
197 |
|
|
{ |
198 |
root |
1.5 |
return event_base_once (x_cur, fd, events, cb, arg, tv); |
199 |
root |
1.1 |
} |
200 |
|
|
|
201 |
|
|
int event_add (struct event *ev, struct timeval *tv) |
202 |
|
|
{ |
203 |
root |
1.7 |
dLOOPev; |
204 |
|
|
|
205 |
root |
1.4 |
/* disable all watchers */ |
206 |
|
|
event_del (ev); |
207 |
|
|
|
208 |
|
|
if (ev->ev_events & EV_SIGNAL) |
209 |
root |
1.1 |
{ |
210 |
root |
1.4 |
ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
211 |
root |
1.7 |
ev_signal_start (EV_A_ &ev->iosig.sig); |
212 |
root |
1.1 |
} |
213 |
root |
1.4 |
else if (ev->ev_events & (EV_READ | EV_WRITE)) |
214 |
root |
1.1 |
{ |
215 |
root |
1.4 |
ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
216 |
root |
1.7 |
ev_io_start (EV_A_ &ev->iosig.io); |
217 |
root |
1.1 |
} |
218 |
|
|
|
219 |
root |
1.4 |
if (tv) |
220 |
root |
1.1 |
{ |
221 |
root |
1.4 |
ev_timer_set (&ev->to, tv_get (tv), 0.); |
222 |
root |
1.7 |
ev_timer_start (EV_A_ &ev->to); |
223 |
root |
1.1 |
} |
224 |
|
|
|
225 |
|
|
return 0; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
int event_del (struct event *ev) |
229 |
|
|
{ |
230 |
root |
1.7 |
dLOOPev; |
231 |
|
|
|
232 |
root |
1.4 |
if (ev->ev_events & EV_SIGNAL) |
233 |
|
|
{ |
234 |
|
|
/* sig */ |
235 |
|
|
if (ev_is_active (&ev->iosig.sig)) |
236 |
root |
1.7 |
ev_signal_stop (EV_A_ &ev->iosig.sig); |
237 |
root |
1.4 |
} |
238 |
root |
1.16 |
else if (ev->ev_events & (EV_READ | EV_WRITE)) |
239 |
root |
1.1 |
{ |
240 |
root |
1.4 |
/* io */ |
241 |
|
|
if (ev_is_active (&ev->iosig.io)) |
242 |
root |
1.7 |
ev_io_stop (EV_A_ &ev->iosig.io); |
243 |
root |
1.1 |
} |
244 |
|
|
|
245 |
|
|
if (ev_is_active (&ev->to)) |
246 |
root |
1.7 |
ev_timer_stop (EV_A_ &ev->to); |
247 |
root |
1.1 |
|
248 |
|
|
return 0; |
249 |
|
|
} |
250 |
|
|
|
251 |
root |
1.21 |
void event_active (struct event *ev, int res, short ncalls) |
252 |
|
|
{ |
253 |
|
|
if (res & EV_TIMEOUT) |
254 |
|
|
ev_feed_event (&iosig.to, res & EV_TIMEOUT); |
255 |
|
|
|
256 |
|
|
if (res & EV_SIGNAL) |
257 |
|
|
ev_feed_event (&iosig.sig, res & EV_SIGNAL); |
258 |
|
|
|
259 |
|
|
if (res & (EV_READ | EV_WRITE)) |
260 |
|
|
ev_feed_event (&iosig.io, res & (EV_READ | EV_WRITE)); |
261 |
|
|
} |
262 |
|
|
|
263 |
root |
1.1 |
int event_pending (struct event *ev, short events, struct timeval *tv) |
264 |
|
|
{ |
265 |
root |
1.18 |
short revents = 0; |
266 |
root |
1.7 |
dLOOPev; |
267 |
|
|
|
268 |
root |
1.1 |
|
269 |
root |
1.4 |
if (ev->ev_events & EV_SIGNAL) |
270 |
|
|
{ |
271 |
|
|
/* sig */ |
272 |
root |
1.14 |
if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
273 |
root |
1.4 |
revents |= EV_SIGNAL; |
274 |
|
|
} |
275 |
root |
1.16 |
else if (ev->ev_events & (EV_READ | EV_WRITE)) |
276 |
root |
1.4 |
{ |
277 |
|
|
/* io */ |
278 |
root |
1.14 |
if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
279 |
root |
1.4 |
revents |= ev->ev_events & (EV_READ | EV_WRITE); |
280 |
|
|
} |
281 |
root |
1.1 |
|
282 |
root |
1.14 |
if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
283 |
root |
1.1 |
{ |
284 |
|
|
revents |= EV_TIMEOUT; |
285 |
|
|
|
286 |
|
|
if (tv) |
287 |
root |
1.7 |
tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
288 |
root |
1.1 |
} |
289 |
|
|
|
290 |
|
|
return events & revents; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
int event_priority_init (int npri) |
294 |
|
|
{ |
295 |
|
|
return event_base_priority_init (x_cur, npri); |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
int event_priority_set (struct event *ev, int pri) |
299 |
|
|
{ |
300 |
|
|
ev->ev_pri = pri; |
301 |
|
|
|
302 |
|
|
return 0; |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
int event_base_set (struct event_base *base, struct event *ev) |
306 |
|
|
{ |
307 |
|
|
ev->ev_base = base; |
308 |
|
|
|
309 |
|
|
return 0; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
int event_base_loop (struct event_base *base, int flags) |
313 |
|
|
{ |
314 |
root |
1.7 |
dLOOPbase; |
315 |
root |
1.8 |
|
316 |
|
|
ev_loop (EV_A_ flags); |
317 |
root |
1.1 |
|
318 |
|
|
return 0; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
int event_base_dispatch (struct event_base *base) |
322 |
|
|
{ |
323 |
|
|
return event_base_loop (base, 0); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
static void |
327 |
root |
1.9 |
x_loopexit_cb (int revents, void *base) |
328 |
root |
1.1 |
{ |
329 |
root |
1.9 |
dLOOPbase; |
330 |
|
|
|
331 |
|
|
ev_unloop (EV_A_ EVUNLOOP_ONCE); |
332 |
root |
1.1 |
} |
333 |
|
|
|
334 |
|
|
int event_base_loopexit (struct event_base *base, struct timeval *tv) |
335 |
|
|
{ |
336 |
root |
1.18 |
ev_tstamp after = tv_get (tv); |
337 |
root |
1.7 |
dLOOPbase; |
338 |
root |
1.1 |
|
339 |
root |
1.7 |
ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
340 |
root |
1.5 |
|
341 |
|
|
return -1; |
342 |
root |
1.1 |
} |
343 |
|
|
|
344 |
|
|
struct x_once |
345 |
|
|
{ |
346 |
|
|
int fd; |
347 |
|
|
void (*cb)(int, short, void *); |
348 |
|
|
void *arg; |
349 |
|
|
}; |
350 |
|
|
|
351 |
|
|
static void |
352 |
|
|
x_once_cb (int revents, void *arg) |
353 |
|
|
{ |
354 |
root |
1.19 |
struct x_once *once = (struct x_once *)arg; |
355 |
root |
1.1 |
|
356 |
|
|
once->cb (once->fd, revents, once->arg); |
357 |
|
|
free (once); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
361 |
|
|
{ |
362 |
root |
1.19 |
struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
363 |
root |
1.7 |
dLOOPbase; |
364 |
root |
1.1 |
|
365 |
|
|
if (!once) |
366 |
|
|
return -1; |
367 |
|
|
|
368 |
|
|
once->fd = fd; |
369 |
|
|
once->cb = cb; |
370 |
|
|
once->arg = arg; |
371 |
|
|
|
372 |
root |
1.7 |
ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
373 |
root |
1.1 |
|
374 |
|
|
return 0; |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
int event_base_priority_init (struct event_base *base, int npri) |
378 |
|
|
{ |
379 |
root |
1.13 |
/*dLOOPbase;*/ |
380 |
root |
1.7 |
|
381 |
root |
1.1 |
return 0; |
382 |
|
|
} |
383 |
|
|
|