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