… | |
… | |
57 | # endif |
57 | # endif |
58 | # ifndef EV_USE_MONOTONIC |
58 | # ifndef EV_USE_MONOTONIC |
59 | # define EV_USE_MONOTONIC 1 |
59 | # define EV_USE_MONOTONIC 1 |
60 | # endif |
60 | # endif |
61 | # endif |
61 | # endif |
|
|
62 | # elif !defined(EV_USE_CLOCK_SYSCALL) |
|
|
63 | # define EV_USE_CLOCK_SYSCALL 0 |
62 | # endif |
64 | # endif |
63 | |
65 | |
64 | # if HAVE_CLOCK_GETTIME |
66 | # if HAVE_CLOCK_GETTIME |
65 | # ifndef EV_USE_MONOTONIC |
67 | # ifndef EV_USE_MONOTONIC |
66 | # define EV_USE_MONOTONIC 1 |
68 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
282 | |
284 | |
283 | #ifndef EV_HEAP_CACHE_AT |
285 | #ifndef EV_HEAP_CACHE_AT |
284 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
286 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
285 | #endif |
287 | #endif |
286 | |
288 | |
|
|
289 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
290 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
291 | #if EV_USE_CLOCK_SYSCALL |
|
|
292 | # include <syscall.h> |
|
|
293 | # ifdef SYS_clock_gettime |
|
|
294 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
295 | # undef EV_USE_MONOTONIC |
|
|
296 | # define EV_USE_MONOTONIC 1 |
|
|
297 | # else |
|
|
298 | # undef EV_USE_CLOCK_SYSCALL |
|
|
299 | # define EV_USE_CLOCK_SYSCALL 0 |
|
|
300 | # endif |
|
|
301 | #endif |
|
|
302 | |
287 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
303 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
288 | |
304 | |
289 | #ifndef CLOCK_MONOTONIC |
305 | #ifndef CLOCK_MONOTONIC |
290 | # undef EV_USE_MONOTONIC |
306 | # undef EV_USE_MONOTONIC |
291 | # define EV_USE_MONOTONIC 0 |
307 | # define EV_USE_MONOTONIC 0 |
… | |
… | |
320 | |
336 | |
321 | #if EV_SELECT_IS_WINSOCKET |
337 | #if EV_SELECT_IS_WINSOCKET |
322 | # include <winsock.h> |
338 | # include <winsock.h> |
323 | #endif |
339 | #endif |
324 | |
340 | |
325 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
326 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
327 | #if EV_USE_CLOCK_SYSCALL |
|
|
328 | # include <syscall.h> |
|
|
329 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
330 | # undef EV_USE_MONOTONIC |
|
|
331 | # define EV_USE_MONOTONIC 1 |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #if EV_USE_EVENTFD |
341 | #if EV_USE_EVENTFD |
335 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
342 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
336 | # include <stdint.h> |
343 | # include <stdint.h> |
337 | # ifdef __cplusplus |
344 | # ifdef __cplusplus |
338 | extern "C" { |
345 | extern "C" { |
… | |
… | |
478 | #define ev_malloc(size) ev_realloc (0, (size)) |
485 | #define ev_malloc(size) ev_realloc (0, (size)) |
479 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
486 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
480 | |
487 | |
481 | /*****************************************************************************/ |
488 | /*****************************************************************************/ |
482 | |
489 | |
|
|
490 | /* file descriptor info structure */ |
483 | typedef struct |
491 | typedef struct |
484 | { |
492 | { |
485 | WL head; |
493 | WL head; |
486 | unsigned char events; |
494 | unsigned char events; /* the events watched for */ |
487 | unsigned char reify; |
495 | unsigned char reify; /* flag set when this ANFD needs reification */ |
488 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
496 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
489 | unsigned char unused; |
497 | unsigned char unused; |
490 | #if EV_USE_EPOLL |
498 | #if EV_USE_EPOLL |
491 | unsigned int egen; /* generation counter to counter epoll bugs */ |
499 | unsigned int egen; /* generation counter to counter epoll bugs */ |
492 | #endif |
500 | #endif |
493 | #if EV_SELECT_IS_WINSOCKET |
501 | #if EV_SELECT_IS_WINSOCKET |
494 | SOCKET handle; |
502 | SOCKET handle; |
495 | #endif |
503 | #endif |
496 | } ANFD; |
504 | } ANFD; |
497 | |
505 | |
|
|
506 | /* stores the pending event set for a given watcher */ |
498 | typedef struct |
507 | typedef struct |
499 | { |
508 | { |
500 | W w; |
509 | W w; |
501 | int events; |
510 | int events; /* the pending event set for the given watcher */ |
502 | } ANPENDING; |
511 | } ANPENDING; |
503 | |
512 | |
504 | #if EV_USE_INOTIFY |
513 | #if EV_USE_INOTIFY |
505 | /* hash table entry per inotify-id */ |
514 | /* hash table entry per inotify-id */ |
506 | typedef struct |
515 | typedef struct |
… | |
… | |
509 | } ANFS; |
518 | } ANFS; |
510 | #endif |
519 | #endif |
511 | |
520 | |
512 | /* Heap Entry */ |
521 | /* Heap Entry */ |
513 | #if EV_HEAP_CACHE_AT |
522 | #if EV_HEAP_CACHE_AT |
|
|
523 | /* a heap element */ |
514 | typedef struct { |
524 | typedef struct { |
515 | ev_tstamp at; |
525 | ev_tstamp at; |
516 | WT w; |
526 | WT w; |
517 | } ANHE; |
527 | } ANHE; |
518 | |
528 | |
519 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
529 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
520 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
530 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
521 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
531 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
522 | #else |
532 | #else |
|
|
533 | /* a heap element */ |
523 | typedef WT ANHE; |
534 | typedef WT ANHE; |
524 | |
535 | |
525 | #define ANHE_w(he) (he) |
536 | #define ANHE_w(he) (he) |
526 | #define ANHE_at(he) (he)->at |
537 | #define ANHE_at(he) (he)->at |
527 | #define ANHE_at_cache(he) |
538 | #define ANHE_at_cache(he) |
… | |
… | |
553 | |
564 | |
554 | #endif |
565 | #endif |
555 | |
566 | |
556 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
557 | |
568 | |
|
|
569 | #ifndef EV_HAVE_EV_TIME |
558 | ev_tstamp |
570 | ev_tstamp |
559 | ev_time (void) |
571 | ev_time (void) |
560 | { |
572 | { |
561 | #if EV_USE_REALTIME |
573 | #if EV_USE_REALTIME |
562 | if (expect_true (have_realtime)) |
574 | if (expect_true (have_realtime)) |
… | |
… | |
569 | |
581 | |
570 | struct timeval tv; |
582 | struct timeval tv; |
571 | gettimeofday (&tv, 0); |
583 | gettimeofday (&tv, 0); |
572 | return tv.tv_sec + tv.tv_usec * 1e-6; |
584 | return tv.tv_sec + tv.tv_usec * 1e-6; |
573 | } |
585 | } |
|
|
586 | #endif |
574 | |
587 | |
575 | ev_tstamp inline_size |
588 | inline_size ev_tstamp |
576 | get_clock (void) |
589 | get_clock (void) |
577 | { |
590 | { |
578 | #if EV_USE_MONOTONIC |
591 | #if EV_USE_MONOTONIC |
579 | if (expect_true (have_monotonic)) |
592 | if (expect_true (have_monotonic)) |
580 | { |
593 | { |
… | |
… | |
625 | |
638 | |
626 | /*****************************************************************************/ |
639 | /*****************************************************************************/ |
627 | |
640 | |
628 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
641 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
629 | |
642 | |
630 | int inline_size |
643 | /* find a suitable new size for the given array, */ |
|
|
644 | /* hopefully by rounding to a ncie-to-malloc size */ |
|
|
645 | inline_size int |
631 | array_nextsize (int elem, int cur, int cnt) |
646 | array_nextsize (int elem, int cur, int cnt) |
632 | { |
647 | { |
633 | int ncur = cur + 1; |
648 | int ncur = cur + 1; |
634 | |
649 | |
635 | do |
650 | do |
… | |
… | |
676 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
691 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
677 | } |
692 | } |
678 | #endif |
693 | #endif |
679 | |
694 | |
680 | #define array_free(stem, idx) \ |
695 | #define array_free(stem, idx) \ |
681 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
696 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
682 | |
697 | |
683 | /*****************************************************************************/ |
698 | /*****************************************************************************/ |
|
|
699 | |
|
|
700 | /* dummy callback for pending events */ |
|
|
701 | static void noinline |
|
|
702 | pendingcb (EV_P_ ev_prepare *w, int revents) |
|
|
703 | { |
|
|
704 | } |
684 | |
705 | |
685 | void noinline |
706 | void noinline |
686 | ev_feed_event (EV_P_ void *w, int revents) |
707 | ev_feed_event (EV_P_ void *w, int revents) |
687 | { |
708 | { |
688 | W w_ = (W)w; |
709 | W w_ = (W)w; |
… | |
… | |
697 | pendings [pri][w_->pending - 1].w = w_; |
718 | pendings [pri][w_->pending - 1].w = w_; |
698 | pendings [pri][w_->pending - 1].events = revents; |
719 | pendings [pri][w_->pending - 1].events = revents; |
699 | } |
720 | } |
700 | } |
721 | } |
701 | |
722 | |
702 | void inline_speed |
723 | inline_speed void |
|
|
724 | feed_reverse (EV_P_ W w) |
|
|
725 | { |
|
|
726 | array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2); |
|
|
727 | rfeeds [rfeedcnt++] = w; |
|
|
728 | } |
|
|
729 | |
|
|
730 | inline_size void |
|
|
731 | feed_reverse_done (EV_P_ int revents) |
|
|
732 | { |
|
|
733 | do |
|
|
734 | ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents); |
|
|
735 | while (rfeedcnt); |
|
|
736 | } |
|
|
737 | |
|
|
738 | inline_speed void |
703 | queue_events (EV_P_ W *events, int eventcnt, int type) |
739 | queue_events (EV_P_ W *events, int eventcnt, int type) |
704 | { |
740 | { |
705 | int i; |
741 | int i; |
706 | |
742 | |
707 | for (i = 0; i < eventcnt; ++i) |
743 | for (i = 0; i < eventcnt; ++i) |
708 | ev_feed_event (EV_A_ events [i], type); |
744 | ev_feed_event (EV_A_ events [i], type); |
709 | } |
745 | } |
710 | |
746 | |
711 | /*****************************************************************************/ |
747 | /*****************************************************************************/ |
712 | |
748 | |
713 | void inline_speed |
749 | inline_speed void |
714 | fd_event (EV_P_ int fd, int revents) |
750 | fd_event (EV_P_ int fd, int revents) |
715 | { |
751 | { |
716 | ANFD *anfd = anfds + fd; |
752 | ANFD *anfd = anfds + fd; |
717 | ev_io *w; |
753 | ev_io *w; |
718 | |
754 | |
… | |
… | |
730 | { |
766 | { |
731 | if (fd >= 0 && fd < anfdmax) |
767 | if (fd >= 0 && fd < anfdmax) |
732 | fd_event (EV_A_ fd, revents); |
768 | fd_event (EV_A_ fd, revents); |
733 | } |
769 | } |
734 | |
770 | |
735 | void inline_size |
771 | /* make sure the external fd watch events are in-sync */ |
|
|
772 | /* with the kernel/libev internal state */ |
|
|
773 | inline_size void |
736 | fd_reify (EV_P) |
774 | fd_reify (EV_P) |
737 | { |
775 | { |
738 | int i; |
776 | int i; |
739 | |
777 | |
740 | for (i = 0; i < fdchangecnt; ++i) |
778 | for (i = 0; i < fdchangecnt; ++i) |
… | |
… | |
766 | unsigned char o_reify = anfd->reify; |
804 | unsigned char o_reify = anfd->reify; |
767 | |
805 | |
768 | anfd->reify = 0; |
806 | anfd->reify = 0; |
769 | anfd->events = events; |
807 | anfd->events = events; |
770 | |
808 | |
771 | if (o_events != events || o_reify & EV_IOFDSET) |
809 | if (o_events != events || o_reify & EV__IOFDSET) |
772 | backend_modify (EV_A_ fd, o_events, events); |
810 | backend_modify (EV_A_ fd, o_events, events); |
773 | } |
811 | } |
774 | } |
812 | } |
775 | |
813 | |
776 | fdchangecnt = 0; |
814 | fdchangecnt = 0; |
777 | } |
815 | } |
778 | |
816 | |
779 | void inline_size |
817 | /* something about the given fd changed */ |
|
|
818 | inline_size void |
780 | fd_change (EV_P_ int fd, int flags) |
819 | fd_change (EV_P_ int fd, int flags) |
781 | { |
820 | { |
782 | unsigned char reify = anfds [fd].reify; |
821 | unsigned char reify = anfds [fd].reify; |
783 | anfds [fd].reify |= flags; |
822 | anfds [fd].reify |= flags; |
784 | |
823 | |
… | |
… | |
788 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
827 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
789 | fdchanges [fdchangecnt - 1] = fd; |
828 | fdchanges [fdchangecnt - 1] = fd; |
790 | } |
829 | } |
791 | } |
830 | } |
792 | |
831 | |
793 | void inline_speed |
832 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
|
|
833 | inline_speed void |
794 | fd_kill (EV_P_ int fd) |
834 | fd_kill (EV_P_ int fd) |
795 | { |
835 | { |
796 | ev_io *w; |
836 | ev_io *w; |
797 | |
837 | |
798 | while ((w = (ev_io *)anfds [fd].head)) |
838 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
800 | ev_io_stop (EV_A_ w); |
840 | ev_io_stop (EV_A_ w); |
801 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
841 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
802 | } |
842 | } |
803 | } |
843 | } |
804 | |
844 | |
805 | int inline_size |
845 | /* check whether the given fd is atcually valid, for error recovery */ |
|
|
846 | inline_size int |
806 | fd_valid (int fd) |
847 | fd_valid (int fd) |
807 | { |
848 | { |
808 | #ifdef _WIN32 |
849 | #ifdef _WIN32 |
809 | return _get_osfhandle (fd) != -1; |
850 | return _get_osfhandle (fd) != -1; |
810 | #else |
851 | #else |
… | |
… | |
847 | for (fd = 0; fd < anfdmax; ++fd) |
888 | for (fd = 0; fd < anfdmax; ++fd) |
848 | if (anfds [fd].events) |
889 | if (anfds [fd].events) |
849 | { |
890 | { |
850 | anfds [fd].events = 0; |
891 | anfds [fd].events = 0; |
851 | anfds [fd].emask = 0; |
892 | anfds [fd].emask = 0; |
852 | fd_change (EV_A_ fd, EV_IOFDSET | 1); |
893 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
853 | } |
894 | } |
854 | } |
895 | } |
855 | |
896 | |
856 | /*****************************************************************************/ |
897 | /*****************************************************************************/ |
857 | |
898 | |
… | |
… | |
873 | #define HEAP0 (DHEAP - 1) /* index of first element in heap */ |
914 | #define HEAP0 (DHEAP - 1) /* index of first element in heap */ |
874 | #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) |
915 | #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) |
875 | #define UPHEAP_DONE(p,k) ((p) == (k)) |
916 | #define UPHEAP_DONE(p,k) ((p) == (k)) |
876 | |
917 | |
877 | /* away from the root */ |
918 | /* away from the root */ |
878 | void inline_speed |
919 | inline_speed void |
879 | downheap (ANHE *heap, int N, int k) |
920 | downheap (ANHE *heap, int N, int k) |
880 | { |
921 | { |
881 | ANHE he = heap [k]; |
922 | ANHE he = heap [k]; |
882 | ANHE *E = heap + N + HEAP0; |
923 | ANHE *E = heap + N + HEAP0; |
883 | |
924 | |
… | |
… | |
923 | #define HEAP0 1 |
964 | #define HEAP0 1 |
924 | #define HPARENT(k) ((k) >> 1) |
965 | #define HPARENT(k) ((k) >> 1) |
925 | #define UPHEAP_DONE(p,k) (!(p)) |
966 | #define UPHEAP_DONE(p,k) (!(p)) |
926 | |
967 | |
927 | /* away from the root */ |
968 | /* away from the root */ |
928 | void inline_speed |
969 | inline_speed void |
929 | downheap (ANHE *heap, int N, int k) |
970 | downheap (ANHE *heap, int N, int k) |
930 | { |
971 | { |
931 | ANHE he = heap [k]; |
972 | ANHE he = heap [k]; |
932 | |
973 | |
933 | for (;;) |
974 | for (;;) |
… | |
… | |
953 | ev_active (ANHE_w (he)) = k; |
994 | ev_active (ANHE_w (he)) = k; |
954 | } |
995 | } |
955 | #endif |
996 | #endif |
956 | |
997 | |
957 | /* towards the root */ |
998 | /* towards the root */ |
958 | void inline_speed |
999 | inline_speed void |
959 | upheap (ANHE *heap, int k) |
1000 | upheap (ANHE *heap, int k) |
960 | { |
1001 | { |
961 | ANHE he = heap [k]; |
1002 | ANHE he = heap [k]; |
962 | |
1003 | |
963 | for (;;) |
1004 | for (;;) |
… | |
… | |
974 | |
1015 | |
975 | heap [k] = he; |
1016 | heap [k] = he; |
976 | ev_active (ANHE_w (he)) = k; |
1017 | ev_active (ANHE_w (he)) = k; |
977 | } |
1018 | } |
978 | |
1019 | |
979 | void inline_size |
1020 | /* move an element suitably so it is in a correct place */ |
|
|
1021 | inline_size void |
980 | adjustheap (ANHE *heap, int N, int k) |
1022 | adjustheap (ANHE *heap, int N, int k) |
981 | { |
1023 | { |
982 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
1024 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
983 | upheap (heap, k); |
1025 | upheap (heap, k); |
984 | else |
1026 | else |
985 | downheap (heap, N, k); |
1027 | downheap (heap, N, k); |
986 | } |
1028 | } |
987 | |
1029 | |
988 | /* rebuild the heap: this function is used only once and executed rarely */ |
1030 | /* rebuild the heap: this function is used only once and executed rarely */ |
989 | void inline_size |
1031 | inline_size void |
990 | reheap (ANHE *heap, int N) |
1032 | reheap (ANHE *heap, int N) |
991 | { |
1033 | { |
992 | int i; |
1034 | int i; |
993 | |
1035 | |
994 | /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ |
1036 | /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ |
… | |
… | |
997 | upheap (heap, i + HEAP0); |
1039 | upheap (heap, i + HEAP0); |
998 | } |
1040 | } |
999 | |
1041 | |
1000 | /*****************************************************************************/ |
1042 | /*****************************************************************************/ |
1001 | |
1043 | |
|
|
1044 | /* associate signal watchers to a signal signal */ |
1002 | typedef struct |
1045 | typedef struct |
1003 | { |
1046 | { |
1004 | WL head; |
1047 | WL head; |
1005 | EV_ATOMIC_T gotsig; |
1048 | EV_ATOMIC_T gotsig; |
1006 | } ANSIG; |
1049 | } ANSIG; |
… | |
… | |
1010 | |
1053 | |
1011 | static EV_ATOMIC_T gotsig; |
1054 | static EV_ATOMIC_T gotsig; |
1012 | |
1055 | |
1013 | /*****************************************************************************/ |
1056 | /*****************************************************************************/ |
1014 | |
1057 | |
1015 | void inline_speed |
1058 | /* used to prepare libev internal fd's */ |
|
|
1059 | /* this is not fork-safe */ |
|
|
1060 | inline_speed void |
1016 | fd_intern (int fd) |
1061 | fd_intern (int fd) |
1017 | { |
1062 | { |
1018 | #ifdef _WIN32 |
1063 | #ifdef _WIN32 |
1019 | unsigned long arg = 1; |
1064 | unsigned long arg = 1; |
1020 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
1065 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
1025 | } |
1070 | } |
1026 | |
1071 | |
1027 | static void noinline |
1072 | static void noinline |
1028 | evpipe_init (EV_P) |
1073 | evpipe_init (EV_P) |
1029 | { |
1074 | { |
1030 | if (!ev_is_active (&pipeev)) |
1075 | if (!ev_is_active (&pipe_w)) |
1031 | { |
1076 | { |
1032 | #if EV_USE_EVENTFD |
1077 | #if EV_USE_EVENTFD |
1033 | if ((evfd = eventfd (0, 0)) >= 0) |
1078 | if ((evfd = eventfd (0, 0)) >= 0) |
1034 | { |
1079 | { |
1035 | evpipe [0] = -1; |
1080 | evpipe [0] = -1; |
1036 | fd_intern (evfd); |
1081 | fd_intern (evfd); |
1037 | ev_io_set (&pipeev, evfd, EV_READ); |
1082 | ev_io_set (&pipe_w, evfd, EV_READ); |
1038 | } |
1083 | } |
1039 | else |
1084 | else |
1040 | #endif |
1085 | #endif |
1041 | { |
1086 | { |
1042 | while (pipe (evpipe)) |
1087 | while (pipe (evpipe)) |
1043 | ev_syserr ("(libev) error creating signal/async pipe"); |
1088 | ev_syserr ("(libev) error creating signal/async pipe"); |
1044 | |
1089 | |
1045 | fd_intern (evpipe [0]); |
1090 | fd_intern (evpipe [0]); |
1046 | fd_intern (evpipe [1]); |
1091 | fd_intern (evpipe [1]); |
1047 | ev_io_set (&pipeev, evpipe [0], EV_READ); |
1092 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
1048 | } |
1093 | } |
1049 | |
1094 | |
1050 | ev_io_start (EV_A_ &pipeev); |
1095 | ev_io_start (EV_A_ &pipe_w); |
1051 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1096 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1052 | } |
1097 | } |
1053 | } |
1098 | } |
1054 | |
1099 | |
1055 | void inline_size |
1100 | inline_size void |
1056 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1101 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1057 | { |
1102 | { |
1058 | if (!*flag) |
1103 | if (!*flag) |
1059 | { |
1104 | { |
1060 | int old_errno = errno; /* save errno because write might clobber it */ |
1105 | int old_errno = errno; /* save errno because write might clobber it */ |
… | |
… | |
1073 | |
1118 | |
1074 | errno = old_errno; |
1119 | errno = old_errno; |
1075 | } |
1120 | } |
1076 | } |
1121 | } |
1077 | |
1122 | |
|
|
1123 | /* called whenever the libev signal pipe */ |
|
|
1124 | /* got some events (signal, async) */ |
1078 | static void |
1125 | static void |
1079 | pipecb (EV_P_ ev_io *iow, int revents) |
1126 | pipecb (EV_P_ ev_io *iow, int revents) |
1080 | { |
1127 | { |
1081 | #if EV_USE_EVENTFD |
1128 | #if EV_USE_EVENTFD |
1082 | if (evfd >= 0) |
1129 | if (evfd >= 0) |
… | |
… | |
1164 | |
1211 | |
1165 | #ifndef WIFCONTINUED |
1212 | #ifndef WIFCONTINUED |
1166 | # define WIFCONTINUED(status) 0 |
1213 | # define WIFCONTINUED(status) 0 |
1167 | #endif |
1214 | #endif |
1168 | |
1215 | |
1169 | void inline_speed |
1216 | /* handle a single child status event */ |
|
|
1217 | inline_speed void |
1170 | child_reap (EV_P_ int chain, int pid, int status) |
1218 | child_reap (EV_P_ int chain, int pid, int status) |
1171 | { |
1219 | { |
1172 | ev_child *w; |
1220 | ev_child *w; |
1173 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1221 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1174 | |
1222 | |
… | |
… | |
1187 | |
1235 | |
1188 | #ifndef WCONTINUED |
1236 | #ifndef WCONTINUED |
1189 | # define WCONTINUED 0 |
1237 | # define WCONTINUED 0 |
1190 | #endif |
1238 | #endif |
1191 | |
1239 | |
|
|
1240 | /* called on sigchld etc., calls waitpid */ |
1192 | static void |
1241 | static void |
1193 | childcb (EV_P_ ev_signal *sw, int revents) |
1242 | childcb (EV_P_ ev_signal *sw, int revents) |
1194 | { |
1243 | { |
1195 | int pid, status; |
1244 | int pid, status; |
1196 | |
1245 | |
… | |
… | |
1321 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1370 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1322 | { |
1371 | { |
1323 | timeout_blocktime = interval; |
1372 | timeout_blocktime = interval; |
1324 | } |
1373 | } |
1325 | |
1374 | |
|
|
1375 | /* initialise a loop structure, must be zero-initialised */ |
1326 | static void noinline |
1376 | static void noinline |
1327 | loop_init (EV_P_ unsigned int flags) |
1377 | loop_init (EV_P_ unsigned int flags) |
1328 | { |
1378 | { |
1329 | if (!backend) |
1379 | if (!backend) |
1330 | { |
1380 | { |
… | |
… | |
1390 | #endif |
1440 | #endif |
1391 | #if EV_USE_SELECT |
1441 | #if EV_USE_SELECT |
1392 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1442 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1393 | #endif |
1443 | #endif |
1394 | |
1444 | |
|
|
1445 | ev_prepare_init (&pending_w, pendingcb); |
|
|
1446 | |
1395 | ev_init (&pipeev, pipecb); |
1447 | ev_init (&pipe_w, pipecb); |
1396 | ev_set_priority (&pipeev, EV_MAXPRI); |
1448 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1397 | } |
1449 | } |
1398 | } |
1450 | } |
1399 | |
1451 | |
|
|
1452 | /* free up a loop structure */ |
1400 | static void noinline |
1453 | static void noinline |
1401 | loop_destroy (EV_P) |
1454 | loop_destroy (EV_P) |
1402 | { |
1455 | { |
1403 | int i; |
1456 | int i; |
1404 | |
1457 | |
1405 | if (ev_is_active (&pipeev)) |
1458 | if (ev_is_active (&pipe_w)) |
1406 | { |
1459 | { |
1407 | ev_ref (EV_A); /* signal watcher */ |
1460 | ev_ref (EV_A); /* signal watcher */ |
1408 | ev_io_stop (EV_A_ &pipeev); |
1461 | ev_io_stop (EV_A_ &pipe_w); |
1409 | |
1462 | |
1410 | #if EV_USE_EVENTFD |
1463 | #if EV_USE_EVENTFD |
1411 | if (evfd >= 0) |
1464 | if (evfd >= 0) |
1412 | close (evfd); |
1465 | close (evfd); |
1413 | #endif |
1466 | #endif |
… | |
… | |
1452 | } |
1505 | } |
1453 | |
1506 | |
1454 | ev_free (anfds); anfdmax = 0; |
1507 | ev_free (anfds); anfdmax = 0; |
1455 | |
1508 | |
1456 | /* have to use the microsoft-never-gets-it-right macro */ |
1509 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
1510 | array_free (rfeed, EMPTY); |
1457 | array_free (fdchange, EMPTY); |
1511 | array_free (fdchange, EMPTY); |
1458 | array_free (timer, EMPTY); |
1512 | array_free (timer, EMPTY); |
1459 | #if EV_PERIODIC_ENABLE |
1513 | #if EV_PERIODIC_ENABLE |
1460 | array_free (periodic, EMPTY); |
1514 | array_free (periodic, EMPTY); |
1461 | #endif |
1515 | #endif |
… | |
… | |
1470 | |
1524 | |
1471 | backend = 0; |
1525 | backend = 0; |
1472 | } |
1526 | } |
1473 | |
1527 | |
1474 | #if EV_USE_INOTIFY |
1528 | #if EV_USE_INOTIFY |
1475 | void inline_size infy_fork (EV_P); |
1529 | inline_size void infy_fork (EV_P); |
1476 | #endif |
1530 | #endif |
1477 | |
1531 | |
1478 | void inline_size |
1532 | inline_size void |
1479 | loop_fork (EV_P) |
1533 | loop_fork (EV_P) |
1480 | { |
1534 | { |
1481 | #if EV_USE_PORT |
1535 | #if EV_USE_PORT |
1482 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
1536 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
1483 | #endif |
1537 | #endif |
… | |
… | |
1489 | #endif |
1543 | #endif |
1490 | #if EV_USE_INOTIFY |
1544 | #if EV_USE_INOTIFY |
1491 | infy_fork (EV_A); |
1545 | infy_fork (EV_A); |
1492 | #endif |
1546 | #endif |
1493 | |
1547 | |
1494 | if (ev_is_active (&pipeev)) |
1548 | if (ev_is_active (&pipe_w)) |
1495 | { |
1549 | { |
1496 | /* this "locks" the handlers against writing to the pipe */ |
1550 | /* this "locks" the handlers against writing to the pipe */ |
1497 | /* while we modify the fd vars */ |
1551 | /* while we modify the fd vars */ |
1498 | gotsig = 1; |
1552 | gotsig = 1; |
1499 | #if EV_ASYNC_ENABLE |
1553 | #if EV_ASYNC_ENABLE |
1500 | gotasync = 1; |
1554 | gotasync = 1; |
1501 | #endif |
1555 | #endif |
1502 | |
1556 | |
1503 | ev_ref (EV_A); |
1557 | ev_ref (EV_A); |
1504 | ev_io_stop (EV_A_ &pipeev); |
1558 | ev_io_stop (EV_A_ &pipe_w); |
1505 | |
1559 | |
1506 | #if EV_USE_EVENTFD |
1560 | #if EV_USE_EVENTFD |
1507 | if (evfd >= 0) |
1561 | if (evfd >= 0) |
1508 | close (evfd); |
1562 | close (evfd); |
1509 | #endif |
1563 | #endif |
… | |
… | |
1514 | close (evpipe [1]); |
1568 | close (evpipe [1]); |
1515 | } |
1569 | } |
1516 | |
1570 | |
1517 | evpipe_init (EV_A); |
1571 | evpipe_init (EV_A); |
1518 | /* now iterate over everything, in case we missed something */ |
1572 | /* now iterate over everything, in case we missed something */ |
1519 | pipecb (EV_A_ &pipeev, EV_READ); |
1573 | pipecb (EV_A_ &pipe_w, EV_READ); |
1520 | } |
1574 | } |
1521 | |
1575 | |
1522 | postfork = 0; |
1576 | postfork = 0; |
1523 | } |
1577 | } |
1524 | |
1578 | |
… | |
… | |
1720 | ev_invoke (EV_P_ void *w, int revents) |
1774 | ev_invoke (EV_P_ void *w, int revents) |
1721 | { |
1775 | { |
1722 | EV_CB_INVOKE ((W)w, revents); |
1776 | EV_CB_INVOKE ((W)w, revents); |
1723 | } |
1777 | } |
1724 | |
1778 | |
1725 | void inline_speed |
1779 | inline_speed void |
1726 | call_pending (EV_P) |
1780 | call_pending (EV_P) |
1727 | { |
1781 | { |
1728 | int pri; |
1782 | int pri; |
1729 | |
1783 | |
1730 | for (pri = NUMPRI; pri--; ) |
1784 | for (pri = NUMPRI; pri--; ) |
1731 | while (pendingcnt [pri]) |
1785 | while (pendingcnt [pri]) |
1732 | { |
1786 | { |
1733 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1787 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1734 | |
1788 | |
1735 | if (expect_true (p->w)) |
|
|
1736 | { |
|
|
1737 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
1789 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
1790 | /* ^ this is no longer true, as pending_w could be here */ |
1738 | |
1791 | |
1739 | p->w->pending = 0; |
1792 | p->w->pending = 0; |
1740 | EV_CB_INVOKE (p->w, p->events); |
1793 | EV_CB_INVOKE (p->w, p->events); |
1741 | EV_FREQUENT_CHECK; |
1794 | EV_FREQUENT_CHECK; |
1742 | } |
|
|
1743 | } |
1795 | } |
1744 | } |
1796 | } |
1745 | |
1797 | |
1746 | #if EV_IDLE_ENABLE |
1798 | #if EV_IDLE_ENABLE |
1747 | void inline_size |
1799 | /* make idle watchers pending. this handles the "call-idle */ |
|
|
1800 | /* only when higher priorities are idle" logic */ |
|
|
1801 | inline_size void |
1748 | idle_reify (EV_P) |
1802 | idle_reify (EV_P) |
1749 | { |
1803 | { |
1750 | if (expect_false (idleall)) |
1804 | if (expect_false (idleall)) |
1751 | { |
1805 | { |
1752 | int pri; |
1806 | int pri; |
… | |
… | |
1764 | } |
1818 | } |
1765 | } |
1819 | } |
1766 | } |
1820 | } |
1767 | #endif |
1821 | #endif |
1768 | |
1822 | |
1769 | void inline_size |
1823 | /* make timers pending */ |
|
|
1824 | inline_size void |
1770 | timers_reify (EV_P) |
1825 | timers_reify (EV_P) |
1771 | { |
1826 | { |
1772 | EV_FREQUENT_CHECK; |
1827 | EV_FREQUENT_CHECK; |
1773 | |
1828 | |
1774 | while (timercnt && ANHE_at (timers [HEAP0]) < mn_now) |
1829 | if (timercnt && ANHE_at (timers [HEAP0]) < mn_now) |
1775 | { |
1830 | { |
1776 | ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); |
1831 | do |
1777 | |
|
|
1778 | /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1779 | |
|
|
1780 | /* first reschedule or stop timer */ |
|
|
1781 | if (w->repeat) |
|
|
1782 | { |
1832 | { |
|
|
1833 | ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); |
|
|
1834 | |
|
|
1835 | /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1836 | |
|
|
1837 | /* first reschedule or stop timer */ |
|
|
1838 | if (w->repeat) |
|
|
1839 | { |
1783 | ev_at (w) += w->repeat; |
1840 | ev_at (w) += w->repeat; |
1784 | if (ev_at (w) < mn_now) |
1841 | if (ev_at (w) < mn_now) |
1785 | ev_at (w) = mn_now; |
1842 | ev_at (w) = mn_now; |
1786 | |
1843 | |
1787 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1844 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1788 | |
1845 | |
1789 | ANHE_at_cache (timers [HEAP0]); |
1846 | ANHE_at_cache (timers [HEAP0]); |
1790 | downheap (timers, timercnt, HEAP0); |
1847 | downheap (timers, timercnt, HEAP0); |
|
|
1848 | } |
|
|
1849 | else |
|
|
1850 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1851 | |
|
|
1852 | EV_FREQUENT_CHECK; |
|
|
1853 | feed_reverse (EV_A_ (W)w); |
1791 | } |
1854 | } |
1792 | else |
1855 | while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); |
1793 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1794 | |
1856 | |
1795 | EV_FREQUENT_CHECK; |
|
|
1796 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1857 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1797 | } |
1858 | } |
1798 | } |
1859 | } |
1799 | |
1860 | |
1800 | #if EV_PERIODIC_ENABLE |
1861 | #if EV_PERIODIC_ENABLE |
1801 | void inline_size |
1862 | /* make periodics pending */ |
|
|
1863 | inline_size void |
1802 | periodics_reify (EV_P) |
1864 | periodics_reify (EV_P) |
1803 | { |
1865 | { |
1804 | EV_FREQUENT_CHECK; |
1866 | EV_FREQUENT_CHECK; |
1805 | |
1867 | |
1806 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1868 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1807 | { |
1869 | { |
1808 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1870 | int feed_count = 0; |
1809 | |
1871 | |
1810 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1872 | do |
1811 | |
|
|
1812 | /* first reschedule or stop timer */ |
|
|
1813 | if (w->reschedule_cb) |
|
|
1814 | { |
1873 | { |
|
|
1874 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
|
|
1875 | |
|
|
1876 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
|
|
1877 | |
|
|
1878 | /* first reschedule or stop timer */ |
|
|
1879 | if (w->reschedule_cb) |
|
|
1880 | { |
1815 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1881 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1816 | |
1882 | |
1817 | assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1883 | assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1818 | |
1884 | |
1819 | ANHE_at_cache (periodics [HEAP0]); |
1885 | ANHE_at_cache (periodics [HEAP0]); |
1820 | downheap (periodics, periodiccnt, HEAP0); |
1886 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1887 | } |
|
|
1888 | else if (w->interval) |
|
|
1889 | { |
|
|
1890 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1891 | /* if next trigger time is not sufficiently in the future, put it there */ |
|
|
1892 | /* this might happen because of floating point inexactness */ |
|
|
1893 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
|
|
1894 | { |
|
|
1895 | ev_at (w) += w->interval; |
|
|
1896 | |
|
|
1897 | /* if interval is unreasonably low we might still have a time in the past */ |
|
|
1898 | /* so correct this. this will make the periodic very inexact, but the user */ |
|
|
1899 | /* has effectively asked to get triggered more often than possible */ |
|
|
1900 | if (ev_at (w) < ev_rt_now) |
|
|
1901 | ev_at (w) = ev_rt_now; |
|
|
1902 | } |
|
|
1903 | |
|
|
1904 | ANHE_at_cache (periodics [HEAP0]); |
|
|
1905 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1906 | } |
|
|
1907 | else |
|
|
1908 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1909 | |
|
|
1910 | EV_FREQUENT_CHECK; |
|
|
1911 | feed_reverse (EV_A_ (W)w); |
1821 | } |
1912 | } |
1822 | else if (w->interval) |
1913 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now); |
1823 | { |
|
|
1824 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1825 | /* if next trigger time is not sufficiently in the future, put it there */ |
|
|
1826 | /* this might happen because of floating point inexactness */ |
|
|
1827 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
|
|
1828 | { |
|
|
1829 | ev_at (w) += w->interval; |
|
|
1830 | |
1914 | |
1831 | /* if interval is unreasonably low we might still have a time in the past */ |
|
|
1832 | /* so correct this. this will make the periodic very inexact, but the user */ |
|
|
1833 | /* has effectively asked to get triggered more often than possible */ |
|
|
1834 | if (ev_at (w) < ev_rt_now) |
|
|
1835 | ev_at (w) = ev_rt_now; |
|
|
1836 | } |
|
|
1837 | |
|
|
1838 | ANHE_at_cache (periodics [HEAP0]); |
|
|
1839 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1840 | } |
|
|
1841 | else |
|
|
1842 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1843 | |
|
|
1844 | EV_FREQUENT_CHECK; |
|
|
1845 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1915 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1846 | } |
1916 | } |
1847 | } |
1917 | } |
1848 | |
1918 | |
|
|
1919 | /* simply recalculate all periodics */ |
|
|
1920 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
1849 | static void noinline |
1921 | static void noinline |
1850 | periodics_reschedule (EV_P) |
1922 | periodics_reschedule (EV_P) |
1851 | { |
1923 | { |
1852 | int i; |
1924 | int i; |
1853 | |
1925 | |
… | |
… | |
1866 | |
1938 | |
1867 | reheap (periodics, periodiccnt); |
1939 | reheap (periodics, periodiccnt); |
1868 | } |
1940 | } |
1869 | #endif |
1941 | #endif |
1870 | |
1942 | |
1871 | void inline_speed |
1943 | /* adjust all timers by a given offset */ |
|
|
1944 | static void noinline |
|
|
1945 | timers_reschedule (EV_P_ ev_tstamp adjust) |
|
|
1946 | { |
|
|
1947 | int i; |
|
|
1948 | |
|
|
1949 | for (i = 0; i < timercnt; ++i) |
|
|
1950 | { |
|
|
1951 | ANHE *he = timers + i + HEAP0; |
|
|
1952 | ANHE_w (*he)->at += adjust; |
|
|
1953 | ANHE_at_cache (*he); |
|
|
1954 | } |
|
|
1955 | } |
|
|
1956 | |
|
|
1957 | /* fetch new monotonic and realtime times from the kernel */ |
|
|
1958 | /* also detetc if there was a timejump, and act accordingly */ |
|
|
1959 | inline_speed void |
1872 | time_update (EV_P_ ev_tstamp max_block) |
1960 | time_update (EV_P_ ev_tstamp max_block) |
1873 | { |
1961 | { |
1874 | int i; |
|
|
1875 | |
|
|
1876 | #if EV_USE_MONOTONIC |
1962 | #if EV_USE_MONOTONIC |
1877 | if (expect_true (have_monotonic)) |
1963 | if (expect_true (have_monotonic)) |
1878 | { |
1964 | { |
|
|
1965 | int i; |
1879 | ev_tstamp odiff = rtmn_diff; |
1966 | ev_tstamp odiff = rtmn_diff; |
1880 | |
1967 | |
1881 | mn_now = get_clock (); |
1968 | mn_now = get_clock (); |
1882 | |
1969 | |
1883 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
1970 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
1909 | ev_rt_now = ev_time (); |
1996 | ev_rt_now = ev_time (); |
1910 | mn_now = get_clock (); |
1997 | mn_now = get_clock (); |
1911 | now_floor = mn_now; |
1998 | now_floor = mn_now; |
1912 | } |
1999 | } |
1913 | |
2000 | |
|
|
2001 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
2002 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1914 | # if EV_PERIODIC_ENABLE |
2003 | # if EV_PERIODIC_ENABLE |
1915 | periodics_reschedule (EV_A); |
2004 | periodics_reschedule (EV_A); |
1916 | # endif |
2005 | # endif |
1917 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1918 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
|
|
1919 | } |
2006 | } |
1920 | else |
2007 | else |
1921 | #endif |
2008 | #endif |
1922 | { |
2009 | { |
1923 | ev_rt_now = ev_time (); |
2010 | ev_rt_now = ev_time (); |
1924 | |
2011 | |
1925 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
2012 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
1926 | { |
2013 | { |
|
|
2014 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
|
|
2015 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
1927 | #if EV_PERIODIC_ENABLE |
2016 | #if EV_PERIODIC_ENABLE |
1928 | periodics_reschedule (EV_A); |
2017 | periodics_reschedule (EV_A); |
1929 | #endif |
2018 | #endif |
1930 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
|
|
1931 | for (i = 0; i < timercnt; ++i) |
|
|
1932 | { |
|
|
1933 | ANHE *he = timers + i + HEAP0; |
|
|
1934 | ANHE_w (*he)->at += ev_rt_now - mn_now; |
|
|
1935 | ANHE_at_cache (*he); |
|
|
1936 | } |
|
|
1937 | } |
2019 | } |
1938 | |
2020 | |
1939 | mn_now = ev_rt_now; |
2021 | mn_now = ev_rt_now; |
1940 | } |
2022 | } |
1941 | } |
|
|
1942 | |
|
|
1943 | void |
|
|
1944 | ev_ref (EV_P) |
|
|
1945 | { |
|
|
1946 | ++activecnt; |
|
|
1947 | } |
|
|
1948 | |
|
|
1949 | void |
|
|
1950 | ev_unref (EV_P) |
|
|
1951 | { |
|
|
1952 | --activecnt; |
|
|
1953 | } |
|
|
1954 | |
|
|
1955 | void |
|
|
1956 | ev_now_update (EV_P) |
|
|
1957 | { |
|
|
1958 | time_update (EV_A_ 1e100); |
|
|
1959 | } |
2023 | } |
1960 | |
2024 | |
1961 | static int loop_done; |
2025 | static int loop_done; |
1962 | |
2026 | |
1963 | void |
2027 | void |
… | |
… | |
1997 | { |
2061 | { |
1998 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2062 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1999 | call_pending (EV_A); |
2063 | call_pending (EV_A); |
2000 | } |
2064 | } |
2001 | |
2065 | |
2002 | if (expect_false (!activecnt)) |
|
|
2003 | break; |
|
|
2004 | |
|
|
2005 | /* we might have forked, so reify kernel state if necessary */ |
2066 | /* we might have forked, so reify kernel state if necessary */ |
2006 | if (expect_false (postfork)) |
2067 | if (expect_false (postfork)) |
2007 | loop_fork (EV_A); |
2068 | loop_fork (EV_A); |
2008 | |
2069 | |
2009 | /* update fd-related kernel structures */ |
2070 | /* update fd-related kernel structures */ |
… | |
… | |
2088 | ev_unloop (EV_P_ int how) |
2149 | ev_unloop (EV_P_ int how) |
2089 | { |
2150 | { |
2090 | loop_done = how; |
2151 | loop_done = how; |
2091 | } |
2152 | } |
2092 | |
2153 | |
|
|
2154 | void |
|
|
2155 | ev_ref (EV_P) |
|
|
2156 | { |
|
|
2157 | ++activecnt; |
|
|
2158 | } |
|
|
2159 | |
|
|
2160 | void |
|
|
2161 | ev_unref (EV_P) |
|
|
2162 | { |
|
|
2163 | --activecnt; |
|
|
2164 | } |
|
|
2165 | |
|
|
2166 | void |
|
|
2167 | ev_now_update (EV_P) |
|
|
2168 | { |
|
|
2169 | time_update (EV_A_ 1e100); |
|
|
2170 | } |
|
|
2171 | |
|
|
2172 | void |
|
|
2173 | ev_suspend (EV_P) |
|
|
2174 | { |
|
|
2175 | ev_now_update (EV_A); |
|
|
2176 | } |
|
|
2177 | |
|
|
2178 | void |
|
|
2179 | ev_resume (EV_P) |
|
|
2180 | { |
|
|
2181 | ev_tstamp mn_prev = mn_now; |
|
|
2182 | |
|
|
2183 | ev_now_update (EV_A); |
|
|
2184 | timers_reschedule (EV_A_ mn_now - mn_prev); |
|
|
2185 | #if EV_PERIODIC_ENABLE |
|
|
2186 | /* TODO: really do this? */ |
|
|
2187 | periodics_reschedule (EV_A); |
|
|
2188 | #endif |
|
|
2189 | } |
|
|
2190 | |
2093 | /*****************************************************************************/ |
2191 | /*****************************************************************************/ |
|
|
2192 | /* singly-linked list management, used when the expected list length is short */ |
2094 | |
2193 | |
2095 | void inline_size |
2194 | inline_size void |
2096 | wlist_add (WL *head, WL elem) |
2195 | wlist_add (WL *head, WL elem) |
2097 | { |
2196 | { |
2098 | elem->next = *head; |
2197 | elem->next = *head; |
2099 | *head = elem; |
2198 | *head = elem; |
2100 | } |
2199 | } |
2101 | |
2200 | |
2102 | void inline_size |
2201 | inline_size void |
2103 | wlist_del (WL *head, WL elem) |
2202 | wlist_del (WL *head, WL elem) |
2104 | { |
2203 | { |
2105 | while (*head) |
2204 | while (*head) |
2106 | { |
2205 | { |
2107 | if (*head == elem) |
2206 | if (*head == elem) |
… | |
… | |
2112 | |
2211 | |
2113 | head = &(*head)->next; |
2212 | head = &(*head)->next; |
2114 | } |
2213 | } |
2115 | } |
2214 | } |
2116 | |
2215 | |
2117 | void inline_speed |
2216 | /* internal, faster, version of ev_clear_pending */ |
|
|
2217 | inline_speed void |
2118 | clear_pending (EV_P_ W w) |
2218 | clear_pending (EV_P_ W w) |
2119 | { |
2219 | { |
2120 | if (w->pending) |
2220 | if (w->pending) |
2121 | { |
2221 | { |
2122 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
2222 | pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; |
2123 | w->pending = 0; |
2223 | w->pending = 0; |
2124 | } |
2224 | } |
2125 | } |
2225 | } |
2126 | |
2226 | |
2127 | int |
2227 | int |
… | |
… | |
2131 | int pending = w_->pending; |
2231 | int pending = w_->pending; |
2132 | |
2232 | |
2133 | if (expect_true (pending)) |
2233 | if (expect_true (pending)) |
2134 | { |
2234 | { |
2135 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
2235 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
2236 | p->w = (W)&pending_w; |
2136 | w_->pending = 0; |
2237 | w_->pending = 0; |
2137 | p->w = 0; |
|
|
2138 | return p->events; |
2238 | return p->events; |
2139 | } |
2239 | } |
2140 | else |
2240 | else |
2141 | return 0; |
2241 | return 0; |
2142 | } |
2242 | } |
2143 | |
2243 | |
2144 | void inline_size |
2244 | inline_size void |
2145 | pri_adjust (EV_P_ W w) |
2245 | pri_adjust (EV_P_ W w) |
2146 | { |
2246 | { |
2147 | int pri = w->priority; |
2247 | int pri = w->priority; |
2148 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2248 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2149 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2249 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2150 | w->priority = pri; |
2250 | w->priority = pri; |
2151 | } |
2251 | } |
2152 | |
2252 | |
2153 | void inline_speed |
2253 | inline_speed void |
2154 | ev_start (EV_P_ W w, int active) |
2254 | ev_start (EV_P_ W w, int active) |
2155 | { |
2255 | { |
2156 | pri_adjust (EV_A_ w); |
2256 | pri_adjust (EV_A_ w); |
2157 | w->active = active; |
2257 | w->active = active; |
2158 | ev_ref (EV_A); |
2258 | ev_ref (EV_A); |
2159 | } |
2259 | } |
2160 | |
2260 | |
2161 | void inline_size |
2261 | inline_size void |
2162 | ev_stop (EV_P_ W w) |
2262 | ev_stop (EV_P_ W w) |
2163 | { |
2263 | { |
2164 | ev_unref (EV_A); |
2264 | ev_unref (EV_A); |
2165 | w->active = 0; |
2265 | w->active = 0; |
2166 | } |
2266 | } |
… | |
… | |
2174 | |
2274 | |
2175 | if (expect_false (ev_is_active (w))) |
2275 | if (expect_false (ev_is_active (w))) |
2176 | return; |
2276 | return; |
2177 | |
2277 | |
2178 | assert (("libev: ev_io_start called with negative fd", fd >= 0)); |
2278 | assert (("libev: ev_io_start called with negative fd", fd >= 0)); |
2179 | assert (("libev: ev_io start called with illegal event mask", !(w->events & ~(EV_IOFDSET | EV_READ | EV_WRITE)))); |
2279 | assert (("libev: ev_io start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); |
2180 | |
2280 | |
2181 | EV_FREQUENT_CHECK; |
2281 | EV_FREQUENT_CHECK; |
2182 | |
2282 | |
2183 | ev_start (EV_A_ (W)w, 1); |
2283 | ev_start (EV_A_ (W)w, 1); |
2184 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2284 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2185 | wlist_add (&anfds[fd].head, (WL)w); |
2285 | wlist_add (&anfds[fd].head, (WL)w); |
2186 | |
2286 | |
2187 | fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1); |
2287 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2188 | w->events &= ~EV_IOFDSET; |
2288 | w->events &= ~EV__IOFDSET; |
2189 | |
2289 | |
2190 | EV_FREQUENT_CHECK; |
2290 | EV_FREQUENT_CHECK; |
2191 | } |
2291 | } |
2192 | |
2292 | |
2193 | void noinline |
2293 | void noinline |
… | |
… | |
2591 | |
2691 | |
2592 | for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) |
2692 | for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) |
2593 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
2693 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
2594 | } |
2694 | } |
2595 | |
2695 | |
2596 | void inline_size |
2696 | inline_size void |
2597 | check_2625 (EV_P) |
2697 | check_2625 (EV_P) |
2598 | { |
2698 | { |
2599 | /* kernels < 2.6.25 are borked |
2699 | /* kernels < 2.6.25 are borked |
2600 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
2700 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
2601 | */ |
2701 | */ |
… | |
… | |
2614 | return; |
2714 | return; |
2615 | |
2715 | |
2616 | fs_2625 = 1; |
2716 | fs_2625 = 1; |
2617 | } |
2717 | } |
2618 | |
2718 | |
2619 | void inline_size |
2719 | inline_size void |
2620 | infy_init (EV_P) |
2720 | infy_init (EV_P) |
2621 | { |
2721 | { |
2622 | if (fs_fd != -2) |
2722 | if (fs_fd != -2) |
2623 | return; |
2723 | return; |
2624 | |
2724 | |
… | |
… | |
2634 | ev_set_priority (&fs_w, EV_MAXPRI); |
2734 | ev_set_priority (&fs_w, EV_MAXPRI); |
2635 | ev_io_start (EV_A_ &fs_w); |
2735 | ev_io_start (EV_A_ &fs_w); |
2636 | } |
2736 | } |
2637 | } |
2737 | } |
2638 | |
2738 | |
2639 | void inline_size |
2739 | inline_size void |
2640 | infy_fork (EV_P) |
2740 | infy_fork (EV_P) |
2641 | { |
2741 | { |
2642 | int slot; |
2742 | int slot; |
2643 | |
2743 | |
2644 | if (fs_fd < 0) |
2744 | if (fs_fd < 0) |
… | |
… | |
3146 | ev_timer_set (&once->to, timeout, 0.); |
3246 | ev_timer_set (&once->to, timeout, 0.); |
3147 | ev_timer_start (EV_A_ &once->to); |
3247 | ev_timer_start (EV_A_ &once->to); |
3148 | } |
3248 | } |
3149 | } |
3249 | } |
3150 | |
3250 | |
|
|
3251 | /*****************************************************************************/ |
|
|
3252 | |
|
|
3253 | #if EV_WALK_ENABLE |
|
|
3254 | void |
|
|
3255 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
|
|
3256 | { |
|
|
3257 | int i, j; |
|
|
3258 | ev_watcher_list *wl, *wn; |
|
|
3259 | |
|
|
3260 | if (types & (EV_IO | EV_EMBED)) |
|
|
3261 | for (i = 0; i < anfdmax; ++i) |
|
|
3262 | for (wl = anfds [i].head; wl; ) |
|
|
3263 | { |
|
|
3264 | wn = wl->next; |
|
|
3265 | |
|
|
3266 | #if EV_EMBED_ENABLE |
|
|
3267 | if (ev_cb ((ev_io *)wl) == embed_io_cb) |
|
|
3268 | { |
|
|
3269 | if (types & EV_EMBED) |
|
|
3270 | cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io)); |
|
|
3271 | } |
|
|
3272 | else |
|
|
3273 | #endif |
|
|
3274 | #if EV_USE_INOTIFY |
|
|
3275 | if (ev_cb ((ev_io *)wl) == infy_cb) |
|
|
3276 | ; |
|
|
3277 | else |
|
|
3278 | #endif |
|
|
3279 | if ((ev_io *)wl != &pipe_w) |
|
|
3280 | if (types & EV_IO) |
|
|
3281 | cb (EV_A_ EV_IO, wl); |
|
|
3282 | |
|
|
3283 | wl = wn; |
|
|
3284 | } |
|
|
3285 | |
|
|
3286 | if (types & (EV_TIMER | EV_STAT)) |
|
|
3287 | for (i = timercnt + HEAP0; i-- > HEAP0; ) |
|
|
3288 | #if EV_STAT_ENABLE |
|
|
3289 | /*TODO: timer is not always active*/ |
|
|
3290 | if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb) |
|
|
3291 | { |
|
|
3292 | if (types & EV_STAT) |
|
|
3293 | cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer)); |
|
|
3294 | } |
|
|
3295 | else |
|
|
3296 | #endif |
|
|
3297 | if (types & EV_TIMER) |
|
|
3298 | cb (EV_A_ EV_TIMER, ANHE_w (timers [i])); |
|
|
3299 | |
|
|
3300 | #if EV_PERIODIC_ENABLE |
|
|
3301 | if (types & EV_PERIODIC) |
|
|
3302 | for (i = periodiccnt + HEAP0; i-- > HEAP0; ) |
|
|
3303 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
|
|
3304 | #endif |
|
|
3305 | |
|
|
3306 | #if EV_IDLE_ENABLE |
|
|
3307 | if (types & EV_IDLE) |
|
|
3308 | for (j = NUMPRI; i--; ) |
|
|
3309 | for (i = idlecnt [j]; i--; ) |
|
|
3310 | cb (EV_A_ EV_IDLE, idles [j][i]); |
|
|
3311 | #endif |
|
|
3312 | |
|
|
3313 | #if EV_FORK_ENABLE |
|
|
3314 | if (types & EV_FORK) |
|
|
3315 | for (i = forkcnt; i--; ) |
|
|
3316 | if (ev_cb (forks [i]) != embed_fork_cb) |
|
|
3317 | cb (EV_A_ EV_FORK, forks [i]); |
|
|
3318 | #endif |
|
|
3319 | |
|
|
3320 | #if EV_ASYNC_ENABLE |
|
|
3321 | if (types & EV_ASYNC) |
|
|
3322 | for (i = asynccnt; i--; ) |
|
|
3323 | cb (EV_A_ EV_ASYNC, asyncs [i]); |
|
|
3324 | #endif |
|
|
3325 | |
|
|
3326 | if (types & EV_PREPARE) |
|
|
3327 | for (i = preparecnt; i--; ) |
|
|
3328 | #if EV_EMBED_ENABLE |
|
|
3329 | if (ev_cb (prepares [i]) != embed_prepare_cb) |
|
|
3330 | #endif |
|
|
3331 | cb (EV_A_ EV_PREPARE, prepares [i]); |
|
|
3332 | |
|
|
3333 | if (types & EV_CHECK) |
|
|
3334 | for (i = checkcnt; i--; ) |
|
|
3335 | cb (EV_A_ EV_CHECK, checks [i]); |
|
|
3336 | |
|
|
3337 | if (types & EV_SIGNAL) |
|
|
3338 | for (i = 0; i < signalmax; ++i) |
|
|
3339 | for (wl = signals [i].head; wl; ) |
|
|
3340 | { |
|
|
3341 | wn = wl->next; |
|
|
3342 | cb (EV_A_ EV_SIGNAL, wl); |
|
|
3343 | wl = wn; |
|
|
3344 | } |
|
|
3345 | |
|
|
3346 | if (types & EV_CHILD) |
|
|
3347 | for (i = EV_PID_HASHSIZE; i--; ) |
|
|
3348 | for (wl = childs [i]; wl; ) |
|
|
3349 | { |
|
|
3350 | wn = wl->next; |
|
|
3351 | cb (EV_A_ EV_CHILD, wl); |
|
|
3352 | wl = wn; |
|
|
3353 | } |
|
|
3354 | /* EV_STAT 0x00001000 /* stat data changed */ |
|
|
3355 | /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ |
|
|
3356 | } |
|
|
3357 | #endif |
|
|
3358 | |
3151 | #if EV_MULTIPLICITY |
3359 | #if EV_MULTIPLICITY |
3152 | #include "ev_wrap.h" |
3360 | #include "ev_wrap.h" |
3153 | #endif |
3361 | #endif |
3154 | |
3362 | |
3155 | #ifdef __cplusplus |
3363 | #ifdef __cplusplus |