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

Comparing libev/ev.c (file contents):
Revision 1.222 by root, Sun Apr 6 12:45:58 2008 UTC vs.
Revision 1.241 by root, Fri May 9 13:57:00 2008 UTC

300# define expect(expr,value) __builtin_expect ((expr),(value)) 300# define expect(expr,value) __builtin_expect ((expr),(value))
301# define noinline __attribute__ ((noinline)) 301# define noinline __attribute__ ((noinline))
302#else 302#else
303# define expect(expr,value) (expr) 303# define expect(expr,value) (expr)
304# define noinline 304# define noinline
305# if __STDC_VERSION__ < 199901L 305# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
306# define inline 306# define inline
307# endif 307# endif
308#endif 308#endif
309 309
310#define expect_false(expr) expect ((expr) != 0, 0) 310#define expect_false(expr) expect ((expr) != 0, 0)
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#if EV_HEAP_CACHE_AT
436 typedef struct {
437 WT w;
438 ev_tstamp at;
439 } ANHE;
440
441 #define ANHE_w(he) (he) /* access watcher, read-write */
442 #define ANHE_at(he) (he)->at /* acces cahced at, read-only */
443 #define ANHE_at_set(he) (he)->at = (he)->w->at /* update at from watcher */
444#else
445 typedef WT ANHE;
446
447 #define ANHE_w(he) (he)
448 #define ANHE_at(he) (he)->at
449 #define ANHE_at_set(he)
413#endif 450#endif
414 451
415#if EV_MULTIPLICITY 452#if EV_MULTIPLICITY
416 453
417 struct ev_loop 454 struct ev_loop
502 } 539 }
503} 540}
504 541
505/*****************************************************************************/ 542/*****************************************************************************/
506 543
544#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
545
507int inline_size 546int inline_size
508array_nextsize (int elem, int cur, int cnt) 547array_nextsize (int elem, int cur, int cnt)
509{ 548{
510 int ncur = cur + 1; 549 int ncur = cur + 1;
511 550
512 do 551 do
513 ncur <<= 1; 552 ncur <<= 1;
514 while (cnt > ncur); 553 while (cnt > ncur);
515 554
516 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 555 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
517 if (elem * ncur > 4096) 556 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
518 { 557 {
519 ncur *= elem; 558 ncur *= elem;
520 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 559 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
521 ncur = ncur - sizeof (void *) * 4; 560 ncur = ncur - sizeof (void *) * 4;
522 ncur /= elem; 561 ncur /= elem;
523 } 562 }
524 563
525 return ncur; 564 return ncur;
739 } 778 }
740} 779}
741 780
742/*****************************************************************************/ 781/*****************************************************************************/
743 782
783/*
784 * the heap functions want a real array index. array index 0 uis guaranteed to not
785 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
786 * the branching factor of the d-tree.
787 */
788
789/*
790 * at the moment we allow libev the luxury of two heaps,
791 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
792 * which is more cache-efficient.
793 * the difference is about 5% with 50000+ watchers.
794 */
795#define EV_USE_4HEAP !EV_MINIMAL
796#if EV_USE_4HEAP
797
798#define DHEAP 4
799#define HEAP0 (DHEAP - 1) /* index of first element in heap */
800
801/* towards the root */
744void inline_speed 802void inline_speed
745upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
746{ 804{
747 WT w = heap [k]; 805 ANHE he = heap [k];
748 806
749 while (k) 807 for (;;)
750 { 808 {
751 int p = (k - 1) >> 1; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
752 810
753 if (heap [p]->at <= w->at) 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
754 break; 812 break;
755 813
756 heap [k] = heap [p]; 814 heap [k] = heap [p];
757 ((W)heap [k])->active = k + 1; 815 ev_active (ANHE_w (heap [k])) = k;
758 k = p; 816 k = p;
759 } 817 }
760 818
819 ev_active (ANHE_w (he)) = k;
820 heap [k] = he;
821}
822
823/* away from the root */
824void inline_speed
825downheap (ANHE *heap, int N, int k)
826{
827 ANHE he = heap [k];
828 ANHE *E = heap + N + HEAP0;
829
830 for (;;)
831 {
832 ev_tstamp minat;
833 ANHE *minpos;
834 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
835
836 // find minimum child
837 if (expect_true (pos + DHEAP - 1 < E))
838 {
839 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
840 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
843 }
844 else if (pos < E)
845 {
846 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
847 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
848 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
849 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
850 }
851 else
852 break;
853
854 if (ANHE_at (he) <= minat)
855 break;
856
857 ev_active (ANHE_w (*minpos)) = k;
858 heap [k] = *minpos;
859
860 k = minpos - heap;
861 }
862
863 ev_active (ANHE_w (he)) = k;
864 heap [k] = he;
865}
866
867#else // 4HEAP
868
869#define HEAP0 1
870
871/* towards the root */
872void inline_speed
873upheap (ANHE *heap, int k)
874{
875 ANHE he = heap [k];
876
877 for (;;)
878 {
879 int p = k >> 1;
880
881 /* maybe we could use a dummy element at heap [0]? */
882 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
883 break;
884
885 heap [k] = heap [p];
886 ev_active (ANHE_w (heap [k])) = k;
887 k = p;
888 }
889
761 heap [k] = w; 890 heap [k] = w;
762 ((W)heap [k])->active = k + 1; 891 ev_active (ANHE_w (heap [k])) = k;
763} 892}
764 893
894/* away from the root */
765void inline_speed 895void inline_speed
766downheap (WT *heap, int N, int k) 896downheap (ANHE *heap, int N, int k)
767{ 897{
768 WT w = heap [k]; 898 ANHE he = heap [k];
769 899
770 for (;;) 900 for (;;)
771 { 901 {
772 int c = (k << 1) + 1; 902 int c = k << 1;
773 903
774 if (c >= N) 904 if (c > N)
775 break; 905 break;
776 906
777 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 907 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
778 ? 1 : 0; 908 ? 1 : 0;
779 909
780 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
781 break; 911 break;
782 912
783 heap [k] = heap [c]; 913 heap [k] = heap [c];
784 ((W)heap [k])->active = k + 1; 914 ev_active (ANHE_w (heap [k])) = k;
785 915
786 k = c; 916 k = c;
787 } 917 }
788 918
789 heap [k] = w; 919 heap [k] = he;
790 ((W)heap [k])->active = k + 1; 920 ev_active (ANHE_w (he)) = k;
791} 921}
922#endif
792 923
793void inline_size 924void inline_size
794adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
795{ 926{
796 upheap (heap, k); 927 upheap (heap, k);
797 downheap (heap, N, k); 928 downheap (heap, N, k);
798} 929}
799 930
891pipecb (EV_P_ ev_io *iow, int revents) 1022pipecb (EV_P_ ev_io *iow, int revents)
892{ 1023{
893#if EV_USE_EVENTFD 1024#if EV_USE_EVENTFD
894 if (evfd >= 0) 1025 if (evfd >= 0)
895 { 1026 {
896 uint64_t counter = 1; 1027 uint64_t counter;
897 read (evfd, &counter, sizeof (uint64_t)); 1028 read (evfd, &counter, sizeof (uint64_t));
898 } 1029 }
899 else 1030 else
900#endif 1031#endif
901 { 1032 {
1170 if (!(flags & EVFLAG_NOENV) 1301 if (!(flags & EVFLAG_NOENV)
1171 && !enable_secure () 1302 && !enable_secure ()
1172 && getenv ("LIBEV_FLAGS")) 1303 && getenv ("LIBEV_FLAGS"))
1173 flags = atoi (getenv ("LIBEV_FLAGS")); 1304 flags = atoi (getenv ("LIBEV_FLAGS"));
1174 1305
1175 if (!(flags & 0x0000ffffUL)) 1306 if (!(flags & 0x0000ffffU))
1176 flags |= ev_recommended_backends (); 1307 flags |= ev_recommended_backends ();
1177 1308
1178#if EV_USE_PORT 1309#if EV_USE_PORT
1179 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1310 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1180#endif 1311#endif
1268#endif 1399#endif
1269 1400
1270 backend = 0; 1401 backend = 0;
1271} 1402}
1272 1403
1404#if EV_USE_INOTIFY
1273void inline_size infy_fork (EV_P); 1405void inline_size infy_fork (EV_P);
1406#endif
1274 1407
1275void inline_size 1408void inline_size
1276loop_fork (EV_P) 1409loop_fork (EV_P)
1277{ 1410{
1278#if EV_USE_PORT 1411#if EV_USE_PORT
1345void 1478void
1346ev_loop_fork (EV_P) 1479ev_loop_fork (EV_P)
1347{ 1480{
1348 postfork = 1; /* must be in line with ev_default_fork */ 1481 postfork = 1; /* must be in line with ev_default_fork */
1349} 1482}
1350
1351#endif 1483#endif
1352 1484
1353#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1354struct ev_loop * 1486struct ev_loop *
1355ev_default_loop_init (unsigned int flags) 1487ev_default_loop_init (unsigned int flags)
1436 EV_CB_INVOKE (p->w, p->events); 1568 EV_CB_INVOKE (p->w, p->events);
1437 } 1569 }
1438 } 1570 }
1439} 1571}
1440 1572
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 1573#if EV_IDLE_ENABLE
1522void inline_size 1574void inline_size
1523idle_reify (EV_P) 1575idle_reify (EV_P)
1524{ 1576{
1525 if (expect_false (idleall)) 1577 if (expect_false (idleall))
1536 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1588 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1537 break; 1589 break;
1538 } 1590 }
1539 } 1591 }
1540 } 1592 }
1593}
1594#endif
1595
1596void inline_size
1597timers_reify (EV_P)
1598{
1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1600 {
1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1602
1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1604
1605 /* first reschedule or stop timer */
1606 if (w->repeat)
1607 {
1608 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1609
1610 ev_at (w) += w->repeat;
1611 if (ev_at (w) < mn_now)
1612 ev_at (w) = mn_now;
1613
1614 downheap (timers, timercnt, HEAP0);
1615 }
1616 else
1617 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1618
1619 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1620 }
1621}
1622
1623#if EV_PERIODIC_ENABLE
1624void inline_size
1625periodics_reify (EV_P)
1626{
1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1628 {
1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1630
1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1632
1633 /* first reschedule or stop timer */
1634 if (w->reschedule_cb)
1635 {
1636 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1637 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1638 downheap (periodics, periodiccnt, 1);
1639 }
1640 else if (w->interval)
1641 {
1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1643 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1644 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1645 downheap (periodics, periodiccnt, HEAP0);
1646 }
1647 else
1648 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1649
1650 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1651 }
1652}
1653
1654static void noinline
1655periodics_reschedule (EV_P)
1656{
1657 int i;
1658
1659 /* adjust periodics after time jump */
1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1661 {
1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1663
1664 if (w->reschedule_cb)
1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1666 else if (w->interval)
1667 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1668 }
1669
1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1671 for (i = periodiccnt >> 1; --i; )
1672 downheap (periodics, periodiccnt, i + HEAP0);
1541} 1673}
1542#endif 1674#endif
1543 1675
1544void inline_speed 1676void inline_speed
1545time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1574 */ 1706 */
1575 for (i = 4; --i; ) 1707 for (i = 4; --i; )
1576 { 1708 {
1577 rtmn_diff = ev_rt_now - mn_now; 1709 rtmn_diff = ev_rt_now - mn_now;
1578 1710
1579 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1711 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1580 return; /* all is well */ 1712 return; /* all is well */
1581 1713
1582 ev_rt_now = ev_time (); 1714 ev_rt_now = ev_time ();
1583 mn_now = get_clock (); 1715 mn_now = get_clock ();
1584 now_floor = mn_now; 1716 now_floor = mn_now;
1600#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1601 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1602#endif 1734#endif
1603 /* adjust timers. this is easy, as the offset is the same for all of them */ 1735 /* adjust timers. this is easy, as the offset is the same for all of them */
1604 for (i = 0; i < timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1737 {
1738 ANHE *he = timers + i + HEAP0;
1605 ((WT)timers [i])->at += ev_rt_now - mn_now; 1739 ANHE_w (*he)->at += ev_rt_now - mn_now;
1740 ANHE_at_set (*he);
1741 }
1606 } 1742 }
1607 1743
1608 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1609 } 1745 }
1610} 1746}
1680 1816
1681 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1682 1818
1683 if (timercnt) 1819 if (timercnt)
1684 { 1820 {
1685 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1686 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1687 } 1823 }
1688 1824
1689#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1690 if (periodiccnt) 1826 if (periodiccnt)
1691 { 1827 {
1692 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1828 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1693 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1694 } 1830 }
1695#endif 1831#endif
1696 1832
1697 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1863ev_timer_start (EV_P_ ev_timer *w) 1999ev_timer_start (EV_P_ ev_timer *w)
1864{ 2000{
1865 if (expect_false (ev_is_active (w))) 2001 if (expect_false (ev_is_active (w)))
1866 return; 2002 return;
1867 2003
1868 ((WT)w)->at += mn_now; 2004 ev_at (w) += mn_now;
1869 2005
1870 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2006 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1871 2007
1872 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1873 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1874 timers [timercnt - 1] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
1875 upheap (timers, timercnt - 1); 2011 ANHE_at_set (timers [ev_active (w)]);
2012 upheap (timers, ev_active (w));
1876 2013
1877 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1878} 2015}
1879 2016
1880void noinline 2017void noinline
1881ev_timer_stop (EV_P_ ev_timer *w) 2018ev_timer_stop (EV_P_ ev_timer *w)
1882{ 2019{
1883 clear_pending (EV_A_ (W)w); 2020 clear_pending (EV_A_ (W)w);
1884 if (expect_false (!ev_is_active (w))) 2021 if (expect_false (!ev_is_active (w)))
1885 return; 2022 return;
1886 2023
1887 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1888
1889 { 2024 {
1890 int active = ((W)w)->active; 2025 int active = ev_active (w);
1891 2026
2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2028
1892 if (expect_true (--active < --timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1893 { 2030 {
1894 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1895 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1896 } 2033 }
2034
2035 --timercnt;
1897 } 2036 }
1898 2037
1899 ((WT)w)->at -= mn_now; 2038 ev_at (w) -= mn_now;
1900 2039
1901 ev_stop (EV_A_ (W)w); 2040 ev_stop (EV_A_ (W)w);
1902} 2041}
1903 2042
1904void noinline 2043void noinline
1906{ 2045{
1907 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1908 { 2047 {
1909 if (w->repeat) 2048 if (w->repeat)
1910 { 2049 {
1911 ((WT)w)->at = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1912 adjustheap (timers, timercnt, ((W)w)->active - 1); 2052 adjustheap (timers, timercnt, ev_active (w));
1913 } 2053 }
1914 else 2054 else
1915 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1916 } 2056 }
1917 else if (w->repeat) 2057 else if (w->repeat)
1918 { 2058 {
1919 w->at = w->repeat; 2059 ev_at (w) = w->repeat;
1920 ev_timer_start (EV_A_ w); 2060 ev_timer_start (EV_A_ w);
1921 } 2061 }
1922} 2062}
1923 2063
1924#if EV_PERIODIC_ENABLE 2064#if EV_PERIODIC_ENABLE
1927{ 2067{
1928 if (expect_false (ev_is_active (w))) 2068 if (expect_false (ev_is_active (w)))
1929 return; 2069 return;
1930 2070
1931 if (w->reschedule_cb) 2071 if (w->reschedule_cb)
1932 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2072 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1933 else if (w->interval) 2073 else if (w->interval)
1934 { 2074 {
1935 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2075 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 */ 2076 /* 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; 2077 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1938 } 2078 }
1939 else 2079 else
1940 ((WT)w)->at = w->offset; 2080 ev_at (w) = w->offset;
1941 2081
1942 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1943 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1944 periodics [periodiccnt - 1] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1945 upheap (periodics, periodiccnt - 1); 2085 upheap (periodics, ev_active (w));
1946 2086
1947 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2087 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1948} 2088}
1949 2089
1950void noinline 2090void noinline
1951ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1952{ 2092{
1953 clear_pending (EV_A_ (W)w); 2093 clear_pending (EV_A_ (W)w);
1954 if (expect_false (!ev_is_active (w))) 2094 if (expect_false (!ev_is_active (w)))
1955 return; 2095 return;
1956 2096
1957 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1958
1959 { 2097 {
1960 int active = ((W)w)->active; 2098 int active = ev_active (w);
1961 2099
2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2101
1962 if (expect_true (--active < --periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1963 { 2103 {
1964 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1965 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1966 } 2106 }
2107
2108 --periodiccnt;
1967 } 2109 }
1968 2110
1969 ev_stop (EV_A_ (W)w); 2111 ev_stop (EV_A_ (W)w);
1970} 2112}
1971 2113
2087 if (w->wd < 0) 2229 if (w->wd < 0)
2088 { 2230 {
2089 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2231 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2090 2232
2091 /* monitor some parent directory for speedup hints */ 2233 /* monitor some parent directory for speedup hints */
2234 /* note that exceeding the hardcoded limit is not a correctness issue, */
2235 /* but an efficiency issue only */
2092 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2236 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2093 { 2237 {
2094 char path [4096]; 2238 char path [4096];
2095 strcpy (path, w->path); 2239 strcpy (path, w->path);
2096 2240
2341 clear_pending (EV_A_ (W)w); 2485 clear_pending (EV_A_ (W)w);
2342 if (expect_false (!ev_is_active (w))) 2486 if (expect_false (!ev_is_active (w)))
2343 return; 2487 return;
2344 2488
2345 { 2489 {
2346 int active = ((W)w)->active; 2490 int active = ev_active (w);
2347 2491
2348 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2492 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2349 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2493 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2350 2494
2351 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2352 --idleall; 2496 --idleall;
2353 } 2497 }
2354} 2498}
2371 clear_pending (EV_A_ (W)w); 2515 clear_pending (EV_A_ (W)w);
2372 if (expect_false (!ev_is_active (w))) 2516 if (expect_false (!ev_is_active (w)))
2373 return; 2517 return;
2374 2518
2375 { 2519 {
2376 int active = ((W)w)->active; 2520 int active = ev_active (w);
2521
2377 prepares [active - 1] = prepares [--preparecnt]; 2522 prepares [active - 1] = prepares [--preparecnt];
2378 ((W)prepares [active - 1])->active = active; 2523 ev_active (prepares [active - 1]) = active;
2379 } 2524 }
2380 2525
2381 ev_stop (EV_A_ (W)w); 2526 ev_stop (EV_A_ (W)w);
2382} 2527}
2383 2528
2398 clear_pending (EV_A_ (W)w); 2543 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2544 if (expect_false (!ev_is_active (w)))
2400 return; 2545 return;
2401 2546
2402 { 2547 {
2403 int active = ((W)w)->active; 2548 int active = ev_active (w);
2549
2404 checks [active - 1] = checks [--checkcnt]; 2550 checks [active - 1] = checks [--checkcnt];
2405 ((W)checks [active - 1])->active = active; 2551 ev_active (checks [active - 1]) = active;
2406 } 2552 }
2407 2553
2408 ev_stop (EV_A_ (W)w); 2554 ev_stop (EV_A_ (W)w);
2409} 2555}
2410 2556
2506 clear_pending (EV_A_ (W)w); 2652 clear_pending (EV_A_ (W)w);
2507 if (expect_false (!ev_is_active (w))) 2653 if (expect_false (!ev_is_active (w)))
2508 return; 2654 return;
2509 2655
2510 { 2656 {
2511 int active = ((W)w)->active; 2657 int active = ev_active (w);
2658
2512 forks [active - 1] = forks [--forkcnt]; 2659 forks [active - 1] = forks [--forkcnt];
2513 ((W)forks [active - 1])->active = active; 2660 ev_active (forks [active - 1]) = active;
2514 } 2661 }
2515 2662
2516 ev_stop (EV_A_ (W)w); 2663 ev_stop (EV_A_ (W)w);
2517} 2664}
2518#endif 2665#endif
2537 clear_pending (EV_A_ (W)w); 2684 clear_pending (EV_A_ (W)w);
2538 if (expect_false (!ev_is_active (w))) 2685 if (expect_false (!ev_is_active (w)))
2539 return; 2686 return;
2540 2687
2541 { 2688 {
2542 int active = ((W)w)->active; 2689 int active = ev_active (w);
2690
2543 asyncs [active - 1] = asyncs [--asynccnt]; 2691 asyncs [active - 1] = asyncs [--asynccnt];
2544 ((W)asyncs [active - 1])->active = active; 2692 ev_active (asyncs [active - 1]) = active;
2545 } 2693 }
2546 2694
2547 ev_stop (EV_A_ (W)w); 2695 ev_stop (EV_A_ (W)w);
2548} 2696}
2549 2697

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines