… | |
… | |
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" { |
… | |
… | |
384 | # define inline_speed static noinline |
391 | # define inline_speed static noinline |
385 | #else |
392 | #else |
386 | # define inline_speed static inline |
393 | # define inline_speed static inline |
387 | #endif |
394 | #endif |
388 | |
395 | |
389 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
396 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
397 | |
|
|
398 | #if EV_MINPRI == EV_MAXPRI |
|
|
399 | # define ABSPRI(w) (((W)w), 0) |
|
|
400 | #else |
390 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
401 | # define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
|
|
402 | #endif |
391 | |
403 | |
392 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
404 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
393 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
405 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
394 | |
406 | |
395 | typedef ev_watcher *W; |
407 | typedef ev_watcher *W; |
… | |
… | |
478 | #define ev_malloc(size) ev_realloc (0, (size)) |
490 | #define ev_malloc(size) ev_realloc (0, (size)) |
479 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
491 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
480 | |
492 | |
481 | /*****************************************************************************/ |
493 | /*****************************************************************************/ |
482 | |
494 | |
|
|
495 | /* set in reify when reification needed */ |
|
|
496 | #define EV_ANFD_REIFY 1 |
|
|
497 | |
|
|
498 | /* file descriptor info structure */ |
483 | typedef struct |
499 | typedef struct |
484 | { |
500 | { |
485 | WL head; |
501 | WL head; |
486 | unsigned char events; |
502 | unsigned char events; /* the events watched for */ |
487 | unsigned char reify; |
503 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
488 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
504 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
489 | unsigned char unused; |
505 | unsigned char unused; |
490 | #if EV_USE_EPOLL |
506 | #if EV_USE_EPOLL |
491 | unsigned int egen; /* generation counter to counter epoll bugs */ |
507 | unsigned int egen; /* generation counter to counter epoll bugs */ |
492 | #endif |
508 | #endif |
493 | #if EV_SELECT_IS_WINSOCKET |
509 | #if EV_SELECT_IS_WINSOCKET |
494 | SOCKET handle; |
510 | SOCKET handle; |
495 | #endif |
511 | #endif |
496 | } ANFD; |
512 | } ANFD; |
497 | |
513 | |
|
|
514 | /* stores the pending event set for a given watcher */ |
498 | typedef struct |
515 | typedef struct |
499 | { |
516 | { |
500 | W w; |
517 | W w; |
501 | int events; |
518 | int events; /* the pending event set for the given watcher */ |
502 | } ANPENDING; |
519 | } ANPENDING; |
503 | |
520 | |
504 | #if EV_USE_INOTIFY |
521 | #if EV_USE_INOTIFY |
505 | /* hash table entry per inotify-id */ |
522 | /* hash table entry per inotify-id */ |
506 | typedef struct |
523 | typedef struct |
… | |
… | |
509 | } ANFS; |
526 | } ANFS; |
510 | #endif |
527 | #endif |
511 | |
528 | |
512 | /* Heap Entry */ |
529 | /* Heap Entry */ |
513 | #if EV_HEAP_CACHE_AT |
530 | #if EV_HEAP_CACHE_AT |
|
|
531 | /* a heap element */ |
514 | typedef struct { |
532 | typedef struct { |
515 | ev_tstamp at; |
533 | ev_tstamp at; |
516 | WT w; |
534 | WT w; |
517 | } ANHE; |
535 | } ANHE; |
518 | |
536 | |
519 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
537 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
520 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
538 | #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 */ |
539 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
522 | #else |
540 | #else |
|
|
541 | /* a heap element */ |
523 | typedef WT ANHE; |
542 | typedef WT ANHE; |
524 | |
543 | |
525 | #define ANHE_w(he) (he) |
544 | #define ANHE_w(he) (he) |
526 | #define ANHE_at(he) (he)->at |
545 | #define ANHE_at(he) (he)->at |
527 | #define ANHE_at_cache(he) |
546 | #define ANHE_at_cache(he) |
… | |
… | |
551 | |
570 | |
552 | static int ev_default_loop_ptr; |
571 | static int ev_default_loop_ptr; |
553 | |
572 | |
554 | #endif |
573 | #endif |
555 | |
574 | |
|
|
575 | #if EV_MINIMAL < 2 |
|
|
576 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
|
|
577 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
|
|
578 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
|
|
579 | #else |
|
|
580 | # define EV_RELEASE_CB (void)0 |
|
|
581 | # define EV_ACQUIRE_CB (void)0 |
|
|
582 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
|
|
583 | #endif |
|
|
584 | |
|
|
585 | #define EVUNLOOP_RECURSE 0x80 |
|
|
586 | |
556 | /*****************************************************************************/ |
587 | /*****************************************************************************/ |
557 | |
588 | |
|
|
589 | #ifndef EV_HAVE_EV_TIME |
558 | ev_tstamp |
590 | ev_tstamp |
559 | ev_time (void) |
591 | ev_time (void) |
560 | { |
592 | { |
561 | #if EV_USE_REALTIME |
593 | #if EV_USE_REALTIME |
562 | if (expect_true (have_realtime)) |
594 | if (expect_true (have_realtime)) |
… | |
… | |
569 | |
601 | |
570 | struct timeval tv; |
602 | struct timeval tv; |
571 | gettimeofday (&tv, 0); |
603 | gettimeofday (&tv, 0); |
572 | return tv.tv_sec + tv.tv_usec * 1e-6; |
604 | return tv.tv_sec + tv.tv_usec * 1e-6; |
573 | } |
605 | } |
|
|
606 | #endif |
574 | |
607 | |
575 | inline_size ev_tstamp |
608 | inline_size ev_tstamp |
576 | get_clock (void) |
609 | get_clock (void) |
577 | { |
610 | { |
578 | #if EV_USE_MONOTONIC |
611 | #if EV_USE_MONOTONIC |
… | |
… | |
614 | |
647 | |
615 | tv.tv_sec = (time_t)delay; |
648 | tv.tv_sec = (time_t)delay; |
616 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
649 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
617 | |
650 | |
618 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
651 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
619 | /* somehting nto guaranteed by newer posix versions, but guaranteed */ |
652 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
620 | /* by older ones */ |
653 | /* by older ones */ |
621 | select (0, 0, 0, 0, &tv); |
654 | select (0, 0, 0, 0, &tv); |
622 | #endif |
655 | #endif |
623 | } |
656 | } |
624 | } |
657 | } |
625 | |
658 | |
626 | /*****************************************************************************/ |
659 | /*****************************************************************************/ |
627 | |
660 | |
628 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
661 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
629 | |
662 | |
|
|
663 | /* find a suitable new size for the given array, */ |
|
|
664 | /* hopefully by rounding to a ncie-to-malloc size */ |
630 | inline_size int |
665 | inline_size int |
631 | array_nextsize (int elem, int cur, int cnt) |
666 | array_nextsize (int elem, int cur, int cnt) |
632 | { |
667 | { |
633 | int ncur = cur + 1; |
668 | int ncur = cur + 1; |
634 | |
669 | |
… | |
… | |
680 | #define array_free(stem, idx) \ |
715 | #define array_free(stem, idx) \ |
681 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
716 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
682 | |
717 | |
683 | /*****************************************************************************/ |
718 | /*****************************************************************************/ |
684 | |
719 | |
|
|
720 | /* dummy callback for pending events */ |
|
|
721 | static void noinline |
|
|
722 | pendingcb (EV_P_ ev_prepare *w, int revents) |
|
|
723 | { |
|
|
724 | } |
|
|
725 | |
685 | void noinline |
726 | void noinline |
686 | ev_feed_event (EV_P_ void *w, int revents) |
727 | ev_feed_event (EV_P_ void *w, int revents) |
687 | { |
728 | { |
688 | W w_ = (W)w; |
729 | W w_ = (W)w; |
689 | int pri = ABSPRI (w_); |
730 | int pri = ABSPRI (w_); |
… | |
… | |
724 | } |
765 | } |
725 | |
766 | |
726 | /*****************************************************************************/ |
767 | /*****************************************************************************/ |
727 | |
768 | |
728 | inline_speed void |
769 | inline_speed void |
729 | fd_event (EV_P_ int fd, int revents) |
770 | fd_event_nc (EV_P_ int fd, int revents) |
730 | { |
771 | { |
731 | ANFD *anfd = anfds + fd; |
772 | ANFD *anfd = anfds + fd; |
732 | ev_io *w; |
773 | ev_io *w; |
733 | |
774 | |
734 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
775 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
738 | if (ev) |
779 | if (ev) |
739 | ev_feed_event (EV_A_ (W)w, ev); |
780 | ev_feed_event (EV_A_ (W)w, ev); |
740 | } |
781 | } |
741 | } |
782 | } |
742 | |
783 | |
|
|
784 | /* do not submit kernel events for fds that have reify set */ |
|
|
785 | /* because that means they changed while we were polling for new events */ |
|
|
786 | inline_speed void |
|
|
787 | fd_event (EV_P_ int fd, int revents) |
|
|
788 | { |
|
|
789 | ANFD *anfd = anfds + fd; |
|
|
790 | |
|
|
791 | if (expect_true (!anfd->reify)) |
|
|
792 | fd_event_nc (EV_A_ fd, revents); |
|
|
793 | } |
|
|
794 | |
743 | void |
795 | void |
744 | ev_feed_fd_event (EV_P_ int fd, int revents) |
796 | ev_feed_fd_event (EV_P_ int fd, int revents) |
745 | { |
797 | { |
746 | if (fd >= 0 && fd < anfdmax) |
798 | if (fd >= 0 && fd < anfdmax) |
747 | fd_event (EV_A_ fd, revents); |
799 | fd_event_nc (EV_A_ fd, revents); |
748 | } |
800 | } |
749 | |
801 | |
|
|
802 | /* make sure the external fd watch events are in-sync */ |
|
|
803 | /* with the kernel/libev internal state */ |
750 | inline_size void |
804 | inline_size void |
751 | fd_reify (EV_P) |
805 | fd_reify (EV_P) |
752 | { |
806 | { |
753 | int i; |
807 | int i; |
754 | |
808 | |
… | |
… | |
789 | } |
843 | } |
790 | |
844 | |
791 | fdchangecnt = 0; |
845 | fdchangecnt = 0; |
792 | } |
846 | } |
793 | |
847 | |
|
|
848 | /* something about the given fd changed */ |
794 | inline_size void |
849 | inline_size void |
795 | fd_change (EV_P_ int fd, int flags) |
850 | fd_change (EV_P_ int fd, int flags) |
796 | { |
851 | { |
797 | unsigned char reify = anfds [fd].reify; |
852 | unsigned char reify = anfds [fd].reify; |
798 | anfds [fd].reify |= flags; |
853 | anfds [fd].reify |= flags; |
… | |
… | |
803 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
858 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
804 | fdchanges [fdchangecnt - 1] = fd; |
859 | fdchanges [fdchangecnt - 1] = fd; |
805 | } |
860 | } |
806 | } |
861 | } |
807 | |
862 | |
|
|
863 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
808 | inline_speed void |
864 | inline_speed void |
809 | fd_kill (EV_P_ int fd) |
865 | fd_kill (EV_P_ int fd) |
810 | { |
866 | { |
811 | ev_io *w; |
867 | ev_io *w; |
812 | |
868 | |
… | |
… | |
815 | ev_io_stop (EV_A_ w); |
871 | ev_io_stop (EV_A_ w); |
816 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
872 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
817 | } |
873 | } |
818 | } |
874 | } |
819 | |
875 | |
|
|
876 | /* check whether the given fd is atcually valid, for error recovery */ |
820 | inline_size int |
877 | inline_size int |
821 | fd_valid (int fd) |
878 | fd_valid (int fd) |
822 | { |
879 | { |
823 | #ifdef _WIN32 |
880 | #ifdef _WIN32 |
824 | return _get_osfhandle (fd) != -1; |
881 | return _get_osfhandle (fd) != -1; |
… | |
… | |
862 | for (fd = 0; fd < anfdmax; ++fd) |
919 | for (fd = 0; fd < anfdmax; ++fd) |
863 | if (anfds [fd].events) |
920 | if (anfds [fd].events) |
864 | { |
921 | { |
865 | anfds [fd].events = 0; |
922 | anfds [fd].events = 0; |
866 | anfds [fd].emask = 0; |
923 | anfds [fd].emask = 0; |
867 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
924 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
868 | } |
925 | } |
869 | } |
926 | } |
870 | |
927 | |
871 | /*****************************************************************************/ |
928 | /*****************************************************************************/ |
872 | |
929 | |
… | |
… | |
989 | |
1046 | |
990 | heap [k] = he; |
1047 | heap [k] = he; |
991 | ev_active (ANHE_w (he)) = k; |
1048 | ev_active (ANHE_w (he)) = k; |
992 | } |
1049 | } |
993 | |
1050 | |
|
|
1051 | /* move an element suitably so it is in a correct place */ |
994 | inline_size void |
1052 | inline_size void |
995 | adjustheap (ANHE *heap, int N, int k) |
1053 | adjustheap (ANHE *heap, int N, int k) |
996 | { |
1054 | { |
997 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
1055 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
998 | upheap (heap, k); |
1056 | upheap (heap, k); |
… | |
… | |
1012 | upheap (heap, i + HEAP0); |
1070 | upheap (heap, i + HEAP0); |
1013 | } |
1071 | } |
1014 | |
1072 | |
1015 | /*****************************************************************************/ |
1073 | /*****************************************************************************/ |
1016 | |
1074 | |
|
|
1075 | /* associate signal watchers to a signal signal */ |
1017 | typedef struct |
1076 | typedef struct |
1018 | { |
1077 | { |
1019 | WL head; |
1078 | WL head; |
1020 | EV_ATOMIC_T gotsig; |
1079 | EV_ATOMIC_T gotsig; |
1021 | } ANSIG; |
1080 | } ANSIG; |
… | |
… | |
1025 | |
1084 | |
1026 | static EV_ATOMIC_T gotsig; |
1085 | static EV_ATOMIC_T gotsig; |
1027 | |
1086 | |
1028 | /*****************************************************************************/ |
1087 | /*****************************************************************************/ |
1029 | |
1088 | |
|
|
1089 | /* used to prepare libev internal fd's */ |
|
|
1090 | /* this is not fork-safe */ |
1030 | inline_speed void |
1091 | inline_speed void |
1031 | fd_intern (int fd) |
1092 | fd_intern (int fd) |
1032 | { |
1093 | { |
1033 | #ifdef _WIN32 |
1094 | #ifdef _WIN32 |
1034 | unsigned long arg = 1; |
1095 | unsigned long arg = 1; |
… | |
… | |
1040 | } |
1101 | } |
1041 | |
1102 | |
1042 | static void noinline |
1103 | static void noinline |
1043 | evpipe_init (EV_P) |
1104 | evpipe_init (EV_P) |
1044 | { |
1105 | { |
1045 | if (!ev_is_active (&pipeev)) |
1106 | if (!ev_is_active (&pipe_w)) |
1046 | { |
1107 | { |
1047 | #if EV_USE_EVENTFD |
1108 | #if EV_USE_EVENTFD |
1048 | if ((evfd = eventfd (0, 0)) >= 0) |
1109 | if ((evfd = eventfd (0, 0)) >= 0) |
1049 | { |
1110 | { |
1050 | evpipe [0] = -1; |
1111 | evpipe [0] = -1; |
1051 | fd_intern (evfd); |
1112 | fd_intern (evfd); |
1052 | ev_io_set (&pipeev, evfd, EV_READ); |
1113 | ev_io_set (&pipe_w, evfd, EV_READ); |
1053 | } |
1114 | } |
1054 | else |
1115 | else |
1055 | #endif |
1116 | #endif |
1056 | { |
1117 | { |
1057 | while (pipe (evpipe)) |
1118 | while (pipe (evpipe)) |
1058 | ev_syserr ("(libev) error creating signal/async pipe"); |
1119 | ev_syserr ("(libev) error creating signal/async pipe"); |
1059 | |
1120 | |
1060 | fd_intern (evpipe [0]); |
1121 | fd_intern (evpipe [0]); |
1061 | fd_intern (evpipe [1]); |
1122 | fd_intern (evpipe [1]); |
1062 | ev_io_set (&pipeev, evpipe [0], EV_READ); |
1123 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
1063 | } |
1124 | } |
1064 | |
1125 | |
1065 | ev_io_start (EV_A_ &pipeev); |
1126 | ev_io_start (EV_A_ &pipe_w); |
1066 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1127 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1067 | } |
1128 | } |
1068 | } |
1129 | } |
1069 | |
1130 | |
1070 | inline_size void |
1131 | inline_size void |
… | |
… | |
1088 | |
1149 | |
1089 | errno = old_errno; |
1150 | errno = old_errno; |
1090 | } |
1151 | } |
1091 | } |
1152 | } |
1092 | |
1153 | |
|
|
1154 | /* called whenever the libev signal pipe */ |
|
|
1155 | /* got some events (signal, async) */ |
1093 | static void |
1156 | static void |
1094 | pipecb (EV_P_ ev_io *iow, int revents) |
1157 | pipecb (EV_P_ ev_io *iow, int revents) |
1095 | { |
1158 | { |
1096 | #if EV_USE_EVENTFD |
1159 | #if EV_USE_EVENTFD |
1097 | if (evfd >= 0) |
1160 | if (evfd >= 0) |
… | |
… | |
1179 | |
1242 | |
1180 | #ifndef WIFCONTINUED |
1243 | #ifndef WIFCONTINUED |
1181 | # define WIFCONTINUED(status) 0 |
1244 | # define WIFCONTINUED(status) 0 |
1182 | #endif |
1245 | #endif |
1183 | |
1246 | |
|
|
1247 | /* handle a single child status event */ |
1184 | inline_speed void |
1248 | inline_speed void |
1185 | child_reap (EV_P_ int chain, int pid, int status) |
1249 | child_reap (EV_P_ int chain, int pid, int status) |
1186 | { |
1250 | { |
1187 | ev_child *w; |
1251 | ev_child *w; |
1188 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1252 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
… | |
… | |
1202 | |
1266 | |
1203 | #ifndef WCONTINUED |
1267 | #ifndef WCONTINUED |
1204 | # define WCONTINUED 0 |
1268 | # define WCONTINUED 0 |
1205 | #endif |
1269 | #endif |
1206 | |
1270 | |
|
|
1271 | /* called on sigchld etc., calls waitpid */ |
1207 | static void |
1272 | static void |
1208 | childcb (EV_P_ ev_signal *sw, int revents) |
1273 | childcb (EV_P_ ev_signal *sw, int revents) |
1209 | { |
1274 | { |
1210 | int pid, status; |
1275 | int pid, status; |
1211 | |
1276 | |
… | |
… | |
1318 | ev_backend (EV_P) |
1383 | ev_backend (EV_P) |
1319 | { |
1384 | { |
1320 | return backend; |
1385 | return backend; |
1321 | } |
1386 | } |
1322 | |
1387 | |
|
|
1388 | #if EV_MINIMAL < 2 |
1323 | unsigned int |
1389 | unsigned int |
1324 | ev_loop_count (EV_P) |
1390 | ev_loop_count (EV_P) |
1325 | { |
1391 | { |
1326 | return loop_count; |
1392 | return loop_count; |
1327 | } |
1393 | } |
1328 | |
1394 | |
|
|
1395 | unsigned int |
|
|
1396 | ev_loop_depth (EV_P) |
|
|
1397 | { |
|
|
1398 | return loop_depth; |
|
|
1399 | } |
|
|
1400 | |
1329 | void |
1401 | void |
1330 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1402 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1331 | { |
1403 | { |
1332 | io_blocktime = interval; |
1404 | io_blocktime = interval; |
1333 | } |
1405 | } |
… | |
… | |
1336 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1408 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1337 | { |
1409 | { |
1338 | timeout_blocktime = interval; |
1410 | timeout_blocktime = interval; |
1339 | } |
1411 | } |
1340 | |
1412 | |
|
|
1413 | void |
|
|
1414 | ev_set_userdata (EV_P_ void *data) |
|
|
1415 | { |
|
|
1416 | userdata = data; |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | void * |
|
|
1420 | ev_userdata (EV_P) |
|
|
1421 | { |
|
|
1422 | return userdata; |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
|
|
1426 | { |
|
|
1427 | invoke_cb = invoke_pending_cb; |
|
|
1428 | } |
|
|
1429 | |
|
|
1430 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
|
|
1431 | { |
|
|
1432 | release_cb = release; |
|
|
1433 | acquire_cb = acquire; |
|
|
1434 | } |
|
|
1435 | #endif |
|
|
1436 | |
|
|
1437 | /* initialise a loop structure, must be zero-initialised */ |
1341 | static void noinline |
1438 | static void noinline |
1342 | loop_init (EV_P_ unsigned int flags) |
1439 | loop_init (EV_P_ unsigned int flags) |
1343 | { |
1440 | { |
1344 | if (!backend) |
1441 | if (!backend) |
1345 | { |
1442 | { |
… | |
… | |
1365 | |
1462 | |
1366 | ev_rt_now = ev_time (); |
1463 | ev_rt_now = ev_time (); |
1367 | mn_now = get_clock (); |
1464 | mn_now = get_clock (); |
1368 | now_floor = mn_now; |
1465 | now_floor = mn_now; |
1369 | rtmn_diff = ev_rt_now - mn_now; |
1466 | rtmn_diff = ev_rt_now - mn_now; |
|
|
1467 | #if EV_MINIMAL < 2 |
|
|
1468 | invoke_cb = ev_invoke_pending; |
|
|
1469 | #endif |
1370 | |
1470 | |
1371 | io_blocktime = 0.; |
1471 | io_blocktime = 0.; |
1372 | timeout_blocktime = 0.; |
1472 | timeout_blocktime = 0.; |
1373 | backend = 0; |
1473 | backend = 0; |
1374 | backend_fd = -1; |
1474 | backend_fd = -1; |
… | |
… | |
1405 | #endif |
1505 | #endif |
1406 | #if EV_USE_SELECT |
1506 | #if EV_USE_SELECT |
1407 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1507 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1408 | #endif |
1508 | #endif |
1409 | |
1509 | |
|
|
1510 | ev_prepare_init (&pending_w, pendingcb); |
|
|
1511 | |
1410 | ev_init (&pipeev, pipecb); |
1512 | ev_init (&pipe_w, pipecb); |
1411 | ev_set_priority (&pipeev, EV_MAXPRI); |
1513 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1412 | } |
1514 | } |
1413 | } |
1515 | } |
1414 | |
1516 | |
|
|
1517 | /* free up a loop structure */ |
1415 | static void noinline |
1518 | static void noinline |
1416 | loop_destroy (EV_P) |
1519 | loop_destroy (EV_P) |
1417 | { |
1520 | { |
1418 | int i; |
1521 | int i; |
1419 | |
1522 | |
1420 | if (ev_is_active (&pipeev)) |
1523 | if (ev_is_active (&pipe_w)) |
1421 | { |
1524 | { |
1422 | ev_ref (EV_A); /* signal watcher */ |
1525 | ev_ref (EV_A); /* signal watcher */ |
1423 | ev_io_stop (EV_A_ &pipeev); |
1526 | ev_io_stop (EV_A_ &pipe_w); |
1424 | |
1527 | |
1425 | #if EV_USE_EVENTFD |
1528 | #if EV_USE_EVENTFD |
1426 | if (evfd >= 0) |
1529 | if (evfd >= 0) |
1427 | close (evfd); |
1530 | close (evfd); |
1428 | #endif |
1531 | #endif |
… | |
… | |
1505 | #endif |
1608 | #endif |
1506 | #if EV_USE_INOTIFY |
1609 | #if EV_USE_INOTIFY |
1507 | infy_fork (EV_A); |
1610 | infy_fork (EV_A); |
1508 | #endif |
1611 | #endif |
1509 | |
1612 | |
1510 | if (ev_is_active (&pipeev)) |
1613 | if (ev_is_active (&pipe_w)) |
1511 | { |
1614 | { |
1512 | /* this "locks" the handlers against writing to the pipe */ |
1615 | /* this "locks" the handlers against writing to the pipe */ |
1513 | /* while we modify the fd vars */ |
1616 | /* while we modify the fd vars */ |
1514 | gotsig = 1; |
1617 | gotsig = 1; |
1515 | #if EV_ASYNC_ENABLE |
1618 | #if EV_ASYNC_ENABLE |
1516 | gotasync = 1; |
1619 | gotasync = 1; |
1517 | #endif |
1620 | #endif |
1518 | |
1621 | |
1519 | ev_ref (EV_A); |
1622 | ev_ref (EV_A); |
1520 | ev_io_stop (EV_A_ &pipeev); |
1623 | ev_io_stop (EV_A_ &pipe_w); |
1521 | |
1624 | |
1522 | #if EV_USE_EVENTFD |
1625 | #if EV_USE_EVENTFD |
1523 | if (evfd >= 0) |
1626 | if (evfd >= 0) |
1524 | close (evfd); |
1627 | close (evfd); |
1525 | #endif |
1628 | #endif |
… | |
… | |
1530 | close (evpipe [1]); |
1633 | close (evpipe [1]); |
1531 | } |
1634 | } |
1532 | |
1635 | |
1533 | evpipe_init (EV_A); |
1636 | evpipe_init (EV_A); |
1534 | /* now iterate over everything, in case we missed something */ |
1637 | /* now iterate over everything, in case we missed something */ |
1535 | pipecb (EV_A_ &pipeev, EV_READ); |
1638 | pipecb (EV_A_ &pipe_w, EV_READ); |
1536 | } |
1639 | } |
1537 | |
1640 | |
1538 | postfork = 0; |
1641 | postfork = 0; |
1539 | } |
1642 | } |
1540 | |
1643 | |
… | |
… | |
1565 | void |
1668 | void |
1566 | ev_loop_fork (EV_P) |
1669 | ev_loop_fork (EV_P) |
1567 | { |
1670 | { |
1568 | postfork = 1; /* must be in line with ev_default_fork */ |
1671 | postfork = 1; /* must be in line with ev_default_fork */ |
1569 | } |
1672 | } |
|
|
1673 | #endif /* multiplicity */ |
1570 | |
1674 | |
1571 | #if EV_VERIFY |
1675 | #if EV_VERIFY |
1572 | static void noinline |
1676 | static void noinline |
1573 | verify_watcher (EV_P_ W w) |
1677 | verify_watcher (EV_P_ W w) |
1574 | { |
1678 | { |
… | |
… | |
1602 | verify_watcher (EV_A_ ws [cnt]); |
1706 | verify_watcher (EV_A_ ws [cnt]); |
1603 | } |
1707 | } |
1604 | } |
1708 | } |
1605 | #endif |
1709 | #endif |
1606 | |
1710 | |
|
|
1711 | #if EV_MINIMAL < 2 |
1607 | void |
1712 | void |
1608 | ev_loop_verify (EV_P) |
1713 | ev_loop_verify (EV_P) |
1609 | { |
1714 | { |
1610 | #if EV_VERIFY |
1715 | #if EV_VERIFY |
1611 | int i; |
1716 | int i; |
… | |
… | |
1664 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1769 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1665 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1770 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1666 | # endif |
1771 | # endif |
1667 | #endif |
1772 | #endif |
1668 | } |
1773 | } |
1669 | |
1774 | #endif |
1670 | #endif /* multiplicity */ |
|
|
1671 | |
1775 | |
1672 | #if EV_MULTIPLICITY |
1776 | #if EV_MULTIPLICITY |
1673 | struct ev_loop * |
1777 | struct ev_loop * |
1674 | ev_default_loop_init (unsigned int flags) |
1778 | ev_default_loop_init (unsigned int flags) |
1675 | #else |
1779 | #else |
… | |
… | |
1736 | ev_invoke (EV_P_ void *w, int revents) |
1840 | ev_invoke (EV_P_ void *w, int revents) |
1737 | { |
1841 | { |
1738 | EV_CB_INVOKE ((W)w, revents); |
1842 | EV_CB_INVOKE ((W)w, revents); |
1739 | } |
1843 | } |
1740 | |
1844 | |
1741 | inline_speed void |
1845 | unsigned int |
1742 | call_pending (EV_P) |
1846 | ev_pending_count (EV_P) |
|
|
1847 | { |
|
|
1848 | int pri; |
|
|
1849 | unsigned int count = 0; |
|
|
1850 | |
|
|
1851 | for (pri = NUMPRI; pri--; ) |
|
|
1852 | count += pendingcnt [pri]; |
|
|
1853 | |
|
|
1854 | return count; |
|
|
1855 | } |
|
|
1856 | |
|
|
1857 | void noinline |
|
|
1858 | ev_invoke_pending (EV_P) |
1743 | { |
1859 | { |
1744 | int pri; |
1860 | int pri; |
1745 | |
1861 | |
1746 | for (pri = NUMPRI; pri--; ) |
1862 | for (pri = NUMPRI; pri--; ) |
1747 | while (pendingcnt [pri]) |
1863 | while (pendingcnt [pri]) |
1748 | { |
1864 | { |
1749 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1865 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1750 | |
1866 | |
1751 | if (expect_true (p->w)) |
|
|
1752 | { |
|
|
1753 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
1867 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
1868 | /* ^ this is no longer true, as pending_w could be here */ |
1754 | |
1869 | |
1755 | p->w->pending = 0; |
1870 | p->w->pending = 0; |
1756 | EV_CB_INVOKE (p->w, p->events); |
1871 | EV_CB_INVOKE (p->w, p->events); |
1757 | EV_FREQUENT_CHECK; |
1872 | EV_FREQUENT_CHECK; |
1758 | } |
|
|
1759 | } |
1873 | } |
1760 | } |
1874 | } |
1761 | |
1875 | |
1762 | #if EV_IDLE_ENABLE |
1876 | #if EV_IDLE_ENABLE |
|
|
1877 | /* make idle watchers pending. this handles the "call-idle */ |
|
|
1878 | /* only when higher priorities are idle" logic */ |
1763 | inline_size void |
1879 | inline_size void |
1764 | idle_reify (EV_P) |
1880 | idle_reify (EV_P) |
1765 | { |
1881 | { |
1766 | if (expect_false (idleall)) |
1882 | if (expect_false (idleall)) |
1767 | { |
1883 | { |
… | |
… | |
1780 | } |
1896 | } |
1781 | } |
1897 | } |
1782 | } |
1898 | } |
1783 | #endif |
1899 | #endif |
1784 | |
1900 | |
|
|
1901 | /* make timers pending */ |
1785 | inline_size void |
1902 | inline_size void |
1786 | timers_reify (EV_P) |
1903 | timers_reify (EV_P) |
1787 | { |
1904 | { |
1788 | EV_FREQUENT_CHECK; |
1905 | EV_FREQUENT_CHECK; |
1789 | |
1906 | |
… | |
… | |
1818 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1935 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1819 | } |
1936 | } |
1820 | } |
1937 | } |
1821 | |
1938 | |
1822 | #if EV_PERIODIC_ENABLE |
1939 | #if EV_PERIODIC_ENABLE |
|
|
1940 | /* make periodics pending */ |
1823 | inline_size void |
1941 | inline_size void |
1824 | periodics_reify (EV_P) |
1942 | periodics_reify (EV_P) |
1825 | { |
1943 | { |
1826 | EV_FREQUENT_CHECK; |
1944 | EV_FREQUENT_CHECK; |
1827 | |
1945 | |
… | |
… | |
1874 | |
1992 | |
1875 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1993 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1876 | } |
1994 | } |
1877 | } |
1995 | } |
1878 | |
1996 | |
|
|
1997 | /* simply recalculate all periodics */ |
|
|
1998 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
1879 | static void noinline |
1999 | static void noinline |
1880 | periodics_reschedule (EV_P) |
2000 | periodics_reschedule (EV_P) |
1881 | { |
2001 | { |
1882 | int i; |
2002 | int i; |
1883 | |
2003 | |
… | |
… | |
1896 | |
2016 | |
1897 | reheap (periodics, periodiccnt); |
2017 | reheap (periodics, periodiccnt); |
1898 | } |
2018 | } |
1899 | #endif |
2019 | #endif |
1900 | |
2020 | |
|
|
2021 | /* adjust all timers by a given offset */ |
1901 | static void noinline |
2022 | static void noinline |
1902 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2023 | timers_reschedule (EV_P_ ev_tstamp adjust) |
1903 | { |
2024 | { |
1904 | int i; |
2025 | int i; |
1905 | |
2026 | |
… | |
… | |
1909 | ANHE_w (*he)->at += adjust; |
2030 | ANHE_w (*he)->at += adjust; |
1910 | ANHE_at_cache (*he); |
2031 | ANHE_at_cache (*he); |
1911 | } |
2032 | } |
1912 | } |
2033 | } |
1913 | |
2034 | |
|
|
2035 | /* fetch new monotonic and realtime times from the kernel */ |
|
|
2036 | /* also detetc if there was a timejump, and act accordingly */ |
1914 | inline_speed void |
2037 | inline_speed void |
1915 | time_update (EV_P_ ev_tstamp max_block) |
2038 | time_update (EV_P_ ev_tstamp max_block) |
1916 | { |
2039 | { |
1917 | int i; |
|
|
1918 | |
|
|
1919 | #if EV_USE_MONOTONIC |
2040 | #if EV_USE_MONOTONIC |
1920 | if (expect_true (have_monotonic)) |
2041 | if (expect_true (have_monotonic)) |
1921 | { |
2042 | { |
|
|
2043 | int i; |
1922 | ev_tstamp odiff = rtmn_diff; |
2044 | ev_tstamp odiff = rtmn_diff; |
1923 | |
2045 | |
1924 | mn_now = get_clock (); |
2046 | mn_now = get_clock (); |
1925 | |
2047 | |
1926 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
2048 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
1976 | |
2098 | |
1977 | mn_now = ev_rt_now; |
2099 | mn_now = ev_rt_now; |
1978 | } |
2100 | } |
1979 | } |
2101 | } |
1980 | |
2102 | |
1981 | static int loop_done; |
|
|
1982 | |
|
|
1983 | void |
2103 | void |
1984 | ev_loop (EV_P_ int flags) |
2104 | ev_loop (EV_P_ int flags) |
1985 | { |
2105 | { |
|
|
2106 | #if EV_MINIMAL < 2 |
|
|
2107 | ++loop_depth; |
|
|
2108 | #endif |
|
|
2109 | |
|
|
2110 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
|
|
2111 | |
1986 | loop_done = EVUNLOOP_CANCEL; |
2112 | loop_done = EVUNLOOP_CANCEL; |
1987 | |
2113 | |
1988 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2114 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
1989 | |
2115 | |
1990 | do |
2116 | do |
1991 | { |
2117 | { |
1992 | #if EV_VERIFY >= 2 |
2118 | #if EV_VERIFY >= 2 |
1993 | ev_loop_verify (EV_A); |
2119 | ev_loop_verify (EV_A); |
… | |
… | |
2006 | /* we might have forked, so queue fork handlers */ |
2132 | /* we might have forked, so queue fork handlers */ |
2007 | if (expect_false (postfork)) |
2133 | if (expect_false (postfork)) |
2008 | if (forkcnt) |
2134 | if (forkcnt) |
2009 | { |
2135 | { |
2010 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2136 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2011 | call_pending (EV_A); |
2137 | EV_INVOKE_PENDING; |
2012 | } |
2138 | } |
2013 | #endif |
2139 | #endif |
2014 | |
2140 | |
2015 | /* queue prepare watchers (and execute them) */ |
2141 | /* queue prepare watchers (and execute them) */ |
2016 | if (expect_false (preparecnt)) |
2142 | if (expect_false (preparecnt)) |
2017 | { |
2143 | { |
2018 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2144 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2019 | call_pending (EV_A); |
2145 | EV_INVOKE_PENDING; |
2020 | } |
2146 | } |
|
|
2147 | |
|
|
2148 | if (expect_false (loop_done)) |
|
|
2149 | break; |
2021 | |
2150 | |
2022 | /* we might have forked, so reify kernel state if necessary */ |
2151 | /* we might have forked, so reify kernel state if necessary */ |
2023 | if (expect_false (postfork)) |
2152 | if (expect_false (postfork)) |
2024 | loop_fork (EV_A); |
2153 | loop_fork (EV_A); |
2025 | |
2154 | |
… | |
… | |
2031 | ev_tstamp waittime = 0.; |
2160 | ev_tstamp waittime = 0.; |
2032 | ev_tstamp sleeptime = 0.; |
2161 | ev_tstamp sleeptime = 0.; |
2033 | |
2162 | |
2034 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2163 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2035 | { |
2164 | { |
|
|
2165 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2166 | ev_tstamp prev_mn_now = mn_now; |
|
|
2167 | |
2036 | /* update time to cancel out callback processing overhead */ |
2168 | /* update time to cancel out callback processing overhead */ |
2037 | time_update (EV_A_ 1e100); |
2169 | time_update (EV_A_ 1e100); |
|
|
2170 | |
|
|
2171 | waittime = MAX_BLOCKTIME; |
2038 | |
2172 | |
2039 | if (timercnt) |
2173 | if (timercnt) |
2040 | { |
2174 | { |
2041 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2175 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2042 | if (waittime > to) waittime = to; |
2176 | if (waittime > to) waittime = to; |
… | |
… | |
2048 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2182 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2049 | if (waittime > to) waittime = to; |
2183 | if (waittime > to) waittime = to; |
2050 | } |
2184 | } |
2051 | #endif |
2185 | #endif |
2052 | |
2186 | |
|
|
2187 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2053 | if (expect_false (waittime < timeout_blocktime)) |
2188 | if (expect_false (waittime < timeout_blocktime)) |
2054 | waittime = timeout_blocktime; |
2189 | waittime = timeout_blocktime; |
2055 | |
2190 | |
2056 | sleeptime = waittime - backend_fudge; |
2191 | /* extra check because io_blocktime is commonly 0 */ |
2057 | |
|
|
2058 | if (expect_true (sleeptime > io_blocktime)) |
2192 | if (expect_false (io_blocktime)) |
2059 | sleeptime = io_blocktime; |
|
|
2060 | |
|
|
2061 | if (sleeptime) |
|
|
2062 | { |
2193 | { |
|
|
2194 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
|
|
2195 | |
|
|
2196 | if (sleeptime > waittime - backend_fudge) |
|
|
2197 | sleeptime = waittime - backend_fudge; |
|
|
2198 | |
|
|
2199 | if (expect_true (sleeptime > 0.)) |
|
|
2200 | { |
2063 | ev_sleep (sleeptime); |
2201 | ev_sleep (sleeptime); |
2064 | waittime -= sleeptime; |
2202 | waittime -= sleeptime; |
|
|
2203 | } |
2065 | } |
2204 | } |
2066 | } |
2205 | } |
2067 | |
2206 | |
|
|
2207 | #if EV_MINIMAL < 2 |
2068 | ++loop_count; |
2208 | ++loop_count; |
|
|
2209 | #endif |
|
|
2210 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2069 | backend_poll (EV_A_ waittime); |
2211 | backend_poll (EV_A_ waittime); |
|
|
2212 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2070 | |
2213 | |
2071 | /* update ev_rt_now, do magic */ |
2214 | /* update ev_rt_now, do magic */ |
2072 | time_update (EV_A_ waittime + sleeptime); |
2215 | time_update (EV_A_ waittime + sleeptime); |
2073 | } |
2216 | } |
2074 | |
2217 | |
… | |
… | |
2085 | |
2228 | |
2086 | /* queue check watchers, to be executed first */ |
2229 | /* queue check watchers, to be executed first */ |
2087 | if (expect_false (checkcnt)) |
2230 | if (expect_false (checkcnt)) |
2088 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2231 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2089 | |
2232 | |
2090 | call_pending (EV_A); |
2233 | EV_INVOKE_PENDING; |
2091 | } |
2234 | } |
2092 | while (expect_true ( |
2235 | while (expect_true ( |
2093 | activecnt |
2236 | activecnt |
2094 | && !loop_done |
2237 | && !loop_done |
2095 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2238 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2096 | )); |
2239 | )); |
2097 | |
2240 | |
2098 | if (loop_done == EVUNLOOP_ONE) |
2241 | if (loop_done == EVUNLOOP_ONE) |
2099 | loop_done = EVUNLOOP_CANCEL; |
2242 | loop_done = EVUNLOOP_CANCEL; |
|
|
2243 | |
|
|
2244 | #if EV_MINIMAL < 2 |
|
|
2245 | --loop_depth; |
|
|
2246 | #endif |
2100 | } |
2247 | } |
2101 | |
2248 | |
2102 | void |
2249 | void |
2103 | ev_unloop (EV_P_ int how) |
2250 | ev_unloop (EV_P_ int how) |
2104 | { |
2251 | { |
… | |
… | |
2135 | ev_tstamp mn_prev = mn_now; |
2282 | ev_tstamp mn_prev = mn_now; |
2136 | |
2283 | |
2137 | ev_now_update (EV_A); |
2284 | ev_now_update (EV_A); |
2138 | timers_reschedule (EV_A_ mn_now - mn_prev); |
2285 | timers_reschedule (EV_A_ mn_now - mn_prev); |
2139 | #if EV_PERIODIC_ENABLE |
2286 | #if EV_PERIODIC_ENABLE |
|
|
2287 | /* TODO: really do this? */ |
2140 | periodics_reschedule (EV_A); |
2288 | periodics_reschedule (EV_A); |
2141 | #endif |
2289 | #endif |
2142 | } |
2290 | } |
2143 | |
2291 | |
2144 | /*****************************************************************************/ |
2292 | /*****************************************************************************/ |
|
|
2293 | /* singly-linked list management, used when the expected list length is short */ |
2145 | |
2294 | |
2146 | inline_size void |
2295 | inline_size void |
2147 | wlist_add (WL *head, WL elem) |
2296 | wlist_add (WL *head, WL elem) |
2148 | { |
2297 | { |
2149 | elem->next = *head; |
2298 | elem->next = *head; |
… | |
… | |
2163 | |
2312 | |
2164 | head = &(*head)->next; |
2313 | head = &(*head)->next; |
2165 | } |
2314 | } |
2166 | } |
2315 | } |
2167 | |
2316 | |
|
|
2317 | /* internal, faster, version of ev_clear_pending */ |
2168 | inline_speed void |
2318 | inline_speed void |
2169 | clear_pending (EV_P_ W w) |
2319 | clear_pending (EV_P_ W w) |
2170 | { |
2320 | { |
2171 | if (w->pending) |
2321 | if (w->pending) |
2172 | { |
2322 | { |
2173 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
2323 | pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; |
2174 | w->pending = 0; |
2324 | w->pending = 0; |
2175 | } |
2325 | } |
2176 | } |
2326 | } |
2177 | |
2327 | |
2178 | int |
2328 | int |
… | |
… | |
2182 | int pending = w_->pending; |
2332 | int pending = w_->pending; |
2183 | |
2333 | |
2184 | if (expect_true (pending)) |
2334 | if (expect_true (pending)) |
2185 | { |
2335 | { |
2186 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
2336 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
2337 | p->w = (W)&pending_w; |
2187 | w_->pending = 0; |
2338 | w_->pending = 0; |
2188 | p->w = 0; |
|
|
2189 | return p->events; |
2339 | return p->events; |
2190 | } |
2340 | } |
2191 | else |
2341 | else |
2192 | return 0; |
2342 | return 0; |
2193 | } |
2343 | } |
2194 | |
2344 | |
2195 | inline_size void |
2345 | inline_size void |
2196 | pri_adjust (EV_P_ W w) |
2346 | pri_adjust (EV_P_ W w) |
2197 | { |
2347 | { |
2198 | int pri = w->priority; |
2348 | int pri = ev_priority (w); |
2199 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2349 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2200 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2350 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2201 | w->priority = pri; |
2351 | ev_set_priority (w, pri); |
2202 | } |
2352 | } |
2203 | |
2353 | |
2204 | inline_speed void |
2354 | inline_speed void |
2205 | ev_start (EV_P_ W w, int active) |
2355 | ev_start (EV_P_ W w, int active) |
2206 | { |
2356 | { |
… | |
… | |
2233 | |
2383 | |
2234 | ev_start (EV_A_ (W)w, 1); |
2384 | ev_start (EV_A_ (W)w, 1); |
2235 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2385 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2236 | wlist_add (&anfds[fd].head, (WL)w); |
2386 | wlist_add (&anfds[fd].head, (WL)w); |
2237 | |
2387 | |
2238 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2388 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2239 | w->events &= ~EV__IOFDSET; |
2389 | w->events &= ~EV__IOFDSET; |
2240 | |
2390 | |
2241 | EV_FREQUENT_CHECK; |
2391 | EV_FREQUENT_CHECK; |
2242 | } |
2392 | } |
2243 | |
2393 | |
… | |
… | |
2337 | } |
2487 | } |
2338 | |
2488 | |
2339 | EV_FREQUENT_CHECK; |
2489 | EV_FREQUENT_CHECK; |
2340 | } |
2490 | } |
2341 | |
2491 | |
|
|
2492 | ev_tstamp |
|
|
2493 | ev_timer_remaining (EV_P_ ev_timer *w) |
|
|
2494 | { |
|
|
2495 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
|
|
2496 | } |
|
|
2497 | |
2342 | #if EV_PERIODIC_ENABLE |
2498 | #if EV_PERIODIC_ENABLE |
2343 | void noinline |
2499 | void noinline |
2344 | ev_periodic_start (EV_P_ ev_periodic *w) |
2500 | ev_periodic_start (EV_P_ ev_periodic *w) |
2345 | { |
2501 | { |
2346 | if (expect_false (ev_is_active (w))) |
2502 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2447 | if (!((WL)w)->next) |
2603 | if (!((WL)w)->next) |
2448 | { |
2604 | { |
2449 | #if _WIN32 |
2605 | #if _WIN32 |
2450 | signal (w->signum, ev_sighandler); |
2606 | signal (w->signum, ev_sighandler); |
2451 | #else |
2607 | #else |
2452 | struct sigaction sa; |
2608 | struct sigaction sa = { }; |
2453 | sa.sa_handler = ev_sighandler; |
2609 | sa.sa_handler = ev_sighandler; |
2454 | sigfillset (&sa.sa_mask); |
2610 | sigfillset (&sa.sa_mask); |
2455 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2611 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2456 | sigaction (w->signum, &sa, 0); |
2612 | sigaction (w->signum, &sa, 0); |
2457 | #endif |
2613 | #endif |
… | |
… | |
3199 | } |
3355 | } |
3200 | } |
3356 | } |
3201 | |
3357 | |
3202 | /*****************************************************************************/ |
3358 | /*****************************************************************************/ |
3203 | |
3359 | |
3204 | #if 0 |
3360 | #if EV_WALK_ENABLE |
3205 | void |
3361 | void |
3206 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3362 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3207 | { |
3363 | { |
3208 | int i, j; |
3364 | int i, j; |
3209 | ev_watcher_list *wl, *wn; |
3365 | ev_watcher_list *wl, *wn; |
… | |
… | |
3225 | #if EV_USE_INOTIFY |
3381 | #if EV_USE_INOTIFY |
3226 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3382 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3227 | ; |
3383 | ; |
3228 | else |
3384 | else |
3229 | #endif |
3385 | #endif |
3230 | if ((ev_io *)wl != &pipeev) |
3386 | if ((ev_io *)wl != &pipe_w) |
3231 | if (types & EV_IO) |
3387 | if (types & EV_IO) |
3232 | cb (EV_A_ EV_IO, wl); |
3388 | cb (EV_A_ EV_IO, wl); |
3233 | |
3389 | |
3234 | wl = wn; |
3390 | wl = wn; |
3235 | } |
3391 | } |