… | |
… | |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
47 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
48 | #include <sys/time.h> |
48 | #include <sys/time.h> |
49 | #include <time.h> |
49 | #include <time.h> |
50 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
51 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
52 | # ifdef CLOCK_MONOTONIC |
|
|
53 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
54 | # endif |
|
|
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
58 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
59 | #endif |
59 | #endif |
60 | |
60 | |
61 | #ifndef EV_USE_EPOLL |
61 | #ifndef EV_USE_EPOLL |
62 | # define EV_USE_EPOLL 0 |
62 | # define EV_USE_EPOLL 0 |
63 | #endif |
63 | #endif |
64 | |
64 | |
|
|
65 | #ifndef EV_USE_REALTIME |
|
|
66 | # define EV_USE_REALTIME 1 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | /**/ |
|
|
70 | |
|
|
71 | #ifndef CLOCK_MONOTONIC |
|
|
72 | # undef EV_USE_MONOTONIC |
|
|
73 | # define EV_USE_MONOTONIC 0 |
|
|
74 | #endif |
|
|
75 | |
65 | #ifndef CLOCK_REALTIME |
76 | #ifndef CLOCK_REALTIME |
|
|
77 | # undef EV_USE_REALTIME |
66 | # define EV_USE_REALTIME 0 |
78 | # define EV_USE_REALTIME 0 |
67 | #endif |
79 | #endif |
68 | #ifndef EV_USE_REALTIME |
80 | |
69 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
81 | /**/ |
70 | #endif |
|
|
71 | |
82 | |
72 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
83 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
73 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
84 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
74 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
85 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
75 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
86 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
76 | |
87 | |
77 | #include "ev.h" |
88 | #include "ev.h" |
|
|
89 | |
|
|
90 | #if __GNUC__ >= 3 |
|
|
91 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
92 | # define inline inline |
|
|
93 | #else |
|
|
94 | # define expect(expr,value) (expr) |
|
|
95 | # define inline static |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
99 | #define expect_true(expr) expect ((expr) != 0, 1) |
78 | |
100 | |
79 | typedef struct ev_watcher *W; |
101 | typedef struct ev_watcher *W; |
80 | typedef struct ev_watcher_list *WL; |
102 | typedef struct ev_watcher_list *WL; |
81 | typedef struct ev_watcher_time *WT; |
103 | typedef struct ev_watcher_time *WT; |
82 | |
104 | |
83 | static ev_tstamp now, diff; /* monotonic clock */ |
105 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
84 | ev_tstamp ev_now; |
106 | ev_tstamp ev_now; |
85 | int ev_method; |
107 | int ev_method; |
86 | |
108 | |
87 | static int have_monotonic; /* runtime */ |
109 | static int have_monotonic; /* runtime */ |
88 | |
110 | |
… | |
… | |
108 | |
130 | |
109 | static ev_tstamp |
131 | static ev_tstamp |
110 | get_clock (void) |
132 | get_clock (void) |
111 | { |
133 | { |
112 | #if EV_USE_MONOTONIC |
134 | #if EV_USE_MONOTONIC |
113 | if (have_monotonic) |
135 | if (expect_true (have_monotonic)) |
114 | { |
136 | { |
115 | struct timespec ts; |
137 | struct timespec ts; |
116 | clock_gettime (CLOCK_MONOTONIC, &ts); |
138 | clock_gettime (CLOCK_MONOTONIC, &ts); |
117 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
139 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
118 | } |
140 | } |
… | |
… | |
122 | } |
144 | } |
123 | |
145 | |
124 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
146 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
125 | |
147 | |
126 | #define array_needsize(base,cur,cnt,init) \ |
148 | #define array_needsize(base,cur,cnt,init) \ |
127 | if ((cnt) > cur) \ |
149 | if (expect_false ((cnt) > cur)) \ |
128 | { \ |
150 | { \ |
129 | int newcnt = cur; \ |
151 | int newcnt = cur; \ |
130 | do \ |
152 | do \ |
131 | { \ |
153 | { \ |
132 | newcnt = array_roundsize (base, newcnt << 1); \ |
154 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
363 | |
385 | |
364 | static void |
386 | static void |
365 | sigcb (struct ev_io *iow, int revents) |
387 | sigcb (struct ev_io *iow, int revents) |
366 | { |
388 | { |
367 | struct ev_signal *w; |
389 | struct ev_signal *w; |
368 | int sig; |
390 | int signum; |
369 | |
391 | |
370 | read (sigpipe [0], &revents, 1); |
392 | read (sigpipe [0], &revents, 1); |
371 | gotsig = 0; |
393 | gotsig = 0; |
372 | |
394 | |
373 | for (sig = signalmax; sig--; ) |
395 | for (signum = signalmax; signum--; ) |
374 | if (signals [sig].gotsig) |
396 | if (signals [signum].gotsig) |
375 | { |
397 | { |
376 | signals [sig].gotsig = 0; |
398 | signals [signum].gotsig = 0; |
377 | |
399 | |
378 | for (w = signals [sig].head; w; w = w->next) |
400 | for (w = signals [signum].head; w; w = w->next) |
379 | event ((W)w, EV_SIGNAL); |
401 | event ((W)w, EV_SIGNAL); |
380 | } |
402 | } |
381 | } |
403 | } |
382 | |
404 | |
383 | static void |
405 | static void |
… | |
… | |
420 | struct ev_child *w; |
442 | struct ev_child *w; |
421 | int pid, status; |
443 | int pid, status; |
422 | |
444 | |
423 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
445 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
424 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
446 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
425 | if (w->pid == pid || w->pid == -1) |
447 | if (w->pid == pid || !w->pid) |
426 | { |
448 | { |
427 | w->status = status; |
449 | w->status = status; |
428 | event ((W)w, EV_CHILD); |
450 | event ((W)w, EV_CHILD); |
429 | } |
451 | } |
430 | } |
452 | } |
… | |
… | |
460 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
482 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
461 | have_monotonic = 1; |
483 | have_monotonic = 1; |
462 | } |
484 | } |
463 | #endif |
485 | #endif |
464 | |
486 | |
465 | ev_now = ev_time (); |
487 | ev_now = ev_time (); |
466 | now = get_clock (); |
488 | now = get_clock (); |
|
|
489 | now_floor = now; |
467 | diff = ev_now - now; |
490 | diff = ev_now - now; |
468 | |
491 | |
469 | if (pipe (sigpipe)) |
492 | if (pipe (sigpipe)) |
470 | return 0; |
493 | return 0; |
471 | |
494 | |
472 | ev_method = EVMETHOD_NONE; |
495 | ev_method = EVMETHOD_NONE; |
… | |
… | |
601 | } |
624 | } |
602 | } |
625 | } |
603 | } |
626 | } |
604 | } |
627 | } |
605 | |
628 | |
|
|
629 | static int |
|
|
630 | time_update_monotonic (void) |
|
|
631 | { |
|
|
632 | now = get_clock (); |
|
|
633 | |
|
|
634 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
635 | { |
|
|
636 | ev_now = now + diff; |
|
|
637 | return 0; |
|
|
638 | } |
|
|
639 | else |
|
|
640 | { |
|
|
641 | now_floor = now; |
|
|
642 | ev_now = ev_time (); |
|
|
643 | return 1; |
|
|
644 | } |
|
|
645 | } |
|
|
646 | |
606 | static void |
647 | static void |
607 | time_update (void) |
648 | time_update (void) |
608 | { |
649 | { |
609 | int i; |
650 | int i; |
610 | |
651 | |
611 | ev_now = ev_time (); |
652 | #if EV_USE_MONOTONIC |
612 | |
|
|
613 | if (have_monotonic) |
653 | if (expect_true (have_monotonic)) |
614 | { |
654 | { |
|
|
655 | if (time_update_monotonic ()) |
|
|
656 | { |
615 | ev_tstamp odiff = diff; |
657 | ev_tstamp odiff = diff; |
616 | |
658 | |
617 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
659 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
618 | { |
660 | { |
619 | now = get_clock (); |
|
|
620 | diff = ev_now - now; |
661 | diff = ev_now - now; |
621 | |
662 | |
622 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
663 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
623 | return; /* all is well */ |
664 | return; /* all is well */ |
624 | |
665 | |
625 | ev_now = ev_time (); |
666 | ev_now = ev_time (); |
|
|
667 | now = get_clock (); |
|
|
668 | now_floor = now; |
626 | } |
669 | } |
627 | |
670 | |
628 | periodics_reschedule (diff - odiff); |
671 | periodics_reschedule (diff - odiff); |
629 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
672 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
673 | } |
630 | } |
674 | } |
631 | else |
675 | else |
|
|
676 | #endif |
632 | { |
677 | { |
|
|
678 | ev_now = ev_time (); |
|
|
679 | |
633 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
680 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
634 | { |
681 | { |
635 | periodics_reschedule (ev_now - now); |
682 | periodics_reschedule (ev_now - now); |
636 | |
683 | |
637 | /* adjust timers. this is easy, as the offset is the same for all */ |
684 | /* adjust timers. this is easy, as the offset is the same for all */ |
638 | for (i = 0; i < timercnt; ++i) |
685 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
651 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
698 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
652 | |
699 | |
653 | do |
700 | do |
654 | { |
701 | { |
655 | /* queue check watchers (and execute them) */ |
702 | /* queue check watchers (and execute them) */ |
656 | if (preparecnt) |
703 | if (expect_false (preparecnt)) |
657 | { |
704 | { |
658 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
705 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
659 | call_pending (); |
706 | call_pending (); |
660 | } |
707 | } |
661 | |
708 | |
… | |
… | |
664 | |
711 | |
665 | /* calculate blocking time */ |
712 | /* calculate blocking time */ |
666 | |
713 | |
667 | /* we only need this for !monotonic clockor timers, but as we basically |
714 | /* we only need this for !monotonic clockor timers, but as we basically |
668 | always have timers, we just calculate it always */ |
715 | always have timers, we just calculate it always */ |
|
|
716 | #if EV_USE_MONOTONIC |
|
|
717 | if (expect_true (have_monotonic)) |
|
|
718 | time_update_monotonic (); |
|
|
719 | else |
|
|
720 | #endif |
|
|
721 | { |
669 | ev_now = ev_time (); |
722 | ev_now = ev_time (); |
|
|
723 | now = ev_now; |
|
|
724 | } |
670 | |
725 | |
671 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
726 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
672 | block = 0.; |
727 | block = 0.; |
673 | else |
728 | else |
674 | { |
729 | { |
675 | block = MAX_BLOCKTIME; |
730 | block = MAX_BLOCKTIME; |
676 | |
731 | |
677 | if (timercnt) |
732 | if (timercnt) |
678 | { |
733 | { |
679 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
734 | ev_tstamp to = timers [0]->at - now + method_fudge; |
680 | if (block > to) block = to; |
735 | if (block > to) block = to; |
681 | } |
736 | } |
682 | |
737 | |
683 | if (periodiccnt) |
738 | if (periodiccnt) |
684 | { |
739 | { |
… | |
… | |
763 | /*****************************************************************************/ |
818 | /*****************************************************************************/ |
764 | |
819 | |
765 | void |
820 | void |
766 | ev_io_start (struct ev_io *w) |
821 | ev_io_start (struct ev_io *w) |
767 | { |
822 | { |
|
|
823 | int fd = w->fd; |
|
|
824 | |
768 | if (ev_is_active (w)) |
825 | if (ev_is_active (w)) |
769 | return; |
826 | return; |
770 | |
|
|
771 | int fd = w->fd; |
|
|
772 | |
827 | |
773 | assert (("ev_io_start called with negative fd", fd >= 0)); |
828 | assert (("ev_io_start called with negative fd", fd >= 0)); |
774 | |
829 | |
775 | ev_start ((W)w, 1); |
830 | ev_start ((W)w, 1); |
776 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
831 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |