… | |
… | |
9 | #include <assert.h> |
9 | #include <assert.h> |
10 | #include <errno.h> |
10 | #include <errno.h> |
11 | #include <sys/time.h> |
11 | #include <sys/time.h> |
12 | #include <time.h> |
12 | #include <time.h> |
13 | |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
14 | #ifndef HAVE_MONOTONIC |
16 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
16 | # define HAVE_MONOTONIC 1 |
18 | # define HAVE_MONOTONIC 1 |
17 | # endif |
19 | # endif |
18 | #endif |
20 | #endif |
… | |
… | |
40 | |
42 | |
41 | struct ev_watcher_list { |
43 | struct ev_watcher_list { |
42 | EV_WATCHER_LIST (ev_watcher_list); |
44 | EV_WATCHER_LIST (ev_watcher_list); |
43 | }; |
45 | }; |
44 | |
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
45 | typedef struct ev_watcher *W; |
51 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
52 | typedef struct ev_watcher_list *WL; |
|
|
53 | typedef struct ev_watcher_time *WT; |
47 | |
54 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
55 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
56 | ev_tstamp ev_now; |
50 | int ev_method; |
57 | int ev_method; |
51 | |
58 | |
… | |
… | |
163 | event (events [i], type); |
170 | event (events [i], type); |
164 | } |
171 | } |
165 | |
172 | |
166 | /*****************************************************************************/ |
173 | /*****************************************************************************/ |
167 | |
174 | |
168 | static struct ev_timer **atimers; |
175 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
176 | static int timermax, timercnt; |
170 | |
177 | |
171 | static struct ev_timer **rtimers; |
178 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
179 | static int periodicmax, periodiccnt; |
173 | |
180 | |
174 | static void |
181 | static void |
175 | upheap (struct ev_timer **timers, int k) |
182 | upheap (WT *timers, int k) |
176 | { |
183 | { |
177 | struct ev_timer *w = timers [k]; |
184 | WT w = timers [k]; |
178 | |
185 | |
179 | while (k && timers [k >> 1]->at > w->at) |
186 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
187 | { |
181 | timers [k] = timers [k >> 1]; |
188 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
189 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
194 | timers [k]->active = k + 1; |
188 | |
195 | |
189 | } |
196 | } |
190 | |
197 | |
191 | static void |
198 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
199 | downheap (WT *timers, int N, int k) |
193 | { |
200 | { |
194 | struct ev_timer *w = timers [k]; |
201 | WT w = timers [k]; |
195 | |
202 | |
196 | while (k < (N >> 1)) |
203 | while (k < (N >> 1)) |
197 | { |
204 | { |
198 | int j = k << 1; |
205 | int j = k << 1; |
199 | |
206 | |
… | |
… | |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
332 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
333 | #endif |
327 | |
334 | |
328 | if (ev_method) |
335 | if (ev_method) |
329 | { |
336 | { |
330 | evw_init (&sigev, sigcb, 0); |
337 | evw_init (&sigev, sigcb); |
331 | siginit (); |
338 | siginit (); |
332 | } |
339 | } |
333 | |
340 | |
334 | return ev_method; |
341 | return ev_method; |
335 | } |
342 | } |
336 | |
343 | |
337 | /*****************************************************************************/ |
344 | /*****************************************************************************/ |
338 | |
345 | |
339 | void ev_prefork (void) |
346 | void ev_prefork (void) |
340 | { |
347 | { |
|
|
348 | /* nop */ |
341 | } |
349 | } |
342 | |
350 | |
343 | void ev_postfork_parent (void) |
351 | void ev_postfork_parent (void) |
344 | { |
352 | { |
|
|
353 | /* nop */ |
345 | } |
354 | } |
346 | |
355 | |
347 | void ev_postfork_child (void) |
356 | void ev_postfork_child (void) |
348 | { |
357 | { |
349 | #if HAVE_EPOLL |
358 | #if HAVE_EPOLL |
… | |
… | |
404 | |
413 | |
405 | pendingcnt = 0; |
414 | pendingcnt = 0; |
406 | } |
415 | } |
407 | |
416 | |
408 | static void |
417 | static void |
409 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
418 | timers_reify () |
410 | { |
419 | { |
411 | while (timercnt && timers [0]->at <= now) |
420 | while (timercnt && timers [0]->at <= now) |
412 | { |
421 | { |
413 | struct ev_timer *w = timers [0]; |
422 | struct ev_timer *w = timers [0]; |
414 | |
423 | |
415 | /* first reschedule or stop timer */ |
424 | /* first reschedule or stop timer */ |
416 | if (w->repeat) |
425 | if (w->repeat) |
417 | { |
426 | { |
418 | if (w->is_abs) |
|
|
419 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
420 | else |
|
|
421 | w->at = now + w->repeat; |
427 | w->at = now + w->repeat; |
422 | |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
423 | assert (w->at > now); |
|
|
424 | |
|
|
425 | downheap (timers, timercnt, 0); |
429 | downheap ((WT *)timers, timercnt, 0); |
426 | } |
430 | } |
427 | else |
431 | else |
|
|
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
|
|
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | static void |
|
|
439 | periodics_reify () |
|
|
440 | { |
|
|
441 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
442 | { |
|
|
443 | struct ev_periodic *w = periodics [0]; |
|
|
444 | |
|
|
445 | /* first reschedule or stop timer */ |
|
|
446 | if (w->interval) |
428 | { |
447 | { |
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
448 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
430 | --timercnt; /* maybe pass by reference instead? */ |
449 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
450 | downheap ((WT *)periodics, periodiccnt, 0); |
431 | } |
451 | } |
|
|
452 | else |
|
|
453 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
432 | |
454 | |
433 | event ((W)w, EV_TIMEOUT); |
455 | event ((W)w, EV_TIMEOUT); |
434 | } |
456 | } |
435 | } |
457 | } |
436 | |
458 | |
437 | static void |
459 | static void |
|
|
460 | time_jump (ev_tstamp diff) |
|
|
461 | { |
|
|
462 | int i; |
|
|
463 | |
|
|
464 | /* adjust periodics */ |
|
|
465 | for (i = 0; i < periodiccnt; ++i) |
|
|
466 | { |
|
|
467 | struct ev_periodic *w = periodics [i]; |
|
|
468 | |
|
|
469 | if (w->interval) |
|
|
470 | { |
|
|
471 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
472 | |
|
|
473 | if (fabs (diff) >= 1e-4) |
|
|
474 | { |
|
|
475 | evperiodic_stop (w); |
|
|
476 | evperiodic_start (w); |
|
|
477 | |
|
|
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
479 | } |
|
|
480 | } |
|
|
481 | } |
|
|
482 | |
|
|
483 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
484 | for (i = 0; i < timercnt; ++i) |
|
|
485 | timers [i]->at += diff; |
|
|
486 | } |
|
|
487 | |
|
|
488 | static void |
438 | time_update () |
489 | time_update () |
439 | { |
490 | { |
440 | int i; |
491 | int i; |
|
|
492 | |
441 | ev_now = ev_time (); |
493 | ev_now = ev_time (); |
442 | |
494 | |
443 | if (have_monotonic) |
495 | if (have_monotonic) |
444 | { |
496 | { |
445 | ev_tstamp odiff = diff; |
497 | ev_tstamp odiff = diff; |
446 | |
498 | |
447 | /* detecting time jumps is much more difficult */ |
|
|
448 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
499 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
449 | { |
500 | { |
450 | now = get_clock (); |
501 | now = get_clock (); |
451 | diff = ev_now - now; |
502 | diff = ev_now - now; |
452 | |
503 | |
453 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
504 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
454 | return; /* all is well */ |
505 | return; /* all is well */ |
455 | |
506 | |
456 | ev_now = ev_time (); |
507 | ev_now = ev_time (); |
457 | } |
508 | } |
458 | |
509 | |
459 | /* time jump detected, reschedule atimers */ |
510 | time_jump (diff - odiff); |
460 | for (i = 0; i < atimercnt; ++i) |
|
|
461 | { |
|
|
462 | struct ev_timer *w = atimers [i]; |
|
|
463 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
464 | } |
|
|
465 | } |
511 | } |
466 | else |
512 | else |
467 | { |
513 | { |
468 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
469 | /* time jump detected, adjust rtimers */ |
515 | time_jump (ev_now - now); |
470 | for (i = 0; i < rtimercnt; ++i) |
|
|
471 | rtimers [i]->at += ev_now - now; |
|
|
472 | |
516 | |
473 | now = ev_now; |
517 | now = ev_now; |
474 | } |
518 | } |
475 | } |
519 | } |
476 | |
520 | |
… | |
… | |
491 | { |
535 | { |
492 | /* update fd-related kernel structures */ |
536 | /* update fd-related kernel structures */ |
493 | fd_reify (); |
537 | fd_reify (); |
494 | |
538 | |
495 | /* calculate blocking time */ |
539 | /* calculate blocking time */ |
|
|
540 | |
|
|
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
542 | ev_now = ev_time (); |
|
|
543 | |
496 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
497 | block = 0.; |
545 | block = 0.; |
498 | else |
546 | else |
499 | { |
547 | { |
500 | block = MAX_BLOCKTIME; |
548 | block = MAX_BLOCKTIME; |
501 | |
549 | |
502 | if (rtimercnt) |
550 | if (timercnt) |
503 | { |
551 | { |
504 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
505 | if (block > to) block = to; |
553 | if (block > to) block = to; |
506 | } |
554 | } |
507 | |
555 | |
508 | if (atimercnt) |
556 | if (periodiccnt) |
509 | { |
557 | { |
510 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
558 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
511 | if (block > to) block = to; |
559 | if (block > to) block = to; |
512 | } |
560 | } |
513 | |
561 | |
514 | if (block < 0.) block = 0.; |
562 | if (block < 0.) block = 0.; |
515 | } |
563 | } |
… | |
… | |
518 | |
566 | |
519 | /* update ev_now, do magic */ |
567 | /* update ev_now, do magic */ |
520 | time_update (); |
568 | time_update (); |
521 | |
569 | |
522 | /* queue pending timers and reschedule them */ |
570 | /* queue pending timers and reschedule them */ |
523 | /* absolute timers first */ |
571 | periodics_reify (); /* absolute timers first */ |
524 | timers_reify (atimers, atimercnt, ev_now); |
|
|
525 | /* relative timers second */ |
572 | timers_reify (); /* relative timers second */ |
526 | timers_reify (rtimers, rtimercnt, now); |
|
|
527 | |
573 | |
528 | /* queue idle watchers unless io or timers are pending */ |
574 | /* queue idle watchers unless io or timers are pending */ |
529 | if (!pendingcnt) |
575 | if (!pendingcnt) |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
531 | |
577 | |
… | |
… | |
573 | { |
619 | { |
574 | if (w->pending) |
620 | if (w->pending) |
575 | pendings [w->pending - 1].w = 0; |
621 | pendings [w->pending - 1].w = 0; |
576 | |
622 | |
577 | w->active = 0; |
623 | w->active = 0; |
578 | /* nop */ |
|
|
579 | } |
624 | } |
580 | |
625 | |
581 | /*****************************************************************************/ |
626 | /*****************************************************************************/ |
582 | |
627 | |
583 | void |
628 | void |
… | |
… | |
609 | ++fdchangecnt; |
654 | ++fdchangecnt; |
610 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
611 | fdchanges [fdchangecnt - 1] = w->fd; |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
612 | } |
657 | } |
613 | |
658 | |
|
|
659 | |
614 | void |
660 | void |
615 | evtimer_start (struct ev_timer *w) |
661 | evtimer_start (struct ev_timer *w) |
616 | { |
662 | { |
617 | if (ev_is_active (w)) |
663 | if (ev_is_active (w)) |
618 | return; |
664 | return; |
619 | |
665 | |
620 | if (w->is_abs) |
666 | w->at += now; |
621 | { |
|
|
622 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
623 | if (w->repeat) |
|
|
624 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
625 | |
667 | |
626 | ev_start ((W)w, ++atimercnt); |
668 | ev_start ((W)w, ++timercnt); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
669 | array_needsize (timers, timermax, timercnt, ); |
628 | atimers [atimercnt - 1] = w; |
670 | timers [timercnt - 1] = w; |
629 | upheap (atimers, atimercnt - 1); |
671 | upheap ((WT *)timers, timercnt - 1); |
630 | } |
|
|
631 | else |
|
|
632 | { |
|
|
633 | w->at += now; |
|
|
634 | |
|
|
635 | ev_start ((W)w, ++rtimercnt); |
|
|
636 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
637 | rtimers [rtimercnt - 1] = w; |
|
|
638 | upheap (rtimers, rtimercnt - 1); |
|
|
639 | } |
|
|
640 | |
|
|
641 | } |
672 | } |
642 | |
673 | |
643 | void |
674 | void |
644 | evtimer_stop (struct ev_timer *w) |
675 | evtimer_stop (struct ev_timer *w) |
645 | { |
676 | { |
646 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
647 | return; |
678 | return; |
648 | |
679 | |
649 | if (w->is_abs) |
|
|
650 | { |
|
|
651 | if (w->active < atimercnt--) |
680 | if (w->active < timercnt--) |
652 | { |
681 | { |
653 | atimers [w->active - 1] = atimers [atimercnt]; |
682 | timers [w->active - 1] = timers [timercnt]; |
654 | downheap (atimers, atimercnt, w->active - 1); |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
655 | } |
|
|
656 | } |
|
|
657 | else |
|
|
658 | { |
684 | } |
|
|
685 | |
|
|
686 | ev_stop ((W)w); |
|
|
687 | } |
|
|
688 | |
|
|
689 | void |
|
|
690 | evperiodic_start (struct ev_periodic *w) |
|
|
691 | { |
|
|
692 | if (ev_is_active (w)) |
|
|
693 | return; |
|
|
694 | |
|
|
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
696 | if (w->interval) |
|
|
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
698 | |
|
|
699 | ev_start ((W)w, ++periodiccnt); |
|
|
700 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
701 | periodics [periodiccnt - 1] = w; |
|
|
702 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
703 | } |
|
|
704 | |
|
|
705 | void |
|
|
706 | evperiodic_stop (struct ev_periodic *w) |
|
|
707 | { |
|
|
708 | if (!ev_is_active (w)) |
|
|
709 | return; |
|
|
710 | |
659 | if (w->active < rtimercnt--) |
711 | if (w->active < periodiccnt--) |
660 | { |
712 | { |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
713 | periodics [w->active - 1] = periodics [periodiccnt]; |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
714 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
663 | } |
|
|
664 | } |
715 | } |
665 | |
716 | |
666 | ev_stop ((W)w); |
717 | ev_stop ((W)w); |
667 | } |
718 | } |
668 | |
719 | |
… | |
… | |
731 | ev_stop ((W)w); |
782 | ev_stop ((W)w); |
732 | } |
783 | } |
733 | |
784 | |
734 | /*****************************************************************************/ |
785 | /*****************************************************************************/ |
735 | |
786 | |
736 | #if 0 |
787 | #if 1 |
|
|
788 | |
|
|
789 | struct ev_io wio; |
737 | |
790 | |
738 | static void |
791 | static void |
739 | sin_cb (struct ev_io *w, int revents) |
792 | sin_cb (struct ev_io *w, int revents) |
740 | { |
793 | { |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
794 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
751 | |
804 | |
752 | static void |
805 | static void |
753 | scb (struct ev_signal *w, int revents) |
806 | scb (struct ev_signal *w, int revents) |
754 | { |
807 | { |
755 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
808 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
809 | evio_stop (&wio); |
|
|
810 | evio_start (&wio); |
756 | } |
811 | } |
757 | |
812 | |
758 | static void |
813 | static void |
759 | gcb (struct ev_signal *w, int revents) |
814 | gcb (struct ev_signal *w, int revents) |
760 | { |
815 | { |
761 | fprintf (stderr, "generic %x\n", revents); |
816 | fprintf (stderr, "generic %x\n", revents); |
|
|
817 | |
762 | } |
818 | } |
763 | |
819 | |
764 | int main (void) |
820 | int main (void) |
765 | { |
821 | { |
766 | struct ev_io sin; |
|
|
767 | |
|
|
768 | ev_init (0); |
822 | ev_init (0); |
769 | |
823 | |
770 | evw_init (&sin, sin_cb, 55); |
|
|
771 | evio_set (&sin, 0, EV_READ); |
824 | evio_init (&wio, sin_cb, 0, EV_READ); |
772 | evio_start (&sin); |
825 | evio_start (&wio); |
773 | |
826 | |
774 | struct ev_timer t[10000]; |
827 | struct ev_timer t[10000]; |
775 | |
828 | |
776 | #if 0 |
829 | #if 0 |
777 | int i; |
830 | int i; |
778 | for (i = 0; i < 10000; ++i) |
831 | for (i = 0; i < 10000; ++i) |
779 | { |
832 | { |
780 | struct ev_timer *w = t + i; |
833 | struct ev_timer *w = t + i; |
781 | evw_init (w, ocb, i); |
834 | evw_init (w, ocb, i); |
782 | evtimer_set_abs (w, drand48 (), 0.99775533); |
835 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
783 | evtimer_start (w); |
836 | evtimer_start (w); |
784 | if (drand48 () < 0.5) |
837 | if (drand48 () < 0.5) |
785 | evtimer_stop (w); |
838 | evtimer_stop (w); |
786 | } |
839 | } |
787 | #endif |
840 | #endif |
788 | |
841 | |
789 | struct ev_timer t1; |
842 | struct ev_timer t1; |
790 | evw_init (&t1, ocb, 0); |
843 | evtimer_init (&t1, ocb, 5, 10); |
791 | evtimer_set_abs (&t1, 5, 10); |
|
|
792 | evtimer_start (&t1); |
844 | evtimer_start (&t1); |
793 | |
845 | |
794 | struct ev_signal sig; |
846 | struct ev_signal sig; |
795 | evw_init (&sig, scb, 65535); |
|
|
796 | evsignal_set (&sig, SIGQUIT); |
847 | evsignal_init (&sig, scb, SIGQUIT); |
797 | evsignal_start (&sig); |
848 | evsignal_start (&sig); |
798 | |
849 | |
799 | struct ev_check cw; |
850 | struct ev_check cw; |
800 | evw_init (&cw, gcb, 0); |
851 | evcheck_init (&cw, gcb); |
801 | evcheck_start (&cw); |
852 | evcheck_start (&cw); |
802 | |
853 | |
803 | struct ev_idle iw; |
854 | struct ev_idle iw; |
804 | evw_init (&iw, gcb, 0); |
855 | evidle_init (&iw, gcb); |
805 | evidle_start (&iw); |
856 | evidle_start (&iw); |
806 | |
857 | |
807 | ev_loop (0); |
858 | ev_loop (0); |
808 | |
859 | |
809 | return 0; |
860 | return 0; |