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