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