ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.223 by root, Sun Apr 6 14:34:50 2008 UTC vs.
Revision 1.242 by root, Fri May 9 14:07:19 2008 UTC

325 325
326typedef ev_watcher *W; 326typedef ev_watcher *W;
327typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
328typedef ev_watcher_time *WT; 328typedef 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 */
333static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
334#endif 337#endif
360 perror (msg); 363 perror (msg);
361 abort (); 364 abort ();
362 } 365 }
363} 366}
364 367
368static void *
369ev_realloc_emul (void *ptr, long size)
370{
371 /* some systems, notably openbsd and darwin, fail to properly
372 * implement realloc (x, 0) (as required by both ansi c-98 and
373 * the single unix specification, so work around them here.
374 */
375
376 if (size)
377 return realloc (ptr, size);
378
379 free (ptr);
380 return 0;
381}
382
365static void *(*alloc)(void *ptr, long size); 383static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
366 384
367void 385void
368ev_set_allocator (void *(*cb)(void *ptr, long size)) 386ev_set_allocator (void *(*cb)(void *ptr, long size))
369{ 387{
370 alloc = cb; 388 alloc = cb;
371} 389}
372 390
373inline_speed void * 391inline_speed void *
374ev_realloc (void *ptr, long size) 392ev_realloc (void *ptr, long size)
375{ 393{
376 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 394 ptr = alloc (ptr, size);
377 395
378 if (!ptr && size) 396 if (!ptr && size)
379 { 397 {
380 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 398 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
381 abort (); 399 abort ();
404 W w; 422 W w;
405 int events; 423 int events;
406} ANPENDING; 424} ANPENDING;
407 425
408#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
409typedef struct 428typedef struct
410{ 429{
411 WL head; 430 WL head;
412} 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)
413#endif 451#endif
414 452
415#if EV_MULTIPLICITY 453#if EV_MULTIPLICITY
416 454
417 struct ev_loop 455 struct ev_loop
502 } 540 }
503} 541}
504 542
505/*****************************************************************************/ 543/*****************************************************************************/
506 544
545#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
546
507int inline_size 547int inline_size
508array_nextsize (int elem, int cur, int cnt) 548array_nextsize (int elem, int cur, int cnt)
509{ 549{
510 int ncur = cur + 1; 550 int ncur = cur + 1;
511 551
512 do 552 do
513 ncur <<= 1; 553 ncur <<= 1;
514 while (cnt > ncur); 554 while (cnt > ncur);
515 555
516 /* 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 */
517 if (elem * ncur > 4096) 557 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
518 { 558 {
519 ncur *= elem; 559 ncur *= elem;
520 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 560 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
521 ncur = ncur - sizeof (void *) * 4; 561 ncur = ncur - sizeof (void *) * 4;
522 ncur /= elem; 562 ncur /= elem;
523 } 563 }
524 564
525 return ncur; 565 return ncur;
739 } 779 }
740} 780}
741 781
742/*****************************************************************************/ 782/*****************************************************************************/
743 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 */
744void inline_speed 803void inline_speed
745upheap (WT *heap, int k) 804upheap (ANHE *heap, int k)
746{ 805{
747 WT w = heap [k]; 806 ANHE he = heap [k];
748 807
749 while (k) 808 for (;;)
750 { 809 {
751 int p = (k - 1) >> 1; 810 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
752 811
753 if (heap [p]->at <= w->at) 812 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
754 break; 813 break;
755 814
756 heap [k] = heap [p]; 815 heap [k] = heap [p];
757 ((W)heap [k])->active = k + 1; 816 ev_active (ANHE_w (heap [k])) = k;
758 k = p; 817 k = p;
759 } 818 }
760 819
820 ev_active (ANHE_w (he)) = k;
821 heap [k] = he;
822}
823
824/* away from the root */
825void inline_speed
826downheap (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 */
873void inline_speed
874upheap (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
761 heap [k] = w; 891 heap [k] = w;
762 ((W)heap [k])->active = k + 1; 892 ev_active (ANHE_w (heap [k])) = k;
763} 893}
764 894
895/* away from the root */
765void inline_speed 896void inline_speed
766downheap (WT *heap, int N, int k) 897downheap (ANHE *heap, int N, int k)
767{ 898{
768 WT w = heap [k]; 899 ANHE he = heap [k];
769 900
770 for (;;) 901 for (;;)
771 { 902 {
772 int c = (k << 1) + 1; 903 int c = k << 1;
773 904
774 if (c >= N) 905 if (c > N)
775 break; 906 break;
776 907
777 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])
778 ? 1 : 0; 909 ? 1 : 0;
779 910
780 if (w->at <= heap [c]->at) 911 if (w->at <= ANHE_at (heap [c]))
781 break; 912 break;
782 913
783 heap [k] = heap [c]; 914 heap [k] = heap [c];
784 ((W)heap [k])->active = k + 1; 915 ev_active (ANHE_w (heap [k])) = k;
785 916
786 k = c; 917 k = c;
787 } 918 }
788 919
789 heap [k] = w; 920 heap [k] = he;
790 ((W)heap [k])->active = k + 1; 921 ev_active (ANHE_w (he)) = k;
791} 922}
923#endif
792 924
793void inline_size 925void inline_size
794adjustheap (WT *heap, int N, int k) 926adjustheap (ANHE *heap, int N, int k)
795{ 927{
796 upheap (heap, k); 928 upheap (heap, k);
797 downheap (heap, N, k); 929 downheap (heap, N, k);
798} 930}
799 931
891pipecb (EV_P_ ev_io *iow, int revents) 1023pipecb (EV_P_ ev_io *iow, int revents)
892{ 1024{
893#if EV_USE_EVENTFD 1025#if EV_USE_EVENTFD
894 if (evfd >= 0) 1026 if (evfd >= 0)
895 { 1027 {
896 uint64_t counter = 1; 1028 uint64_t counter;
897 read (evfd, &counter, sizeof (uint64_t)); 1029 read (evfd, &counter, sizeof (uint64_t));
898 } 1030 }
899 else 1031 else
900#endif 1032#endif
901 { 1033 {
1170 if (!(flags & EVFLAG_NOENV) 1302 if (!(flags & EVFLAG_NOENV)
1171 && !enable_secure () 1303 && !enable_secure ()
1172 && getenv ("LIBEV_FLAGS")) 1304 && getenv ("LIBEV_FLAGS"))
1173 flags = atoi (getenv ("LIBEV_FLAGS")); 1305 flags = atoi (getenv ("LIBEV_FLAGS"));
1174 1306
1175 if (!(flags & 0x0000ffffUL)) 1307 if (!(flags & 0x0000ffffU))
1176 flags |= ev_recommended_backends (); 1308 flags |= ev_recommended_backends ();
1177 1309
1178#if EV_USE_PORT 1310#if EV_USE_PORT
1179 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1311 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1180#endif 1312#endif
1268#endif 1400#endif
1269 1401
1270 backend = 0; 1402 backend = 0;
1271} 1403}
1272 1404
1405#if EV_USE_INOTIFY
1273void inline_size infy_fork (EV_P); 1406void inline_size infy_fork (EV_P);
1407#endif
1274 1408
1275void inline_size 1409void inline_size
1276loop_fork (EV_P) 1410loop_fork (EV_P)
1277{ 1411{
1278#if EV_USE_PORT 1412#if EV_USE_PORT
1345void 1479void
1346ev_loop_fork (EV_P) 1480ev_loop_fork (EV_P)
1347{ 1481{
1348 postfork = 1; /* must be in line with ev_default_fork */ 1482 postfork = 1; /* must be in line with ev_default_fork */
1349} 1483}
1350
1351#endif 1484#endif
1352 1485
1353#if EV_MULTIPLICITY 1486#if EV_MULTIPLICITY
1354struct ev_loop * 1487struct ev_loop *
1355ev_default_loop_init (unsigned int flags) 1488ev_default_loop_init (unsigned int flags)
1436 EV_CB_INVOKE (p->w, p->events); 1569 EV_CB_INVOKE (p->w, p->events);
1437 } 1570 }
1438 } 1571 }
1439} 1572}
1440 1573
1441void inline_size
1442timers_reify (EV_P)
1443{
1444 while (timercnt && ((WT)timers [0])->at <= mn_now)
1445 {
1446 ev_timer *w = (ev_timer *)timers [0];
1447
1448 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1449
1450 /* first reschedule or stop timer */
1451 if (w->repeat)
1452 {
1453 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1454
1455 ((WT)w)->at += w->repeat;
1456 if (((WT)w)->at < mn_now)
1457 ((WT)w)->at = mn_now;
1458
1459 downheap (timers, timercnt, 0);
1460 }
1461 else
1462 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1463
1464 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1465 }
1466}
1467
1468#if EV_PERIODIC_ENABLE
1469void inline_size
1470periodics_reify (EV_P)
1471{
1472 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1473 {
1474 ev_periodic *w = (ev_periodic *)periodics [0];
1475
1476 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1477
1478 /* first reschedule or stop timer */
1479 if (w->reschedule_cb)
1480 {
1481 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1482 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1483 downheap (periodics, periodiccnt, 0);
1484 }
1485 else if (w->interval)
1486 {
1487 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1488 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1489 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1490 downheap (periodics, periodiccnt, 0);
1491 }
1492 else
1493 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1494
1495 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1496 }
1497}
1498
1499static void noinline
1500periodics_reschedule (EV_P)
1501{
1502 int i;
1503
1504 /* adjust periodics after time jump */
1505 for (i = 0; i < periodiccnt; ++i)
1506 {
1507 ev_periodic *w = (ev_periodic *)periodics [i];
1508
1509 if (w->reschedule_cb)
1510 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1511 else if (w->interval)
1512 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1513 }
1514
1515 /* now rebuild the heap */
1516 for (i = periodiccnt >> 1; i--; )
1517 downheap (periodics, periodiccnt, i);
1518}
1519#endif
1520
1521#if EV_IDLE_ENABLE 1574#if EV_IDLE_ENABLE
1522void inline_size 1575void inline_size
1523idle_reify (EV_P) 1576idle_reify (EV_P)
1524{ 1577{
1525 if (expect_false (idleall)) 1578 if (expect_false (idleall))
1536 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1589 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1537 break; 1590 break;
1538 } 1591 }
1539 } 1592 }
1540 } 1593 }
1594}
1595#endif
1596
1597void inline_size
1598timers_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
1626void inline_size
1627periodics_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
1658static void noinline
1659periodics_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);
1541} 1679}
1542#endif 1680#endif
1543 1681
1544void inline_speed 1682void inline_speed
1545time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1574 */ 1712 */
1575 for (i = 4; --i; ) 1713 for (i = 4; --i; )
1576 { 1714 {
1577 rtmn_diff = ev_rt_now - mn_now; 1715 rtmn_diff = ev_rt_now - mn_now;
1578 1716
1579 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1717 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1580 return; /* all is well */ 1718 return; /* all is well */
1581 1719
1582 ev_rt_now = ev_time (); 1720 ev_rt_now = ev_time ();
1583 mn_now = get_clock (); 1721 mn_now = get_clock ();
1584 now_floor = mn_now; 1722 now_floor = mn_now;
1600#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1601 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1602#endif 1740#endif
1603 /* 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 */
1604 for (i = 0; i < timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1743 {
1744 ANHE *he = timers + i + HEAP0;
1605 ((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 }
1606 } 1748 }
1607 1749
1608 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1609 } 1751 }
1610} 1752}
1680 1822
1681 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1682 1824
1683 if (timercnt) 1825 if (timercnt)
1684 { 1826 {
1685 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1686 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1687 } 1829 }
1688 1830
1689#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1690 if (periodiccnt) 1832 if (periodiccnt)
1691 { 1833 {
1692 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;
1693 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1694 } 1836 }
1695#endif 1837#endif
1696 1838
1697 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1849{ 1991{
1850 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1851 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1852 return; 1994 return;
1853 1995
1854 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));
1855 1997
1856 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1857 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1858 2000
1859 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1863ev_timer_start (EV_P_ ev_timer *w) 2005ev_timer_start (EV_P_ ev_timer *w)
1864{ 2006{
1865 if (expect_false (ev_is_active (w))) 2007 if (expect_false (ev_is_active (w)))
1866 return; 2008 return;
1867 2009
1868 ((WT)w)->at += mn_now; 2010 ev_at (w) += mn_now;
1869 2011
1870 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.));
1871 2013
1872 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1873 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1874 timers [timercnt - 1] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
1875 upheap (timers, timercnt - 1); 2017 ANHE_at_set (timers [ev_active (w)]);
2018 upheap (timers, ev_active (w));
1876 2019
1877 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1878} 2021}
1879 2022
1880void noinline 2023void noinline
1881ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1882{ 2025{
1883 clear_pending (EV_A_ (W)w); 2026 clear_pending (EV_A_ (W)w);
1884 if (expect_false (!ev_is_active (w))) 2027 if (expect_false (!ev_is_active (w)))
1885 return; 2028 return;
1886 2029
1887 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1888
1889 { 2030 {
1890 int active = ((W)w)->active; 2031 int active = ev_active (w);
1891 2032
2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2034
1892 if (expect_true (--active < --timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1893 { 2036 {
1894 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1895 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1896 } 2039 }
2040
2041 --timercnt;
1897 } 2042 }
1898 2043
1899 ((WT)w)->at -= mn_now; 2044 ev_at (w) -= mn_now;
1900 2045
1901 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1902} 2047}
1903 2048
1904void noinline 2049void noinline
1906{ 2051{
1907 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1908 { 2053 {
1909 if (w->repeat) 2054 if (w->repeat)
1910 { 2055 {
1911 ((WT)w)->at = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1912 adjustheap (timers, timercnt, ((W)w)->active - 1); 2058 adjustheap (timers, timercnt, ev_active (w));
1913 } 2059 }
1914 else 2060 else
1915 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1916 } 2062 }
1917 else if (w->repeat) 2063 else if (w->repeat)
1918 { 2064 {
1919 w->at = w->repeat; 2065 ev_at (w) = w->repeat;
1920 ev_timer_start (EV_A_ w); 2066 ev_timer_start (EV_A_ w);
1921 } 2067 }
1922} 2068}
1923 2069
1924#if EV_PERIODIC_ENABLE 2070#if EV_PERIODIC_ENABLE
1927{ 2073{
1928 if (expect_false (ev_is_active (w))) 2074 if (expect_false (ev_is_active (w)))
1929 return; 2075 return;
1930 2076
1931 if (w->reschedule_cb) 2077 if (w->reschedule_cb)
1932 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2078 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1933 else if (w->interval) 2079 else if (w->interval)
1934 { 2080 {
1935 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.));
1936 /* 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 */
1937 ((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;
1938 } 2084 }
1939 else 2085 else
1940 ((WT)w)->at = w->offset; 2086 ev_at (w) = w->offset;
1941 2087
1942 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1943 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1944 periodics [periodiccnt - 1] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1945 upheap (periodics, periodiccnt - 1); 2091 upheap (periodics, ev_active (w));
1946 2092
1947 /*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));*/
1948} 2094}
1949 2095
1950void noinline 2096void noinline
1951ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1952{ 2098{
1953 clear_pending (EV_A_ (W)w); 2099 clear_pending (EV_A_ (W)w);
1954 if (expect_false (!ev_is_active (w))) 2100 if (expect_false (!ev_is_active (w)))
1955 return; 2101 return;
1956 2102
1957 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1958
1959 { 2103 {
1960 int active = ((W)w)->active; 2104 int active = ev_active (w);
1961 2105
2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2107
1962 if (expect_true (--active < --periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1963 { 2109 {
1964 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1965 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1966 } 2112 }
2113
2114 --periodiccnt;
1967 } 2115 }
1968 2116
1969 ev_stop (EV_A_ (W)w); 2117 ev_stop (EV_A_ (W)w);
1970} 2118}
1971 2119
2087 if (w->wd < 0) 2235 if (w->wd < 0)
2088 { 2236 {
2089 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 */
2090 2238
2091 /* 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 */
2092 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2242 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2093 { 2243 {
2094 char path [4096]; 2244 char path [4096];
2095 strcpy (path, w->path); 2245 strcpy (path, w->path);
2096 2246
2341 clear_pending (EV_A_ (W)w); 2491 clear_pending (EV_A_ (W)w);
2342 if (expect_false (!ev_is_active (w))) 2492 if (expect_false (!ev_is_active (w)))
2343 return; 2493 return;
2344 2494
2345 { 2495 {
2346 int active = ((W)w)->active; 2496 int active = ev_active (w);
2347 2497
2348 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2498 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2349 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2499 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2350 2500
2351 ev_stop (EV_A_ (W)w); 2501 ev_stop (EV_A_ (W)w);
2352 --idleall; 2502 --idleall;
2353 } 2503 }
2354} 2504}
2371 clear_pending (EV_A_ (W)w); 2521 clear_pending (EV_A_ (W)w);
2372 if (expect_false (!ev_is_active (w))) 2522 if (expect_false (!ev_is_active (w)))
2373 return; 2523 return;
2374 2524
2375 { 2525 {
2376 int active = ((W)w)->active; 2526 int active = ev_active (w);
2527
2377 prepares [active - 1] = prepares [--preparecnt]; 2528 prepares [active - 1] = prepares [--preparecnt];
2378 ((W)prepares [active - 1])->active = active; 2529 ev_active (prepares [active - 1]) = active;
2379 } 2530 }
2380 2531
2381 ev_stop (EV_A_ (W)w); 2532 ev_stop (EV_A_ (W)w);
2382} 2533}
2383 2534
2398 clear_pending (EV_A_ (W)w); 2549 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2550 if (expect_false (!ev_is_active (w)))
2400 return; 2551 return;
2401 2552
2402 { 2553 {
2403 int active = ((W)w)->active; 2554 int active = ev_active (w);
2555
2404 checks [active - 1] = checks [--checkcnt]; 2556 checks [active - 1] = checks [--checkcnt];
2405 ((W)checks [active - 1])->active = active; 2557 ev_active (checks [active - 1]) = active;
2406 } 2558 }
2407 2559
2408 ev_stop (EV_A_ (W)w); 2560 ev_stop (EV_A_ (W)w);
2409} 2561}
2410 2562
2506 clear_pending (EV_A_ (W)w); 2658 clear_pending (EV_A_ (W)w);
2507 if (expect_false (!ev_is_active (w))) 2659 if (expect_false (!ev_is_active (w)))
2508 return; 2660 return;
2509 2661
2510 { 2662 {
2511 int active = ((W)w)->active; 2663 int active = ev_active (w);
2664
2512 forks [active - 1] = forks [--forkcnt]; 2665 forks [active - 1] = forks [--forkcnt];
2513 ((W)forks [active - 1])->active = active; 2666 ev_active (forks [active - 1]) = active;
2514 } 2667 }
2515 2668
2516 ev_stop (EV_A_ (W)w); 2669 ev_stop (EV_A_ (W)w);
2517} 2670}
2518#endif 2671#endif
2537 clear_pending (EV_A_ (W)w); 2690 clear_pending (EV_A_ (W)w);
2538 if (expect_false (!ev_is_active (w))) 2691 if (expect_false (!ev_is_active (w)))
2539 return; 2692 return;
2540 2693
2541 { 2694 {
2542 int active = ((W)w)->active; 2695 int active = ev_active (w);
2696
2543 asyncs [active - 1] = asyncs [--asynccnt]; 2697 asyncs [active - 1] = asyncs [--asynccnt];
2544 ((W)asyncs [active - 1])->active = active; 2698 ev_active (asyncs [active - 1]) = active;
2545 } 2699 }
2546 2700
2547 ev_stop (EV_A_ (W)w); 2701 ev_stop (EV_A_ (W)w);
2548} 2702}
2549 2703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines