… | |
… | |
88 | int event_dispatch (void) |
88 | int event_dispatch (void) |
89 | { |
89 | { |
90 | return event_base_dispatch (x_cur); |
90 | return event_base_dispatch (x_cur); |
91 | } |
91 | } |
92 | |
92 | |
|
|
93 | #ifdef EV_STANDALONE |
93 | void event_set_log_callback (event_log_cb cb) |
94 | void event_set_log_callback (event_log_cb cb) |
94 | { |
95 | { |
95 | /* nop */ |
96 | /* nop */ |
96 | } |
97 | } |
|
|
98 | #endif |
97 | |
99 | |
98 | int event_loop (int flags) |
100 | int event_loop (int flags) |
99 | { |
101 | { |
100 | return event_base_loop (x_cur, flags); |
102 | return event_base_loop (x_cur, flags); |
101 | } |
103 | } |
102 | |
104 | |
103 | int event_loopexit (struct timeval *tv) |
105 | int event_loopexit (struct timeval *tv) |
104 | { |
106 | { |
105 | event_base_loopexit (x_cur, tv); |
107 | return event_base_loopexit (x_cur, tv); |
106 | } |
108 | } |
107 | |
109 | |
108 | static void |
110 | static void |
109 | x_cb (struct event *ev, int revents) |
111 | x_cb (struct event *ev, int revents) |
110 | { |
112 | { |
|
|
113 | if (ev->ev_events & EV_SIGNAL) |
|
|
114 | { |
|
|
115 | /* sig */ |
111 | if (ev_is_active (&ev->sig)) |
116 | if (ev_is_active (&ev->iosig.sig)) |
112 | { |
117 | { |
113 | ev_signal_stop (&ev->sig); |
118 | ev_signal_stop (&ev->iosig.sig); |
114 | --x_actives; |
119 | --x_actives; |
|
|
120 | } |
|
|
121 | } |
|
|
122 | else |
115 | } |
123 | { |
116 | |
124 | /* io */ |
117 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->io)) |
125 | if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->iosig.io)) |
118 | { |
126 | { |
119 | ev_io_stop (&ev->io); |
127 | ev_io_stop (&ev->iosig.io); |
120 | --x_actives; |
128 | --x_actives; |
|
|
129 | } |
121 | } |
130 | } |
122 | |
131 | |
123 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
132 | revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL; |
124 | |
133 | |
125 | if (revents & EV_TIMEOUT) |
134 | if (revents & EV_TIMEOUT) |
… | |
… | |
130 | } |
139 | } |
131 | |
140 | |
132 | static void |
141 | static void |
133 | x_cb_io (struct ev_io *w, int revents) |
142 | x_cb_io (struct ev_io *w, int revents) |
134 | { |
143 | { |
135 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, io)), revents); |
144 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.io)), revents); |
136 | } |
145 | } |
137 | |
146 | |
138 | static void |
147 | static void |
139 | x_cb_to (struct ev_timer *w, int revents) |
148 | x_cb_to (struct ev_timer *w, int revents) |
140 | { |
149 | { |
… | |
… | |
142 | } |
151 | } |
143 | |
152 | |
144 | static void |
153 | static void |
145 | x_cb_sig (struct ev_signal *w, int revents) |
154 | x_cb_sig (struct ev_signal *w, int revents) |
146 | { |
155 | { |
147 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, sig)), revents); |
156 | x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents); |
148 | } |
157 | } |
149 | |
158 | |
150 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
159 | void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg) |
151 | { |
160 | { |
|
|
161 | if (events & EV_SIGNAL) |
|
|
162 | ev_watcher_init (&ev->iosig.sig, x_cb_sig); |
|
|
163 | else |
152 | ev_watcher_init (&ev->io, x_cb_io); |
164 | ev_watcher_init (&ev->iosig.io, x_cb_io); |
|
|
165 | |
153 | ev_watcher_init (&ev->to, x_cb_to); |
166 | ev_watcher_init (&ev->to, x_cb_to); |
154 | ev_watcher_init (&ev->sig, x_cb_sig); |
|
|
155 | |
167 | |
156 | ev->ev_base = x_cur; |
168 | ev->ev_base = x_cur; |
157 | ev->ev_fd = fd; |
169 | ev->ev_fd = fd; |
158 | ev->ev_events = events; |
170 | ev->ev_events = events; |
159 | ev->ev_pri = 0; |
171 | ev->ev_pri = 0; |
… | |
… | |
162 | ev->ev_res = 0; |
174 | ev->ev_res = 0; |
163 | } |
175 | } |
164 | |
176 | |
165 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
177 | int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv) |
166 | { |
178 | { |
167 | event_base_once (x_cur, fd, events, cb, arg, tv); |
179 | return event_base_once (x_cur, fd, events, cb, arg, tv); |
168 | } |
180 | } |
169 | |
181 | |
170 | int event_add (struct event *ev, struct timeval *tv) |
182 | int event_add (struct event *ev, struct timeval *tv) |
171 | { |
183 | { |
|
|
184 | /* disable all watchers */ |
|
|
185 | event_del (ev); |
|
|
186 | |
|
|
187 | if (ev->ev_events & EV_SIGNAL) |
|
|
188 | { |
|
|
189 | ev_signal_set (&ev->iosig.sig, ev->ev_fd); |
|
|
190 | ev_signal_start (&ev->iosig.sig); |
|
|
191 | ++x_actives; |
|
|
192 | } |
|
|
193 | else if (ev->ev_events & (EV_READ | EV_WRITE)) |
|
|
194 | { |
|
|
195 | ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
|
|
196 | ev_io_start (&ev->iosig.io); |
|
|
197 | ++x_actives; |
|
|
198 | } |
|
|
199 | |
172 | if (tv) |
200 | if (tv) |
173 | { |
201 | { |
174 | if (ev_is_active (&ev->to)) |
|
|
175 | { |
|
|
176 | ev_timer_stop (&ev->to); |
|
|
177 | --x_actives; |
|
|
178 | } |
|
|
179 | |
|
|
180 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
202 | ev_timer_set (&ev->to, tv_get (tv), 0.); |
181 | ev_timer_start (&ev->to); |
203 | ev_timer_start (&ev->to); |
182 | ++x_actives; |
204 | ++x_actives; |
183 | } |
205 | } |
184 | |
206 | |
185 | if (ev->ev_events & (EV_READ | EV_WRITE)) |
207 | return 0; |
|
|
208 | } |
|
|
209 | |
|
|
210 | int event_del (struct event *ev) |
|
|
211 | { |
|
|
212 | if (ev->ev_events & EV_SIGNAL) |
186 | { |
213 | { |
|
|
214 | /* sig */ |
187 | if (ev_is_active (&ev->io)) |
215 | if (ev_is_active (&ev->iosig.sig)) |
188 | { |
216 | { |
189 | ev_io_stop (&ev->io); |
217 | ev_signal_stop (&ev->iosig.sig); |
190 | --x_actives; |
218 | --x_actives; |
191 | } |
219 | } |
192 | |
|
|
193 | ev_io_set (&ev->io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE)); |
|
|
194 | ev_io_start (&ev->io); |
|
|
195 | ++x_actives; |
|
|
196 | } |
|
|
197 | |
|
|
198 | if (ev->ev_events & EV_SIGNAL) |
|
|
199 | { |
220 | } |
|
|
221 | else |
|
|
222 | { |
|
|
223 | /* io */ |
200 | if (ev_is_active (&ev->sig)) |
224 | if (ev_is_active (&ev->iosig.io)) |
201 | { |
225 | { |
202 | ev_signal_stop (&ev->sig); |
226 | ev_io_stop (&ev->iosig.io); |
203 | --x_actives; |
227 | --x_actives; |
204 | } |
228 | } |
205 | |
|
|
206 | ev_signal_set (&ev->sig, ev->ev_fd); |
|
|
207 | ev_signal_start (&ev->sig); |
|
|
208 | ++x_actives; |
|
|
209 | } |
|
|
210 | |
|
|
211 | return 0; |
|
|
212 | } |
|
|
213 | |
|
|
214 | int event_del (struct event *ev) |
|
|
215 | { |
|
|
216 | if (ev_is_active (&ev->io)) |
|
|
217 | { |
|
|
218 | ev_io_stop (&ev->io); |
|
|
219 | --x_actives; |
|
|
220 | } |
229 | } |
221 | |
230 | |
222 | if (ev_is_active (&ev->to)) |
231 | if (ev_is_active (&ev->to)) |
223 | { |
232 | { |
224 | ev_timer_stop (&ev->to); |
233 | ev_timer_stop (&ev->to); |
225 | --x_actives; |
234 | --x_actives; |
226 | } |
235 | } |
227 | |
236 | |
228 | if (ev_is_active (&ev->sig)) |
|
|
229 | { |
|
|
230 | ev_signal_stop (&ev->sig); |
|
|
231 | --x_actives; |
|
|
232 | } |
|
|
233 | |
|
|
234 | return 0; |
237 | return 0; |
235 | } |
238 | } |
236 | |
239 | |
237 | int event_pending (struct event *ev, short events, struct timeval *tv) |
240 | int event_pending (struct event *ev, short events, struct timeval *tv) |
238 | { |
241 | { |
239 | short revents; |
242 | short revents = 0; |
240 | |
243 | |
|
|
244 | if (ev->ev_events & EV_SIGNAL) |
|
|
245 | { |
|
|
246 | /* sig */ |
|
|
247 | if (ev->iosig.sig.pending) |
|
|
248 | revents |= EV_SIGNAL; |
|
|
249 | } |
|
|
250 | else |
|
|
251 | { |
|
|
252 | /* io */ |
241 | if (ev->io.pending) |
253 | if (ev->iosig.io.pending) |
242 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
254 | revents |= ev->ev_events & (EV_READ | EV_WRITE); |
|
|
255 | } |
243 | |
256 | |
244 | if (ev->to.pending) |
257 | if (ev->to.pending) |
245 | { |
258 | { |
246 | revents |= EV_TIMEOUT; |
259 | revents |= EV_TIMEOUT; |
247 | |
260 | |
248 | if (tv) |
261 | if (tv) |
249 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
262 | tv_set (tv, ev_now); /* not sure if this is right :) */ |
250 | } |
263 | } |
251 | |
264 | |
252 | if (ev->sig.pending) |
|
|
253 | revents |= EV_SIGNAL; |
|
|
254 | |
|
|
255 | return events & revents; |
265 | return events & revents; |
256 | } |
266 | } |
257 | |
267 | |
258 | int event_priority_init (int npri) |
268 | int event_priority_init (int npri) |
259 | { |
269 | { |
… | |
… | |
299 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
309 | int event_base_loopexit (struct event_base *base, struct timeval *tv) |
300 | { |
310 | { |
301 | ev_tstamp after = tv_get (tv); |
311 | ev_tstamp after = tv_get (tv); |
302 | |
312 | |
303 | ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
313 | ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base); |
|
|
314 | |
|
|
315 | return -1; |
304 | } |
316 | } |
305 | |
317 | |
306 | struct x_once |
318 | struct x_once |
307 | { |
319 | { |
308 | int fd; |
320 | int fd; |