… | |
… | |
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 | inline_size ev_tstamp |
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 |
… | |
… | |
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 | |
|
|
643 | /* find a suitable new size for the given array, */ |
|
|
644 | /* hopefully by rounding to a ncie-to-malloc size */ |
630 | inline_size int |
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 | |
… | |
… | |
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; stem ## s idx = 0 |
696 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
682 | |
697 | |
683 | /*****************************************************************************/ |
698 | /*****************************************************************************/ |
684 | |
699 | |
|
|
700 | /* dummy callback for pending events */ |
|
|
701 | static void noinline |
|
|
702 | pendingcb (EV_P_ ev_prepare *w, int revents) |
|
|
703 | { |
|
|
704 | } |
|
|
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; |
689 | int pri = ABSPRI (w_); |
710 | int pri = ABSPRI (w_); |
… | |
… | |
745 | { |
766 | { |
746 | if (fd >= 0 && fd < anfdmax) |
767 | if (fd >= 0 && fd < anfdmax) |
747 | fd_event (EV_A_ fd, revents); |
768 | fd_event (EV_A_ fd, revents); |
748 | } |
769 | } |
749 | |
770 | |
|
|
771 | /* make sure the external fd watch events are in-sync */ |
|
|
772 | /* with the kernel/libev internal state */ |
750 | inline_size void |
773 | inline_size void |
751 | fd_reify (EV_P) |
774 | fd_reify (EV_P) |
752 | { |
775 | { |
753 | int i; |
776 | int i; |
754 | |
777 | |
… | |
… | |
789 | } |
812 | } |
790 | |
813 | |
791 | fdchangecnt = 0; |
814 | fdchangecnt = 0; |
792 | } |
815 | } |
793 | |
816 | |
|
|
817 | /* something about the given fd changed */ |
794 | inline_size void |
818 | inline_size void |
795 | fd_change (EV_P_ int fd, int flags) |
819 | fd_change (EV_P_ int fd, int flags) |
796 | { |
820 | { |
797 | unsigned char reify = anfds [fd].reify; |
821 | unsigned char reify = anfds [fd].reify; |
798 | anfds [fd].reify |= flags; |
822 | anfds [fd].reify |= flags; |
… | |
… | |
803 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
827 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
804 | fdchanges [fdchangecnt - 1] = fd; |
828 | fdchanges [fdchangecnt - 1] = fd; |
805 | } |
829 | } |
806 | } |
830 | } |
807 | |
831 | |
|
|
832 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
808 | inline_speed void |
833 | inline_speed void |
809 | fd_kill (EV_P_ int fd) |
834 | fd_kill (EV_P_ int fd) |
810 | { |
835 | { |
811 | ev_io *w; |
836 | ev_io *w; |
812 | |
837 | |
… | |
… | |
815 | ev_io_stop (EV_A_ w); |
840 | ev_io_stop (EV_A_ w); |
816 | 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); |
817 | } |
842 | } |
818 | } |
843 | } |
819 | |
844 | |
|
|
845 | /* check whether the given fd is atcually valid, for error recovery */ |
820 | inline_size int |
846 | inline_size int |
821 | fd_valid (int fd) |
847 | fd_valid (int fd) |
822 | { |
848 | { |
823 | #ifdef _WIN32 |
849 | #ifdef _WIN32 |
824 | return _get_osfhandle (fd) != -1; |
850 | return _get_osfhandle (fd) != -1; |
… | |
… | |
989 | |
1015 | |
990 | heap [k] = he; |
1016 | heap [k] = he; |
991 | ev_active (ANHE_w (he)) = k; |
1017 | ev_active (ANHE_w (he)) = k; |
992 | } |
1018 | } |
993 | |
1019 | |
|
|
1020 | /* move an element suitably so it is in a correct place */ |
994 | inline_size void |
1021 | inline_size void |
995 | adjustheap (ANHE *heap, int N, int k) |
1022 | adjustheap (ANHE *heap, int N, int k) |
996 | { |
1023 | { |
997 | 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])) |
998 | upheap (heap, k); |
1025 | upheap (heap, k); |
… | |
… | |
1012 | upheap (heap, i + HEAP0); |
1039 | upheap (heap, i + HEAP0); |
1013 | } |
1040 | } |
1014 | |
1041 | |
1015 | /*****************************************************************************/ |
1042 | /*****************************************************************************/ |
1016 | |
1043 | |
|
|
1044 | /* associate signal watchers to a signal signal */ |
1017 | typedef struct |
1045 | typedef struct |
1018 | { |
1046 | { |
1019 | WL head; |
1047 | WL head; |
1020 | EV_ATOMIC_T gotsig; |
1048 | EV_ATOMIC_T gotsig; |
1021 | } ANSIG; |
1049 | } ANSIG; |
… | |
… | |
1025 | |
1053 | |
1026 | static EV_ATOMIC_T gotsig; |
1054 | static EV_ATOMIC_T gotsig; |
1027 | |
1055 | |
1028 | /*****************************************************************************/ |
1056 | /*****************************************************************************/ |
1029 | |
1057 | |
|
|
1058 | /* used to prepare libev internal fd's */ |
|
|
1059 | /* this is not fork-safe */ |
1030 | inline_speed void |
1060 | inline_speed void |
1031 | fd_intern (int fd) |
1061 | fd_intern (int fd) |
1032 | { |
1062 | { |
1033 | #ifdef _WIN32 |
1063 | #ifdef _WIN32 |
1034 | unsigned long arg = 1; |
1064 | unsigned long arg = 1; |
… | |
… | |
1040 | } |
1070 | } |
1041 | |
1071 | |
1042 | static void noinline |
1072 | static void noinline |
1043 | evpipe_init (EV_P) |
1073 | evpipe_init (EV_P) |
1044 | { |
1074 | { |
1045 | if (!ev_is_active (&pipeev)) |
1075 | if (!ev_is_active (&pipe_w)) |
1046 | { |
1076 | { |
1047 | #if EV_USE_EVENTFD |
1077 | #if EV_USE_EVENTFD |
1048 | if ((evfd = eventfd (0, 0)) >= 0) |
1078 | if ((evfd = eventfd (0, 0)) >= 0) |
1049 | { |
1079 | { |
1050 | evpipe [0] = -1; |
1080 | evpipe [0] = -1; |
1051 | fd_intern (evfd); |
1081 | fd_intern (evfd); |
1052 | ev_io_set (&pipeev, evfd, EV_READ); |
1082 | ev_io_set (&pipe_w, evfd, EV_READ); |
1053 | } |
1083 | } |
1054 | else |
1084 | else |
1055 | #endif |
1085 | #endif |
1056 | { |
1086 | { |
1057 | while (pipe (evpipe)) |
1087 | while (pipe (evpipe)) |
1058 | ev_syserr ("(libev) error creating signal/async pipe"); |
1088 | ev_syserr ("(libev) error creating signal/async pipe"); |
1059 | |
1089 | |
1060 | fd_intern (evpipe [0]); |
1090 | fd_intern (evpipe [0]); |
1061 | fd_intern (evpipe [1]); |
1091 | fd_intern (evpipe [1]); |
1062 | ev_io_set (&pipeev, evpipe [0], EV_READ); |
1092 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
1063 | } |
1093 | } |
1064 | |
1094 | |
1065 | ev_io_start (EV_A_ &pipeev); |
1095 | ev_io_start (EV_A_ &pipe_w); |
1066 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1096 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1067 | } |
1097 | } |
1068 | } |
1098 | } |
1069 | |
1099 | |
1070 | inline_size void |
1100 | inline_size void |
… | |
… | |
1088 | |
1118 | |
1089 | errno = old_errno; |
1119 | errno = old_errno; |
1090 | } |
1120 | } |
1091 | } |
1121 | } |
1092 | |
1122 | |
|
|
1123 | /* called whenever the libev signal pipe */ |
|
|
1124 | /* got some events (signal, async) */ |
1093 | static void |
1125 | static void |
1094 | pipecb (EV_P_ ev_io *iow, int revents) |
1126 | pipecb (EV_P_ ev_io *iow, int revents) |
1095 | { |
1127 | { |
1096 | #if EV_USE_EVENTFD |
1128 | #if EV_USE_EVENTFD |
1097 | if (evfd >= 0) |
1129 | if (evfd >= 0) |
… | |
… | |
1179 | |
1211 | |
1180 | #ifndef WIFCONTINUED |
1212 | #ifndef WIFCONTINUED |
1181 | # define WIFCONTINUED(status) 0 |
1213 | # define WIFCONTINUED(status) 0 |
1182 | #endif |
1214 | #endif |
1183 | |
1215 | |
|
|
1216 | /* handle a single child status event */ |
1184 | inline_speed void |
1217 | inline_speed void |
1185 | child_reap (EV_P_ int chain, int pid, int status) |
1218 | child_reap (EV_P_ int chain, int pid, int status) |
1186 | { |
1219 | { |
1187 | ev_child *w; |
1220 | ev_child *w; |
1188 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1221 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
… | |
… | |
1202 | |
1235 | |
1203 | #ifndef WCONTINUED |
1236 | #ifndef WCONTINUED |
1204 | # define WCONTINUED 0 |
1237 | # define WCONTINUED 0 |
1205 | #endif |
1238 | #endif |
1206 | |
1239 | |
|
|
1240 | /* called on sigchld etc., calls waitpid */ |
1207 | static void |
1241 | static void |
1208 | childcb (EV_P_ ev_signal *sw, int revents) |
1242 | childcb (EV_P_ ev_signal *sw, int revents) |
1209 | { |
1243 | { |
1210 | int pid, status; |
1244 | int pid, status; |
1211 | |
1245 | |
… | |
… | |
1336 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1370 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1337 | { |
1371 | { |
1338 | timeout_blocktime = interval; |
1372 | timeout_blocktime = interval; |
1339 | } |
1373 | } |
1340 | |
1374 | |
|
|
1375 | /* initialise a loop structure, must be zero-initialised */ |
1341 | static void noinline |
1376 | static void noinline |
1342 | loop_init (EV_P_ unsigned int flags) |
1377 | loop_init (EV_P_ unsigned int flags) |
1343 | { |
1378 | { |
1344 | if (!backend) |
1379 | if (!backend) |
1345 | { |
1380 | { |
… | |
… | |
1405 | #endif |
1440 | #endif |
1406 | #if EV_USE_SELECT |
1441 | #if EV_USE_SELECT |
1407 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1442 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1408 | #endif |
1443 | #endif |
1409 | |
1444 | |
|
|
1445 | ev_prepare_init (&pending_w, pendingcb); |
|
|
1446 | |
1410 | ev_init (&pipeev, pipecb); |
1447 | ev_init (&pipe_w, pipecb); |
1411 | ev_set_priority (&pipeev, EV_MAXPRI); |
1448 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1412 | } |
1449 | } |
1413 | } |
1450 | } |
1414 | |
1451 | |
|
|
1452 | /* free up a loop structure */ |
1415 | static void noinline |
1453 | static void noinline |
1416 | loop_destroy (EV_P) |
1454 | loop_destroy (EV_P) |
1417 | { |
1455 | { |
1418 | int i; |
1456 | int i; |
1419 | |
1457 | |
1420 | if (ev_is_active (&pipeev)) |
1458 | if (ev_is_active (&pipe_w)) |
1421 | { |
1459 | { |
1422 | ev_ref (EV_A); /* signal watcher */ |
1460 | ev_ref (EV_A); /* signal watcher */ |
1423 | ev_io_stop (EV_A_ &pipeev); |
1461 | ev_io_stop (EV_A_ &pipe_w); |
1424 | |
1462 | |
1425 | #if EV_USE_EVENTFD |
1463 | #if EV_USE_EVENTFD |
1426 | if (evfd >= 0) |
1464 | if (evfd >= 0) |
1427 | close (evfd); |
1465 | close (evfd); |
1428 | #endif |
1466 | #endif |
… | |
… | |
1505 | #endif |
1543 | #endif |
1506 | #if EV_USE_INOTIFY |
1544 | #if EV_USE_INOTIFY |
1507 | infy_fork (EV_A); |
1545 | infy_fork (EV_A); |
1508 | #endif |
1546 | #endif |
1509 | |
1547 | |
1510 | if (ev_is_active (&pipeev)) |
1548 | if (ev_is_active (&pipe_w)) |
1511 | { |
1549 | { |
1512 | /* this "locks" the handlers against writing to the pipe */ |
1550 | /* this "locks" the handlers against writing to the pipe */ |
1513 | /* while we modify the fd vars */ |
1551 | /* while we modify the fd vars */ |
1514 | gotsig = 1; |
1552 | gotsig = 1; |
1515 | #if EV_ASYNC_ENABLE |
1553 | #if EV_ASYNC_ENABLE |
1516 | gotasync = 1; |
1554 | gotasync = 1; |
1517 | #endif |
1555 | #endif |
1518 | |
1556 | |
1519 | ev_ref (EV_A); |
1557 | ev_ref (EV_A); |
1520 | ev_io_stop (EV_A_ &pipeev); |
1558 | ev_io_stop (EV_A_ &pipe_w); |
1521 | |
1559 | |
1522 | #if EV_USE_EVENTFD |
1560 | #if EV_USE_EVENTFD |
1523 | if (evfd >= 0) |
1561 | if (evfd >= 0) |
1524 | close (evfd); |
1562 | close (evfd); |
1525 | #endif |
1563 | #endif |
… | |
… | |
1530 | close (evpipe [1]); |
1568 | close (evpipe [1]); |
1531 | } |
1569 | } |
1532 | |
1570 | |
1533 | evpipe_init (EV_A); |
1571 | evpipe_init (EV_A); |
1534 | /* now iterate over everything, in case we missed something */ |
1572 | /* now iterate over everything, in case we missed something */ |
1535 | pipecb (EV_A_ &pipeev, EV_READ); |
1573 | pipecb (EV_A_ &pipe_w, EV_READ); |
1536 | } |
1574 | } |
1537 | |
1575 | |
1538 | postfork = 0; |
1576 | postfork = 0; |
1539 | } |
1577 | } |
1540 | |
1578 | |
… | |
… | |
1746 | for (pri = NUMPRI; pri--; ) |
1784 | for (pri = NUMPRI; pri--; ) |
1747 | while (pendingcnt [pri]) |
1785 | while (pendingcnt [pri]) |
1748 | { |
1786 | { |
1749 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1787 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1750 | |
1788 | |
1751 | if (expect_true (p->w)) |
|
|
1752 | { |
|
|
1753 | /*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 */ |
1754 | |
1791 | |
1755 | p->w->pending = 0; |
1792 | p->w->pending = 0; |
1756 | EV_CB_INVOKE (p->w, p->events); |
1793 | EV_CB_INVOKE (p->w, p->events); |
1757 | EV_FREQUENT_CHECK; |
1794 | EV_FREQUENT_CHECK; |
1758 | } |
|
|
1759 | } |
1795 | } |
1760 | } |
1796 | } |
1761 | |
1797 | |
1762 | #if EV_IDLE_ENABLE |
1798 | #if EV_IDLE_ENABLE |
|
|
1799 | /* make idle watchers pending. this handles the "call-idle */ |
|
|
1800 | /* only when higher priorities are idle" logic */ |
1763 | inline_size void |
1801 | inline_size void |
1764 | idle_reify (EV_P) |
1802 | idle_reify (EV_P) |
1765 | { |
1803 | { |
1766 | if (expect_false (idleall)) |
1804 | if (expect_false (idleall)) |
1767 | { |
1805 | { |
… | |
… | |
1780 | } |
1818 | } |
1781 | } |
1819 | } |
1782 | } |
1820 | } |
1783 | #endif |
1821 | #endif |
1784 | |
1822 | |
|
|
1823 | /* make timers pending */ |
1785 | inline_size void |
1824 | inline_size void |
1786 | timers_reify (EV_P) |
1825 | timers_reify (EV_P) |
1787 | { |
1826 | { |
1788 | EV_FREQUENT_CHECK; |
1827 | EV_FREQUENT_CHECK; |
1789 | |
1828 | |
… | |
… | |
1818 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1857 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1819 | } |
1858 | } |
1820 | } |
1859 | } |
1821 | |
1860 | |
1822 | #if EV_PERIODIC_ENABLE |
1861 | #if EV_PERIODIC_ENABLE |
|
|
1862 | /* make periodics pending */ |
1823 | inline_size void |
1863 | inline_size void |
1824 | periodics_reify (EV_P) |
1864 | periodics_reify (EV_P) |
1825 | { |
1865 | { |
1826 | EV_FREQUENT_CHECK; |
1866 | EV_FREQUENT_CHECK; |
1827 | |
1867 | |
… | |
… | |
1874 | |
1914 | |
1875 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1915 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1876 | } |
1916 | } |
1877 | } |
1917 | } |
1878 | |
1918 | |
|
|
1919 | /* simply recalculate all periodics */ |
|
|
1920 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
1879 | static void noinline |
1921 | static void noinline |
1880 | periodics_reschedule (EV_P) |
1922 | periodics_reschedule (EV_P) |
1881 | { |
1923 | { |
1882 | int i; |
1924 | int i; |
1883 | |
1925 | |
… | |
… | |
1896 | |
1938 | |
1897 | reheap (periodics, periodiccnt); |
1939 | reheap (periodics, periodiccnt); |
1898 | } |
1940 | } |
1899 | #endif |
1941 | #endif |
1900 | |
1942 | |
|
|
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 */ |
1901 | inline_speed void |
1959 | inline_speed void |
1902 | time_update (EV_P_ ev_tstamp max_block) |
1960 | time_update (EV_P_ ev_tstamp max_block) |
1903 | { |
1961 | { |
1904 | int i; |
|
|
1905 | |
|
|
1906 | #if EV_USE_MONOTONIC |
1962 | #if EV_USE_MONOTONIC |
1907 | if (expect_true (have_monotonic)) |
1963 | if (expect_true (have_monotonic)) |
1908 | { |
1964 | { |
|
|
1965 | int i; |
1909 | ev_tstamp odiff = rtmn_diff; |
1966 | ev_tstamp odiff = rtmn_diff; |
1910 | |
1967 | |
1911 | mn_now = get_clock (); |
1968 | mn_now = get_clock (); |
1912 | |
1969 | |
1913 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
1970 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
1939 | ev_rt_now = ev_time (); |
1996 | ev_rt_now = ev_time (); |
1940 | mn_now = get_clock (); |
1997 | mn_now = get_clock (); |
1941 | now_floor = mn_now; |
1998 | now_floor = mn_now; |
1942 | } |
1999 | } |
1943 | |
2000 | |
|
|
2001 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
2002 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1944 | # if EV_PERIODIC_ENABLE |
2003 | # if EV_PERIODIC_ENABLE |
1945 | periodics_reschedule (EV_A); |
2004 | periodics_reschedule (EV_A); |
1946 | # endif |
2005 | # endif |
1947 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1948 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
|
|
1949 | } |
2006 | } |
1950 | else |
2007 | else |
1951 | #endif |
2008 | #endif |
1952 | { |
2009 | { |
1953 | ev_rt_now = ev_time (); |
2010 | ev_rt_now = ev_time (); |
1954 | |
2011 | |
1955 | 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)) |
1956 | { |
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); |
1957 | #if EV_PERIODIC_ENABLE |
2016 | #if EV_PERIODIC_ENABLE |
1958 | periodics_reschedule (EV_A); |
2017 | periodics_reschedule (EV_A); |
1959 | #endif |
2018 | #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 | } |
2019 | } |
1968 | |
2020 | |
1969 | mn_now = ev_rt_now; |
2021 | mn_now = ev_rt_now; |
1970 | } |
2022 | } |
1971 | } |
|
|
1972 | |
|
|
1973 | 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 | } |
2023 | } |
1990 | |
2024 | |
1991 | static int loop_done; |
2025 | static int loop_done; |
1992 | |
2026 | |
1993 | void |
2027 | void |
… | |
… | |
2044 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2078 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2045 | { |
2079 | { |
2046 | /* update time to cancel out callback processing overhead */ |
2080 | /* update time to cancel out callback processing overhead */ |
2047 | time_update (EV_A_ 1e100); |
2081 | time_update (EV_A_ 1e100); |
2048 | |
2082 | |
|
|
2083 | waittime = MAX_BLOCKTIME; |
|
|
2084 | |
2049 | if (timercnt) |
2085 | if (timercnt) |
2050 | { |
2086 | { |
2051 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2087 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2052 | if (waittime > to) waittime = to; |
2088 | if (waittime > to) waittime = to; |
2053 | } |
2089 | } |
… | |
… | |
2113 | ev_unloop (EV_P_ int how) |
2149 | ev_unloop (EV_P_ int how) |
2114 | { |
2150 | { |
2115 | loop_done = how; |
2151 | loop_done = how; |
2116 | } |
2152 | } |
2117 | |
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 | |
2118 | /*****************************************************************************/ |
2191 | /*****************************************************************************/ |
|
|
2192 | /* singly-linked list management, used when the expected list length is short */ |
2119 | |
2193 | |
2120 | inline_size void |
2194 | inline_size void |
2121 | wlist_add (WL *head, WL elem) |
2195 | wlist_add (WL *head, WL elem) |
2122 | { |
2196 | { |
2123 | elem->next = *head; |
2197 | elem->next = *head; |
… | |
… | |
2137 | |
2211 | |
2138 | head = &(*head)->next; |
2212 | head = &(*head)->next; |
2139 | } |
2213 | } |
2140 | } |
2214 | } |
2141 | |
2215 | |
|
|
2216 | /* internal, faster, version of ev_clear_pending */ |
2142 | inline_speed void |
2217 | inline_speed void |
2143 | clear_pending (EV_P_ W w) |
2218 | clear_pending (EV_P_ W w) |
2144 | { |
2219 | { |
2145 | if (w->pending) |
2220 | if (w->pending) |
2146 | { |
2221 | { |
2147 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
2222 | pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; |
2148 | w->pending = 0; |
2223 | w->pending = 0; |
2149 | } |
2224 | } |
2150 | } |
2225 | } |
2151 | |
2226 | |
2152 | int |
2227 | int |
… | |
… | |
2156 | int pending = w_->pending; |
2231 | int pending = w_->pending; |
2157 | |
2232 | |
2158 | if (expect_true (pending)) |
2233 | if (expect_true (pending)) |
2159 | { |
2234 | { |
2160 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
2235 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
2236 | p->w = (W)&pending_w; |
2161 | w_->pending = 0; |
2237 | w_->pending = 0; |
2162 | p->w = 0; |
|
|
2163 | return p->events; |
2238 | return p->events; |
2164 | } |
2239 | } |
2165 | else |
2240 | else |
2166 | return 0; |
2241 | return 0; |
2167 | } |
2242 | } |
… | |
… | |
3173 | } |
3248 | } |
3174 | } |
3249 | } |
3175 | |
3250 | |
3176 | /*****************************************************************************/ |
3251 | /*****************************************************************************/ |
3177 | |
3252 | |
3178 | #if 0 |
3253 | #if EV_WALK_ENABLE |
3179 | void |
3254 | void |
3180 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3255 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3181 | { |
3256 | { |
3182 | int i, j; |
3257 | int i, j; |
3183 | ev_watcher_list *wl, *wn; |
3258 | ev_watcher_list *wl, *wn; |
… | |
… | |
3199 | #if EV_USE_INOTIFY |
3274 | #if EV_USE_INOTIFY |
3200 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3275 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3201 | ; |
3276 | ; |
3202 | else |
3277 | else |
3203 | #endif |
3278 | #endif |
3204 | if ((ev_io *)wl != &pipeev) |
3279 | if ((ev_io *)wl != &pipe_w) |
3205 | if (types & EV_IO) |
3280 | if (types & EV_IO) |
3206 | cb (EV_A_ EV_IO, wl); |
3281 | cb (EV_A_ EV_IO, wl); |
3207 | |
3282 | |
3208 | wl = wn; |
3283 | wl = wn; |
3209 | } |
3284 | } |