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