… | |
… | |
325 | |
325 | |
326 | typedef ev_watcher *W; |
326 | typedef ev_watcher *W; |
327 | typedef ev_watcher_list *WL; |
327 | typedef ev_watcher_list *WL; |
328 | typedef ev_watcher_time *WT; |
328 | typedef ev_watcher_time *WT; |
329 | |
329 | |
|
|
330 | #define ev_active(w) ((W)(w))->active |
|
|
331 | #define ev_at(w) ((WT)(w))->at |
|
|
332 | |
330 | #if EV_USE_MONOTONIC |
333 | #if EV_USE_MONOTONIC |
331 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
334 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
332 | /* giving it a reasonably high chance of working on typical architetcures */ |
335 | /* giving it a reasonably high chance of working on typical architetcures */ |
333 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
336 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
334 | #endif |
337 | #endif |
… | |
… | |
419 | W w; |
422 | W w; |
420 | int events; |
423 | int events; |
421 | } ANPENDING; |
424 | } ANPENDING; |
422 | |
425 | |
423 | #if EV_USE_INOTIFY |
426 | #if EV_USE_INOTIFY |
|
|
427 | /* hash table entry per inotify-id */ |
424 | typedef struct |
428 | typedef struct |
425 | { |
429 | { |
426 | WL head; |
430 | WL head; |
427 | } ANFS; |
431 | } ANFS; |
|
|
432 | #endif |
|
|
433 | |
|
|
434 | /* Heap Entry */ |
|
|
435 | #define EV_HEAP_CACHE_AT 0 |
|
|
436 | #if EV_HEAP_CACHE_AT |
|
|
437 | typedef struct { |
|
|
438 | WT w; |
|
|
439 | ev_tstamp at; |
|
|
440 | } ANHE; |
|
|
441 | |
|
|
442 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
|
|
443 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
|
|
444 | #define ANHE_at_set(he) (he).at = (he).w->at /* update at from watcher */ |
|
|
445 | #else |
|
|
446 | typedef WT ANHE; |
|
|
447 | |
|
|
448 | #define ANHE_w(he) (he) |
|
|
449 | #define ANHE_at(he) (he)->at |
|
|
450 | #define ANHE_at_set(he) |
428 | #endif |
451 | #endif |
429 | |
452 | |
430 | #if EV_MULTIPLICITY |
453 | #if EV_MULTIPLICITY |
431 | |
454 | |
432 | struct ev_loop |
455 | struct ev_loop |
… | |
… | |
517 | } |
540 | } |
518 | } |
541 | } |
519 | |
542 | |
520 | /*****************************************************************************/ |
543 | /*****************************************************************************/ |
521 | |
544 | |
|
|
545 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
|
|
546 | |
522 | int inline_size |
547 | int inline_size |
523 | array_nextsize (int elem, int cur, int cnt) |
548 | array_nextsize (int elem, int cur, int cnt) |
524 | { |
549 | { |
525 | int ncur = cur + 1; |
550 | int ncur = cur + 1; |
526 | |
551 | |
527 | do |
552 | do |
528 | ncur <<= 1; |
553 | ncur <<= 1; |
529 | while (cnt > ncur); |
554 | while (cnt > ncur); |
530 | |
555 | |
531 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
556 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
532 | if (elem * ncur > 4096) |
557 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
533 | { |
558 | { |
534 | ncur *= elem; |
559 | ncur *= elem; |
535 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
560 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
536 | ncur = ncur - sizeof (void *) * 4; |
561 | ncur = ncur - sizeof (void *) * 4; |
537 | ncur /= elem; |
562 | ncur /= elem; |
538 | } |
563 | } |
539 | |
564 | |
540 | return ncur; |
565 | return ncur; |
… | |
… | |
754 | } |
779 | } |
755 | } |
780 | } |
756 | |
781 | |
757 | /*****************************************************************************/ |
782 | /*****************************************************************************/ |
758 | |
783 | |
|
|
784 | /* |
|
|
785 | * the heap functions want a real array index. array index 0 uis guaranteed to not |
|
|
786 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
|
|
787 | * the branching factor of the d-tree. |
|
|
788 | */ |
|
|
789 | |
|
|
790 | /* |
|
|
791 | * at the moment we allow libev the luxury of two heaps, |
|
|
792 | * a small-code-size 2-heap one and a ~1.5kb larger 4-heap |
|
|
793 | * which is more cache-efficient. |
|
|
794 | * the difference is about 5% with 50000+ watchers. |
|
|
795 | */ |
|
|
796 | #define EV_USE_4HEAP !EV_MINIMAL |
|
|
797 | #if EV_USE_4HEAP |
|
|
798 | |
|
|
799 | #define DHEAP 4 |
|
|
800 | #define HEAP0 (DHEAP - 1) /* index of first element in heap */ |
|
|
801 | |
|
|
802 | /* towards the root */ |
759 | void inline_speed |
803 | void inline_speed |
760 | upheap (WT *heap, int k) |
804 | upheap (ANHE *heap, int k) |
761 | { |
805 | { |
762 | WT w = heap [k]; |
806 | ANHE he = heap [k]; |
763 | |
807 | |
764 | while (k) |
808 | for (;;) |
765 | { |
809 | { |
766 | int p = (k - 1) >> 1; |
810 | int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0; |
767 | |
811 | |
768 | if (heap [p]->at <= w->at) |
812 | if (p == k || ANHE_at (heap [p]) <= ANHE_at (he)) |
769 | break; |
813 | break; |
770 | |
814 | |
771 | heap [k] = heap [p]; |
815 | heap [k] = heap [p]; |
772 | ((W)heap [k])->active = k + 1; |
816 | ev_active (ANHE_w (heap [k])) = k; |
773 | k = p; |
817 | k = p; |
774 | } |
818 | } |
775 | |
819 | |
|
|
820 | ev_active (ANHE_w (he)) = k; |
|
|
821 | heap [k] = he; |
|
|
822 | } |
|
|
823 | |
|
|
824 | /* away from the root */ |
|
|
825 | void inline_speed |
|
|
826 | downheap (ANHE *heap, int N, int k) |
|
|
827 | { |
|
|
828 | ANHE he = heap [k]; |
|
|
829 | ANHE *E = heap + N + HEAP0; |
|
|
830 | |
|
|
831 | for (;;) |
|
|
832 | { |
|
|
833 | ev_tstamp minat; |
|
|
834 | ANHE *minpos; |
|
|
835 | ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0; |
|
|
836 | |
|
|
837 | // find minimum child |
|
|
838 | if (expect_true (pos + DHEAP - 1 < E)) |
|
|
839 | { |
|
|
840 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
|
|
841 | if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
|
|
842 | if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
|
|
843 | if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
|
|
844 | } |
|
|
845 | else if (pos < E) |
|
|
846 | { |
|
|
847 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
|
|
848 | if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
|
|
849 | if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
|
|
850 | if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
|
|
851 | } |
|
|
852 | else |
|
|
853 | break; |
|
|
854 | |
|
|
855 | if (ANHE_at (he) <= minat) |
|
|
856 | break; |
|
|
857 | |
|
|
858 | ev_active (ANHE_w (*minpos)) = k; |
|
|
859 | heap [k] = *minpos; |
|
|
860 | |
|
|
861 | k = minpos - heap; |
|
|
862 | } |
|
|
863 | |
|
|
864 | ev_active (ANHE_w (he)) = k; |
|
|
865 | heap [k] = he; |
|
|
866 | } |
|
|
867 | |
|
|
868 | #else // 4HEAP |
|
|
869 | |
|
|
870 | #define HEAP0 1 |
|
|
871 | |
|
|
872 | /* towards the root */ |
|
|
873 | void inline_speed |
|
|
874 | upheap (ANHE *heap, int k) |
|
|
875 | { |
|
|
876 | ANHE he = heap [k]; |
|
|
877 | |
|
|
878 | for (;;) |
|
|
879 | { |
|
|
880 | int p = k >> 1; |
|
|
881 | |
|
|
882 | /* maybe we could use a dummy element at heap [0]? */ |
|
|
883 | if (!p || ANHE_at (heap [p]) <= ANHE_at (he)) |
|
|
884 | break; |
|
|
885 | |
|
|
886 | heap [k] = heap [p]; |
|
|
887 | ev_active (ANHE_w (heap [k])) = k; |
|
|
888 | k = p; |
|
|
889 | } |
|
|
890 | |
776 | heap [k] = w; |
891 | heap [k] = w; |
777 | ((W)heap [k])->active = k + 1; |
892 | ev_active (ANHE_w (heap [k])) = k; |
778 | } |
893 | } |
779 | |
894 | |
|
|
895 | /* away from the root */ |
780 | void inline_speed |
896 | void inline_speed |
781 | downheap (WT *heap, int N, int k) |
897 | downheap (ANHE *heap, int N, int k) |
782 | { |
898 | { |
783 | WT w = heap [k]; |
899 | ANHE he = heap [k]; |
784 | |
900 | |
785 | for (;;) |
901 | for (;;) |
786 | { |
902 | { |
787 | int c = (k << 1) + 1; |
903 | int c = k << 1; |
788 | |
904 | |
789 | if (c >= N) |
905 | if (c > N) |
790 | break; |
906 | break; |
791 | |
907 | |
792 | c += c + 1 < N && heap [c]->at > heap [c + 1]->at |
908 | c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
793 | ? 1 : 0; |
909 | ? 1 : 0; |
794 | |
910 | |
795 | if (w->at <= heap [c]->at) |
911 | if (w->at <= ANHE_at (heap [c])) |
796 | break; |
912 | break; |
797 | |
913 | |
798 | heap [k] = heap [c]; |
914 | heap [k] = heap [c]; |
799 | ((W)heap [k])->active = k + 1; |
915 | ev_active (ANHE_w (heap [k])) = k; |
800 | |
916 | |
801 | k = c; |
917 | k = c; |
802 | } |
918 | } |
803 | |
919 | |
804 | heap [k] = w; |
920 | heap [k] = he; |
805 | ((W)heap [k])->active = k + 1; |
921 | ev_active (ANHE_w (he)) = k; |
806 | } |
922 | } |
|
|
923 | #endif |
807 | |
924 | |
808 | void inline_size |
925 | void inline_size |
809 | adjustheap (WT *heap, int N, int k) |
926 | adjustheap (ANHE *heap, int N, int k) |
810 | { |
927 | { |
811 | upheap (heap, k); |
928 | upheap (heap, k); |
812 | downheap (heap, N, k); |
929 | downheap (heap, N, k); |
813 | } |
930 | } |
814 | |
931 | |
… | |
… | |
906 | pipecb (EV_P_ ev_io *iow, int revents) |
1023 | pipecb (EV_P_ ev_io *iow, int revents) |
907 | { |
1024 | { |
908 | #if EV_USE_EVENTFD |
1025 | #if EV_USE_EVENTFD |
909 | if (evfd >= 0) |
1026 | if (evfd >= 0) |
910 | { |
1027 | { |
911 | uint64_t counter = 1; |
1028 | uint64_t counter; |
912 | read (evfd, &counter, sizeof (uint64_t)); |
1029 | read (evfd, &counter, sizeof (uint64_t)); |
913 | } |
1030 | } |
914 | else |
1031 | else |
915 | #endif |
1032 | #endif |
916 | { |
1033 | { |
… | |
… | |
1185 | if (!(flags & EVFLAG_NOENV) |
1302 | if (!(flags & EVFLAG_NOENV) |
1186 | && !enable_secure () |
1303 | && !enable_secure () |
1187 | && getenv ("LIBEV_FLAGS")) |
1304 | && getenv ("LIBEV_FLAGS")) |
1188 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1305 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1189 | |
1306 | |
1190 | if (!(flags & 0x0000ffffUL)) |
1307 | if (!(flags & 0x0000ffffU)) |
1191 | flags |= ev_recommended_backends (); |
1308 | flags |= ev_recommended_backends (); |
1192 | |
1309 | |
1193 | #if EV_USE_PORT |
1310 | #if EV_USE_PORT |
1194 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1311 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1195 | #endif |
1312 | #endif |
… | |
… | |
1283 | #endif |
1400 | #endif |
1284 | |
1401 | |
1285 | backend = 0; |
1402 | backend = 0; |
1286 | } |
1403 | } |
1287 | |
1404 | |
|
|
1405 | #if EV_USE_INOTIFY |
1288 | void inline_size infy_fork (EV_P); |
1406 | void inline_size infy_fork (EV_P); |
|
|
1407 | #endif |
1289 | |
1408 | |
1290 | void inline_size |
1409 | void inline_size |
1291 | loop_fork (EV_P) |
1410 | loop_fork (EV_P) |
1292 | { |
1411 | { |
1293 | #if EV_USE_PORT |
1412 | #if EV_USE_PORT |
… | |
… | |
1360 | void |
1479 | void |
1361 | ev_loop_fork (EV_P) |
1480 | ev_loop_fork (EV_P) |
1362 | { |
1481 | { |
1363 | postfork = 1; /* must be in line with ev_default_fork */ |
1482 | postfork = 1; /* must be in line with ev_default_fork */ |
1364 | } |
1483 | } |
1365 | |
|
|
1366 | #endif |
1484 | #endif |
1367 | |
1485 | |
1368 | #if EV_MULTIPLICITY |
1486 | #if EV_MULTIPLICITY |
1369 | struct ev_loop * |
1487 | struct ev_loop * |
1370 | ev_default_loop_init (unsigned int flags) |
1488 | ev_default_loop_init (unsigned int flags) |
… | |
… | |
1451 | EV_CB_INVOKE (p->w, p->events); |
1569 | EV_CB_INVOKE (p->w, p->events); |
1452 | } |
1570 | } |
1453 | } |
1571 | } |
1454 | } |
1572 | } |
1455 | |
1573 | |
1456 | void inline_size |
|
|
1457 | timers_reify (EV_P) |
|
|
1458 | { |
|
|
1459 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
|
|
1460 | { |
|
|
1461 | ev_timer *w = (ev_timer *)timers [0]; |
|
|
1462 | |
|
|
1463 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1464 | |
|
|
1465 | /* first reschedule or stop timer */ |
|
|
1466 | if (w->repeat) |
|
|
1467 | { |
|
|
1468 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1469 | |
|
|
1470 | ((WT)w)->at += w->repeat; |
|
|
1471 | if (((WT)w)->at < mn_now) |
|
|
1472 | ((WT)w)->at = mn_now; |
|
|
1473 | |
|
|
1474 | downheap (timers, timercnt, 0); |
|
|
1475 | } |
|
|
1476 | else |
|
|
1477 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1478 | |
|
|
1479 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
|
|
1480 | } |
|
|
1481 | } |
|
|
1482 | |
|
|
1483 | #if EV_PERIODIC_ENABLE |
|
|
1484 | void inline_size |
|
|
1485 | periodics_reify (EV_P) |
|
|
1486 | { |
|
|
1487 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
|
|
1488 | { |
|
|
1489 | ev_periodic *w = (ev_periodic *)periodics [0]; |
|
|
1490 | |
|
|
1491 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
|
|
1492 | |
|
|
1493 | /* first reschedule or stop timer */ |
|
|
1494 | if (w->reschedule_cb) |
|
|
1495 | { |
|
|
1496 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
|
|
1497 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1498 | downheap (periodics, periodiccnt, 0); |
|
|
1499 | } |
|
|
1500 | else if (w->interval) |
|
|
1501 | { |
|
|
1502 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1503 | if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; |
|
|
1504 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
|
|
1505 | downheap (periodics, periodiccnt, 0); |
|
|
1506 | } |
|
|
1507 | else |
|
|
1508 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1509 | |
|
|
1510 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
|
|
1511 | } |
|
|
1512 | } |
|
|
1513 | |
|
|
1514 | static void noinline |
|
|
1515 | periodics_reschedule (EV_P) |
|
|
1516 | { |
|
|
1517 | int i; |
|
|
1518 | |
|
|
1519 | /* adjust periodics after time jump */ |
|
|
1520 | for (i = 0; i < periodiccnt; ++i) |
|
|
1521 | { |
|
|
1522 | ev_periodic *w = (ev_periodic *)periodics [i]; |
|
|
1523 | |
|
|
1524 | if (w->reschedule_cb) |
|
|
1525 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1526 | else if (w->interval) |
|
|
1527 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1528 | } |
|
|
1529 | |
|
|
1530 | /* now rebuild the heap */ |
|
|
1531 | for (i = periodiccnt >> 1; i--; ) |
|
|
1532 | downheap (periodics, periodiccnt, i); |
|
|
1533 | } |
|
|
1534 | #endif |
|
|
1535 | |
|
|
1536 | #if EV_IDLE_ENABLE |
1574 | #if EV_IDLE_ENABLE |
1537 | void inline_size |
1575 | void inline_size |
1538 | idle_reify (EV_P) |
1576 | idle_reify (EV_P) |
1539 | { |
1577 | { |
1540 | if (expect_false (idleall)) |
1578 | if (expect_false (idleall)) |
… | |
… | |
1551 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
1589 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
1552 | break; |
1590 | break; |
1553 | } |
1591 | } |
1554 | } |
1592 | } |
1555 | } |
1593 | } |
|
|
1594 | } |
|
|
1595 | #endif |
|
|
1596 | |
|
|
1597 | void inline_size |
|
|
1598 | timers_reify (EV_P) |
|
|
1599 | { |
|
|
1600 | while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now) |
|
|
1601 | { |
|
|
1602 | ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); |
|
|
1603 | |
|
|
1604 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1605 | |
|
|
1606 | /* first reschedule or stop timer */ |
|
|
1607 | if (w->repeat) |
|
|
1608 | { |
|
|
1609 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1610 | |
|
|
1611 | ev_at (w) += w->repeat; |
|
|
1612 | if (ev_at (w) < mn_now) |
|
|
1613 | ev_at (w) = mn_now; |
|
|
1614 | |
|
|
1615 | ANHE_at_set (timers [HEAP0]); |
|
|
1616 | downheap (timers, timercnt, HEAP0); |
|
|
1617 | } |
|
|
1618 | else |
|
|
1619 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1620 | |
|
|
1621 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
|
|
1622 | } |
|
|
1623 | } |
|
|
1624 | |
|
|
1625 | #if EV_PERIODIC_ENABLE |
|
|
1626 | void inline_size |
|
|
1627 | periodics_reify (EV_P) |
|
|
1628 | { |
|
|
1629 | while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now) |
|
|
1630 | { |
|
|
1631 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
|
|
1632 | |
|
|
1633 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
|
|
1634 | |
|
|
1635 | /* first reschedule or stop timer */ |
|
|
1636 | if (w->reschedule_cb) |
|
|
1637 | { |
|
|
1638 | ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
|
|
1639 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now)); |
|
|
1640 | ANHE_at_set (periodics [HEAP0]); |
|
|
1641 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1642 | } |
|
|
1643 | else if (w->interval) |
|
|
1644 | { |
|
|
1645 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1646 | if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; |
|
|
1647 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); |
|
|
1648 | ANHE_at_set (periodics [HEAP0]); |
|
|
1649 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1650 | } |
|
|
1651 | else |
|
|
1652 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1653 | |
|
|
1654 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
|
|
1655 | } |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | static void noinline |
|
|
1659 | periodics_reschedule (EV_P) |
|
|
1660 | { |
|
|
1661 | int i; |
|
|
1662 | |
|
|
1663 | /* adjust periodics after time jump */ |
|
|
1664 | for (i = HEAP0; i < periodiccnt + HEAP0; ++i) |
|
|
1665 | { |
|
|
1666 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
|
|
1667 | |
|
|
1668 | if (w->reschedule_cb) |
|
|
1669 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
|
|
1670 | else if (w->interval) |
|
|
1671 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1672 | |
|
|
1673 | ANHE_at_set (periodics [i]); |
|
|
1674 | } |
|
|
1675 | |
|
|
1676 | /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */ |
|
|
1677 | for (i = periodiccnt >> 1; --i; ) |
|
|
1678 | downheap (periodics, periodiccnt, i + HEAP0); |
1556 | } |
1679 | } |
1557 | #endif |
1680 | #endif |
1558 | |
1681 | |
1559 | void inline_speed |
1682 | void inline_speed |
1560 | time_update (EV_P_ ev_tstamp max_block) |
1683 | time_update (EV_P_ ev_tstamp max_block) |
… | |
… | |
1589 | */ |
1712 | */ |
1590 | for (i = 4; --i; ) |
1713 | for (i = 4; --i; ) |
1591 | { |
1714 | { |
1592 | rtmn_diff = ev_rt_now - mn_now; |
1715 | rtmn_diff = ev_rt_now - mn_now; |
1593 | |
1716 | |
1594 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1717 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
1595 | return; /* all is well */ |
1718 | return; /* all is well */ |
1596 | |
1719 | |
1597 | ev_rt_now = ev_time (); |
1720 | ev_rt_now = ev_time (); |
1598 | mn_now = get_clock (); |
1721 | mn_now = get_clock (); |
1599 | now_floor = mn_now; |
1722 | now_floor = mn_now; |
… | |
… | |
1615 | #if EV_PERIODIC_ENABLE |
1738 | #if EV_PERIODIC_ENABLE |
1616 | periodics_reschedule (EV_A); |
1739 | periodics_reschedule (EV_A); |
1617 | #endif |
1740 | #endif |
1618 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1741 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1619 | for (i = 0; i < timercnt; ++i) |
1742 | for (i = 0; i < timercnt; ++i) |
|
|
1743 | { |
|
|
1744 | ANHE *he = timers + i + HEAP0; |
1620 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1745 | ANHE_w (*he)->at += ev_rt_now - mn_now; |
|
|
1746 | ANHE_at_set (*he); |
|
|
1747 | } |
1621 | } |
1748 | } |
1622 | |
1749 | |
1623 | mn_now = ev_rt_now; |
1750 | mn_now = ev_rt_now; |
1624 | } |
1751 | } |
1625 | } |
1752 | } |
… | |
… | |
1695 | |
1822 | |
1696 | waittime = MAX_BLOCKTIME; |
1823 | waittime = MAX_BLOCKTIME; |
1697 | |
1824 | |
1698 | if (timercnt) |
1825 | if (timercnt) |
1699 | { |
1826 | { |
1700 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1827 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
1701 | if (waittime > to) waittime = to; |
1828 | if (waittime > to) waittime = to; |
1702 | } |
1829 | } |
1703 | |
1830 | |
1704 | #if EV_PERIODIC_ENABLE |
1831 | #if EV_PERIODIC_ENABLE |
1705 | if (periodiccnt) |
1832 | if (periodiccnt) |
1706 | { |
1833 | { |
1707 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1834 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
1708 | if (waittime > to) waittime = to; |
1835 | if (waittime > to) waittime = to; |
1709 | } |
1836 | } |
1710 | #endif |
1837 | #endif |
1711 | |
1838 | |
1712 | if (expect_false (waittime < timeout_blocktime)) |
1839 | if (expect_false (waittime < timeout_blocktime)) |
… | |
… | |
1864 | { |
1991 | { |
1865 | clear_pending (EV_A_ (W)w); |
1992 | clear_pending (EV_A_ (W)w); |
1866 | if (expect_false (!ev_is_active (w))) |
1993 | if (expect_false (!ev_is_active (w))) |
1867 | return; |
1994 | return; |
1868 | |
1995 | |
1869 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1996 | assert (("ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1870 | |
1997 | |
1871 | wlist_del (&anfds[w->fd].head, (WL)w); |
1998 | wlist_del (&anfds[w->fd].head, (WL)w); |
1872 | ev_stop (EV_A_ (W)w); |
1999 | ev_stop (EV_A_ (W)w); |
1873 | |
2000 | |
1874 | fd_change (EV_A_ w->fd, 1); |
2001 | fd_change (EV_A_ w->fd, 1); |
… | |
… | |
1878 | ev_timer_start (EV_P_ ev_timer *w) |
2005 | ev_timer_start (EV_P_ ev_timer *w) |
1879 | { |
2006 | { |
1880 | if (expect_false (ev_is_active (w))) |
2007 | if (expect_false (ev_is_active (w))) |
1881 | return; |
2008 | return; |
1882 | |
2009 | |
1883 | ((WT)w)->at += mn_now; |
2010 | ev_at (w) += mn_now; |
1884 | |
2011 | |
1885 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
2012 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1886 | |
2013 | |
1887 | ev_start (EV_A_ (W)w, ++timercnt); |
2014 | ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1); |
1888 | array_needsize (WT, timers, timermax, timercnt, EMPTY2); |
2015 | array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); |
1889 | timers [timercnt - 1] = (WT)w; |
2016 | ANHE_w (timers [ev_active (w)]) = (WT)w; |
1890 | upheap (timers, timercnt - 1); |
2017 | ANHE_at_set (timers [ev_active (w)]); |
|
|
2018 | upheap (timers, ev_active (w)); |
1891 | |
2019 | |
1892 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
2020 | /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
1893 | } |
2021 | } |
1894 | |
2022 | |
1895 | void noinline |
2023 | void noinline |
1896 | ev_timer_stop (EV_P_ ev_timer *w) |
2024 | ev_timer_stop (EV_P_ ev_timer *w) |
1897 | { |
2025 | { |
1898 | clear_pending (EV_A_ (W)w); |
2026 | clear_pending (EV_A_ (W)w); |
1899 | if (expect_false (!ev_is_active (w))) |
2027 | if (expect_false (!ev_is_active (w))) |
1900 | return; |
2028 | return; |
1901 | |
2029 | |
1902 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); |
|
|
1903 | |
|
|
1904 | { |
2030 | { |
1905 | int active = ((W)w)->active; |
2031 | int active = ev_active (w); |
1906 | |
2032 | |
|
|
2033 | assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w)); |
|
|
2034 | |
1907 | if (expect_true (--active < --timercnt)) |
2035 | if (expect_true (active < timercnt + HEAP0 - 1)) |
1908 | { |
2036 | { |
1909 | timers [active] = timers [timercnt]; |
2037 | timers [active] = timers [timercnt + HEAP0 - 1]; |
1910 | adjustheap (timers, timercnt, active); |
2038 | adjustheap (timers, timercnt, active); |
1911 | } |
2039 | } |
|
|
2040 | |
|
|
2041 | --timercnt; |
1912 | } |
2042 | } |
1913 | |
2043 | |
1914 | ((WT)w)->at -= mn_now; |
2044 | ev_at (w) -= mn_now; |
1915 | |
2045 | |
1916 | ev_stop (EV_A_ (W)w); |
2046 | ev_stop (EV_A_ (W)w); |
1917 | } |
2047 | } |
1918 | |
2048 | |
1919 | void noinline |
2049 | void noinline |
… | |
… | |
1921 | { |
2051 | { |
1922 | if (ev_is_active (w)) |
2052 | if (ev_is_active (w)) |
1923 | { |
2053 | { |
1924 | if (w->repeat) |
2054 | if (w->repeat) |
1925 | { |
2055 | { |
1926 | ((WT)w)->at = mn_now + w->repeat; |
2056 | ev_at (w) = mn_now + w->repeat; |
|
|
2057 | ANHE_at_set (timers [ev_active (w)]); |
1927 | adjustheap (timers, timercnt, ((W)w)->active - 1); |
2058 | adjustheap (timers, timercnt, ev_active (w)); |
1928 | } |
2059 | } |
1929 | else |
2060 | else |
1930 | ev_timer_stop (EV_A_ w); |
2061 | ev_timer_stop (EV_A_ w); |
1931 | } |
2062 | } |
1932 | else if (w->repeat) |
2063 | else if (w->repeat) |
1933 | { |
2064 | { |
1934 | w->at = w->repeat; |
2065 | ev_at (w) = w->repeat; |
1935 | ev_timer_start (EV_A_ w); |
2066 | ev_timer_start (EV_A_ w); |
1936 | } |
2067 | } |
1937 | } |
2068 | } |
1938 | |
2069 | |
1939 | #if EV_PERIODIC_ENABLE |
2070 | #if EV_PERIODIC_ENABLE |
… | |
… | |
1942 | { |
2073 | { |
1943 | if (expect_false (ev_is_active (w))) |
2074 | if (expect_false (ev_is_active (w))) |
1944 | return; |
2075 | return; |
1945 | |
2076 | |
1946 | if (w->reschedule_cb) |
2077 | if (w->reschedule_cb) |
1947 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
2078 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1948 | else if (w->interval) |
2079 | else if (w->interval) |
1949 | { |
2080 | { |
1950 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2081 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1951 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2082 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1952 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2083 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1953 | } |
2084 | } |
1954 | else |
2085 | else |
1955 | ((WT)w)->at = w->offset; |
2086 | ev_at (w) = w->offset; |
1956 | |
2087 | |
1957 | ev_start (EV_A_ (W)w, ++periodiccnt); |
2088 | ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1); |
1958 | array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); |
2089 | array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); |
1959 | periodics [periodiccnt - 1] = (WT)w; |
2090 | ANHE_w (periodics [ev_active (w)]) = (WT)w; |
1960 | upheap (periodics, periodiccnt - 1); |
2091 | upheap (periodics, ev_active (w)); |
1961 | |
2092 | |
1962 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
2093 | /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
1963 | } |
2094 | } |
1964 | |
2095 | |
1965 | void noinline |
2096 | void noinline |
1966 | ev_periodic_stop (EV_P_ ev_periodic *w) |
2097 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1967 | { |
2098 | { |
1968 | clear_pending (EV_A_ (W)w); |
2099 | clear_pending (EV_A_ (W)w); |
1969 | if (expect_false (!ev_is_active (w))) |
2100 | if (expect_false (!ev_is_active (w))) |
1970 | return; |
2101 | return; |
1971 | |
2102 | |
1972 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); |
|
|
1973 | |
|
|
1974 | { |
2103 | { |
1975 | int active = ((W)w)->active; |
2104 | int active = ev_active (w); |
1976 | |
2105 | |
|
|
2106 | assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w)); |
|
|
2107 | |
1977 | if (expect_true (--active < --periodiccnt)) |
2108 | if (expect_true (active < periodiccnt + HEAP0 - 1)) |
1978 | { |
2109 | { |
1979 | periodics [active] = periodics [periodiccnt]; |
2110 | periodics [active] = periodics [periodiccnt + HEAP0 - 1]; |
1980 | adjustheap (periodics, periodiccnt, active); |
2111 | adjustheap (periodics, periodiccnt, active); |
1981 | } |
2112 | } |
|
|
2113 | |
|
|
2114 | --periodiccnt; |
1982 | } |
2115 | } |
1983 | |
2116 | |
1984 | ev_stop (EV_A_ (W)w); |
2117 | ev_stop (EV_A_ (W)w); |
1985 | } |
2118 | } |
1986 | |
2119 | |
… | |
… | |
2102 | if (w->wd < 0) |
2235 | if (w->wd < 0) |
2103 | { |
2236 | { |
2104 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2237 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2105 | |
2238 | |
2106 | /* monitor some parent directory for speedup hints */ |
2239 | /* monitor some parent directory for speedup hints */ |
|
|
2240 | /* note that exceeding the hardcoded limit is not a correctness issue, */ |
|
|
2241 | /* but an efficiency issue only */ |
2107 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2242 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2108 | { |
2243 | { |
2109 | char path [4096]; |
2244 | char path [4096]; |
2110 | strcpy (path, w->path); |
2245 | strcpy (path, w->path); |
2111 | |
2246 | |
… | |
… | |
2356 | clear_pending (EV_A_ (W)w); |
2491 | clear_pending (EV_A_ (W)w); |
2357 | if (expect_false (!ev_is_active (w))) |
2492 | if (expect_false (!ev_is_active (w))) |
2358 | return; |
2493 | return; |
2359 | |
2494 | |
2360 | { |
2495 | { |
2361 | int active = ((W)w)->active; |
2496 | int active = ev_active (w); |
2362 | |
2497 | |
2363 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2498 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2364 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
2499 | ev_active (idles [ABSPRI (w)][active - 1]) = active; |
2365 | |
2500 | |
2366 | ev_stop (EV_A_ (W)w); |
2501 | ev_stop (EV_A_ (W)w); |
2367 | --idleall; |
2502 | --idleall; |
2368 | } |
2503 | } |
2369 | } |
2504 | } |
… | |
… | |
2386 | clear_pending (EV_A_ (W)w); |
2521 | clear_pending (EV_A_ (W)w); |
2387 | if (expect_false (!ev_is_active (w))) |
2522 | if (expect_false (!ev_is_active (w))) |
2388 | return; |
2523 | return; |
2389 | |
2524 | |
2390 | { |
2525 | { |
2391 | int active = ((W)w)->active; |
2526 | int active = ev_active (w); |
|
|
2527 | |
2392 | prepares [active - 1] = prepares [--preparecnt]; |
2528 | prepares [active - 1] = prepares [--preparecnt]; |
2393 | ((W)prepares [active - 1])->active = active; |
2529 | ev_active (prepares [active - 1]) = active; |
2394 | } |
2530 | } |
2395 | |
2531 | |
2396 | ev_stop (EV_A_ (W)w); |
2532 | ev_stop (EV_A_ (W)w); |
2397 | } |
2533 | } |
2398 | |
2534 | |
… | |
… | |
2413 | clear_pending (EV_A_ (W)w); |
2549 | clear_pending (EV_A_ (W)w); |
2414 | if (expect_false (!ev_is_active (w))) |
2550 | if (expect_false (!ev_is_active (w))) |
2415 | return; |
2551 | return; |
2416 | |
2552 | |
2417 | { |
2553 | { |
2418 | int active = ((W)w)->active; |
2554 | int active = ev_active (w); |
|
|
2555 | |
2419 | checks [active - 1] = checks [--checkcnt]; |
2556 | checks [active - 1] = checks [--checkcnt]; |
2420 | ((W)checks [active - 1])->active = active; |
2557 | ev_active (checks [active - 1]) = active; |
2421 | } |
2558 | } |
2422 | |
2559 | |
2423 | ev_stop (EV_A_ (W)w); |
2560 | ev_stop (EV_A_ (W)w); |
2424 | } |
2561 | } |
2425 | |
2562 | |
… | |
… | |
2521 | clear_pending (EV_A_ (W)w); |
2658 | clear_pending (EV_A_ (W)w); |
2522 | if (expect_false (!ev_is_active (w))) |
2659 | if (expect_false (!ev_is_active (w))) |
2523 | return; |
2660 | return; |
2524 | |
2661 | |
2525 | { |
2662 | { |
2526 | int active = ((W)w)->active; |
2663 | int active = ev_active (w); |
|
|
2664 | |
2527 | forks [active - 1] = forks [--forkcnt]; |
2665 | forks [active - 1] = forks [--forkcnt]; |
2528 | ((W)forks [active - 1])->active = active; |
2666 | ev_active (forks [active - 1]) = active; |
2529 | } |
2667 | } |
2530 | |
2668 | |
2531 | ev_stop (EV_A_ (W)w); |
2669 | ev_stop (EV_A_ (W)w); |
2532 | } |
2670 | } |
2533 | #endif |
2671 | #endif |
… | |
… | |
2552 | clear_pending (EV_A_ (W)w); |
2690 | clear_pending (EV_A_ (W)w); |
2553 | if (expect_false (!ev_is_active (w))) |
2691 | if (expect_false (!ev_is_active (w))) |
2554 | return; |
2692 | return; |
2555 | |
2693 | |
2556 | { |
2694 | { |
2557 | int active = ((W)w)->active; |
2695 | int active = ev_active (w); |
|
|
2696 | |
2558 | asyncs [active - 1] = asyncs [--asynccnt]; |
2697 | asyncs [active - 1] = asyncs [--asynccnt]; |
2559 | ((W)asyncs [active - 1])->active = active; |
2698 | ev_active (asyncs [active - 1]) = active; |
2560 | } |
2699 | } |
2561 | |
2700 | |
2562 | ev_stop (EV_A_ (W)w); |
2701 | ev_stop (EV_A_ (W)w); |
2563 | } |
2702 | } |
2564 | |
2703 | |