… | |
… | |
32 | #include <stddef.h> |
32 | #include <stddef.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <sys/time.h> |
34 | #include <sys/time.h> |
35 | #include <assert.h> |
35 | #include <assert.h> |
36 | |
36 | |
37 | #ifndef EV_EMBED |
37 | #include "ev.h" |
38 | # include "event.h" |
38 | #include "event.h" |
39 | #endif |
|
|
40 | |
39 | |
41 | #if EV_MULTIPLICITY |
40 | #if EV_MULTIPLICITY |
42 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
41 | # define dLOOPev struct ev_loop *loop = (struct ev_loop *)ev->ev_base |
43 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
42 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
44 | #else |
43 | #else |
… | |
… | |
86 | { |
85 | { |
87 | #if EV_MULTIPLICITY |
86 | #if EV_MULTIPLICITY |
88 | if (x_cur) |
87 | if (x_cur) |
89 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
88 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
90 | else |
89 | else |
91 | x_cur = ev_default_loop (EVMETHOD_AUTO); |
90 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
92 | #else |
91 | #else |
93 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
92 | assert (("multiple event bases not supported when not compiled with EV_MULTIPLICITY", !x_cur)); |
94 | |
93 | |
95 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
94 | x_cur = (struct event_base *)(long)ev_default_loop (EVMETHOD_AUTO); |
96 | #endif |
95 | #endif |
97 | |
96 | |
98 | return x_cur; |
97 | return x_cur; |
99 | } |
98 | } |
100 | |
99 | |
101 | void event_base_free (struct event_base *base) |
100 | void event_base_free (struct event_base *base) |
102 | { |
101 | { |
103 | dLOOPbase; |
102 | dLOOPbase; |
104 | |
103 | |
105 | #if EV_MULTIPLICITY |
104 | #if EV_MULTIPLICITY |
|
|
105 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
106 | ev_loop_delete (loop); |
106 | ev_loop_destroy (loop); |
107 | #endif |
107 | #endif |
108 | } |
108 | } |
109 | |
109 | |
110 | int event_dispatch (void) |
110 | int event_dispatch (void) |
111 | { |
111 | { |
112 | return event_base_dispatch (x_cur); |
112 | return event_base_dispatch (x_cur); |
113 | } |
113 | } |
114 | |
114 | |
115 | #ifdef EV_EMBED |
115 | #ifdef EV_STANDALONE |
116 | void event_set_log_callback (event_log_cb cb) |
116 | void event_set_log_callback (event_log_cb cb) |
117 | { |
117 | { |
118 | /* nop */ |
118 | /* nop */ |
119 | } |
119 | } |
120 | #endif |
120 | #endif |
… | |
… | |
156 | } |
156 | } |
157 | |
157 | |
158 | static void |
158 | static void |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
160 | { |
160 | { |
161 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents); |
161 | struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, to)); |
|
|
162 | |
|
|
163 | event_del (ev); |
|
|
164 | |
|
|
165 | x_cb (ev, revents); |
162 | } |
166 | } |
163 | |
167 | |
164 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
168 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
165 | { |
169 | { |
166 | if (!ev->initialised) |
|
|
167 | { |
|
|
168 | ev->initialised = 1; |
|
|
169 | |
|
|
170 | if (events & EV_SIGNAL) |
170 | if (events & EV_SIGNAL) |
171 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
171 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
172 | else |
172 | else |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
174 | |
174 | |
175 | ev_watcher_init (&ev->to, x_cb_to); |
175 | ev_watcher_init (&ev->to, x_cb_to); |
176 | } |
|
|
177 | |
176 | |
178 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
177 | ev->ev_base = x_cur; /* not threadsafe, but its like libevent works */ |
179 | ev->ev_fd = fd; |
178 | ev->ev_fd = fd; |
180 | ev->ev_events = events; |
179 | ev->ev_events = events; |
181 | ev->ev_pri = 0; |
180 | ev->ev_pri = 0; |
… | |
… | |
224 | { |
223 | { |
225 | /* sig */ |
224 | /* sig */ |
226 | if (ev_is_active (&ev->iosig.sig)) |
225 | if (ev_is_active (&ev->iosig.sig)) |
227 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
226 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
228 | } |
227 | } |
229 | else |
228 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
230 | { |
229 | { |
231 | /* io */ |
230 | /* io */ |
232 | if (ev_is_active (&ev->iosig.io)) |
231 | if (ev_is_active (&ev->iosig.io)) |
233 | ev_io_stop (EV_A_ &ev->iosig.io); |
232 | ev_io_stop (EV_A_ &ev->iosig.io); |
234 | } |
233 | } |
… | |
… | |
246 | short revents = 0; |
245 | short revents = 0; |
247 | |
246 | |
248 | if (ev->ev_events & EV_SIGNAL) |
247 | if (ev->ev_events & EV_SIGNAL) |
249 | { |
248 | { |
250 | /* sig */ |
249 | /* sig */ |
251 | if (ev->iosig.sig.pending) |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
252 | revents |= EV_SIGNAL; |
251 | revents |= EV_SIGNAL; |
253 | } |
252 | } |
254 | else |
253 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
255 | { |
254 | { |
256 | /* io */ |
255 | /* io */ |
257 | if (ev->iosig.io.pending) |
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
258 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
259 | } |
258 | } |
260 | |
259 | |
261 | if (ev->to.pending) |
260 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
262 | { |
261 | { |
263 | revents |= EV_TIMEOUT; |
262 | revents |= EV_TIMEOUT; |
264 | |
263 | |
265 | if (tv) |
264 | if (tv) |
266 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
265 | tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */ |
… | |
… | |
353 | return 0; |
352 | return 0; |
354 | } |
353 | } |
355 | |
354 | |
356 | int event_base_priority_init (struct event_base *base, int npri) |
355 | int event_base_priority_init (struct event_base *base, int npri) |
357 | { |
356 | { |
358 | dLOOPbase; |
357 | /*dLOOPbase;*/ |
359 | |
358 | |
360 | return 0; |
359 | return 0; |
361 | } |
360 | } |
362 | |
361 | |