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