… | |
… | |
113 | } |
113 | } |
114 | |
114 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
117 | { \ |
117 | { \ |
118 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
119 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
120 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
121 | cur = newcnt; \ |
127 | cur = newcnt; \ |
122 | } |
128 | } |
123 | |
129 | |
… | |
… | |
191 | event (events [i], type); |
197 | event (events [i], type); |
192 | } |
198 | } |
193 | |
199 | |
194 | /* called on EBADF to verify fds */ |
200 | /* called on EBADF to verify fds */ |
195 | static void |
201 | static void |
196 | fd_recheck () |
202 | fd_recheck (void) |
197 | { |
203 | { |
198 | int fd; |
204 | int fd; |
199 | |
205 | |
200 | for (fd = 0; fd < anfdmax; ++fd) |
206 | for (fd = 0; fd < anfdmax; ++fd) |
201 | if (anfds [fd].wev) |
207 | if (anfds [fd].wev) |
202 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
203 | while (anfds [fd].head) |
209 | while (anfds [fd].head) |
|
|
210 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
204 | evio_stop (anfds [fd].head); |
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
205 | } |
214 | } |
206 | |
215 | |
207 | /*****************************************************************************/ |
216 | /*****************************************************************************/ |
208 | |
217 | |
209 | static struct ev_timer **timers; |
218 | static struct ev_timer **timers; |
… | |
… | |
366 | #endif |
375 | #endif |
367 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
368 | # include "ev_select.c" |
377 | # include "ev_select.c" |
369 | #endif |
378 | #endif |
370 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
371 | int ev_init (int flags) |
392 | int ev_init (int flags) |
372 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
373 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
374 | { |
397 | { |
375 | struct timespec ts; |
398 | struct timespec ts; |
376 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
377 | have_monotonic = 1; |
400 | have_monotonic = 1; |
378 | } |
401 | } |
379 | #endif |
402 | #endif |
380 | |
403 | |
381 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
382 | now = get_clock (); |
405 | now = get_clock (); |
383 | diff = ev_now - now; |
406 | diff = ev_now - now; |
384 | |
407 | |
385 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
386 | return 0; |
409 | return 0; |
387 | |
410 | |
388 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
389 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
390 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
391 | #endif |
414 | #endif |
392 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
393 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
394 | #endif |
417 | #endif |
395 | |
418 | |
396 | if (ev_method) |
419 | if (ev_method) |
397 | { |
420 | { |
398 | evw_init (&sigev, sigcb); |
421 | evw_init (&sigev, sigcb); |
399 | siginit (); |
422 | siginit (); |
400 | |
423 | |
401 | evsignal_init (&childev, childcb, SIGCHLD); |
424 | evsignal_init (&childev, childcb, SIGCHLD); |
402 | evsignal_start (&childev); |
425 | evsignal_start (&childev); |
|
|
426 | } |
403 | } |
427 | } |
404 | |
428 | |
405 | return ev_method; |
429 | return ev_method; |
406 | } |
430 | } |
407 | |
431 | |
408 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
409 | |
433 | |
|
|
434 | void |
410 | void ev_prefork (void) |
435 | ev_prefork (void) |
411 | { |
436 | { |
412 | /* nop */ |
437 | /* nop */ |
413 | } |
438 | } |
414 | |
439 | |
|
|
440 | void |
415 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
416 | { |
442 | { |
417 | /* nop */ |
443 | /* nop */ |
418 | } |
444 | } |
419 | |
445 | |
|
|
446 | void |
420 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
421 | { |
448 | { |
422 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
423 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
424 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
425 | #endif |
452 | #endif |
… | |
… | |
458 | |
485 | |
459 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
460 | } |
487 | } |
461 | |
488 | |
462 | static void |
489 | static void |
463 | call_pending () |
490 | call_pending (void) |
464 | { |
491 | { |
465 | while (pendingcnt) |
492 | while (pendingcnt) |
466 | { |
493 | { |
467 | ANPENDING *p = pendings + --pendingcnt; |
494 | ANPENDING *p = pendings + --pendingcnt; |
468 | |
495 | |
… | |
… | |
473 | } |
500 | } |
474 | } |
501 | } |
475 | } |
502 | } |
476 | |
503 | |
477 | static void |
504 | static void |
478 | timers_reify () |
505 | timers_reify (void) |
479 | { |
506 | { |
480 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
481 | { |
508 | { |
482 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
483 | |
510 | |
… | |
… | |
494 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
495 | } |
522 | } |
496 | } |
523 | } |
497 | |
524 | |
498 | static void |
525 | static void |
499 | periodics_reify () |
526 | periodics_reify (void) |
500 | { |
527 | { |
501 | while (periodiccnt && periodics [0]->at <= ev_now) |
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
502 | { |
529 | { |
503 | struct ev_periodic *w = periodics [0]; |
530 | struct ev_periodic *w = periodics [0]; |
504 | |
531 | |
… | |
… | |
540 | } |
567 | } |
541 | } |
568 | } |
542 | } |
569 | } |
543 | |
570 | |
544 | static void |
571 | static void |
545 | time_update () |
572 | time_update (void) |
546 | { |
573 | { |
547 | int i; |
574 | int i; |
548 | |
575 | |
549 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
550 | |
577 | |
… | |
… | |
713 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
714 | |
741 | |
715 | ++fdchangecnt; |
742 | ++fdchangecnt; |
716 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
717 | fdchanges [fdchangecnt - 1] = fd; |
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
718 | } |
748 | } |
719 | |
749 | |
720 | void |
750 | void |
721 | evio_stop (struct ev_io *w) |
751 | evio_stop (struct ev_io *w) |
722 | { |
752 | { |
|
|
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
723 | ev_clear ((W)w); |
755 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
756 | if (!ev_is_active (w)) |
725 | return; |
757 | return; |
726 | |
758 | |
727 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |