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