… | |
… | |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <sys/time.h> |
34 | #include <sys/time.h> |
35 | |
35 | |
36 | #include "event.h" |
36 | #include "event.h" |
37 | |
37 | |
|
|
38 | #if EV_MULTIPLICITY |
|
|
39 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
|
|
40 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
|
|
41 | #else |
38 | #define dLOOPev |
42 | # define dLOOPev |
39 | #define dLOOPbase |
43 | # define dLOOPbase |
|
|
44 | #endif |
40 | |
45 | |
|
|
46 | /* never accessed, will always be cast from/to ev_loop */ |
41 | struct event_base |
47 | struct event_base |
42 | { |
48 | { |
43 | int dummy; |
49 | int dummy; |
44 | }; |
50 | }; |
45 | |
51 | |
… | |
… | |
73 | return "libev"; |
79 | return "libev"; |
74 | } |
80 | } |
75 | |
81 | |
76 | void *event_init (void) |
82 | void *event_init (void) |
77 | { |
83 | { |
78 | if (!x_cur && ev_init (0)) |
84 | #if EV_MULTIPLICITY |
|
|
85 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
|
|
86 | #else |
79 | return x_cur = &x_base; |
87 | x_cur = &x_base; |
|
|
88 | #endif |
80 | |
89 | |
81 | return 0; |
90 | return x_cur; |
82 | } |
91 | } |
83 | |
92 | |
84 | void event_base_free (struct event_base *base) |
93 | void event_base_free (struct event_base *base) |
85 | { |
94 | { |
86 | /* nop */ |
95 | dLOOPbase; |
|
|
96 | |
|
|
97 | #if EV_MULTIPLICITY |
|
|
98 | ev_loop_delete (loop); |
|
|
99 | #endif |
87 | } |
100 | } |
88 | |
101 | |
89 | int event_dispatch (void) |
102 | int event_dispatch (void) |
90 | { |
103 | { |
91 | return event_base_dispatch (x_cur); |
104 | return event_base_dispatch (x_cur); |
… | |
… | |
127 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
140 | x_cb_io (EV_P_ struct ev_io *w, int revents) |
128 | { |
141 | { |
129 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
142 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io)); |
130 | |
143 | |
131 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
144 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w)) |
132 | ev_io_stop (w); |
145 | ev_io_stop (EV_A_ w); |
133 | |
146 | |
134 | x_cb (ev, revents); |
147 | x_cb (ev, revents); |
135 | } |
148 | } |
136 | |
149 | |
137 | static void |
150 | static void |
… | |
… | |
140 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
153 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
141 | } |
154 | } |
142 | |
155 | |
143 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
156 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
144 | { |
157 | { |
|
|
158 | if (!ev->initialised) |
|
|
159 | { |
|
|
160 | ev->initialised = 1; |
|
|
161 | |
145 | if (events & EV_SIGNAL) |
162 | if (events & EV_SIGNAL) |
146 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
163 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
147 | else |
164 | else |
148 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
165 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
149 | |
166 | |
150 | ev_watcher_init (&ev->to, x_cb_to); |
167 | ev_watcher_init (&ev->to, x_cb_to); |
|
|
168 | } |
151 | |
169 | |
152 | ev->ev_base = x_cur; |
170 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
153 | ev->ev_fd = fd; |
171 | ev->ev_fd = fd; |
154 | ev->ev_events = events; |
172 | ev->ev_events = events; |
155 | ev->ev_pri = 0; |
173 | ev->ev_pri = 0; |
156 | ev->ev_callback = cb; |
174 | ev->ev_callback = cb; |
157 | ev->ev_arg = arg; |
175 | ev->ev_arg = arg; |
… | |
… | |
263 | } |
281 | } |
264 | |
282 | |
265 | int event_base_loop (struct event_base *base, int flags) |
283 | int event_base_loop (struct event_base *base, int flags) |
266 | { |
284 | { |
267 | dLOOPbase; |
285 | dLOOPbase; |
268 | ev_loop (EV_A_ flags | EVLOOP_ONESHOT); |
286 | |
|
|
287 | ev_loop (EV_A_ flags); |
269 | |
288 | |
270 | return 0; |
289 | return 0; |
271 | } |
290 | } |
272 | |
291 | |
273 | int event_base_dispatch (struct event_base *base) |
292 | int event_base_dispatch (struct event_base *base) |
274 | { |
293 | { |
275 | return event_base_loop (base, 0); |
294 | return event_base_loop (base, 0); |
276 | } |
295 | } |
277 | |
296 | |
278 | static void |
297 | static void |
279 | x_loopexit_cb (EV_P_ int revents, void *arg) |
298 | x_loopexit_cb (int revents, void *base) |
280 | { |
299 | { |
|
|
300 | dLOOPbase; |
|
|
301 | |
281 | ev_unloop (EV_A_ 2); |
302 | ev_unloop (EV_A_ EVUNLOOP_ONCE); |
282 | } |
303 | } |
283 | |
304 | |
284 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
305 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
285 | { |
306 | { |
286 | dLOOPbase; |
307 | dLOOPbase; |