… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
58 | |
58 | |
59 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #ifndef EV_USE_POLL |
63 | #ifndef EV_USEV_POLL |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
69 | #endif |
69 | #endif |
… | |
… | |
113 | |
113 | |
114 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
117 | |
117 | |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | ev_tstamp ev_now; |
|
|
120 | int ev_method; |
|
|
121 | |
|
|
122 | static int have_monotonic; /* runtime */ |
|
|
123 | |
|
|
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
125 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
126 | static void (*method_poll)(ev_tstamp timeout); |
|
|
127 | |
119 | |
128 | /*****************************************************************************/ |
120 | /*****************************************************************************/ |
129 | |
121 | |
130 | ev_tstamp |
122 | typedef struct |
|
|
123 | { |
|
|
124 | struct ev_watcher_list *head; |
|
|
125 | unsigned char events; |
|
|
126 | unsigned char reify; |
|
|
127 | } ANFD; |
|
|
128 | |
|
|
129 | typedef struct |
|
|
130 | { |
|
|
131 | W w; |
|
|
132 | int events; |
|
|
133 | } ANPENDING; |
|
|
134 | |
|
|
135 | #ifdef EV_MULTIPLICITY |
|
|
136 | |
|
|
137 | struct ev_loop |
|
|
138 | { |
|
|
139 | # define VAR(name,decl) decl; |
|
|
140 | # include "ev_vars.h" |
|
|
141 | }; |
|
|
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
|
|
144 | |
|
|
145 | #else |
|
|
146 | |
|
|
147 | # define VAR(name,decl) static decl; |
|
|
148 | # include "ev_vars.h" |
|
|
149 | # undef VAR |
|
|
150 | |
|
|
151 | #endif |
|
|
152 | |
|
|
153 | /*****************************************************************************/ |
|
|
154 | |
|
|
155 | inline ev_tstamp |
131 | ev_time (void) |
156 | ev_time (void) |
132 | { |
157 | { |
133 | #if EV_USE_REALTIME |
158 | #if EV_USE_REALTIME |
134 | struct timespec ts; |
159 | struct timespec ts; |
135 | clock_gettime (CLOCK_REALTIME, &ts); |
160 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
139 | gettimeofday (&tv, 0); |
164 | gettimeofday (&tv, 0); |
140 | return tv.tv_sec + tv.tv_usec * 1e-6; |
165 | return tv.tv_sec + tv.tv_usec * 1e-6; |
141 | #endif |
166 | #endif |
142 | } |
167 | } |
143 | |
168 | |
144 | static ev_tstamp |
169 | inline ev_tstamp |
145 | get_clock (void) |
170 | get_clock (void) |
146 | { |
171 | { |
147 | #if EV_USE_MONOTONIC |
172 | #if EV_USE_MONOTONIC |
148 | if (expect_true (have_monotonic)) |
173 | if (expect_true (have_monotonic)) |
149 | { |
174 | { |
… | |
… | |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
177 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
153 | } |
178 | } |
154 | #endif |
179 | #endif |
155 | |
180 | |
156 | return ev_time (); |
181 | return ev_time (); |
|
|
182 | } |
|
|
183 | |
|
|
184 | ev_tstamp |
|
|
185 | ev_now (EV_P) |
|
|
186 | { |
|
|
187 | return rt_now; |
157 | } |
188 | } |
158 | |
189 | |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
160 | |
191 | |
161 | #define array_needsize(base,cur,cnt,init) \ |
192 | #define array_needsize(base,cur,cnt,init) \ |
… | |
… | |
173 | cur = newcnt; \ |
204 | cur = newcnt; \ |
174 | } |
205 | } |
175 | |
206 | |
176 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
177 | |
208 | |
178 | typedef struct |
|
|
179 | { |
|
|
180 | struct ev_io *head; |
|
|
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
|
|
183 | } ANFD; |
|
|
184 | |
|
|
185 | static ANFD *anfds; |
|
|
186 | static int anfdmax; |
|
|
187 | |
|
|
188 | static void |
209 | static void |
189 | anfds_init (ANFD *base, int count) |
210 | anfds_init (ANFD *base, int count) |
190 | { |
211 | { |
191 | while (count--) |
212 | while (count--) |
192 | { |
213 | { |
… | |
… | |
196 | |
217 | |
197 | ++base; |
218 | ++base; |
198 | } |
219 | } |
199 | } |
220 | } |
200 | |
221 | |
201 | typedef struct |
|
|
202 | { |
|
|
203 | W w; |
|
|
204 | int events; |
|
|
205 | } ANPENDING; |
|
|
206 | |
|
|
207 | static ANPENDING *pendings [NUMPRI]; |
|
|
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
209 | |
|
|
210 | static void |
222 | static void |
211 | event (W w, int events) |
223 | event (EV_P_ W w, int events) |
212 | { |
224 | { |
213 | if (w->pending) |
225 | if (w->pending) |
214 | { |
226 | { |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
227 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
216 | return; |
228 | return; |
… | |
… | |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
233 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
234 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
223 | } |
235 | } |
224 | |
236 | |
225 | static void |
237 | static void |
226 | queue_events (W *events, int eventcnt, int type) |
238 | queue_events (EV_P_ W *events, int eventcnt, int type) |
227 | { |
239 | { |
228 | int i; |
240 | int i; |
229 | |
241 | |
230 | for (i = 0; i < eventcnt; ++i) |
242 | for (i = 0; i < eventcnt; ++i) |
231 | event (events [i], type); |
243 | event (EV_A_ events [i], type); |
232 | } |
244 | } |
233 | |
245 | |
234 | static void |
246 | static void |
235 | fd_event (int fd, int events) |
247 | fd_event (EV_P_ int fd, int events) |
236 | { |
248 | { |
237 | ANFD *anfd = anfds + fd; |
249 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
250 | struct ev_io *w; |
239 | |
251 | |
240 | for (w = anfd->head; w; w = w->next) |
252 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
253 | { |
242 | int ev = w->events & events; |
254 | int ev = w->events & events; |
243 | |
255 | |
244 | if (ev) |
256 | if (ev) |
245 | event ((W)w, ev); |
257 | event (EV_A_ (W)w, ev); |
246 | } |
258 | } |
247 | } |
259 | } |
248 | |
260 | |
249 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
250 | |
262 | |
251 | static int *fdchanges; |
|
|
252 | static int fdchangemax, fdchangecnt; |
|
|
253 | |
|
|
254 | static void |
263 | static void |
255 | fd_reify (void) |
264 | fd_reify (EV_P) |
256 | { |
265 | { |
257 | int i; |
266 | int i; |
258 | |
267 | |
259 | for (i = 0; i < fdchangecnt; ++i) |
268 | for (i = 0; i < fdchangecnt; ++i) |
260 | { |
269 | { |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
271 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
272 | struct ev_io *w; |
264 | |
273 | |
265 | int events = 0; |
274 | int events = 0; |
266 | |
275 | |
267 | for (w = anfd->head; w; w = w->next) |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
277 | events |= w->events; |
269 | |
278 | |
270 | anfd->reify = 0; |
279 | anfd->reify = 0; |
271 | |
280 | |
272 | if (anfd->events != events) |
281 | if (anfd->events != events) |
273 | { |
282 | { |
274 | method_modify (fd, anfd->events, events); |
283 | method_modify (EV_A_ fd, anfd->events, events); |
275 | anfd->events = events; |
284 | anfd->events = events; |
276 | } |
285 | } |
277 | } |
286 | } |
278 | |
287 | |
279 | fdchangecnt = 0; |
288 | fdchangecnt = 0; |
280 | } |
289 | } |
281 | |
290 | |
282 | static void |
291 | static void |
283 | fd_change (int fd) |
292 | fd_change (EV_P_ int fd) |
284 | { |
293 | { |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
294 | if (anfds [fd].reify || fdchangecnt < 0) |
286 | return; |
295 | return; |
287 | |
296 | |
288 | anfds [fd].reify = 1; |
297 | anfds [fd].reify = 1; |
… | |
… | |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
300 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
292 | fdchanges [fdchangecnt - 1] = fd; |
301 | fdchanges [fdchangecnt - 1] = fd; |
293 | } |
302 | } |
294 | |
303 | |
295 | static void |
304 | static void |
296 | fd_kill (int fd) |
305 | fd_kill (EV_P_ int fd) |
297 | { |
306 | { |
298 | struct ev_io *w; |
307 | struct ev_io *w; |
299 | |
308 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
309 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
310 | { |
303 | ev_io_stop (w); |
311 | ev_io_stop (EV_A_ w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
312 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
313 | } |
306 | } |
314 | } |
307 | |
315 | |
308 | /* called on EBADF to verify fds */ |
316 | /* called on EBADF to verify fds */ |
309 | static void |
317 | static void |
310 | fd_ebadf (void) |
318 | fd_ebadf (EV_P) |
311 | { |
319 | { |
312 | int fd; |
320 | int fd; |
313 | |
321 | |
314 | for (fd = 0; fd < anfdmax; ++fd) |
322 | for (fd = 0; fd < anfdmax; ++fd) |
315 | if (anfds [fd].events) |
323 | if (anfds [fd].events) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
324 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
317 | fd_kill (fd); |
325 | fd_kill (EV_A_ fd); |
318 | } |
326 | } |
319 | |
327 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
329 | static void |
322 | fd_enomem (void) |
330 | fd_enomem (EV_P) |
323 | { |
331 | { |
324 | int fd = anfdmax; |
332 | int fd = anfdmax; |
325 | |
333 | |
326 | while (fd--) |
334 | while (fd--) |
327 | if (anfds [fd].events) |
335 | if (anfds [fd].events) |
328 | { |
336 | { |
329 | close (fd); |
337 | close (fd); |
330 | fd_kill (fd); |
338 | fd_kill (EV_A_ fd); |
331 | return; |
339 | return; |
332 | } |
340 | } |
333 | } |
341 | } |
334 | |
342 | |
335 | /*****************************************************************************/ |
343 | /*****************************************************************************/ |
336 | |
344 | |
337 | static struct ev_timer **timers; |
|
|
338 | static int timermax, timercnt; |
|
|
339 | |
|
|
340 | static struct ev_periodic **periodics; |
|
|
341 | static int periodicmax, periodiccnt; |
|
|
342 | |
|
|
343 | static void |
345 | static void |
344 | upheap (WT *timers, int k) |
346 | upheap (WT *heap, int k) |
345 | { |
347 | { |
346 | WT w = timers [k]; |
348 | WT w = heap [k]; |
347 | |
349 | |
348 | while (k && timers [k >> 1]->at > w->at) |
350 | while (k && heap [k >> 1]->at > w->at) |
349 | { |
351 | { |
350 | timers [k] = timers [k >> 1]; |
352 | heap [k] = heap [k >> 1]; |
351 | timers [k]->active = k + 1; |
353 | heap [k]->active = k + 1; |
352 | k >>= 1; |
354 | k >>= 1; |
353 | } |
355 | } |
354 | |
356 | |
355 | timers [k] = w; |
357 | heap [k] = w; |
356 | timers [k]->active = k + 1; |
358 | heap [k]->active = k + 1; |
357 | |
359 | |
358 | } |
360 | } |
359 | |
361 | |
360 | static void |
362 | static void |
361 | downheap (WT *timers, int N, int k) |
363 | downheap (WT *heap, int N, int k) |
362 | { |
364 | { |
363 | WT w = timers [k]; |
365 | WT w = heap [k]; |
364 | |
366 | |
365 | while (k < (N >> 1)) |
367 | while (k < (N >> 1)) |
366 | { |
368 | { |
367 | int j = k << 1; |
369 | int j = k << 1; |
368 | |
370 | |
369 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
371 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
370 | ++j; |
372 | ++j; |
371 | |
373 | |
372 | if (w->at <= timers [j]->at) |
374 | if (w->at <= heap [j]->at) |
373 | break; |
375 | break; |
374 | |
376 | |
375 | timers [k] = timers [j]; |
377 | heap [k] = heap [j]; |
376 | timers [k]->active = k + 1; |
378 | heap [k]->active = k + 1; |
377 | k = j; |
379 | k = j; |
378 | } |
380 | } |
379 | |
381 | |
380 | timers [k] = w; |
382 | heap [k] = w; |
381 | timers [k]->active = k + 1; |
383 | heap [k]->active = k + 1; |
382 | } |
384 | } |
383 | |
385 | |
384 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
385 | |
387 | |
386 | typedef struct |
388 | typedef struct |
387 | { |
389 | { |
388 | struct ev_signal *head; |
390 | struct ev_watcher_list *head; |
389 | sig_atomic_t volatile gotsig; |
391 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
392 | } ANSIG; |
391 | |
393 | |
392 | static ANSIG *signals; |
394 | static ANSIG *signals; |
393 | static int signalmax; |
395 | static int signalmax; |
394 | |
396 | |
395 | static int sigpipe [2]; |
397 | static int sigpipe [2]; |
396 | static sig_atomic_t volatile gotsig; |
398 | static sig_atomic_t volatile gotsig; |
397 | static struct ev_io sigev; |
|
|
398 | |
399 | |
399 | static void |
400 | static void |
400 | signals_init (ANSIG *base, int count) |
401 | signals_init (ANSIG *base, int count) |
401 | { |
402 | { |
402 | while (count--) |
403 | while (count--) |
… | |
… | |
413 | { |
414 | { |
414 | signals [signum - 1].gotsig = 1; |
415 | signals [signum - 1].gotsig = 1; |
415 | |
416 | |
416 | if (!gotsig) |
417 | if (!gotsig) |
417 | { |
418 | { |
|
|
419 | int old_errno = errno; |
418 | gotsig = 1; |
420 | gotsig = 1; |
419 | write (sigpipe [1], &signum, 1); |
421 | write (sigpipe [1], &signum, 1); |
|
|
422 | errno = old_errno; |
420 | } |
423 | } |
421 | } |
424 | } |
422 | |
425 | |
423 | static void |
426 | static void |
424 | sigcb (struct ev_io *iow, int revents) |
427 | sigcb (EV_P_ struct ev_io *iow, int revents) |
425 | { |
428 | { |
426 | struct ev_signal *w; |
429 | struct ev_watcher_list *w; |
427 | int signum; |
430 | int signum; |
428 | |
431 | |
429 | read (sigpipe [0], &revents, 1); |
432 | read (sigpipe [0], &revents, 1); |
430 | gotsig = 0; |
433 | gotsig = 0; |
431 | |
434 | |
… | |
… | |
433 | if (signals [signum].gotsig) |
436 | if (signals [signum].gotsig) |
434 | { |
437 | { |
435 | signals [signum].gotsig = 0; |
438 | signals [signum].gotsig = 0; |
436 | |
439 | |
437 | for (w = signals [signum].head; w; w = w->next) |
440 | for (w = signals [signum].head; w; w = w->next) |
438 | event ((W)w, EV_SIGNAL); |
441 | event (EV_A_ (W)w, EV_SIGNAL); |
439 | } |
442 | } |
440 | } |
443 | } |
441 | |
444 | |
442 | static void |
445 | static void |
443 | siginit (void) |
446 | siginit (EV_P) |
444 | { |
447 | { |
445 | #ifndef WIN32 |
448 | #ifndef WIN32 |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
450 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | |
451 | |
… | |
… | |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
454 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | #endif |
455 | #endif |
453 | |
456 | |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
457 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_start (&sigev); |
458 | ev_io_start (EV_A_ &sigev); |
|
|
459 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
456 | } |
460 | } |
457 | |
461 | |
458 | /*****************************************************************************/ |
462 | /*****************************************************************************/ |
459 | |
|
|
460 | static struct ev_idle **idles; |
|
|
461 | static int idlemax, idlecnt; |
|
|
462 | |
|
|
463 | static struct ev_prepare **prepares; |
|
|
464 | static int preparemax, preparecnt; |
|
|
465 | |
|
|
466 | static struct ev_check **checks; |
|
|
467 | static int checkmax, checkcnt; |
|
|
468 | |
|
|
469 | /*****************************************************************************/ |
|
|
470 | |
|
|
471 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
472 | static struct ev_signal childev; |
|
|
473 | |
463 | |
474 | #ifndef WIN32 |
464 | #ifndef WIN32 |
475 | |
465 | |
476 | #ifndef WCONTINUED |
466 | #ifndef WCONTINUED |
477 | # define WCONTINUED 0 |
467 | # define WCONTINUED 0 |
478 | #endif |
468 | #endif |
479 | |
469 | |
480 | static void |
470 | static void |
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
471 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
482 | { |
472 | { |
483 | struct ev_child *w; |
473 | struct ev_child *w; |
484 | |
474 | |
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
475 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
486 | if (w->pid == pid || !w->pid) |
476 | if (w->pid == pid || !w->pid) |
487 | { |
477 | { |
488 | w->priority = sw->priority; /* need to do it *now* */ |
478 | w->priority = sw->priority; /* need to do it *now* */ |
489 | w->rpid = pid; |
479 | w->rpid = pid; |
490 | w->rstatus = status; |
480 | w->rstatus = status; |
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
481 | event (EV_A_ (W)w, EV_CHILD); |
493 | } |
482 | } |
494 | } |
483 | } |
495 | |
484 | |
496 | static void |
485 | static void |
497 | childcb (struct ev_signal *sw, int revents) |
486 | childcb (EV_P_ struct ev_signal *sw, int revents) |
498 | { |
487 | { |
499 | int pid, status; |
488 | int pid, status; |
500 | |
489 | |
501 | printf ("chld %x\n", revents);//D |
|
|
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
490 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
503 | { |
491 | { |
504 | /* make sure we are called again until all childs have been reaped */ |
492 | /* make sure we are called again until all childs have been reaped */ |
505 | event ((W)sw, EV_SIGNAL); |
493 | event (EV_A_ (W)sw, EV_SIGNAL); |
506 | |
494 | |
507 | child_reap (sw, pid, pid, status); |
495 | child_reap (EV_A_ sw, pid, pid, status); |
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
496 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
509 | } |
497 | } |
510 | } |
498 | } |
511 | |
499 | |
512 | #endif |
500 | #endif |
513 | |
501 | |
… | |
… | |
517 | # include "ev_kqueue.c" |
505 | # include "ev_kqueue.c" |
518 | #endif |
506 | #endif |
519 | #if EV_USE_EPOLL |
507 | #if EV_USE_EPOLL |
520 | # include "ev_epoll.c" |
508 | # include "ev_epoll.c" |
521 | #endif |
509 | #endif |
522 | #if EV_USE_POLL |
510 | #if EV_USEV_POLL |
523 | # include "ev_poll.c" |
511 | # include "ev_poll.c" |
524 | #endif |
512 | #endif |
525 | #if EV_USE_SELECT |
513 | #if EV_USE_SELECT |
526 | # include "ev_select.c" |
514 | # include "ev_select.c" |
527 | #endif |
515 | #endif |
… | |
… | |
536 | ev_version_minor (void) |
524 | ev_version_minor (void) |
537 | { |
525 | { |
538 | return EV_VERSION_MINOR; |
526 | return EV_VERSION_MINOR; |
539 | } |
527 | } |
540 | |
528 | |
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
529 | /* return true if we are running with elevated privileges and should ignore env variables */ |
542 | static int |
530 | static int |
543 | enable_secure () |
531 | enable_secure (void) |
544 | { |
532 | { |
|
|
533 | #ifdef WIN32 |
|
|
534 | return 0; |
|
|
535 | #else |
545 | return getuid () != geteuid () |
536 | return getuid () != geteuid () |
546 | || getgid () != getegid (); |
537 | || getgid () != getegid (); |
|
|
538 | #endif |
547 | } |
539 | } |
548 | |
540 | |
549 | int ev_init (int methods) |
541 | int |
|
|
542 | ev_method (EV_P) |
550 | { |
543 | { |
|
|
544 | return method; |
|
|
545 | } |
|
|
546 | |
|
|
547 | static void |
|
|
548 | loop_init (EV_P_ int methods) |
|
|
549 | { |
551 | if (!ev_method) |
550 | if (!method) |
552 | { |
551 | { |
553 | #if EV_USE_MONOTONIC |
552 | #if EV_USE_MONOTONIC |
554 | { |
553 | { |
555 | struct timespec ts; |
554 | struct timespec ts; |
556 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
555 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
557 | have_monotonic = 1; |
556 | have_monotonic = 1; |
558 | } |
557 | } |
559 | #endif |
558 | #endif |
560 | |
559 | |
561 | ev_now = ev_time (); |
560 | rt_now = ev_time (); |
562 | now = get_clock (); |
561 | mn_now = get_clock (); |
563 | now_floor = now; |
562 | now_floor = mn_now; |
564 | diff = ev_now - now; |
563 | rtmn_diff = rt_now - mn_now; |
565 | |
564 | |
566 | if (pipe (sigpipe)) |
565 | if (pipe (sigpipe)) |
567 | return 0; |
566 | return 0; |
568 | |
567 | |
569 | if (methods == EVMETHOD_AUTO) |
568 | if (methods == EVMETHOD_AUTO) |
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
569 | if (!enable_secure () && getenv ("LIBmethodS")) |
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
570 | methods = atoi (getenv ("LIBmethodS")); |
572 | else |
571 | else |
573 | methods = EVMETHOD_ANY; |
572 | methods = EVMETHOD_ANY; |
574 | |
573 | |
575 | ev_method = 0; |
574 | method = 0; |
576 | #if EV_USE_KQUEUE |
575 | #if EV_USE_KQUEUE |
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
576 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
578 | #endif |
577 | #endif |
579 | #if EV_USE_EPOLL |
578 | #if EV_USE_EPOLL |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
579 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
581 | #endif |
580 | #endif |
582 | #if EV_USE_POLL |
581 | #if EV_USEV_POLL |
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
582 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
584 | #endif |
583 | #endif |
585 | #if EV_USE_SELECT |
584 | #if EV_USE_SELECT |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
585 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
587 | #endif |
586 | #endif |
588 | |
587 | |
589 | if (ev_method) |
588 | if (method) |
590 | { |
589 | { |
591 | ev_watcher_init (&sigev, sigcb); |
590 | ev_watcher_init (&sigev, sigcb); |
592 | ev_set_priority (&sigev, EV_MAXPRI); |
591 | ev_set_priority (&sigev, EV_MAXPRI); |
593 | siginit (); |
592 | siginit (EV_A); |
594 | |
593 | |
595 | #ifndef WIN32 |
594 | #ifndef WIN32 |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
595 | ev_signal_init (&childev, childcb, SIGCHLD); |
597 | ev_set_priority (&childev, EV_MAXPRI); |
596 | ev_set_priority (&childev, EV_MAXPRI); |
598 | ev_signal_start (&childev); |
597 | ev_signal_start (EV_A_ &childev); |
|
|
598 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
599 | #endif |
599 | #endif |
600 | } |
600 | } |
601 | } |
601 | } |
602 | |
602 | |
603 | return ev_method; |
603 | return method; |
604 | } |
604 | } |
|
|
605 | |
|
|
606 | #ifdef EV_MULTIPLICITY |
|
|
607 | |
|
|
608 | struct ev_loop * |
|
|
609 | ev_loop_new (int methods) |
|
|
610 | { |
|
|
611 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
612 | |
|
|
613 | loop_init (EV_A_ methods); |
|
|
614 | |
|
|
615 | return loop; |
|
|
616 | } |
|
|
617 | |
|
|
618 | void |
|
|
619 | ev_loop_delete (EV_P) |
|
|
620 | { |
|
|
621 | /*TODO*/ |
|
|
622 | free (loop); |
|
|
623 | } |
|
|
624 | |
|
|
625 | #else |
|
|
626 | |
|
|
627 | int |
|
|
628 | ev_init (int methods) |
|
|
629 | { |
|
|
630 | loop_init (); |
|
|
631 | } |
|
|
632 | |
|
|
633 | #endif |
605 | |
634 | |
606 | /*****************************************************************************/ |
635 | /*****************************************************************************/ |
607 | |
636 | |
608 | void |
637 | void |
609 | ev_fork_prepare (void) |
638 | ev_fork_prepare (void) |
… | |
… | |
618 | } |
647 | } |
619 | |
648 | |
620 | void |
649 | void |
621 | ev_fork_child (void) |
650 | ev_fork_child (void) |
622 | { |
651 | { |
|
|
652 | /*TODO*/ |
|
|
653 | #if !EV_MULTIPLICITY |
623 | #if EV_USE_EPOLL |
654 | #if EV_USE_EPOLL |
624 | if (ev_method == EVMETHOD_EPOLL) |
655 | if (method == EVMETHOD_EPOLL) |
625 | epoll_postfork_child (); |
656 | epoll_postfork_child (EV_A); |
626 | #endif |
657 | #endif |
627 | |
658 | |
628 | ev_io_stop (&sigev); |
659 | ev_io_stop (EV_A_ &sigev); |
629 | close (sigpipe [0]); |
660 | close (sigpipe [0]); |
630 | close (sigpipe [1]); |
661 | close (sigpipe [1]); |
631 | pipe (sigpipe); |
662 | pipe (sigpipe); |
632 | siginit (); |
663 | siginit (EV_A); |
|
|
664 | #endif |
633 | } |
665 | } |
634 | |
666 | |
635 | /*****************************************************************************/ |
667 | /*****************************************************************************/ |
636 | |
668 | |
637 | static void |
669 | static void |
638 | call_pending (void) |
670 | call_pending (EV_P) |
639 | { |
671 | { |
640 | int pri; |
672 | int pri; |
641 | |
673 | |
642 | for (pri = NUMPRI; pri--; ) |
674 | for (pri = NUMPRI; pri--; ) |
643 | while (pendingcnt [pri]) |
675 | while (pendingcnt [pri]) |
… | |
… | |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
677 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
646 | |
678 | |
647 | if (p->w) |
679 | if (p->w) |
648 | { |
680 | { |
649 | p->w->pending = 0; |
681 | p->w->pending = 0; |
650 | p->w->cb (p->w, p->events); |
682 | p->w->cb (EV_A_ p->w, p->events); |
651 | } |
683 | } |
652 | } |
684 | } |
653 | } |
685 | } |
654 | |
686 | |
655 | static void |
687 | static void |
656 | timers_reify (void) |
688 | timers_reify (EV_P) |
657 | { |
689 | { |
658 | while (timercnt && timers [0]->at <= now) |
690 | while (timercnt && timers [0]->at <= mn_now) |
659 | { |
691 | { |
660 | struct ev_timer *w = timers [0]; |
692 | struct ev_timer *w = timers [0]; |
661 | |
693 | |
662 | /* first reschedule or stop timer */ |
694 | /* first reschedule or stop timer */ |
663 | if (w->repeat) |
695 | if (w->repeat) |
664 | { |
696 | { |
665 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
697 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
666 | w->at = now + w->repeat; |
698 | w->at = mn_now + w->repeat; |
667 | downheap ((WT *)timers, timercnt, 0); |
699 | downheap ((WT *)timers, timercnt, 0); |
668 | } |
700 | } |
669 | else |
701 | else |
670 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
702 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
671 | |
703 | |
672 | event ((W)w, EV_TIMEOUT); |
704 | event (EV_A_ (W)w, EV_TIMEOUT); |
673 | } |
705 | } |
674 | } |
706 | } |
675 | |
707 | |
676 | static void |
708 | static void |
677 | periodics_reify (void) |
709 | periodics_reify (EV_P) |
678 | { |
710 | { |
679 | while (periodiccnt && periodics [0]->at <= ev_now) |
711 | while (periodiccnt && periodics [0]->at <= rt_now) |
680 | { |
712 | { |
681 | struct ev_periodic *w = periodics [0]; |
713 | struct ev_periodic *w = periodics [0]; |
682 | |
714 | |
683 | /* first reschedule or stop timer */ |
715 | /* first reschedule or stop timer */ |
684 | if (w->interval) |
716 | if (w->interval) |
685 | { |
717 | { |
686 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
718 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
687 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
719 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
688 | downheap ((WT *)periodics, periodiccnt, 0); |
720 | downheap ((WT *)periodics, periodiccnt, 0); |
689 | } |
721 | } |
690 | else |
722 | else |
691 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
723 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
692 | |
724 | |
693 | event ((W)w, EV_PERIODIC); |
725 | event (EV_A_ (W)w, EV_PERIODIC); |
694 | } |
726 | } |
695 | } |
727 | } |
696 | |
728 | |
697 | static void |
729 | static void |
698 | periodics_reschedule (ev_tstamp diff) |
730 | periodics_reschedule (EV_P) |
699 | { |
731 | { |
700 | int i; |
732 | int i; |
701 | |
733 | |
702 | /* adjust periodics after time jump */ |
734 | /* adjust periodics after time jump */ |
703 | for (i = 0; i < periodiccnt; ++i) |
735 | for (i = 0; i < periodiccnt; ++i) |
704 | { |
736 | { |
705 | struct ev_periodic *w = periodics [i]; |
737 | struct ev_periodic *w = periodics [i]; |
706 | |
738 | |
707 | if (w->interval) |
739 | if (w->interval) |
708 | { |
740 | { |
709 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
741 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
710 | |
742 | |
711 | if (fabs (diff) >= 1e-4) |
743 | if (fabs (diff) >= 1e-4) |
712 | { |
744 | { |
713 | ev_periodic_stop (w); |
745 | ev_periodic_stop (EV_A_ w); |
714 | ev_periodic_start (w); |
746 | ev_periodic_start (EV_A_ w); |
715 | |
747 | |
716 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
748 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
717 | } |
749 | } |
718 | } |
750 | } |
719 | } |
751 | } |
720 | } |
752 | } |
721 | |
753 | |
722 | static int |
754 | inline int |
723 | time_update_monotonic (void) |
755 | time_update_monotonic (EV_P) |
724 | { |
756 | { |
725 | now = get_clock (); |
757 | mn_now = get_clock (); |
726 | |
758 | |
727 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
759 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
728 | { |
760 | { |
729 | ev_now = now + diff; |
761 | rt_now = rtmn_diff + mn_now; |
730 | return 0; |
762 | return 0; |
731 | } |
763 | } |
732 | else |
764 | else |
733 | { |
765 | { |
734 | now_floor = now; |
766 | now_floor = mn_now; |
735 | ev_now = ev_time (); |
767 | rt_now = ev_time (); |
736 | return 1; |
768 | return 1; |
737 | } |
769 | } |
738 | } |
770 | } |
739 | |
771 | |
740 | static void |
772 | static void |
741 | time_update (void) |
773 | time_update (EV_P) |
742 | { |
774 | { |
743 | int i; |
775 | int i; |
744 | |
776 | |
745 | #if EV_USE_MONOTONIC |
777 | #if EV_USE_MONOTONIC |
746 | if (expect_true (have_monotonic)) |
778 | if (expect_true (have_monotonic)) |
747 | { |
779 | { |
748 | if (time_update_monotonic ()) |
780 | if (time_update_monotonic (EV_A)) |
749 | { |
781 | { |
750 | ev_tstamp odiff = diff; |
782 | ev_tstamp odiff = rtmn_diff; |
751 | |
783 | |
752 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
784 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
753 | { |
785 | { |
754 | diff = ev_now - now; |
786 | rtmn_diff = rt_now - mn_now; |
755 | |
787 | |
756 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
788 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
757 | return; /* all is well */ |
789 | return; /* all is well */ |
758 | |
790 | |
759 | ev_now = ev_time (); |
791 | rt_now = ev_time (); |
760 | now = get_clock (); |
792 | mn_now = get_clock (); |
761 | now_floor = now; |
793 | now_floor = mn_now; |
762 | } |
794 | } |
763 | |
795 | |
764 | periodics_reschedule (diff - odiff); |
796 | periodics_reschedule (EV_A); |
765 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
797 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
798 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
766 | } |
799 | } |
767 | } |
800 | } |
768 | else |
801 | else |
769 | #endif |
802 | #endif |
770 | { |
803 | { |
771 | ev_now = ev_time (); |
804 | rt_now = ev_time (); |
772 | |
805 | |
773 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
806 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
774 | { |
807 | { |
775 | periodics_reschedule (ev_now - now); |
808 | periodics_reschedule (EV_A); |
776 | |
809 | |
777 | /* adjust timers. this is easy, as the offset is the same for all */ |
810 | /* adjust timers. this is easy, as the offset is the same for all */ |
778 | for (i = 0; i < timercnt; ++i) |
811 | for (i = 0; i < timercnt; ++i) |
779 | timers [i]->at += diff; |
812 | timers [i]->at += rt_now - mn_now; |
780 | } |
813 | } |
781 | |
814 | |
782 | now = ev_now; |
815 | mn_now = rt_now; |
783 | } |
816 | } |
784 | } |
817 | } |
785 | |
818 | |
786 | int ev_loop_done; |
819 | void |
|
|
820 | ev_ref (EV_P) |
|
|
821 | { |
|
|
822 | ++activecnt; |
|
|
823 | } |
787 | |
824 | |
|
|
825 | void |
|
|
826 | ev_unref (EV_P) |
|
|
827 | { |
|
|
828 | --activecnt; |
|
|
829 | } |
|
|
830 | |
|
|
831 | static int loop_done; |
|
|
832 | |
|
|
833 | void |
788 | void ev_loop (int flags) |
834 | ev_loop (EV_P_ int flags) |
789 | { |
835 | { |
790 | double block; |
836 | double block; |
791 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
837 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
792 | |
838 | |
793 | do |
839 | do |
794 | { |
840 | { |
795 | /* queue check watchers (and execute them) */ |
841 | /* queue check watchers (and execute them) */ |
796 | if (expect_false (preparecnt)) |
842 | if (expect_false (preparecnt)) |
797 | { |
843 | { |
798 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
844 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
799 | call_pending (); |
845 | call_pending (EV_A); |
800 | } |
846 | } |
801 | |
847 | |
802 | /* update fd-related kernel structures */ |
848 | /* update fd-related kernel structures */ |
803 | fd_reify (); |
849 | fd_reify (EV_A); |
804 | |
850 | |
805 | /* calculate blocking time */ |
851 | /* calculate blocking time */ |
806 | |
852 | |
807 | /* we only need this for !monotonic clockor timers, but as we basically |
853 | /* we only need this for !monotonic clockor timers, but as we basically |
808 | always have timers, we just calculate it always */ |
854 | always have timers, we just calculate it always */ |
809 | #if EV_USE_MONOTONIC |
855 | #if EV_USE_MONOTONIC |
810 | if (expect_true (have_monotonic)) |
856 | if (expect_true (have_monotonic)) |
811 | time_update_monotonic (); |
857 | time_update_monotonic (EV_A); |
812 | else |
858 | else |
813 | #endif |
859 | #endif |
814 | { |
860 | { |
815 | ev_now = ev_time (); |
861 | rt_now = ev_time (); |
816 | now = ev_now; |
862 | mn_now = rt_now; |
817 | } |
863 | } |
818 | |
864 | |
819 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
865 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
820 | block = 0.; |
866 | block = 0.; |
821 | else |
867 | else |
822 | { |
868 | { |
823 | block = MAX_BLOCKTIME; |
869 | block = MAX_BLOCKTIME; |
824 | |
870 | |
825 | if (timercnt) |
871 | if (timercnt) |
826 | { |
872 | { |
827 | ev_tstamp to = timers [0]->at - now + method_fudge; |
873 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
828 | if (block > to) block = to; |
874 | if (block > to) block = to; |
829 | } |
875 | } |
830 | |
876 | |
831 | if (periodiccnt) |
877 | if (periodiccnt) |
832 | { |
878 | { |
833 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
879 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
834 | if (block > to) block = to; |
880 | if (block > to) block = to; |
835 | } |
881 | } |
836 | |
882 | |
837 | if (block < 0.) block = 0.; |
883 | if (block < 0.) block = 0.; |
838 | } |
884 | } |
839 | |
885 | |
840 | method_poll (block); |
886 | method_poll (EV_A_ block); |
841 | |
887 | |
842 | /* update ev_now, do magic */ |
888 | /* update rt_now, do magic */ |
843 | time_update (); |
889 | time_update (EV_A); |
844 | |
890 | |
845 | /* queue pending timers and reschedule them */ |
891 | /* queue pending timers and reschedule them */ |
846 | timers_reify (); /* relative timers called last */ |
892 | timers_reify (EV_A); /* relative timers called last */ |
847 | periodics_reify (); /* absolute timers called first */ |
893 | periodics_reify (EV_A); /* absolute timers called first */ |
848 | |
894 | |
849 | /* queue idle watchers unless io or timers are pending */ |
895 | /* queue idle watchers unless io or timers are pending */ |
850 | if (!pendingcnt) |
896 | if (!pendingcnt) |
851 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
897 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
852 | |
898 | |
853 | /* queue check watchers, to be executed first */ |
899 | /* queue check watchers, to be executed first */ |
854 | if (checkcnt) |
900 | if (checkcnt) |
855 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
901 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
856 | |
902 | |
857 | call_pending (); |
903 | call_pending (EV_A); |
858 | } |
904 | } |
859 | while (!ev_loop_done); |
905 | while (activecnt && !loop_done); |
860 | |
906 | |
861 | if (ev_loop_done != 2) |
907 | if (loop_done != 2) |
862 | ev_loop_done = 0; |
908 | loop_done = 0; |
|
|
909 | } |
|
|
910 | |
|
|
911 | void |
|
|
912 | ev_unloop (EV_P_ int how) |
|
|
913 | { |
|
|
914 | loop_done = how; |
863 | } |
915 | } |
864 | |
916 | |
865 | /*****************************************************************************/ |
917 | /*****************************************************************************/ |
866 | |
918 | |
867 | static void |
919 | inline void |
868 | wlist_add (WL *head, WL elem) |
920 | wlist_add (WL *head, WL elem) |
869 | { |
921 | { |
870 | elem->next = *head; |
922 | elem->next = *head; |
871 | *head = elem; |
923 | *head = elem; |
872 | } |
924 | } |
873 | |
925 | |
874 | static void |
926 | inline void |
875 | wlist_del (WL *head, WL elem) |
927 | wlist_del (WL *head, WL elem) |
876 | { |
928 | { |
877 | while (*head) |
929 | while (*head) |
878 | { |
930 | { |
879 | if (*head == elem) |
931 | if (*head == elem) |
… | |
… | |
884 | |
936 | |
885 | head = &(*head)->next; |
937 | head = &(*head)->next; |
886 | } |
938 | } |
887 | } |
939 | } |
888 | |
940 | |
889 | static void |
941 | inline void |
890 | ev_clear_pending (W w) |
942 | ev_clear_pending (EV_P_ W w) |
891 | { |
943 | { |
892 | if (w->pending) |
944 | if (w->pending) |
893 | { |
945 | { |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
946 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
895 | w->pending = 0; |
947 | w->pending = 0; |
896 | } |
948 | } |
897 | } |
949 | } |
898 | |
950 | |
899 | static void |
951 | inline void |
900 | ev_start (W w, int active) |
952 | ev_start (EV_P_ W w, int active) |
901 | { |
953 | { |
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
954 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
955 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
904 | |
956 | |
905 | w->active = active; |
957 | w->active = active; |
|
|
958 | ev_ref (EV_A); |
906 | } |
959 | } |
907 | |
960 | |
908 | static void |
961 | inline void |
909 | ev_stop (W w) |
962 | ev_stop (EV_P_ W w) |
910 | { |
963 | { |
|
|
964 | ev_unref (EV_A); |
911 | w->active = 0; |
965 | w->active = 0; |
912 | } |
966 | } |
913 | |
967 | |
914 | /*****************************************************************************/ |
968 | /*****************************************************************************/ |
915 | |
969 | |
916 | void |
970 | void |
917 | ev_io_start (struct ev_io *w) |
971 | ev_io_start (EV_P_ struct ev_io *w) |
918 | { |
972 | { |
919 | int fd = w->fd; |
973 | int fd = w->fd; |
920 | |
974 | |
921 | if (ev_is_active (w)) |
975 | if (ev_is_active (w)) |
922 | return; |
976 | return; |
923 | |
977 | |
924 | assert (("ev_io_start called with negative fd", fd >= 0)); |
978 | assert (("ev_io_start called with negative fd", fd >= 0)); |
925 | |
979 | |
926 | ev_start ((W)w, 1); |
980 | ev_start (EV_A_ (W)w, 1); |
927 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
981 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
928 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
982 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
929 | |
983 | |
930 | fd_change (fd); |
984 | fd_change (EV_A_ fd); |
931 | } |
985 | } |
932 | |
986 | |
933 | void |
987 | void |
934 | ev_io_stop (struct ev_io *w) |
988 | ev_io_stop (EV_P_ struct ev_io *w) |
935 | { |
989 | { |
936 | ev_clear_pending ((W)w); |
990 | ev_clear_pending (EV_A_ (W)w); |
937 | if (!ev_is_active (w)) |
991 | if (!ev_is_active (w)) |
938 | return; |
992 | return; |
939 | |
993 | |
940 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
994 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
941 | ev_stop ((W)w); |
995 | ev_stop (EV_A_ (W)w); |
942 | |
996 | |
943 | fd_change (w->fd); |
997 | fd_change (EV_A_ w->fd); |
944 | } |
998 | } |
945 | |
999 | |
946 | void |
1000 | void |
947 | ev_timer_start (struct ev_timer *w) |
1001 | ev_timer_start (EV_P_ struct ev_timer *w) |
948 | { |
1002 | { |
949 | if (ev_is_active (w)) |
1003 | if (ev_is_active (w)) |
950 | return; |
1004 | return; |
951 | |
1005 | |
952 | w->at += now; |
1006 | w->at += mn_now; |
953 | |
1007 | |
954 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1008 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
955 | |
1009 | |
956 | ev_start ((W)w, ++timercnt); |
1010 | ev_start (EV_A_ (W)w, ++timercnt); |
957 | array_needsize (timers, timermax, timercnt, ); |
1011 | array_needsize (timers, timermax, timercnt, ); |
958 | timers [timercnt - 1] = w; |
1012 | timers [timercnt - 1] = w; |
959 | upheap ((WT *)timers, timercnt - 1); |
1013 | upheap ((WT *)timers, timercnt - 1); |
960 | } |
1014 | } |
961 | |
1015 | |
962 | void |
1016 | void |
963 | ev_timer_stop (struct ev_timer *w) |
1017 | ev_timer_stop (EV_P_ struct ev_timer *w) |
964 | { |
1018 | { |
965 | ev_clear_pending ((W)w); |
1019 | ev_clear_pending (EV_A_ (W)w); |
966 | if (!ev_is_active (w)) |
1020 | if (!ev_is_active (w)) |
967 | return; |
1021 | return; |
968 | |
1022 | |
969 | if (w->active < timercnt--) |
1023 | if (w->active < timercnt--) |
970 | { |
1024 | { |
… | |
… | |
972 | downheap ((WT *)timers, timercnt, w->active - 1); |
1026 | downheap ((WT *)timers, timercnt, w->active - 1); |
973 | } |
1027 | } |
974 | |
1028 | |
975 | w->at = w->repeat; |
1029 | w->at = w->repeat; |
976 | |
1030 | |
977 | ev_stop ((W)w); |
1031 | ev_stop (EV_A_ (W)w); |
978 | } |
1032 | } |
979 | |
1033 | |
980 | void |
1034 | void |
981 | ev_timer_again (struct ev_timer *w) |
1035 | ev_timer_again (EV_P_ struct ev_timer *w) |
982 | { |
1036 | { |
983 | if (ev_is_active (w)) |
1037 | if (ev_is_active (w)) |
984 | { |
1038 | { |
985 | if (w->repeat) |
1039 | if (w->repeat) |
986 | { |
1040 | { |
987 | w->at = now + w->repeat; |
1041 | w->at = mn_now + w->repeat; |
988 | downheap ((WT *)timers, timercnt, w->active - 1); |
1042 | downheap ((WT *)timers, timercnt, w->active - 1); |
989 | } |
1043 | } |
990 | else |
1044 | else |
991 | ev_timer_stop (w); |
1045 | ev_timer_stop (EV_A_ w); |
992 | } |
1046 | } |
993 | else if (w->repeat) |
1047 | else if (w->repeat) |
994 | ev_timer_start (w); |
1048 | ev_timer_start (EV_A_ w); |
995 | } |
1049 | } |
996 | |
1050 | |
997 | void |
1051 | void |
998 | ev_periodic_start (struct ev_periodic *w) |
1052 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
999 | { |
1053 | { |
1000 | if (ev_is_active (w)) |
1054 | if (ev_is_active (w)) |
1001 | return; |
1055 | return; |
1002 | |
1056 | |
1003 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1057 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1004 | |
1058 | |
1005 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1059 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1006 | if (w->interval) |
1060 | if (w->interval) |
1007 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1061 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1008 | |
1062 | |
1009 | ev_start ((W)w, ++periodiccnt); |
1063 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1010 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1064 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1011 | periodics [periodiccnt - 1] = w; |
1065 | periodics [periodiccnt - 1] = w; |
1012 | upheap ((WT *)periodics, periodiccnt - 1); |
1066 | upheap ((WT *)periodics, periodiccnt - 1); |
1013 | } |
1067 | } |
1014 | |
1068 | |
1015 | void |
1069 | void |
1016 | ev_periodic_stop (struct ev_periodic *w) |
1070 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1017 | { |
1071 | { |
1018 | ev_clear_pending ((W)w); |
1072 | ev_clear_pending (EV_A_ (W)w); |
1019 | if (!ev_is_active (w)) |
1073 | if (!ev_is_active (w)) |
1020 | return; |
1074 | return; |
1021 | |
1075 | |
1022 | if (w->active < periodiccnt--) |
1076 | if (w->active < periodiccnt--) |
1023 | { |
1077 | { |
1024 | periodics [w->active - 1] = periodics [periodiccnt]; |
1078 | periodics [w->active - 1] = periodics [periodiccnt]; |
1025 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1079 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1026 | } |
1080 | } |
1027 | |
1081 | |
1028 | ev_stop ((W)w); |
1082 | ev_stop (EV_A_ (W)w); |
1029 | } |
1083 | } |
1030 | |
1084 | |
1031 | #ifndef SA_RESTART |
1085 | #ifndef SA_RESTART |
1032 | # define SA_RESTART 0 |
1086 | # define SA_RESTART 0 |
1033 | #endif |
1087 | #endif |
1034 | |
1088 | |
1035 | void |
1089 | void |
1036 | ev_signal_start (struct ev_signal *w) |
1090 | ev_signal_start (EV_P_ struct ev_signal *w) |
1037 | { |
1091 | { |
1038 | if (ev_is_active (w)) |
1092 | if (ev_is_active (w)) |
1039 | return; |
1093 | return; |
1040 | |
1094 | |
1041 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1095 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1042 | |
1096 | |
1043 | ev_start ((W)w, 1); |
1097 | ev_start (EV_A_ (W)w, 1); |
1044 | array_needsize (signals, signalmax, w->signum, signals_init); |
1098 | array_needsize (signals, signalmax, w->signum, signals_init); |
1045 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1099 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1046 | |
1100 | |
1047 | if (!w->next) |
1101 | if (!w->next) |
1048 | { |
1102 | { |
… | |
… | |
1053 | sigaction (w->signum, &sa, 0); |
1107 | sigaction (w->signum, &sa, 0); |
1054 | } |
1108 | } |
1055 | } |
1109 | } |
1056 | |
1110 | |
1057 | void |
1111 | void |
1058 | ev_signal_stop (struct ev_signal *w) |
1112 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1059 | { |
1113 | { |
1060 | ev_clear_pending ((W)w); |
1114 | ev_clear_pending (EV_A_ (W)w); |
1061 | if (!ev_is_active (w)) |
1115 | if (!ev_is_active (w)) |
1062 | return; |
1116 | return; |
1063 | |
1117 | |
1064 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1118 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1065 | ev_stop ((W)w); |
1119 | ev_stop (EV_A_ (W)w); |
1066 | |
1120 | |
1067 | if (!signals [w->signum - 1].head) |
1121 | if (!signals [w->signum - 1].head) |
1068 | signal (w->signum, SIG_DFL); |
1122 | signal (w->signum, SIG_DFL); |
1069 | } |
1123 | } |
1070 | |
1124 | |
1071 | void |
1125 | void |
1072 | ev_idle_start (struct ev_idle *w) |
1126 | ev_idle_start (EV_P_ struct ev_idle *w) |
1073 | { |
1127 | { |
1074 | if (ev_is_active (w)) |
1128 | if (ev_is_active (w)) |
1075 | return; |
1129 | return; |
1076 | |
1130 | |
1077 | ev_start ((W)w, ++idlecnt); |
1131 | ev_start (EV_A_ (W)w, ++idlecnt); |
1078 | array_needsize (idles, idlemax, idlecnt, ); |
1132 | array_needsize (idles, idlemax, idlecnt, ); |
1079 | idles [idlecnt - 1] = w; |
1133 | idles [idlecnt - 1] = w; |
1080 | } |
1134 | } |
1081 | |
1135 | |
1082 | void |
1136 | void |
1083 | ev_idle_stop (struct ev_idle *w) |
1137 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1084 | { |
1138 | { |
1085 | ev_clear_pending ((W)w); |
1139 | ev_clear_pending (EV_A_ (W)w); |
1086 | if (ev_is_active (w)) |
1140 | if (ev_is_active (w)) |
1087 | return; |
1141 | return; |
1088 | |
1142 | |
1089 | idles [w->active - 1] = idles [--idlecnt]; |
1143 | idles [w->active - 1] = idles [--idlecnt]; |
1090 | ev_stop ((W)w); |
1144 | ev_stop (EV_A_ (W)w); |
1091 | } |
1145 | } |
1092 | |
1146 | |
1093 | void |
1147 | void |
1094 | ev_prepare_start (struct ev_prepare *w) |
1148 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1095 | { |
1149 | { |
1096 | if (ev_is_active (w)) |
1150 | if (ev_is_active (w)) |
1097 | return; |
1151 | return; |
1098 | |
1152 | |
1099 | ev_start ((W)w, ++preparecnt); |
1153 | ev_start (EV_A_ (W)w, ++preparecnt); |
1100 | array_needsize (prepares, preparemax, preparecnt, ); |
1154 | array_needsize (prepares, preparemax, preparecnt, ); |
1101 | prepares [preparecnt - 1] = w; |
1155 | prepares [preparecnt - 1] = w; |
1102 | } |
1156 | } |
1103 | |
1157 | |
1104 | void |
1158 | void |
1105 | ev_prepare_stop (struct ev_prepare *w) |
1159 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1106 | { |
1160 | { |
1107 | ev_clear_pending ((W)w); |
1161 | ev_clear_pending (EV_A_ (W)w); |
1108 | if (ev_is_active (w)) |
1162 | if (ev_is_active (w)) |
1109 | return; |
1163 | return; |
1110 | |
1164 | |
1111 | prepares [w->active - 1] = prepares [--preparecnt]; |
1165 | prepares [w->active - 1] = prepares [--preparecnt]; |
1112 | ev_stop ((W)w); |
1166 | ev_stop (EV_A_ (W)w); |
1113 | } |
1167 | } |
1114 | |
1168 | |
1115 | void |
1169 | void |
1116 | ev_check_start (struct ev_check *w) |
1170 | ev_check_start (EV_P_ struct ev_check *w) |
1117 | { |
1171 | { |
1118 | if (ev_is_active (w)) |
1172 | if (ev_is_active (w)) |
1119 | return; |
1173 | return; |
1120 | |
1174 | |
1121 | ev_start ((W)w, ++checkcnt); |
1175 | ev_start (EV_A_ (W)w, ++checkcnt); |
1122 | array_needsize (checks, checkmax, checkcnt, ); |
1176 | array_needsize (checks, checkmax, checkcnt, ); |
1123 | checks [checkcnt - 1] = w; |
1177 | checks [checkcnt - 1] = w; |
1124 | } |
1178 | } |
1125 | |
1179 | |
1126 | void |
1180 | void |
1127 | ev_check_stop (struct ev_check *w) |
1181 | ev_check_stop (EV_P_ struct ev_check *w) |
1128 | { |
1182 | { |
1129 | ev_clear_pending ((W)w); |
1183 | ev_clear_pending (EV_A_ (W)w); |
1130 | if (ev_is_active (w)) |
1184 | if (ev_is_active (w)) |
1131 | return; |
1185 | return; |
1132 | |
1186 | |
1133 | checks [w->active - 1] = checks [--checkcnt]; |
1187 | checks [w->active - 1] = checks [--checkcnt]; |
1134 | ev_stop ((W)w); |
1188 | ev_stop (EV_A_ (W)w); |
1135 | } |
1189 | } |
1136 | |
1190 | |
1137 | void |
1191 | void |
1138 | ev_child_start (struct ev_child *w) |
1192 | ev_child_start (EV_P_ struct ev_child *w) |
1139 | { |
1193 | { |
1140 | if (ev_is_active (w)) |
1194 | if (ev_is_active (w)) |
1141 | return; |
1195 | return; |
1142 | |
1196 | |
1143 | ev_start ((W)w, 1); |
1197 | ev_start (EV_A_ (W)w, 1); |
1144 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1198 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1145 | } |
1199 | } |
1146 | |
1200 | |
1147 | void |
1201 | void |
1148 | ev_child_stop (struct ev_child *w) |
1202 | ev_child_stop (EV_P_ struct ev_child *w) |
1149 | { |
1203 | { |
1150 | ev_clear_pending ((W)w); |
1204 | ev_clear_pending (EV_A_ (W)w); |
1151 | if (ev_is_active (w)) |
1205 | if (ev_is_active (w)) |
1152 | return; |
1206 | return; |
1153 | |
1207 | |
1154 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1208 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1155 | ev_stop ((W)w); |
1209 | ev_stop (EV_A_ (W)w); |
1156 | } |
1210 | } |
1157 | |
1211 | |
1158 | /*****************************************************************************/ |
1212 | /*****************************************************************************/ |
1159 | |
1213 | |
1160 | struct ev_once |
1214 | struct ev_once |
… | |
… | |
1164 | void (*cb)(int revents, void *arg); |
1218 | void (*cb)(int revents, void *arg); |
1165 | void *arg; |
1219 | void *arg; |
1166 | }; |
1220 | }; |
1167 | |
1221 | |
1168 | static void |
1222 | static void |
1169 | once_cb (struct ev_once *once, int revents) |
1223 | once_cb (EV_P_ struct ev_once *once, int revents) |
1170 | { |
1224 | { |
1171 | void (*cb)(int revents, void *arg) = once->cb; |
1225 | void (*cb)(int revents, void *arg) = once->cb; |
1172 | void *arg = once->arg; |
1226 | void *arg = once->arg; |
1173 | |
1227 | |
1174 | ev_io_stop (&once->io); |
1228 | ev_io_stop (EV_A_ &once->io); |
1175 | ev_timer_stop (&once->to); |
1229 | ev_timer_stop (EV_A_ &once->to); |
1176 | free (once); |
1230 | free (once); |
1177 | |
1231 | |
1178 | cb (revents, arg); |
1232 | cb (revents, arg); |
1179 | } |
1233 | } |
1180 | |
1234 | |
1181 | static void |
1235 | static void |
1182 | once_cb_io (struct ev_io *w, int revents) |
1236 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1183 | { |
1237 | { |
1184 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1238 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1185 | } |
1239 | } |
1186 | |
1240 | |
1187 | static void |
1241 | static void |
1188 | once_cb_to (struct ev_timer *w, int revents) |
1242 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1189 | { |
1243 | { |
1190 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1244 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1191 | } |
1245 | } |
1192 | |
1246 | |
1193 | void |
1247 | void |
1194 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1248 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1195 | { |
1249 | { |
1196 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1250 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1197 | |
1251 | |
1198 | if (!once) |
1252 | if (!once) |
1199 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1253 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1204 | |
1258 | |
1205 | ev_watcher_init (&once->io, once_cb_io); |
1259 | ev_watcher_init (&once->io, once_cb_io); |
1206 | if (fd >= 0) |
1260 | if (fd >= 0) |
1207 | { |
1261 | { |
1208 | ev_io_set (&once->io, fd, events); |
1262 | ev_io_set (&once->io, fd, events); |
1209 | ev_io_start (&once->io); |
1263 | ev_io_start (EV_A_ &once->io); |
1210 | } |
1264 | } |
1211 | |
1265 | |
1212 | ev_watcher_init (&once->to, once_cb_to); |
1266 | ev_watcher_init (&once->to, once_cb_to); |
1213 | if (timeout >= 0.) |
1267 | if (timeout >= 0.) |
1214 | { |
1268 | { |
1215 | ev_timer_set (&once->to, timeout, 0.); |
1269 | ev_timer_set (&once->to, timeout, 0.); |
1216 | ev_timer_start (&once->to); |
1270 | ev_timer_start (EV_A_ &once->to); |
1217 | } |
1271 | } |
1218 | } |
1272 | } |
1219 | } |
1273 | } |
1220 | |
1274 | |
1221 | /*****************************************************************************/ |
1275 | /*****************************************************************************/ |