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 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 | * |
… | |
… | |
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 | |
88 | const char *event_get_version (void) |
81 | const char *event_get_version (void) |
89 | { |
82 | { |
|
|
83 | /* returns ABI, not API or library, version */ |
90 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
84 | return EVENT_VERSION (EV_VERSION_MAJOR, EV_VERSION_MINOR); |
91 | } |
85 | } |
92 | |
86 | |
93 | const char *event_get_method (void) |
87 | const char *event_get_method (void) |
94 | { |
88 | { |
… | |
… | |
96 | } |
90 | } |
97 | |
91 | |
98 | void *event_init (void) |
92 | void *event_init (void) |
99 | { |
93 | { |
100 | #if EV_MULTIPLICITY |
94 | #if EV_MULTIPLICITY |
101 | if (x_cur) |
95 | if (ev_x_cur) |
102 | x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
96 | ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO); |
103 | else |
97 | else |
104 | x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
98 | ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO); |
105 | #else |
99 | #else |
106 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
100 | assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur)); |
107 | |
101 | |
108 | x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
102 | ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO); |
109 | #endif |
103 | #endif |
110 | |
104 | |
111 | return x_cur; |
105 | return ev_x_cur; |
112 | } |
106 | } |
113 | |
107 | |
114 | void event_base_free (struct event_base *base) |
108 | void event_base_free (struct event_base *base) |
115 | { |
109 | { |
116 | dLOOPbase; |
110 | dLOOPbase; |
117 | |
111 | |
118 | #if EV_MULTIPLICITY |
112 | #if EV_MULTIPLICITY |
119 | if (ev_default_loop (EVFLAG_AUTO) != loop) |
113 | if (!ev_is_default_loop (loop)) |
120 | ev_loop_destroy (loop); |
114 | ev_loop_destroy (loop); |
121 | #endif |
115 | #endif |
122 | } |
116 | } |
123 | |
117 | |
124 | int event_dispatch (void) |
118 | int event_dispatch (void) |
125 | { |
119 | { |
126 | return event_base_dispatch (x_cur); |
120 | return event_base_dispatch (ev_x_cur); |
127 | } |
121 | } |
128 | |
122 | |
129 | #ifdef EV_STANDALONE |
123 | #ifdef EV_STANDALONE |
130 | void event_set_log_callback (event_log_cb cb) |
124 | void event_set_log_callback (event_log_cb cb) |
131 | { |
125 | { |
… | |
… | |
133 | } |
127 | } |
134 | #endif |
128 | #endif |
135 | |
129 | |
136 | int event_loop (int flags) |
130 | int event_loop (int flags) |
137 | { |
131 | { |
138 | return event_base_loop (x_cur, flags); |
132 | return event_base_loop (ev_x_cur, flags); |
139 | } |
133 | } |
140 | |
134 | |
141 | int event_loopexit (struct timeval *tv) |
135 | int event_loopexit (struct timeval *tv) |
142 | { |
136 | { |
143 | return event_base_loopexit (x_cur, tv); |
137 | return event_base_loopexit (ev_x_cur, tv); |
144 | } |
138 | } |
145 | |
139 | |
146 | static void |
140 | static void |
147 | x_cb (struct event *ev, int revents) |
141 | ev_x_cb (struct event *ev, int revents) |
148 | { |
142 | { |
149 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
143 | revents &= EV_READ | EV_WRITE | EV_TIMER | EV_SIGNAL; |
150 | |
144 | |
151 | ev->ev_res = revents; |
145 | ev->ev_res = revents; |
152 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
146 | ev->ev_callback (ev->ev_fd, (short)revents, ev->ev_arg); |
153 | } |
147 | } |
154 | |
148 | |
155 | static void |
149 | static void |
156 | x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
150 | ev_x_cb_sig (EV_P_ struct ev_signal *w, int revents) |
157 | { |
151 | { |
158 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
152 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.sig)); |
159 | |
153 | |
160 | if (revents & EV_ERROR) |
154 | if (revents & EV_ERROR) |
161 | event_del (ev); |
155 | event_del (ev); |
162 | |
156 | |
163 | x_cb (ev, revents); |
157 | ev_x_cb (ev, revents); |
164 | } |
158 | } |
165 | |
159 | |
166 | static void |
160 | static void |
167 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
161 | ev_x_cb_io (EV_P_ struct ev_io *w, int revents) |
168 | { |
162 | { |
169 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
163 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
170 | |
164 | |
171 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
165 | if ((revents & EV_ERROR) || !(ev->ev_events & EV_PERSIST)) |
172 | event_del (ev); |
166 | event_del (ev); |
173 | |
167 | |
174 | x_cb (ev, revents); |
168 | ev_x_cb (ev, revents); |
175 | } |
169 | } |
176 | |
170 | |
177 | static void |
171 | static void |
178 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
172 | ev_x_cb_to (EV_P_ struct ev_timer *w, int revents) |
179 | { |
173 | { |
180 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
174 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
181 | |
175 | |
182 | event_del (ev); |
176 | event_del (ev); |
183 | |
177 | |
184 | x_cb (ev, revents); |
178 | ev_x_cb (ev, revents); |
185 | } |
179 | } |
186 | |
180 | |
187 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
181 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
188 | { |
182 | { |
189 | if (events & EV_SIGNAL) |
183 | if (events & EV_SIGNAL) |
190 | ev_init (&ev->iosig.sig, x_cb_sig); |
184 | ev_init (&ev->iosig.sig, ev_x_cb_sig); |
191 | else |
185 | else |
192 | ev_init (&ev->iosig.io, x_cb_io); |
186 | ev_init (&ev->iosig.io, ev_x_cb_io); |
193 | |
187 | |
194 | ev_init (&ev->to, x_cb_to); |
188 | ev_init (&ev->to, ev_x_cb_to); |
195 | |
189 | |
196 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
190 | ev->ev_base = ev_x_cur; /* not threadsafe, but it's how libevent works */ |
197 | ev->ev_fd = fd; |
191 | ev->ev_fd = fd; |
198 | ev->ev_events = events; |
192 | ev->ev_events = events; |
199 | ev->ev_pri = 0; |
193 | ev->ev_pri = 0; |
200 | ev->ev_callback = cb; |
194 | ev->ev_callback = cb; |
201 | ev->ev_arg = arg; |
195 | ev->ev_arg = arg; |
… | |
… | |
203 | ev->ev_flags = EVLIST_INIT; |
197 | ev->ev_flags = EVLIST_INIT; |
204 | } |
198 | } |
205 | |
199 | |
206 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
200 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
207 | { |
201 | { |
208 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
202 | return event_base_once (ev_x_cur, fd, events, cb, arg, tv); |
209 | } |
203 | } |
210 | |
204 | |
211 | int event_add (struct event *ev, struct timeval *tv) |
205 | int event_add (struct event *ev, struct timeval *tv) |
212 | { |
206 | { |
213 | dLOOPev; |
207 | dLOOPev; |
… | |
… | |
233 | } |
227 | } |
234 | } |
228 | } |
235 | |
229 | |
236 | if (tv) |
230 | if (tv) |
237 | { |
231 | { |
238 | ev->to.repeat = tv_get (tv); |
232 | ev->to.repeat = ev_tv_get (tv); |
239 | ev_timer_again (EV_A_ &ev->to); |
233 | ev_timer_again (EV_A_ &ev->to); |
240 | ev->ev_flags |= EVLIST_TIMEOUT; |
234 | ev->ev_flags |= EVLIST_TIMEOUT; |
241 | } |
235 | } |
242 | else |
236 | else |
243 | { |
237 | { |
… | |
… | |
284 | int event_pending (struct event *ev, short events, struct timeval *tv) |
278 | int event_pending (struct event *ev, short events, struct timeval *tv) |
285 | { |
279 | { |
286 | short revents = 0; |
280 | short revents = 0; |
287 | dLOOPev; |
281 | dLOOPev; |
288 | |
282 | |
289 | |
|
|
290 | if (ev->ev_events & EV_SIGNAL) |
283 | if (ev->ev_events & EV_SIGNAL) |
291 | { |
284 | { |
292 | /* sig */ |
285 | /* sig */ |
293 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
286 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
294 | revents |= EV_SIGNAL; |
287 | revents |= EV_SIGNAL; |
… | |
… | |
303 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
296 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
304 | { |
297 | { |
305 | revents |= EV_TIMEOUT; |
298 | revents |= EV_TIMEOUT; |
306 | |
299 | |
307 | if (tv) |
300 | if (tv) |
308 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
301 | { |
|
|
302 | ev_tstamp at = ev_now (EV_A); |
|
|
303 | |
|
|
304 | tv->tv_sec = (long)at; |
|
|
305 | tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); |
|
|
306 | } |
309 | } |
307 | } |
310 | |
308 | |
311 | return events & revents; |
309 | return events & revents; |
312 | } |
310 | } |
313 | |
311 | |
314 | int event_priority_init (int npri) |
312 | int event_priority_init (int npri) |
315 | { |
313 | { |
316 | return event_base_priority_init (x_cur, npri); |
314 | return event_base_priority_init (ev_x_cur, npri); |
317 | } |
315 | } |
318 | |
316 | |
319 | int event_priority_set (struct event *ev, int pri) |
317 | int event_priority_set (struct event *ev, int pri) |
320 | { |
318 | { |
321 | ev->ev_pri = pri; |
319 | ev->ev_pri = pri; |
… | |
… | |
332 | |
330 | |
333 | int event_base_loop (struct event_base *base, int flags) |
331 | int event_base_loop (struct event_base *base, int flags) |
334 | { |
332 | { |
335 | dLOOPbase; |
333 | dLOOPbase; |
336 | |
334 | |
337 | ev_loop (EV_A_ flags); |
335 | ev_run (EV_A_ flags); |
338 | |
336 | |
339 | return 0; |
337 | return 0; |
340 | } |
338 | } |
341 | |
339 | |
342 | int event_base_dispatch (struct event_base *base) |
340 | int event_base_dispatch (struct event_base *base) |
343 | { |
341 | { |
344 | return event_base_loop (base, 0); |
342 | return event_base_loop (base, 0); |
345 | } |
343 | } |
346 | |
344 | |
347 | static void |
345 | static void |
348 | x_loopexit_cb (int revents, void *base) |
346 | ev_x_loopexit_cb (int revents, void *base) |
349 | { |
347 | { |
350 | dLOOPbase; |
348 | dLOOPbase; |
351 | |
349 | |
352 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
350 | ev_break (EV_A_ EVBREAK_ONE); |
353 | } |
351 | } |
354 | |
352 | |
355 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
353 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
356 | { |
354 | { |
357 | ev_tstamp after = tv_get (tv); |
355 | ev_tstamp after = ev_tv_get (tv); |
358 | dLOOPbase; |
356 | dLOOPbase; |
359 | |
357 | |
360 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
358 | ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., ev_x_loopexit_cb, (void *)base); |
361 | |
359 | |
362 | return 0; |
360 | return 0; |
363 | } |
361 | } |
364 | |
362 | |
365 | struct x_once |
363 | struct ev_x_once |
366 | { |
364 | { |
367 | int fd; |
365 | int fd; |
368 | void (*cb)(int, short, void *); |
366 | void (*cb)(int, short, void *); |
369 | void *arg; |
367 | void *arg; |
370 | }; |
368 | }; |
371 | |
369 | |
372 | static void |
370 | static void |
373 | x_once_cb (int revents, void *arg) |
371 | ev_x_once_cb (int revents, void *arg) |
374 | { |
372 | { |
375 | struct x_once *once = (struct x_once *)arg; |
373 | struct ev_x_once *once = (struct ev_x_once *)arg; |
376 | |
374 | |
377 | once->cb (once->fd, (short)revents, once->arg); |
375 | once->cb (once->fd, (short)revents, once->arg); |
378 | free (once); |
376 | free (once); |
379 | } |
377 | } |
380 | |
378 | |
381 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
379 | int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
382 | { |
380 | { |
383 | struct x_once *once = (struct x_once *)malloc (sizeof (struct x_once)); |
381 | struct ev_x_once *once = (struct ev_x_once *)malloc (sizeof (struct ev_x_once)); |
384 | dLOOPbase; |
382 | dLOOPbase; |
385 | |
383 | |
386 | if (!once) |
384 | if (!once) |
387 | return -1; |
385 | return -1; |
388 | |
386 | |
389 | once->fd = fd; |
387 | once->fd = fd; |
390 | once->cb = cb; |
388 | once->cb = cb; |
391 | once->arg = arg; |
389 | once->arg = arg; |
392 | |
390 | |
393 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once); |
391 | ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), ev_tv_get (tv), ev_x_once_cb, (void *)once); |
394 | |
392 | |
395 | return 0; |
393 | return 0; |
396 | } |
394 | } |
397 | |
395 | |
398 | int event_base_priority_init (struct event_base *base, int npri) |
396 | int event_base_priority_init (struct event_base *base, int npri) |