1 | /* |
1 | /* |
2 | * libevent compatibility layer |
2 | * libevent compatibility layer |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2012 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
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- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
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- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
39 | |
39 | |
40 | #include <stddef.h> |
40 | #include <stddef.h> |
41 | #include <stdlib.h> |
41 | #include <stdlib.h> |
42 | #include <assert.h> |
42 | #include <assert.h> |
43 | |
43 | |
44 | #ifndef WIN32 |
|
|
45 | # include <sys/time.h> |
|
|
46 | #endif |
|
|
47 | |
|
|
48 | #ifdef EV_EVENT_H |
44 | #ifdef EV_EVENT_H |
49 | # include EV_EVENT_H |
45 | # include EV_EVENT_H |
50 | #else |
46 | #else |
51 | # include "event.h" |
47 | # include "event.h" |
52 | #endif |
48 | #endif |
… | |
… | |
63 | struct event_base |
59 | struct event_base |
64 | { |
60 | { |
65 | int dummy; |
61 | int dummy; |
66 | }; |
62 | }; |
67 | |
63 | |
68 | static struct event_base *x_cur; |
64 | static struct event_base *ev_x_cur; |
69 | |
|
|
70 | static void |
|
|
71 | tv_set (struct timeval *tv, ev_tstamp at) |
|
|
72 | { |
|
|
73 | tv->tv_sec = (long)at; |
|
|
74 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
|
|
75 | } |
|
|
76 | |
65 | |
77 | static ev_tstamp |
66 | static ev_tstamp |
78 | tv_get (struct timeval *tv) |
67 | ev_tv_get (struct timeval *tv) |
79 | { |
68 | { |
80 | if (tv) |
69 | if (tv) |
|
|
70 | { |
81 | return tv->tv_sec + tv->tv_usec * 1e-6; |
71 | ev_tstamp after = tv->tv_sec + tv->tv_usec * 1e-6; |
|
|
72 | return after ? after : 1e-6; |
|
|
73 | } |
82 | else |
74 | else |
83 | return -1.; |
75 | return -1.; |
84 | } |
76 | } |
85 | |
77 | |
86 | #define EVENT_VERSION(a,b) # a "." # b |
78 | #define EVENT_STRINGIFY(s) # s |
|
|
79 | #define EVENT_VERSION(a,b) EVENT_STRINGIFY (a) "." EVENT_STRINGIFY (b) |
87 | |
80 | |
|
|
81 | const char * |
88 | const char *event_get_version (void) |
82 | event_get_version (void) |
89 | { |
83 | { |
|
|
84 | /* returns ABI, not API or library, version */ |
90 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
85 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
91 | } |
86 | } |
92 | |
87 | |
|
|
88 | const char * |
93 | const char *event_get_method (void) |
89 | event_get_method (void) |
94 | { |
90 | { |
95 | return "libev"; |
91 | return "libev"; |
96 | } |
92 | } |
97 | |
93 | |
98 | void *event_init (void) |
94 | void *event_init (void) |
99 | { |
95 | { |
100 | #if EV_MULTIPLICITY |
96 | #if EV_MULTIPLICITY |
101 | if (x_cur) |
97 | if (ev_x_cur) |
102 | x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
98 | ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
103 | else |
99 | else |
104 | x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
100 | ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
105 | #else |
101 | #else |
106 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
102 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
107 | |
103 | |
108 | x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
104 | ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
109 | #endif |
105 | #endif |
110 | |
106 | |
111 | return x_cur; |
107 | return ev_x_cur; |
|
|
108 | } |
|
|
109 | |
|
|
110 | const char * |
|
|
111 | event_base_get_method (const struct event_base *base) |
|
|
112 | { |
|
|
113 | return "libev"; |
|
|
114 | } |
|
|
115 | |
|
|
116 | struct event_base * |
|
|
117 | event_base_new (void) |
|
|
118 | { |
|
|
119 | #if EV_MULTIPLICITY |
|
|
120 | return (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
|
|
121 | #else |
|
|
122 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY")); |
|
|
123 | return NULL; |
|
|
124 | #endif |
112 | } |
125 | } |
113 | |
126 | |
114 | void event_base_free (struct event_base *base) |
127 | void event_base_free (struct event_base *base) |
115 | { |
128 | { |
116 | dLOOPbase; |
129 | dLOOPbase; |
117 | |
130 | |
118 | #if EV_MULTIPLICITY |
131 | #if EV_MULTIPLICITY |
119 | if (ev_default_loop (EVFLAG_AUTO) != loop) |
132 | if (!ev_is_default_loop (loop)) |
120 | ev_loop_destroy (loop); |
133 | ev_loop_destroy (loop); |
121 | #endif |
134 | #endif |
122 | } |
135 | } |
123 | |
136 | |
124 | int event_dispatch (void) |
137 | int event_dispatch (void) |
125 | { |
138 | { |
126 | return event_base_dispatch (x_cur); |
139 | return event_base_dispatch (ev_x_cur); |
127 | } |
140 | } |
128 | |
141 | |
129 | #ifdef EV_STANDALONE |
142 | #ifdef EV_STANDALONE |
130 | void event_set_log_callback (event_log_cb cb) |
143 | void event_set_log_callback (event_log_cb cb) |
131 | { |
144 | { |
… | |
… | |
133 | } |
146 | } |
134 | #endif |
147 | #endif |
135 | |
148 | |
136 | int event_loop (int flags) |
149 | int event_loop (int flags) |
137 | { |
150 | { |
138 | return event_base_loop (x_cur, flags); |
151 | return event_base_loop (ev_x_cur, flags); |
139 | } |
152 | } |
140 | |
153 | |
141 | int event_loopexit (struct timeval *tv) |
154 | int event_loopexit (struct timeval *tv) |
142 | { |
155 | { |
143 | return event_base_loopexit (x_cur, tv); |
156 | return event_base_loopexit (ev_x_cur, tv); |
144 | } |
157 | } |
145 | |
158 | |
|
|
159 | event_callback_fn event_get_callback |
|
|
160 | (const struct event *ev) |
|
|
161 | { |
|
|
162 | return ev->ev_callback; |
|
|
163 | } |
|
|
164 | |
146 | static void |
165 | static void |
147 | x_cb (struct event *ev, int revents) |
166 | ev_x_cb (struct event *ev, int revents) |
148 | { |
167 | { |
149 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
168 | revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL; |
150 | |
169 | |
151 | ev->ev_res = revents; |
170 | ev->ev_res = revents; |
152 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
171 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
153 | } |
172 | } |
154 | |
173 | |
155 | static void |
174 | static void |
156 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
175 | ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
157 | { |
176 | { |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
177 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
159 | |
178 | |
160 | if (revents & EV_ERROR) |
179 | if (revents & EV_ERROR) |
161 | event_del (ev); |
180 | event_del (ev); |
162 | |
181 | |
163 | x_cb (ev, revents); |
182 | ev_x_cb (ev, revents); |
164 | } |
183 | } |
165 | |
184 | |
166 | static void |
185 | static void |
167 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
186 | ev_x_cb_io (EV_P_ struct ev_io *w, int revents) |
168 | { |
187 | { |
169 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
188 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
170 | |
189 | |
171 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
190 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
172 | event_del (ev); |
191 | event_del (ev); |
173 | |
192 | |
174 | x_cb (ev, revents); |
193 | ev_x_cb (ev, revents); |
175 | } |
194 | } |
176 | |
195 | |
177 | static void |
196 | static void |
178 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
197 | ev_x_cb_to (EV_P_ struct ev_timer *w, int revents) |
179 | { |
198 | { |
180 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
199 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
181 | |
200 | |
182 | event_del (ev); |
201 | event_del (ev); |
183 | |
202 | |
184 | x_cb (ev, revents); |
203 | ev_x_cb (ev, revents); |
185 | } |
204 | } |
186 | |
205 | |
187 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
206 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
188 | { |
207 | { |
189 | if (events & EV_SIGNAL) |
208 | if (events & EV_SIGNAL) |
190 | ev_init (&ev->iosig.sig, x_cb_sig); |
209 | ev_init (&ev->iosig.sig, ev_x_cb_sig); |
191 | else |
210 | else |
192 | ev_init (&ev->iosig.io, x_cb_io); |
211 | ev_init (&ev->iosig.io, ev_x_cb_io); |
193 | |
212 | |
194 | ev_init (&ev->to, x_cb_to); |
213 | ev_init (&ev->to, ev_x_cb_to); |
195 | |
214 | |
196 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
215 | ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */ |
197 | ev->ev_fd = fd; |
216 | ev->ev_fd = fd; |
198 | ev->ev_events = events; |
217 | ev->ev_events = events; |
199 | ev->ev_pri = 0; |
218 | ev->ev_pri = 0; |
200 | ev->ev_callback = cb; |
219 | ev->ev_callback = cb; |
201 | ev->ev_arg = arg; |
220 | ev->ev_arg = arg; |
… | |
… | |
203 | ev->ev_flags = EVLIST_INIT; |
222 | ev->ev_flags = EVLIST_INIT; |
204 | } |
223 | } |
205 | |
224 | |
206 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
225 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
207 | { |
226 | { |
208 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
227 | return event_base_once (ev_x_cur, fd, events, cb, arg, tv); |
209 | } |
228 | } |
210 | |
229 | |
211 | int event_add (struct event *ev, struct timeval *tv) |
230 | int event_add (struct event *ev, struct timeval *tv) |
212 | { |
231 | { |
213 | dLOOPev; |
232 | dLOOPev; |
… | |
… | |
233 | } |
252 | } |
234 | } |
253 | } |
235 | |
254 | |
236 | if (tv) |
255 | if (tv) |
237 | { |
256 | { |
238 | ev->to.repeat = tv_get (tv); |
257 | ev->to.repeat = ev_tv_get (tv); |
239 | ev_timer_again (EV_A_ &ev->to); |
258 | ev_timer_again (EV_A_ &ev->to); |
240 | ev->ev_flags |= EVLIST_TIMEOUT; |
259 | ev->ev_flags |= EVLIST_TIMEOUT; |
241 | } |
260 | } |
242 | else |
261 | else |
243 | { |
262 | { |
… | |
… | |
284 | int event_pending (struct event *ev, short events, struct timeval *tv) |
303 | int event_pending (struct event *ev, short events, struct timeval *tv) |
285 | { |
304 | { |
286 | short revents = 0; |
305 | short revents = 0; |
287 | dLOOPev; |
306 | dLOOPev; |
288 | |
307 | |
289 | |
|
|
290 | if (ev->ev_events & EV_SIGNAL) |
308 | if (ev->ev_events & EV_SIGNAL) |
291 | { |
309 | { |
292 | /* sig */ |
310 | /* sig */ |
293 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
311 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
294 | revents |= EV_SIGNAL; |
312 | revents |= EV_SIGNAL; |
… | |
… | |
303 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
321 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
304 | { |
322 | { |
305 | revents |= EV_TIMEOUT; |
323 | revents |= EV_TIMEOUT; |
306 | |
324 | |
307 | if (tv) |
325 | if (tv) |
308 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
326 | { |
|
|
327 | ev_tstamp at = ev_now (EV_A); |
|
|
328 | |
|
|
329 | tv->tv_sec = (long)at; |
|
|
330 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
|
|
331 | } |
309 | } |
332 | } |
310 | |
333 | |
311 | return events & revents; |
334 | return events & revents; |
312 | } |
335 | } |
313 | |
336 | |
314 | int event_priority_init (int npri) |
337 | int event_priority_init (int npri) |
315 | { |
338 | { |
316 | return event_base_priority_init (x_cur, npri); |
339 | return event_base_priority_init (ev_x_cur, npri); |
317 | } |
340 | } |
318 | |
341 | |
319 | int event_priority_set (struct event *ev, int pri) |
342 | int event_priority_set (struct event *ev, int pri) |
320 | { |
343 | { |
321 | ev->ev_pri = pri; |
344 | ev->ev_pri = pri; |
… | |
… | |
332 | |
355 | |
333 | int event_base_loop (struct event_base *base, int flags) |
356 | int event_base_loop (struct event_base *base, int flags) |
334 | { |
357 | { |
335 | dLOOPbase; |
358 | dLOOPbase; |
336 | |
359 | |
337 | ev_loop (EV_A_ flags); |
360 | return !ev_run (EV_A_ flags); |
338 | |
|
|
339 | return 0; |
|
|
340 | } |
361 | } |
341 | |
362 | |
342 | int event_base_dispatch (struct event_base *base) |
363 | int event_base_dispatch (struct event_base *base) |
343 | { |
364 | { |
344 | return event_base_loop (base, 0); |
365 | return event_base_loop (base, 0); |
345 | } |
366 | } |
346 | |
367 | |
347 | static void |
368 | static void |
348 | x_loopexit_cb (int revents, void *base) |
369 | ev_x_loopexit_cb (int revents, void *base) |
349 | { |
370 | { |
350 | dLOOPbase; |
371 | dLOOPbase; |
351 | |
372 | |
352 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
373 | ev_break (EV_A_ EVBREAK_ONE); |
353 | } |
374 | } |
354 | |
375 | |
355 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
376 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
356 | { |
377 | { |
357 | ev_tstamp after = tv_get (tv); |
378 | ev_tstamp after = ev_tv_get (tv); |
358 | dLOOPbase; |
379 | dLOOPbase; |
359 | |
380 | |
360 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
381 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base); |
361 | |
382 | |
362 | return 0; |
383 | return 0; |
363 | } |
384 | } |
364 | |
385 | |
365 | struct x_once |
386 | struct ev_x_once |
366 | { |
387 | { |
367 | int fd; |
388 | int fd; |
368 | void (*cb)(int, short, void *); |
389 | void (*cb)(int, short, void *); |
369 | void *arg; |
390 | void *arg; |
370 | }; |
391 | }; |
371 | |
392 | |
372 | static void |
393 | static void |
373 | x_once_cb (int revents, void *arg) |
394 | ev_x_once_cb (int revents, void *arg) |
374 | { |
395 | { |
375 | struct x_once *once = (struct x_once *)arg; |
396 | struct ev_x_once *once = (struct ev_x_once *)arg; |
376 | |
397 | |
377 | once->cb (once->fd, (short)revents, once->arg); |
398 | once->cb (once->fd, (short)revents, once->arg); |
378 | free (once); |
399 | free (once); |
379 | } |
400 | } |
380 | |
401 | |
381 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
402 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
382 | { |
403 | { |
383 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
404 | struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once)); |
384 | dLOOPbase; |
405 | dLOOPbase; |
385 | |
406 | |
386 | if (!once) |
407 | if (!once) |
387 | return -1; |
408 | return -1; |
388 | |
409 | |
389 | once->fd = fd; |
410 | once->fd = fd; |
390 | once->cb = cb; |
411 | once->cb = cb; |
391 | once->arg = arg; |
412 | once->arg = arg; |
392 | |
413 | |
393 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
414 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once); |
394 | |
415 | |
395 | return 0; |
416 | return 0; |
396 | } |
417 | } |
397 | |
418 | |
398 | int event_base_priority_init (struct event_base *base, int npri) |
419 | int event_base_priority_init (struct event_base *base, int npri) |