… | |
… | |
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 | #include "ev.h" |
37 | #include "event.h" |
38 | #include "event.h" |
38 | |
39 | |
39 | #if EV_MULTIPLICITY |
40 | #if EV_MULTIPLICITY |
40 | # 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 |
41 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
42 | # define dLOOPbase struct ev_loop *loop = (struct ev_loop *)base |
… | |
… | |
84 | { |
85 | { |
85 | #if EV_MULTIPLICITY |
86 | #if EV_MULTIPLICITY |
86 | if (x_cur) |
87 | if (x_cur) |
87 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
88 | x_cur = (struct event_base *)ev_loop_new (EVMETHOD_AUTO); |
88 | else |
89 | else |
89 | x_cur = ev_default_loop (EVMETHOD_AUTO); |
90 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
90 | #else |
91 | #else |
91 | 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)); |
92 | |
93 | |
93 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
94 | x_cur = (struct event_base *)ev_default_loop (EVMETHOD_AUTO); |
94 | #endif |
95 | #endif |
… | |
… | |
99 | void event_base_free (struct event_base *base) |
100 | void event_base_free (struct event_base *base) |
100 | { |
101 | { |
101 | dLOOPbase; |
102 | dLOOPbase; |
102 | |
103 | |
103 | #if EV_MULTIPLICITY |
104 | #if EV_MULTIPLICITY |
|
|
105 | if (ev_default_loop (EVMETHOD_AUTO) != loop) |
104 | ev_loop_delete (loop); |
106 | ev_loop_destroy (loop); |
105 | #endif |
107 | #endif |
106 | } |
108 | } |
107 | |
109 | |
108 | int event_dispatch (void) |
110 | int event_dispatch (void) |
109 | { |
111 | { |
… | |
… | |
154 | } |
156 | } |
155 | |
157 | |
156 | static void |
158 | static void |
157 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
159 | x_cb_to (EV_P_ struct ev_timer *w, int revents) |
158 | { |
160 | { |
159 | 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); |
160 | } |
166 | } |
161 | |
167 | |
162 | 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) |
163 | { |
169 | { |
164 | if (!ev->initialised) |
|
|
165 | { |
|
|
166 | ev->initialised = 1; |
|
|
167 | |
|
|
168 | if (events & EV_SIGNAL) |
170 | if (events & EV_SIGNAL) |
169 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
171 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
170 | else |
172 | else |
171 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
173 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
172 | |
174 | |
173 | ev_watcher_init (&ev->to, x_cb_to); |
175 | ev_watcher_init (&ev->to, x_cb_to); |
174 | } |
|
|
175 | |
176 | |
176 | 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 */ |
177 | ev->ev_fd = fd; |
178 | ev->ev_fd = fd; |
178 | ev->ev_events = events; |
179 | ev->ev_events = events; |
179 | ev->ev_pri = 0; |
180 | ev->ev_pri = 0; |
… | |
… | |
222 | { |
223 | { |
223 | /* sig */ |
224 | /* sig */ |
224 | if (ev_is_active (&ev->iosig.sig)) |
225 | if (ev_is_active (&ev->iosig.sig)) |
225 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
226 | ev_signal_stop (EV_A_ &ev->iosig.sig); |
226 | } |
227 | } |
227 | else |
228 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
228 | { |
229 | { |
229 | /* io */ |
230 | /* io */ |
230 | if (ev_is_active (&ev->iosig.io)) |
231 | if (ev_is_active (&ev->iosig.io)) |
231 | ev_io_stop (EV_A_ &ev->iosig.io); |
232 | ev_io_stop (EV_A_ &ev->iosig.io); |
232 | } |
233 | } |
… | |
… | |
244 | short revents = 0; |
245 | short revents = 0; |
245 | |
246 | |
246 | if (ev->ev_events & EV_SIGNAL) |
247 | if (ev->ev_events & EV_SIGNAL) |
247 | { |
248 | { |
248 | /* sig */ |
249 | /* sig */ |
249 | if (ev->iosig.sig.pending) |
250 | if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) |
250 | revents |= EV_SIGNAL; |
251 | revents |= EV_SIGNAL; |
251 | } |
252 | } |
252 | else |
253 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
253 | { |
254 | { |
254 | /* io */ |
255 | /* io */ |
255 | if (ev->iosig.io.pending) |
256 | if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) |
256 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
257 | } |
258 | } |
258 | |
259 | |
259 | if (ev->to.pending) |
260 | if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) |
260 | { |
261 | { |
261 | revents |= EV_TIMEOUT; |
262 | revents |= EV_TIMEOUT; |
262 | |
263 | |
263 | if (tv) |
264 | if (tv) |
264 | 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 :) */ |
… | |
… | |
351 | return 0; |
352 | return 0; |
352 | } |
353 | } |
353 | |
354 | |
354 | int event_base_priority_init (struct event_base *base, int npri) |
355 | int event_base_priority_init (struct event_base *base, int npri) |
355 | { |
356 | { |
356 | dLOOPbase; |
357 | /*dLOOPbase;*/ |
357 | |
358 | |
358 | return 0; |
359 | return 0; |
359 | } |
360 | } |
360 | |
361 | |