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.236 by root, Wed May 7 14:46:22 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 ();
502 } 520 }
503} 521}
504 522
505/*****************************************************************************/ 523/*****************************************************************************/
506 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
507int inline_size 527int inline_size
508array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
509{ 529{
510 int ncur = cur + 1; 530 int ncur = cur + 1;
511 531
512 do 532 do
513 ncur <<= 1; 533 ncur <<= 1;
514 while (cnt > ncur); 534 while (cnt > ncur);
515 535
516 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 536 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
517 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
518 { 538 {
519 ncur *= elem; 539 ncur *= elem;
520 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
521 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
522 ncur /= elem; 542 ncur /= elem;
523 } 543 }
524 544
525 return ncur; 545 return ncur;
739 } 759 }
740} 760}
741 761
742/*****************************************************************************/ 762/*****************************************************************************/
743 763
764/*
765 * at the moment we allow libev the luxury of two heaps,
766 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
767 * which is more cache-efficient.
768 * the difference is about 5% with 50000+ watchers.
769 */
770#define USE_4HEAP !EV_MINIMAL
771#if USE_4HEAP
772
773#define HEAP0 3 /* index of first element in heap */
774
775/* towards the root */
744void inline_speed 776void inline_speed
745upheap (WT *heap, int k) 777upheap (WT *heap, int k)
746{ 778{
747 WT w = heap [k]; 779 WT w = heap [k];
748 780
749 while (k) 781 for (;;)
750 { 782 {
751 int p = (k - 1) >> 1; 783 int p = ((k - HEAP0 - 1) / 4) + HEAP0;
752 784
753 if (heap [p]->at <= w->at) 785 if (p >= HEAP0 || heap [p]->at <= w->at)
754 break; 786 break;
755 787
756 heap [k] = heap [p]; 788 heap [k] = heap [p];
757 ((W)heap [k])->active = k + 1; 789 ev_active (heap [k]) = k;
758 k = p; 790 k = p;
759 } 791 }
760 792
761 heap [k] = w; 793 heap [k] = w;
762 ((W)heap [k])->active = k + 1; 794 ev_active (heap [k]) = k;
763} 795}
764 796
797/* away from the root */
765void inline_speed 798void inline_speed
766downheap (WT *heap, int N, int k) 799downheap (WT *heap, int N, int k)
767{ 800{
768 WT w = heap [k]; 801 WT w = heap [k];
802 WT *E = heap + N + HEAP0;
769 803
770 for (;;) 804 for (;;)
771 { 805 {
806 ev_tstamp minat;
807 WT *minpos;
808 WT *pos = heap + 4 * (k - HEAP0) + HEAP0;
809
810 // find minimum child
811 if (expect_true (pos +3 < E))
812 {
813 /* fast path */
814 (minpos = pos + 0), (minat = (*minpos)->at);
815 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
816 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
817 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
818 }
819 else
820 {
821 /* slow path */
822 if (pos >= E)
823 break;
824 (minpos = pos + 0), (minat = (*minpos)->at);
825 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
826 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
827 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
828 }
829
830 if (w->at <= minat)
831 break;
832
833 ev_active (*minpos) = k;
834 heap [k] = *minpos;
835
836 k = minpos - heap;
837 }
838
839 heap [k] = w;
840 ev_active (heap [k]) = k;
841}
842
843#else // 4HEAP
844
845#define HEAP0 1
846
847/* towards the root */
848void inline_speed
849upheap (WT *heap, int k)
850{
851 WT w = heap [k];
852
853 for (;;)
854 {
855 int p = k >> 1;
856
857 /* maybe we could use a dummy element at heap [0]? */
858 if (!p || heap [p]->at <= w->at)
859 break;
860
861 heap [k] = heap [p];
862 ev_active (heap [k]) = k;
863 k = p;
864 }
865
866 heap [k] = w;
867 ev_active (heap [k]) = k;
868}
869
870/* away from the root */
871void inline_speed
872downheap (WT *heap, int N, int k)
873{
874 WT w = heap [k];
875
876 for (;;)
877 {
772 int c = (k << 1) + 1; 878 int c = k << 1;
773 879
774 if (c >= N) 880 if (c > N)
775 break; 881 break;
776 882
777 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 883 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
778 ? 1 : 0; 884 ? 1 : 0;
779 885
780 if (w->at <= heap [c]->at) 886 if (w->at <= heap [c]->at)
781 break; 887 break;
782 888
783 heap [k] = heap [c]; 889 heap [k] = heap [c];
784 ((W)heap [k])->active = k + 1; 890 ((W)heap [k])->active = k;
785 891
786 k = c; 892 k = c;
787 } 893 }
788 894
789 heap [k] = w; 895 heap [k] = w;
790 ((W)heap [k])->active = k + 1; 896 ev_active (heap [k]) = k;
791} 897}
898#endif
792 899
793void inline_size 900void inline_size
794adjustheap (WT *heap, int N, int k) 901adjustheap (WT *heap, int N, int k)
795{ 902{
796 upheap (heap, k); 903 upheap (heap, k);
891pipecb (EV_P_ ev_io *iow, int revents) 998pipecb (EV_P_ ev_io *iow, int revents)
892{ 999{
893#if EV_USE_EVENTFD 1000#if EV_USE_EVENTFD
894 if (evfd >= 0) 1001 if (evfd >= 0)
895 { 1002 {
896 uint64_t counter = 1; 1003 uint64_t counter;
897 read (evfd, &counter, sizeof (uint64_t)); 1004 read (evfd, &counter, sizeof (uint64_t));
898 } 1005 }
899 else 1006 else
900#endif 1007#endif
901 { 1008 {
1170 if (!(flags & EVFLAG_NOENV) 1277 if (!(flags & EVFLAG_NOENV)
1171 && !enable_secure () 1278 && !enable_secure ()
1172 && getenv ("LIBEV_FLAGS")) 1279 && getenv ("LIBEV_FLAGS"))
1173 flags = atoi (getenv ("LIBEV_FLAGS")); 1280 flags = atoi (getenv ("LIBEV_FLAGS"));
1174 1281
1175 if (!(flags & 0x0000ffffUL)) 1282 if (!(flags & 0x0000ffffU))
1176 flags |= ev_recommended_backends (); 1283 flags |= ev_recommended_backends ();
1177 1284
1178#if EV_USE_PORT 1285#if EV_USE_PORT
1179 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1286 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1180#endif 1287#endif
1268#endif 1375#endif
1269 1376
1270 backend = 0; 1377 backend = 0;
1271} 1378}
1272 1379
1380#if EV_USE_INOTIFY
1273void inline_size infy_fork (EV_P); 1381void inline_size infy_fork (EV_P);
1382#endif
1274 1383
1275void inline_size 1384void inline_size
1276loop_fork (EV_P) 1385loop_fork (EV_P)
1277{ 1386{
1278#if EV_USE_PORT 1387#if EV_USE_PORT
1345void 1454void
1346ev_loop_fork (EV_P) 1455ev_loop_fork (EV_P)
1347{ 1456{
1348 postfork = 1; /* must be in line with ev_default_fork */ 1457 postfork = 1; /* must be in line with ev_default_fork */
1349} 1458}
1350
1351#endif 1459#endif
1352 1460
1353#if EV_MULTIPLICITY 1461#if EV_MULTIPLICITY
1354struct ev_loop * 1462struct ev_loop *
1355ev_default_loop_init (unsigned int flags) 1463ev_default_loop_init (unsigned int flags)
1436 EV_CB_INVOKE (p->w, p->events); 1544 EV_CB_INVOKE (p->w, p->events);
1437 } 1545 }
1438 } 1546 }
1439} 1547}
1440 1548
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 1549#if EV_IDLE_ENABLE
1522void inline_size 1550void inline_size
1523idle_reify (EV_P) 1551idle_reify (EV_P)
1524{ 1552{
1525 if (expect_false (idleall)) 1553 if (expect_false (idleall))
1536 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1564 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1537 break; 1565 break;
1538 } 1566 }
1539 } 1567 }
1540 } 1568 }
1569}
1570#endif
1571
1572void inline_size
1573timers_reify (EV_P)
1574{
1575 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1576 {
1577 ev_timer *w = (ev_timer *)timers [HEAP0];
1578
1579 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1580
1581 /* first reschedule or stop timer */
1582 if (w->repeat)
1583 {
1584 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1585
1586 ev_at (w) += w->repeat;
1587 if (ev_at (w) < mn_now)
1588 ev_at (w) = mn_now;
1589
1590 downheap (timers, timercnt, HEAP0);
1591 }
1592 else
1593 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1594
1595 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1596 }
1597}
1598
1599#if EV_PERIODIC_ENABLE
1600void inline_size
1601periodics_reify (EV_P)
1602{
1603 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1604 {
1605 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1606
1607 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1608
1609 /* first reschedule or stop timer */
1610 if (w->reschedule_cb)
1611 {
1612 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1613 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1614 downheap (periodics, periodiccnt, 1);
1615 }
1616 else if (w->interval)
1617 {
1618 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1619 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1620 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1621 downheap (periodics, periodiccnt, HEAP0);
1622 }
1623 else
1624 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1625
1626 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1627 }
1628}
1629
1630static void noinline
1631periodics_reschedule (EV_P)
1632{
1633 int i;
1634
1635 /* adjust periodics after time jump */
1636 for (i = 1; i <= periodiccnt; ++i)
1637 {
1638 ev_periodic *w = (ev_periodic *)periodics [i];
1639
1640 if (w->reschedule_cb)
1641 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1642 else if (w->interval)
1643 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1644 }
1645
1646 /* now rebuild the heap */
1647 for (i = periodiccnt >> 1; --i; )
1648 downheap (periodics, periodiccnt, i + HEAP0);
1541} 1649}
1542#endif 1650#endif
1543 1651
1544void inline_speed 1652void inline_speed
1545time_update (EV_P_ ev_tstamp max_block) 1653time_update (EV_P_ ev_tstamp max_block)
1574 */ 1682 */
1575 for (i = 4; --i; ) 1683 for (i = 4; --i; )
1576 { 1684 {
1577 rtmn_diff = ev_rt_now - mn_now; 1685 rtmn_diff = ev_rt_now - mn_now;
1578 1686
1579 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1687 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1580 return; /* all is well */ 1688 return; /* all is well */
1581 1689
1582 ev_rt_now = ev_time (); 1690 ev_rt_now = ev_time ();
1583 mn_now = get_clock (); 1691 mn_now = get_clock ();
1584 now_floor = mn_now; 1692 now_floor = mn_now;
1599 { 1707 {
1600#if EV_PERIODIC_ENABLE 1708#if EV_PERIODIC_ENABLE
1601 periodics_reschedule (EV_A); 1709 periodics_reschedule (EV_A);
1602#endif 1710#endif
1603 /* adjust timers. this is easy, as the offset is the same for all of them */ 1711 /* adjust timers. this is easy, as the offset is the same for all of them */
1604 for (i = 0; i < timercnt; ++i) 1712 for (i = 1; i <= timercnt; ++i)
1605 ((WT)timers [i])->at += ev_rt_now - mn_now; 1713 ev_at (timers [i]) += ev_rt_now - mn_now;
1606 } 1714 }
1607 1715
1608 mn_now = ev_rt_now; 1716 mn_now = ev_rt_now;
1609 } 1717 }
1610} 1718}
1680 1788
1681 waittime = MAX_BLOCKTIME; 1789 waittime = MAX_BLOCKTIME;
1682 1790
1683 if (timercnt) 1791 if (timercnt)
1684 { 1792 {
1685 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1793 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1686 if (waittime > to) waittime = to; 1794 if (waittime > to) waittime = to;
1687 } 1795 }
1688 1796
1689#if EV_PERIODIC_ENABLE 1797#if EV_PERIODIC_ENABLE
1690 if (periodiccnt) 1798 if (periodiccnt)
1691 { 1799 {
1692 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1800 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1693 if (waittime > to) waittime = to; 1801 if (waittime > to) waittime = to;
1694 } 1802 }
1695#endif 1803#endif
1696 1804
1697 if (expect_false (waittime < timeout_blocktime)) 1805 if (expect_false (waittime < timeout_blocktime))
1863ev_timer_start (EV_P_ ev_timer *w) 1971ev_timer_start (EV_P_ ev_timer *w)
1864{ 1972{
1865 if (expect_false (ev_is_active (w))) 1973 if (expect_false (ev_is_active (w)))
1866 return; 1974 return;
1867 1975
1868 ((WT)w)->at += mn_now; 1976 ev_at (w) += mn_now;
1869 1977
1870 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1978 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1871 1979
1872 ev_start (EV_A_ (W)w, ++timercnt); 1980 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1873 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1981 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1874 timers [timercnt - 1] = (WT)w; 1982 timers [ev_active (w)] = (WT)w;
1875 upheap (timers, timercnt - 1); 1983 upheap (timers, ev_active (w));
1876 1984
1877 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1985 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1878} 1986}
1879 1987
1880void noinline 1988void noinline
1881ev_timer_stop (EV_P_ ev_timer *w) 1989ev_timer_stop (EV_P_ ev_timer *w)
1882{ 1990{
1883 clear_pending (EV_A_ (W)w); 1991 clear_pending (EV_A_ (W)w);
1884 if (expect_false (!ev_is_active (w))) 1992 if (expect_false (!ev_is_active (w)))
1885 return; 1993 return;
1886 1994
1887 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1888
1889 { 1995 {
1890 int active = ((W)w)->active; 1996 int active = ev_active (w);
1891 1997
1998 assert (("internal timer heap corruption", timers [active] == (WT)w));
1999
1892 if (expect_true (--active < --timercnt)) 2000 if (expect_true (active < timercnt + HEAP0 - 1))
1893 { 2001 {
1894 timers [active] = timers [timercnt]; 2002 timers [active] = timers [timercnt + HEAP0 - 1];
1895 adjustheap (timers, timercnt, active); 2003 adjustheap (timers, timercnt, active);
1896 } 2004 }
2005
2006 --timercnt;
1897 } 2007 }
1898 2008
1899 ((WT)w)->at -= mn_now; 2009 ev_at (w) -= mn_now;
1900 2010
1901 ev_stop (EV_A_ (W)w); 2011 ev_stop (EV_A_ (W)w);
1902} 2012}
1903 2013
1904void noinline 2014void noinline
1906{ 2016{
1907 if (ev_is_active (w)) 2017 if (ev_is_active (w))
1908 { 2018 {
1909 if (w->repeat) 2019 if (w->repeat)
1910 { 2020 {
1911 ((WT)w)->at = mn_now + w->repeat; 2021 ev_at (w) = mn_now + w->repeat;
1912 adjustheap (timers, timercnt, ((W)w)->active - 1); 2022 adjustheap (timers, timercnt, ev_active (w));
1913 } 2023 }
1914 else 2024 else
1915 ev_timer_stop (EV_A_ w); 2025 ev_timer_stop (EV_A_ w);
1916 } 2026 }
1917 else if (w->repeat) 2027 else if (w->repeat)
1918 { 2028 {
1919 w->at = w->repeat; 2029 ev_at (w) = w->repeat;
1920 ev_timer_start (EV_A_ w); 2030 ev_timer_start (EV_A_ w);
1921 } 2031 }
1922} 2032}
1923 2033
1924#if EV_PERIODIC_ENABLE 2034#if EV_PERIODIC_ENABLE
1927{ 2037{
1928 if (expect_false (ev_is_active (w))) 2038 if (expect_false (ev_is_active (w)))
1929 return; 2039 return;
1930 2040
1931 if (w->reschedule_cb) 2041 if (w->reschedule_cb)
1932 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2042 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1933 else if (w->interval) 2043 else if (w->interval)
1934 { 2044 {
1935 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2045 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 */ 2046 /* 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; 2047 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1938 } 2048 }
1939 else 2049 else
1940 ((WT)w)->at = w->offset; 2050 ev_at (w) = w->offset;
1941 2051
1942 ev_start (EV_A_ (W)w, ++periodiccnt); 2052 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1943 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2053 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1944 periodics [periodiccnt - 1] = (WT)w; 2054 periodics [ev_active (w)] = (WT)w;
1945 upheap (periodics, periodiccnt - 1); 2055 upheap (periodics, ev_active (w));
1946 2056
1947 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2057 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1948} 2058}
1949 2059
1950void noinline 2060void noinline
1951ev_periodic_stop (EV_P_ ev_periodic *w) 2061ev_periodic_stop (EV_P_ ev_periodic *w)
1952{ 2062{
1953 clear_pending (EV_A_ (W)w); 2063 clear_pending (EV_A_ (W)w);
1954 if (expect_false (!ev_is_active (w))) 2064 if (expect_false (!ev_is_active (w)))
1955 return; 2065 return;
1956 2066
1957 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1958
1959 { 2067 {
1960 int active = ((W)w)->active; 2068 int active = ev_active (w);
1961 2069
2070 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
2071
1962 if (expect_true (--active < --periodiccnt)) 2072 if (expect_true (active < periodiccnt + HEAP0 - 1))
1963 { 2073 {
1964 periodics [active] = periodics [periodiccnt]; 2074 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1965 adjustheap (periodics, periodiccnt, active); 2075 adjustheap (periodics, periodiccnt, active);
1966 } 2076 }
2077
2078 --periodiccnt;
1967 } 2079 }
1968 2080
1969 ev_stop (EV_A_ (W)w); 2081 ev_stop (EV_A_ (W)w);
1970} 2082}
1971 2083
2087 if (w->wd < 0) 2199 if (w->wd < 0)
2088 { 2200 {
2089 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2201 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2090 2202
2091 /* monitor some parent directory for speedup hints */ 2203 /* monitor some parent directory for speedup hints */
2204 /* note that exceeding the hardcoded limit is not a correctness issue, */
2205 /* but an efficiency issue only */
2092 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2206 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2093 { 2207 {
2094 char path [4096]; 2208 char path [4096];
2095 strcpy (path, w->path); 2209 strcpy (path, w->path);
2096 2210
2341 clear_pending (EV_A_ (W)w); 2455 clear_pending (EV_A_ (W)w);
2342 if (expect_false (!ev_is_active (w))) 2456 if (expect_false (!ev_is_active (w)))
2343 return; 2457 return;
2344 2458
2345 { 2459 {
2346 int active = ((W)w)->active; 2460 int active = ev_active (w);
2347 2461
2348 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2462 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2349 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2463 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2350 2464
2351 ev_stop (EV_A_ (W)w); 2465 ev_stop (EV_A_ (W)w);
2352 --idleall; 2466 --idleall;
2353 } 2467 }
2354} 2468}
2371 clear_pending (EV_A_ (W)w); 2485 clear_pending (EV_A_ (W)w);
2372 if (expect_false (!ev_is_active (w))) 2486 if (expect_false (!ev_is_active (w)))
2373 return; 2487 return;
2374 2488
2375 { 2489 {
2376 int active = ((W)w)->active; 2490 int active = ev_active (w);
2491
2377 prepares [active - 1] = prepares [--preparecnt]; 2492 prepares [active - 1] = prepares [--preparecnt];
2378 ((W)prepares [active - 1])->active = active; 2493 ev_active (prepares [active - 1]) = active;
2379 } 2494 }
2380 2495
2381 ev_stop (EV_A_ (W)w); 2496 ev_stop (EV_A_ (W)w);
2382} 2497}
2383 2498
2398 clear_pending (EV_A_ (W)w); 2513 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2514 if (expect_false (!ev_is_active (w)))
2400 return; 2515 return;
2401 2516
2402 { 2517 {
2403 int active = ((W)w)->active; 2518 int active = ev_active (w);
2519
2404 checks [active - 1] = checks [--checkcnt]; 2520 checks [active - 1] = checks [--checkcnt];
2405 ((W)checks [active - 1])->active = active; 2521 ev_active (checks [active - 1]) = active;
2406 } 2522 }
2407 2523
2408 ev_stop (EV_A_ (W)w); 2524 ev_stop (EV_A_ (W)w);
2409} 2525}
2410 2526
2506 clear_pending (EV_A_ (W)w); 2622 clear_pending (EV_A_ (W)w);
2507 if (expect_false (!ev_is_active (w))) 2623 if (expect_false (!ev_is_active (w)))
2508 return; 2624 return;
2509 2625
2510 { 2626 {
2511 int active = ((W)w)->active; 2627 int active = ev_active (w);
2628
2512 forks [active - 1] = forks [--forkcnt]; 2629 forks [active - 1] = forks [--forkcnt];
2513 ((W)forks [active - 1])->active = active; 2630 ev_active (forks [active - 1]) = active;
2514 } 2631 }
2515 2632
2516 ev_stop (EV_A_ (W)w); 2633 ev_stop (EV_A_ (W)w);
2517} 2634}
2518#endif 2635#endif
2537 clear_pending (EV_A_ (W)w); 2654 clear_pending (EV_A_ (W)w);
2538 if (expect_false (!ev_is_active (w))) 2655 if (expect_false (!ev_is_active (w)))
2539 return; 2656 return;
2540 2657
2541 { 2658 {
2542 int active = ((W)w)->active; 2659 int active = ev_active (w);
2660
2543 asyncs [active - 1] = asyncs [--asynccnt]; 2661 asyncs [active - 1] = asyncs [--asynccnt];
2544 ((W)asyncs [active - 1])->active = active; 2662 ev_active (asyncs [active - 1]) = active;
2545 } 2663 }
2546 2664
2547 ev_stop (EV_A_ (W)w); 2665 ev_stop (EV_A_ (W)w);
2548} 2666}
2549 2667

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines