… | |
… | |
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 | /* file descriptor info structure */ |
483 | typedef struct |
496 | typedef struct |
484 | { |
497 | { |
485 | WL head; |
498 | WL head; |
486 | unsigned char events; |
499 | unsigned char events; /* the events watched for */ |
487 | unsigned char reify; |
500 | unsigned char reify; /* flag set when this ANFD needs reification */ |
488 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
501 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
489 | unsigned char unused; |
502 | unsigned char unused; |
490 | #if EV_USE_EPOLL |
503 | #if EV_USE_EPOLL |
491 | unsigned int egen; /* generation counter to counter epoll bugs */ |
504 | unsigned int egen; /* generation counter to counter epoll bugs */ |
492 | #endif |
505 | #endif |
493 | #if EV_SELECT_IS_WINSOCKET |
506 | #if EV_SELECT_IS_WINSOCKET |
494 | SOCKET handle; |
507 | SOCKET handle; |
495 | #endif |
508 | #endif |
496 | } ANFD; |
509 | } ANFD; |
497 | |
510 | |
|
|
511 | /* stores the pending event set for a given watcher */ |
498 | typedef struct |
512 | typedef struct |
499 | { |
513 | { |
500 | W w; |
514 | W w; |
501 | int events; |
515 | int events; /* the pending event set for the given watcher */ |
502 | } ANPENDING; |
516 | } ANPENDING; |
503 | |
517 | |
504 | #if EV_USE_INOTIFY |
518 | #if EV_USE_INOTIFY |
505 | /* hash table entry per inotify-id */ |
519 | /* hash table entry per inotify-id */ |
506 | typedef struct |
520 | typedef struct |
… | |
… | |
509 | } ANFS; |
523 | } ANFS; |
510 | #endif |
524 | #endif |
511 | |
525 | |
512 | /* Heap Entry */ |
526 | /* Heap Entry */ |
513 | #if EV_HEAP_CACHE_AT |
527 | #if EV_HEAP_CACHE_AT |
|
|
528 | /* a heap element */ |
514 | typedef struct { |
529 | typedef struct { |
515 | ev_tstamp at; |
530 | ev_tstamp at; |
516 | WT w; |
531 | WT w; |
517 | } ANHE; |
532 | } ANHE; |
518 | |
533 | |
519 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
534 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
520 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
535 | #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 */ |
536 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
522 | #else |
537 | #else |
|
|
538 | /* a heap element */ |
523 | typedef WT ANHE; |
539 | typedef WT ANHE; |
524 | |
540 | |
525 | #define ANHE_w(he) (he) |
541 | #define ANHE_w(he) (he) |
526 | #define ANHE_at(he) (he)->at |
542 | #define ANHE_at(he) (he)->at |
527 | #define ANHE_at_cache(he) |
543 | #define ANHE_at_cache(he) |
… | |
… | |
553 | |
569 | |
554 | #endif |
570 | #endif |
555 | |
571 | |
556 | /*****************************************************************************/ |
572 | /*****************************************************************************/ |
557 | |
573 | |
|
|
574 | #ifndef EV_HAVE_EV_TIME |
558 | ev_tstamp |
575 | ev_tstamp |
559 | ev_time (void) |
576 | ev_time (void) |
560 | { |
577 | { |
561 | #if EV_USE_REALTIME |
578 | #if EV_USE_REALTIME |
562 | if (expect_true (have_realtime)) |
579 | if (expect_true (have_realtime)) |
… | |
… | |
569 | |
586 | |
570 | struct timeval tv; |
587 | struct timeval tv; |
571 | gettimeofday (&tv, 0); |
588 | gettimeofday (&tv, 0); |
572 | return tv.tv_sec + tv.tv_usec * 1e-6; |
589 | return tv.tv_sec + tv.tv_usec * 1e-6; |
573 | } |
590 | } |
|
|
591 | #endif |
574 | |
592 | |
575 | inline_size ev_tstamp |
593 | inline_size ev_tstamp |
576 | get_clock (void) |
594 | get_clock (void) |
577 | { |
595 | { |
578 | #if EV_USE_MONOTONIC |
596 | #if EV_USE_MONOTONIC |
… | |
… | |
614 | |
632 | |
615 | tv.tv_sec = (time_t)delay; |
633 | tv.tv_sec = (time_t)delay; |
616 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
634 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
617 | |
635 | |
618 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
636 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
619 | /* somehting nto guaranteed by newer posix versions, but guaranteed */ |
637 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
620 | /* by older ones */ |
638 | /* by older ones */ |
621 | select (0, 0, 0, 0, &tv); |
639 | select (0, 0, 0, 0, &tv); |
622 | #endif |
640 | #endif |
623 | } |
641 | } |
624 | } |
642 | } |
625 | |
643 | |
626 | /*****************************************************************************/ |
644 | /*****************************************************************************/ |
627 | |
645 | |
628 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
646 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
629 | |
647 | |
|
|
648 | /* find a suitable new size for the given array, */ |
|
|
649 | /* hopefully by rounding to a ncie-to-malloc size */ |
630 | inline_size int |
650 | inline_size int |
631 | array_nextsize (int elem, int cur, int cnt) |
651 | array_nextsize (int elem, int cur, int cnt) |
632 | { |
652 | { |
633 | int ncur = cur + 1; |
653 | int ncur = cur + 1; |
634 | |
654 | |
… | |
… | |
680 | #define array_free(stem, idx) \ |
700 | #define array_free(stem, idx) \ |
681 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
701 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
682 | |
702 | |
683 | /*****************************************************************************/ |
703 | /*****************************************************************************/ |
684 | |
704 | |
|
|
705 | /* dummy callback for pending events */ |
|
|
706 | static void noinline |
|
|
707 | pendingcb (EV_P_ ev_prepare *w, int revents) |
|
|
708 | { |
|
|
709 | } |
|
|
710 | |
685 | void noinline |
711 | void noinline |
686 | ev_feed_event (EV_P_ void *w, int revents) |
712 | ev_feed_event (EV_P_ void *w, int revents) |
687 | { |
713 | { |
688 | W w_ = (W)w; |
714 | W w_ = (W)w; |
689 | int pri = ABSPRI (w_); |
715 | int pri = ABSPRI (w_); |
… | |
… | |
745 | { |
771 | { |
746 | if (fd >= 0 && fd < anfdmax) |
772 | if (fd >= 0 && fd < anfdmax) |
747 | fd_event (EV_A_ fd, revents); |
773 | fd_event (EV_A_ fd, revents); |
748 | } |
774 | } |
749 | |
775 | |
|
|
776 | /* make sure the external fd watch events are in-sync */ |
|
|
777 | /* with the kernel/libev internal state */ |
750 | inline_size void |
778 | inline_size void |
751 | fd_reify (EV_P) |
779 | fd_reify (EV_P) |
752 | { |
780 | { |
753 | int i; |
781 | int i; |
754 | |
782 | |
… | |
… | |
789 | } |
817 | } |
790 | |
818 | |
791 | fdchangecnt = 0; |
819 | fdchangecnt = 0; |
792 | } |
820 | } |
793 | |
821 | |
|
|
822 | /* something about the given fd changed */ |
794 | inline_size void |
823 | inline_size void |
795 | fd_change (EV_P_ int fd, int flags) |
824 | fd_change (EV_P_ int fd, int flags) |
796 | { |
825 | { |
797 | unsigned char reify = anfds [fd].reify; |
826 | unsigned char reify = anfds [fd].reify; |
798 | anfds [fd].reify |= flags; |
827 | anfds [fd].reify |= flags; |
… | |
… | |
803 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
832 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
804 | fdchanges [fdchangecnt - 1] = fd; |
833 | fdchanges [fdchangecnt - 1] = fd; |
805 | } |
834 | } |
806 | } |
835 | } |
807 | |
836 | |
|
|
837 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
808 | inline_speed void |
838 | inline_speed void |
809 | fd_kill (EV_P_ int fd) |
839 | fd_kill (EV_P_ int fd) |
810 | { |
840 | { |
811 | ev_io *w; |
841 | ev_io *w; |
812 | |
842 | |
… | |
… | |
815 | ev_io_stop (EV_A_ w); |
845 | ev_io_stop (EV_A_ w); |
816 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
846 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
817 | } |
847 | } |
818 | } |
848 | } |
819 | |
849 | |
|
|
850 | /* check whether the given fd is atcually valid, for error recovery */ |
820 | inline_size int |
851 | inline_size int |
821 | fd_valid (int fd) |
852 | fd_valid (int fd) |
822 | { |
853 | { |
823 | #ifdef _WIN32 |
854 | #ifdef _WIN32 |
824 | return _get_osfhandle (fd) != -1; |
855 | return _get_osfhandle (fd) != -1; |
… | |
… | |
989 | |
1020 | |
990 | heap [k] = he; |
1021 | heap [k] = he; |
991 | ev_active (ANHE_w (he)) = k; |
1022 | ev_active (ANHE_w (he)) = k; |
992 | } |
1023 | } |
993 | |
1024 | |
|
|
1025 | /* move an element suitably so it is in a correct place */ |
994 | inline_size void |
1026 | inline_size void |
995 | adjustheap (ANHE *heap, int N, int k) |
1027 | adjustheap (ANHE *heap, int N, int k) |
996 | { |
1028 | { |
997 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
1029 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
998 | upheap (heap, k); |
1030 | upheap (heap, k); |
… | |
… | |
1012 | upheap (heap, i + HEAP0); |
1044 | upheap (heap, i + HEAP0); |
1013 | } |
1045 | } |
1014 | |
1046 | |
1015 | /*****************************************************************************/ |
1047 | /*****************************************************************************/ |
1016 | |
1048 | |
|
|
1049 | /* associate signal watchers to a signal signal */ |
1017 | typedef struct |
1050 | typedef struct |
1018 | { |
1051 | { |
1019 | WL head; |
1052 | WL head; |
1020 | EV_ATOMIC_T gotsig; |
1053 | EV_ATOMIC_T gotsig; |
1021 | } ANSIG; |
1054 | } ANSIG; |
… | |
… | |
1025 | |
1058 | |
1026 | static EV_ATOMIC_T gotsig; |
1059 | static EV_ATOMIC_T gotsig; |
1027 | |
1060 | |
1028 | /*****************************************************************************/ |
1061 | /*****************************************************************************/ |
1029 | |
1062 | |
|
|
1063 | /* used to prepare libev internal fd's */ |
|
|
1064 | /* this is not fork-safe */ |
1030 | inline_speed void |
1065 | inline_speed void |
1031 | fd_intern (int fd) |
1066 | fd_intern (int fd) |
1032 | { |
1067 | { |
1033 | #ifdef _WIN32 |
1068 | #ifdef _WIN32 |
1034 | unsigned long arg = 1; |
1069 | unsigned long arg = 1; |
… | |
… | |
1040 | } |
1075 | } |
1041 | |
1076 | |
1042 | static void noinline |
1077 | static void noinline |
1043 | evpipe_init (EV_P) |
1078 | evpipe_init (EV_P) |
1044 | { |
1079 | { |
1045 | if (!ev_is_active (&pipeev)) |
1080 | if (!ev_is_active (&pipe_w)) |
1046 | { |
1081 | { |
1047 | #if EV_USE_EVENTFD |
1082 | #if EV_USE_EVENTFD |
1048 | if ((evfd = eventfd (0, 0)) >= 0) |
1083 | if ((evfd = eventfd (0, 0)) >= 0) |
1049 | { |
1084 | { |
1050 | evpipe [0] = -1; |
1085 | evpipe [0] = -1; |
1051 | fd_intern (evfd); |
1086 | fd_intern (evfd); |
1052 | ev_io_set (&pipeev, evfd, EV_READ); |
1087 | ev_io_set (&pipe_w, evfd, EV_READ); |
1053 | } |
1088 | } |
1054 | else |
1089 | else |
1055 | #endif |
1090 | #endif |
1056 | { |
1091 | { |
1057 | while (pipe (evpipe)) |
1092 | while (pipe (evpipe)) |
1058 | ev_syserr ("(libev) error creating signal/async pipe"); |
1093 | ev_syserr ("(libev) error creating signal/async pipe"); |
1059 | |
1094 | |
1060 | fd_intern (evpipe [0]); |
1095 | fd_intern (evpipe [0]); |
1061 | fd_intern (evpipe [1]); |
1096 | fd_intern (evpipe [1]); |
1062 | ev_io_set (&pipeev, evpipe [0], EV_READ); |
1097 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
1063 | } |
1098 | } |
1064 | |
1099 | |
1065 | ev_io_start (EV_A_ &pipeev); |
1100 | ev_io_start (EV_A_ &pipe_w); |
1066 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1101 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1067 | } |
1102 | } |
1068 | } |
1103 | } |
1069 | |
1104 | |
1070 | inline_size void |
1105 | inline_size void |
… | |
… | |
1088 | |
1123 | |
1089 | errno = old_errno; |
1124 | errno = old_errno; |
1090 | } |
1125 | } |
1091 | } |
1126 | } |
1092 | |
1127 | |
|
|
1128 | /* called whenever the libev signal pipe */ |
|
|
1129 | /* got some events (signal, async) */ |
1093 | static void |
1130 | static void |
1094 | pipecb (EV_P_ ev_io *iow, int revents) |
1131 | pipecb (EV_P_ ev_io *iow, int revents) |
1095 | { |
1132 | { |
1096 | #if EV_USE_EVENTFD |
1133 | #if EV_USE_EVENTFD |
1097 | if (evfd >= 0) |
1134 | if (evfd >= 0) |
… | |
… | |
1179 | |
1216 | |
1180 | #ifndef WIFCONTINUED |
1217 | #ifndef WIFCONTINUED |
1181 | # define WIFCONTINUED(status) 0 |
1218 | # define WIFCONTINUED(status) 0 |
1182 | #endif |
1219 | #endif |
1183 | |
1220 | |
|
|
1221 | /* handle a single child status event */ |
1184 | inline_speed void |
1222 | inline_speed void |
1185 | child_reap (EV_P_ int chain, int pid, int status) |
1223 | child_reap (EV_P_ int chain, int pid, int status) |
1186 | { |
1224 | { |
1187 | ev_child *w; |
1225 | ev_child *w; |
1188 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1226 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
… | |
… | |
1202 | |
1240 | |
1203 | #ifndef WCONTINUED |
1241 | #ifndef WCONTINUED |
1204 | # define WCONTINUED 0 |
1242 | # define WCONTINUED 0 |
1205 | #endif |
1243 | #endif |
1206 | |
1244 | |
|
|
1245 | /* called on sigchld etc., calls waitpid */ |
1207 | static void |
1246 | static void |
1208 | childcb (EV_P_ ev_signal *sw, int revents) |
1247 | childcb (EV_P_ ev_signal *sw, int revents) |
1209 | { |
1248 | { |
1210 | int pid, status; |
1249 | int pid, status; |
1211 | |
1250 | |
… | |
… | |
1324 | ev_loop_count (EV_P) |
1363 | ev_loop_count (EV_P) |
1325 | { |
1364 | { |
1326 | return loop_count; |
1365 | return loop_count; |
1327 | } |
1366 | } |
1328 | |
1367 | |
|
|
1368 | unsigned int |
|
|
1369 | ev_loop_depth (EV_P) |
|
|
1370 | { |
|
|
1371 | return loop_depth; |
|
|
1372 | } |
|
|
1373 | |
1329 | void |
1374 | void |
1330 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1375 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1331 | { |
1376 | { |
1332 | io_blocktime = interval; |
1377 | io_blocktime = interval; |
1333 | } |
1378 | } |
… | |
… | |
1336 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1381 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1337 | { |
1382 | { |
1338 | timeout_blocktime = interval; |
1383 | timeout_blocktime = interval; |
1339 | } |
1384 | } |
1340 | |
1385 | |
|
|
1386 | /* initialise a loop structure, must be zero-initialised */ |
1341 | static void noinline |
1387 | static void noinline |
1342 | loop_init (EV_P_ unsigned int flags) |
1388 | loop_init (EV_P_ unsigned int flags) |
1343 | { |
1389 | { |
1344 | if (!backend) |
1390 | if (!backend) |
1345 | { |
1391 | { |
… | |
… | |
1405 | #endif |
1451 | #endif |
1406 | #if EV_USE_SELECT |
1452 | #if EV_USE_SELECT |
1407 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1453 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1408 | #endif |
1454 | #endif |
1409 | |
1455 | |
|
|
1456 | ev_prepare_init (&pending_w, pendingcb); |
|
|
1457 | |
1410 | ev_init (&pipeev, pipecb); |
1458 | ev_init (&pipe_w, pipecb); |
1411 | ev_set_priority (&pipeev, EV_MAXPRI); |
1459 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1412 | } |
1460 | } |
1413 | } |
1461 | } |
1414 | |
1462 | |
|
|
1463 | /* free up a loop structure */ |
1415 | static void noinline |
1464 | static void noinline |
1416 | loop_destroy (EV_P) |
1465 | loop_destroy (EV_P) |
1417 | { |
1466 | { |
1418 | int i; |
1467 | int i; |
1419 | |
1468 | |
1420 | if (ev_is_active (&pipeev)) |
1469 | if (ev_is_active (&pipe_w)) |
1421 | { |
1470 | { |
1422 | ev_ref (EV_A); /* signal watcher */ |
1471 | ev_ref (EV_A); /* signal watcher */ |
1423 | ev_io_stop (EV_A_ &pipeev); |
1472 | ev_io_stop (EV_A_ &pipe_w); |
1424 | |
1473 | |
1425 | #if EV_USE_EVENTFD |
1474 | #if EV_USE_EVENTFD |
1426 | if (evfd >= 0) |
1475 | if (evfd >= 0) |
1427 | close (evfd); |
1476 | close (evfd); |
1428 | #endif |
1477 | #endif |
… | |
… | |
1505 | #endif |
1554 | #endif |
1506 | #if EV_USE_INOTIFY |
1555 | #if EV_USE_INOTIFY |
1507 | infy_fork (EV_A); |
1556 | infy_fork (EV_A); |
1508 | #endif |
1557 | #endif |
1509 | |
1558 | |
1510 | if (ev_is_active (&pipeev)) |
1559 | if (ev_is_active (&pipe_w)) |
1511 | { |
1560 | { |
1512 | /* this "locks" the handlers against writing to the pipe */ |
1561 | /* this "locks" the handlers against writing to the pipe */ |
1513 | /* while we modify the fd vars */ |
1562 | /* while we modify the fd vars */ |
1514 | gotsig = 1; |
1563 | gotsig = 1; |
1515 | #if EV_ASYNC_ENABLE |
1564 | #if EV_ASYNC_ENABLE |
1516 | gotasync = 1; |
1565 | gotasync = 1; |
1517 | #endif |
1566 | #endif |
1518 | |
1567 | |
1519 | ev_ref (EV_A); |
1568 | ev_ref (EV_A); |
1520 | ev_io_stop (EV_A_ &pipeev); |
1569 | ev_io_stop (EV_A_ &pipe_w); |
1521 | |
1570 | |
1522 | #if EV_USE_EVENTFD |
1571 | #if EV_USE_EVENTFD |
1523 | if (evfd >= 0) |
1572 | if (evfd >= 0) |
1524 | close (evfd); |
1573 | close (evfd); |
1525 | #endif |
1574 | #endif |
… | |
… | |
1530 | close (evpipe [1]); |
1579 | close (evpipe [1]); |
1531 | } |
1580 | } |
1532 | |
1581 | |
1533 | evpipe_init (EV_A); |
1582 | evpipe_init (EV_A); |
1534 | /* now iterate over everything, in case we missed something */ |
1583 | /* now iterate over everything, in case we missed something */ |
1535 | pipecb (EV_A_ &pipeev, EV_READ); |
1584 | pipecb (EV_A_ &pipe_w, EV_READ); |
1536 | } |
1585 | } |
1537 | |
1586 | |
1538 | postfork = 0; |
1587 | postfork = 0; |
1539 | } |
1588 | } |
1540 | |
1589 | |
… | |
… | |
1746 | for (pri = NUMPRI; pri--; ) |
1795 | for (pri = NUMPRI; pri--; ) |
1747 | while (pendingcnt [pri]) |
1796 | while (pendingcnt [pri]) |
1748 | { |
1797 | { |
1749 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1798 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1750 | |
1799 | |
1751 | if (expect_true (p->w)) |
|
|
1752 | { |
|
|
1753 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
1800 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
1801 | /* ^ this is no longer true, as pending_w could be here */ |
1754 | |
1802 | |
1755 | p->w->pending = 0; |
1803 | p->w->pending = 0; |
1756 | EV_CB_INVOKE (p->w, p->events); |
1804 | EV_CB_INVOKE (p->w, p->events); |
1757 | EV_FREQUENT_CHECK; |
1805 | EV_FREQUENT_CHECK; |
1758 | } |
|
|
1759 | } |
1806 | } |
1760 | } |
1807 | } |
1761 | |
1808 | |
1762 | #if EV_IDLE_ENABLE |
1809 | #if EV_IDLE_ENABLE |
|
|
1810 | /* make idle watchers pending. this handles the "call-idle */ |
|
|
1811 | /* only when higher priorities are idle" logic */ |
1763 | inline_size void |
1812 | inline_size void |
1764 | idle_reify (EV_P) |
1813 | idle_reify (EV_P) |
1765 | { |
1814 | { |
1766 | if (expect_false (idleall)) |
1815 | if (expect_false (idleall)) |
1767 | { |
1816 | { |
… | |
… | |
1780 | } |
1829 | } |
1781 | } |
1830 | } |
1782 | } |
1831 | } |
1783 | #endif |
1832 | #endif |
1784 | |
1833 | |
|
|
1834 | /* make timers pending */ |
1785 | inline_size void |
1835 | inline_size void |
1786 | timers_reify (EV_P) |
1836 | timers_reify (EV_P) |
1787 | { |
1837 | { |
1788 | EV_FREQUENT_CHECK; |
1838 | EV_FREQUENT_CHECK; |
1789 | |
1839 | |
… | |
… | |
1818 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1868 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1819 | } |
1869 | } |
1820 | } |
1870 | } |
1821 | |
1871 | |
1822 | #if EV_PERIODIC_ENABLE |
1872 | #if EV_PERIODIC_ENABLE |
|
|
1873 | /* make periodics pending */ |
1823 | inline_size void |
1874 | inline_size void |
1824 | periodics_reify (EV_P) |
1875 | periodics_reify (EV_P) |
1825 | { |
1876 | { |
1826 | EV_FREQUENT_CHECK; |
1877 | EV_FREQUENT_CHECK; |
1827 | |
1878 | |
… | |
… | |
1874 | |
1925 | |
1875 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1926 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1876 | } |
1927 | } |
1877 | } |
1928 | } |
1878 | |
1929 | |
|
|
1930 | /* simply recalculate all periodics */ |
|
|
1931 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
1879 | static void noinline |
1932 | static void noinline |
1880 | periodics_reschedule (EV_P) |
1933 | periodics_reschedule (EV_P) |
1881 | { |
1934 | { |
1882 | int i; |
1935 | int i; |
1883 | |
1936 | |
… | |
… | |
1896 | |
1949 | |
1897 | reheap (periodics, periodiccnt); |
1950 | reheap (periodics, periodiccnt); |
1898 | } |
1951 | } |
1899 | #endif |
1952 | #endif |
1900 | |
1953 | |
|
|
1954 | /* adjust all timers by a given offset */ |
|
|
1955 | static void noinline |
|
|
1956 | timers_reschedule (EV_P_ ev_tstamp adjust) |
|
|
1957 | { |
|
|
1958 | int i; |
|
|
1959 | |
|
|
1960 | for (i = 0; i < timercnt; ++i) |
|
|
1961 | { |
|
|
1962 | ANHE *he = timers + i + HEAP0; |
|
|
1963 | ANHE_w (*he)->at += adjust; |
|
|
1964 | ANHE_at_cache (*he); |
|
|
1965 | } |
|
|
1966 | } |
|
|
1967 | |
|
|
1968 | /* fetch new monotonic and realtime times from the kernel */ |
|
|
1969 | /* also detetc if there was a timejump, and act accordingly */ |
1901 | inline_speed void |
1970 | inline_speed void |
1902 | time_update (EV_P_ ev_tstamp max_block) |
1971 | time_update (EV_P_ ev_tstamp max_block) |
1903 | { |
1972 | { |
1904 | int i; |
|
|
1905 | |
|
|
1906 | #if EV_USE_MONOTONIC |
1973 | #if EV_USE_MONOTONIC |
1907 | if (expect_true (have_monotonic)) |
1974 | if (expect_true (have_monotonic)) |
1908 | { |
1975 | { |
|
|
1976 | int i; |
1909 | ev_tstamp odiff = rtmn_diff; |
1977 | ev_tstamp odiff = rtmn_diff; |
1910 | |
1978 | |
1911 | mn_now = get_clock (); |
1979 | mn_now = get_clock (); |
1912 | |
1980 | |
1913 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
1981 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
1939 | ev_rt_now = ev_time (); |
2007 | ev_rt_now = ev_time (); |
1940 | mn_now = get_clock (); |
2008 | mn_now = get_clock (); |
1941 | now_floor = mn_now; |
2009 | now_floor = mn_now; |
1942 | } |
2010 | } |
1943 | |
2011 | |
|
|
2012 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
2013 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1944 | # if EV_PERIODIC_ENABLE |
2014 | # if EV_PERIODIC_ENABLE |
1945 | periodics_reschedule (EV_A); |
2015 | periodics_reschedule (EV_A); |
1946 | # endif |
2016 | # endif |
1947 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1948 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
|
|
1949 | } |
2017 | } |
1950 | else |
2018 | else |
1951 | #endif |
2019 | #endif |
1952 | { |
2020 | { |
1953 | ev_rt_now = ev_time (); |
2021 | ev_rt_now = ev_time (); |
1954 | |
2022 | |
1955 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
2023 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
1956 | { |
2024 | { |
|
|
2025 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
|
|
2026 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
1957 | #if EV_PERIODIC_ENABLE |
2027 | #if EV_PERIODIC_ENABLE |
1958 | periodics_reschedule (EV_A); |
2028 | periodics_reschedule (EV_A); |
1959 | #endif |
2029 | #endif |
1960 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
|
|
1961 | for (i = 0; i < timercnt; ++i) |
|
|
1962 | { |
|
|
1963 | ANHE *he = timers + i + HEAP0; |
|
|
1964 | ANHE_w (*he)->at += ev_rt_now - mn_now; |
|
|
1965 | ANHE_at_cache (*he); |
|
|
1966 | } |
|
|
1967 | } |
2030 | } |
1968 | |
2031 | |
1969 | mn_now = ev_rt_now; |
2032 | mn_now = ev_rt_now; |
1970 | } |
2033 | } |
1971 | } |
2034 | } |
1972 | |
2035 | |
1973 | void |
2036 | void |
1974 | ev_ref (EV_P) |
|
|
1975 | { |
|
|
1976 | ++activecnt; |
|
|
1977 | } |
|
|
1978 | |
|
|
1979 | void |
|
|
1980 | ev_unref (EV_P) |
|
|
1981 | { |
|
|
1982 | --activecnt; |
|
|
1983 | } |
|
|
1984 | |
|
|
1985 | void |
|
|
1986 | ev_now_update (EV_P) |
|
|
1987 | { |
|
|
1988 | time_update (EV_A_ 1e100); |
|
|
1989 | } |
|
|
1990 | |
|
|
1991 | static int loop_done; |
|
|
1992 | |
|
|
1993 | void |
|
|
1994 | ev_loop (EV_P_ int flags) |
2037 | ev_loop (EV_P_ int flags) |
1995 | { |
2038 | { |
|
|
2039 | ++loop_depth; |
|
|
2040 | |
1996 | loop_done = EVUNLOOP_CANCEL; |
2041 | loop_done = EVUNLOOP_CANCEL; |
1997 | |
2042 | |
1998 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2043 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1999 | |
2044 | |
2000 | do |
2045 | do |
… | |
… | |
2041 | ev_tstamp waittime = 0.; |
2086 | ev_tstamp waittime = 0.; |
2042 | ev_tstamp sleeptime = 0.; |
2087 | ev_tstamp sleeptime = 0.; |
2043 | |
2088 | |
2044 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2089 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2045 | { |
2090 | { |
|
|
2091 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2092 | ev_tstamp prev_mn_now = mn_now; |
|
|
2093 | |
2046 | /* update time to cancel out callback processing overhead */ |
2094 | /* update time to cancel out callback processing overhead */ |
2047 | time_update (EV_A_ 1e100); |
2095 | time_update (EV_A_ 1e100); |
|
|
2096 | |
|
|
2097 | waittime = MAX_BLOCKTIME; |
2048 | |
2098 | |
2049 | if (timercnt) |
2099 | if (timercnt) |
2050 | { |
2100 | { |
2051 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2101 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2052 | if (waittime > to) waittime = to; |
2102 | if (waittime > to) waittime = to; |
… | |
… | |
2058 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2108 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2059 | if (waittime > to) waittime = to; |
2109 | if (waittime > to) waittime = to; |
2060 | } |
2110 | } |
2061 | #endif |
2111 | #endif |
2062 | |
2112 | |
|
|
2113 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2063 | if (expect_false (waittime < timeout_blocktime)) |
2114 | if (expect_false (waittime < timeout_blocktime)) |
2064 | waittime = timeout_blocktime; |
2115 | waittime = timeout_blocktime; |
2065 | |
2116 | |
2066 | sleeptime = waittime - backend_fudge; |
2117 | /* extra check because io_blocktime is commonly 0 */ |
2067 | |
|
|
2068 | if (expect_true (sleeptime > io_blocktime)) |
2118 | if (expect_false (io_blocktime)) |
2069 | sleeptime = io_blocktime; |
|
|
2070 | |
|
|
2071 | if (sleeptime) |
|
|
2072 | { |
2119 | { |
|
|
2120 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
|
|
2121 | |
|
|
2122 | if (sleeptime > waittime - backend_fudge) |
|
|
2123 | sleeptime = waittime - backend_fudge; |
|
|
2124 | |
|
|
2125 | if (expect_true (sleeptime > 0.)) |
|
|
2126 | { |
2073 | ev_sleep (sleeptime); |
2127 | ev_sleep (sleeptime); |
2074 | waittime -= sleeptime; |
2128 | waittime -= sleeptime; |
|
|
2129 | } |
2075 | } |
2130 | } |
2076 | } |
2131 | } |
2077 | |
2132 | |
2078 | ++loop_count; |
2133 | ++loop_count; |
2079 | backend_poll (EV_A_ waittime); |
2134 | backend_poll (EV_A_ waittime); |
… | |
… | |
2105 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2160 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2106 | )); |
2161 | )); |
2107 | |
2162 | |
2108 | if (loop_done == EVUNLOOP_ONE) |
2163 | if (loop_done == EVUNLOOP_ONE) |
2109 | loop_done = EVUNLOOP_CANCEL; |
2164 | loop_done = EVUNLOOP_CANCEL; |
|
|
2165 | |
|
|
2166 | --loop_depth; |
2110 | } |
2167 | } |
2111 | |
2168 | |
2112 | void |
2169 | void |
2113 | ev_unloop (EV_P_ int how) |
2170 | ev_unloop (EV_P_ int how) |
2114 | { |
2171 | { |
2115 | loop_done = how; |
2172 | loop_done = how; |
2116 | } |
2173 | } |
2117 | |
2174 | |
|
|
2175 | void |
|
|
2176 | ev_ref (EV_P) |
|
|
2177 | { |
|
|
2178 | ++activecnt; |
|
|
2179 | } |
|
|
2180 | |
|
|
2181 | void |
|
|
2182 | ev_unref (EV_P) |
|
|
2183 | { |
|
|
2184 | --activecnt; |
|
|
2185 | } |
|
|
2186 | |
|
|
2187 | void |
|
|
2188 | ev_now_update (EV_P) |
|
|
2189 | { |
|
|
2190 | time_update (EV_A_ 1e100); |
|
|
2191 | } |
|
|
2192 | |
|
|
2193 | void |
|
|
2194 | ev_suspend (EV_P) |
|
|
2195 | { |
|
|
2196 | ev_now_update (EV_A); |
|
|
2197 | } |
|
|
2198 | |
|
|
2199 | void |
|
|
2200 | ev_resume (EV_P) |
|
|
2201 | { |
|
|
2202 | ev_tstamp mn_prev = mn_now; |
|
|
2203 | |
|
|
2204 | ev_now_update (EV_A); |
|
|
2205 | timers_reschedule (EV_A_ mn_now - mn_prev); |
|
|
2206 | #if EV_PERIODIC_ENABLE |
|
|
2207 | /* TODO: really do this? */ |
|
|
2208 | periodics_reschedule (EV_A); |
|
|
2209 | #endif |
|
|
2210 | } |
|
|
2211 | |
2118 | /*****************************************************************************/ |
2212 | /*****************************************************************************/ |
|
|
2213 | /* singly-linked list management, used when the expected list length is short */ |
2119 | |
2214 | |
2120 | inline_size void |
2215 | inline_size void |
2121 | wlist_add (WL *head, WL elem) |
2216 | wlist_add (WL *head, WL elem) |
2122 | { |
2217 | { |
2123 | elem->next = *head; |
2218 | elem->next = *head; |
… | |
… | |
2137 | |
2232 | |
2138 | head = &(*head)->next; |
2233 | head = &(*head)->next; |
2139 | } |
2234 | } |
2140 | } |
2235 | } |
2141 | |
2236 | |
|
|
2237 | /* internal, faster, version of ev_clear_pending */ |
2142 | inline_speed void |
2238 | inline_speed void |
2143 | clear_pending (EV_P_ W w) |
2239 | clear_pending (EV_P_ W w) |
2144 | { |
2240 | { |
2145 | if (w->pending) |
2241 | if (w->pending) |
2146 | { |
2242 | { |
2147 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
2243 | pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; |
2148 | w->pending = 0; |
2244 | w->pending = 0; |
2149 | } |
2245 | } |
2150 | } |
2246 | } |
2151 | |
2247 | |
2152 | int |
2248 | int |
… | |
… | |
2156 | int pending = w_->pending; |
2252 | int pending = w_->pending; |
2157 | |
2253 | |
2158 | if (expect_true (pending)) |
2254 | if (expect_true (pending)) |
2159 | { |
2255 | { |
2160 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
2256 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
2257 | p->w = (W)&pending_w; |
2161 | w_->pending = 0; |
2258 | w_->pending = 0; |
2162 | p->w = 0; |
|
|
2163 | return p->events; |
2259 | return p->events; |
2164 | } |
2260 | } |
2165 | else |
2261 | else |
2166 | return 0; |
2262 | return 0; |
2167 | } |
2263 | } |
2168 | |
2264 | |
2169 | inline_size void |
2265 | inline_size void |
2170 | pri_adjust (EV_P_ W w) |
2266 | pri_adjust (EV_P_ W w) |
2171 | { |
2267 | { |
2172 | int pri = w->priority; |
2268 | int pri = ev_priority (w); |
2173 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2269 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2174 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2270 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2175 | w->priority = pri; |
2271 | ev_set_priority (w, pri); |
2176 | } |
2272 | } |
2177 | |
2273 | |
2178 | inline_speed void |
2274 | inline_speed void |
2179 | ev_start (EV_P_ W w, int active) |
2275 | ev_start (EV_P_ W w, int active) |
2180 | { |
2276 | { |
… | |
… | |
3173 | } |
3269 | } |
3174 | } |
3270 | } |
3175 | |
3271 | |
3176 | /*****************************************************************************/ |
3272 | /*****************************************************************************/ |
3177 | |
3273 | |
3178 | #if 0 |
3274 | #if EV_WALK_ENABLE |
3179 | void |
3275 | void |
3180 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3276 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3181 | { |
3277 | { |
3182 | int i, j; |
3278 | int i, j; |
3183 | ev_watcher_list *wl, *wn; |
3279 | ev_watcher_list *wl, *wn; |
… | |
… | |
3199 | #if EV_USE_INOTIFY |
3295 | #if EV_USE_INOTIFY |
3200 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3296 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3201 | ; |
3297 | ; |
3202 | else |
3298 | else |
3203 | #endif |
3299 | #endif |
3204 | if ((ev_io *)wl != &pipeev) |
3300 | if ((ev_io *)wl != &pipe_w) |
3205 | if (types & EV_IO) |
3301 | if (types & EV_IO) |
3206 | cb (EV_A_ EV_IO, wl); |
3302 | cb (EV_A_ EV_IO, wl); |
3207 | |
3303 | |
3208 | wl = wn; |
3304 | wl = wn; |
3209 | } |
3305 | } |