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

Comparing libev/ev.c (file contents):
Revision 1.221 by root, Sun Apr 6 12:44:49 2008 UTC vs.
Revision 1.235 by root, Wed May 7 14:45:17 2008 UTC

269#endif 269#endif
270 270
271#if EV_USE_EVENTFD 271#if EV_USE_EVENTFD
272/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 272/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
273# include <stdint.h> 273# include <stdint.h>
274# ifdef __cplusplus
275extern "C" {
276# endif
274int eventfd (unsigned int initval, int flags); 277int eventfd (unsigned int initval, int flags);
278# ifdef __cplusplus
279}
280# endif
275#endif 281#endif
276 282
277/**/ 283/**/
278 284
279/* 285/*
294# define expect(expr,value) __builtin_expect ((expr),(value)) 300# define expect(expr,value) __builtin_expect ((expr),(value))
295# define noinline __attribute__ ((noinline)) 301# define noinline __attribute__ ((noinline))
296#else 302#else
297# define expect(expr,value) (expr) 303# define expect(expr,value) (expr)
298# define noinline 304# define noinline
299# if __STDC_VERSION__ < 199901L 305# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
300# define inline 306# define inline
301# endif 307# endif
302#endif 308#endif
303 309
304#define expect_false(expr) expect ((expr) != 0, 0) 310#define expect_false(expr) expect ((expr) != 0, 0)
319 325
320typedef ev_watcher *W; 326typedef ev_watcher *W;
321typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
322typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
323 329
330#define ev_active(w) ((W)(w))->active
331#define ev_at(w) ((WT)(w))->at
332
324#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
325/* 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 */
326/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
327static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
328#endif 337#endif
354 perror (msg); 363 perror (msg);
355 abort (); 364 abort ();
356 } 365 }
357} 366}
358 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
359static void *(*alloc)(void *ptr, long size); 383static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
360 384
361void 385void
362ev_set_allocator (void *(*cb)(void *ptr, long size)) 386ev_set_allocator (void *(*cb)(void *ptr, long size))
363{ 387{
364 alloc = cb; 388 alloc = cb;
365} 389}
366 390
367inline_speed void * 391inline_speed void *
368ev_realloc (void *ptr, long size) 392ev_realloc (void *ptr, long size)
369{ 393{
370 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 394 ptr = alloc (ptr, size);
371 395
372 if (!ptr && size) 396 if (!ptr && size)
373 { 397 {
374 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 398 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
375 abort (); 399 abort ();
496 } 520 }
497} 521}
498 522
499/*****************************************************************************/ 523/*****************************************************************************/
500 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
501int inline_size 527int inline_size
502array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
503{ 529{
504 int ncur = cur + 1; 530 int ncur = cur + 1;
505 531
506 do 532 do
507 ncur <<= 1; 533 ncur <<= 1;
508 while (cnt > ncur); 534 while (cnt > ncur);
509 535
510 /* 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 */
511 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
512 { 538 {
513 ncur *= elem; 539 ncur *= elem;
514 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
515 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
516 ncur /= elem; 542 ncur /= elem;
517 } 543 }
518 544
519 return ncur; 545 return ncur;
733 } 759 }
734} 760}
735 761
736/*****************************************************************************/ 762/*****************************************************************************/
737 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 */
738void inline_speed 776void inline_speed
739upheap (WT *heap, int k) 777upheap (WT *heap, int k)
740{ 778{
741 WT w = heap [k]; 779 WT w = heap [k];
742 780
743 while (k) 781 for (;;)
744 { 782 {
745 int p = (k - 1) >> 1; 783 int p = ((k - HEAP0 - 1) / 4) + HEAP0;
746 784
747 if (heap [p]->at <= w->at) 785 if (p >= HEAP0 || heap [p]->at <= w->at)
748 break; 786 break;
749 787
750 heap [k] = heap [p]; 788 heap [k] = heap [p];
751 ((W)heap [k])->active = k + 1; 789 ev_active (heap [k]) = k;
752 k = p; 790 k = p;
753 } 791 }
754 792
755 heap [k] = w; 793 heap [k] = w;
756 ((W)heap [k])->active = k + 1; 794 ev_active (heap [k]) = k;
757} 795}
758 796
797/* away from the root */
759void inline_speed 798void inline_speed
760downheap (WT *heap, int N, int k) 799downheap (WT *heap, int N, int k)
761{ 800{
762 WT w = heap [k]; 801 WT w = heap [k];
802 WT *E = heap + N + HEAP0;
763 803
764 for (;;) 804 for (;;)
765 { 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 (minpos = pos + 0), (minat = (*minpos)->at);
814 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
815 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
816 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
817 }
818 else
819 {
820 if (pos >= E)
821 break;
822
823 (minpos = pos + 0), (minat = (*minpos)->at);
824 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
825 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
826 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
827 }
828
829 if (w->at <= minat)
830 break;
831
832 ev_active (*minpos) = k;
833 heap [k] = *minpos;
834
835 k = minpos - heap;
836 }
837
838 heap [k] = w;
839 ev_active (heap [k]) = k;
840}
841
842#else // 4HEAP
843
844#define HEAP0 1
845
846/* towards the root */
847void inline_speed
848upheap (WT *heap, int k)
849{
850 WT w = heap [k];
851
852 for (;;)
853 {
854 int p = k >> 1;
855
856 /* maybe we could use a dummy element at heap [0]? */
857 if (!p || heap [p]->at <= w->at)
858 break;
859
860 heap [k] = heap [p];
861 ev_active (heap [k]) = k;
862 k = p;
863 }
864
865 heap [k] = w;
866 ev_active (heap [k]) = k;
867}
868
869/* away from the root */
870void inline_speed
871downheap (WT *heap, int N, int k)
872{
873 WT w = heap [k];
874
875 for (;;)
876 {
766 int c = (k << 1) + 1; 877 int c = k << 1;
767 878
768 if (c >= N) 879 if (c > N)
769 break; 880 break;
770 881
771 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 882 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
772 ? 1 : 0; 883 ? 1 : 0;
773 884
774 if (w->at <= heap [c]->at) 885 if (w->at <= heap [c]->at)
775 break; 886 break;
776 887
777 heap [k] = heap [c]; 888 heap [k] = heap [c];
778 ((W)heap [k])->active = k + 1; 889 ((W)heap [k])->active = k;
779 890
780 k = c; 891 k = c;
781 } 892 }
782 893
783 heap [k] = w; 894 heap [k] = w;
784 ((W)heap [k])->active = k + 1; 895 ev_active (heap [k]) = k;
785} 896}
897#endif
786 898
787void inline_size 899void inline_size
788adjustheap (WT *heap, int N, int k) 900adjustheap (WT *heap, int N, int k)
789{ 901{
790 upheap (heap, k); 902 upheap (heap, k);
885pipecb (EV_P_ ev_io *iow, int revents) 997pipecb (EV_P_ ev_io *iow, int revents)
886{ 998{
887#if EV_USE_EVENTFD 999#if EV_USE_EVENTFD
888 if (evfd >= 0) 1000 if (evfd >= 0)
889 { 1001 {
890 uint64_t counter = 1; 1002 uint64_t counter;
891 read (evfd, &counter, sizeof (uint64_t)); 1003 read (evfd, &counter, sizeof (uint64_t));
892 } 1004 }
893 else 1005 else
894#endif 1006#endif
895 { 1007 {
1164 if (!(flags & EVFLAG_NOENV) 1276 if (!(flags & EVFLAG_NOENV)
1165 && !enable_secure () 1277 && !enable_secure ()
1166 && getenv ("LIBEV_FLAGS")) 1278 && getenv ("LIBEV_FLAGS"))
1167 flags = atoi (getenv ("LIBEV_FLAGS")); 1279 flags = atoi (getenv ("LIBEV_FLAGS"));
1168 1280
1169 if (!(flags & 0x0000ffffUL)) 1281 if (!(flags & 0x0000ffffU))
1170 flags |= ev_recommended_backends (); 1282 flags |= ev_recommended_backends ();
1171 1283
1172#if EV_USE_PORT 1284#if EV_USE_PORT
1173 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1285 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1174#endif 1286#endif
1262#endif 1374#endif
1263 1375
1264 backend = 0; 1376 backend = 0;
1265} 1377}
1266 1378
1379#if EV_USE_INOTIFY
1267void inline_size infy_fork (EV_P); 1380void inline_size infy_fork (EV_P);
1381#endif
1268 1382
1269void inline_size 1383void inline_size
1270loop_fork (EV_P) 1384loop_fork (EV_P)
1271{ 1385{
1272#if EV_USE_PORT 1386#if EV_USE_PORT
1339void 1453void
1340ev_loop_fork (EV_P) 1454ev_loop_fork (EV_P)
1341{ 1455{
1342 postfork = 1; /* must be in line with ev_default_fork */ 1456 postfork = 1; /* must be in line with ev_default_fork */
1343} 1457}
1344
1345#endif 1458#endif
1346 1459
1347#if EV_MULTIPLICITY 1460#if EV_MULTIPLICITY
1348struct ev_loop * 1461struct ev_loop *
1349ev_default_loop_init (unsigned int flags) 1462ev_default_loop_init (unsigned int flags)
1430 EV_CB_INVOKE (p->w, p->events); 1543 EV_CB_INVOKE (p->w, p->events);
1431 } 1544 }
1432 } 1545 }
1433} 1546}
1434 1547
1435void inline_size
1436timers_reify (EV_P)
1437{
1438 while (timercnt && ((WT)timers [0])->at <= mn_now)
1439 {
1440 ev_timer *w = (ev_timer *)timers [0];
1441
1442 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1443
1444 /* first reschedule or stop timer */
1445 if (w->repeat)
1446 {
1447 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1448
1449 ((WT)w)->at += w->repeat;
1450 if (((WT)w)->at < mn_now)
1451 ((WT)w)->at = mn_now;
1452
1453 downheap (timers, timercnt, 0);
1454 }
1455 else
1456 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1457
1458 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1459 }
1460}
1461
1462#if EV_PERIODIC_ENABLE
1463void inline_size
1464periodics_reify (EV_P)
1465{
1466 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1467 {
1468 ev_periodic *w = (ev_periodic *)periodics [0];
1469
1470 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1471
1472 /* first reschedule or stop timer */
1473 if (w->reschedule_cb)
1474 {
1475 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1476 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1477 downheap (periodics, periodiccnt, 0);
1478 }
1479 else if (w->interval)
1480 {
1481 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1482 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1483 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1484 downheap (periodics, periodiccnt, 0);
1485 }
1486 else
1487 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1488
1489 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1490 }
1491}
1492
1493static void noinline
1494periodics_reschedule (EV_P)
1495{
1496 int i;
1497
1498 /* adjust periodics after time jump */
1499 for (i = 0; i < periodiccnt; ++i)
1500 {
1501 ev_periodic *w = (ev_periodic *)periodics [i];
1502
1503 if (w->reschedule_cb)
1504 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1505 else if (w->interval)
1506 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1507 }
1508
1509 /* now rebuild the heap */
1510 for (i = periodiccnt >> 1; i--; )
1511 downheap (periodics, periodiccnt, i);
1512}
1513#endif
1514
1515#if EV_IDLE_ENABLE 1548#if EV_IDLE_ENABLE
1516void inline_size 1549void inline_size
1517idle_reify (EV_P) 1550idle_reify (EV_P)
1518{ 1551{
1519 if (expect_false (idleall)) 1552 if (expect_false (idleall))
1530 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1563 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1531 break; 1564 break;
1532 } 1565 }
1533 } 1566 }
1534 } 1567 }
1568}
1569#endif
1570
1571void inline_size
1572timers_reify (EV_P)
1573{
1574 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1575 {
1576 ev_timer *w = (ev_timer *)timers [HEAP0];
1577
1578 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1579
1580 /* first reschedule or stop timer */
1581 if (w->repeat)
1582 {
1583 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1584
1585 ev_at (w) += w->repeat;
1586 if (ev_at (w) < mn_now)
1587 ev_at (w) = mn_now;
1588
1589 downheap (timers, timercnt, HEAP0);
1590 }
1591 else
1592 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1593
1594 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1595 }
1596}
1597
1598#if EV_PERIODIC_ENABLE
1599void inline_size
1600periodics_reify (EV_P)
1601{
1602 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1603 {
1604 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1605
1606 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1607
1608 /* first reschedule or stop timer */
1609 if (w->reschedule_cb)
1610 {
1611 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1612 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1613 downheap (periodics, periodiccnt, 1);
1614 }
1615 else if (w->interval)
1616 {
1617 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1618 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1619 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1620 downheap (periodics, periodiccnt, HEAP0);
1621 }
1622 else
1623 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1624
1625 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1626 }
1627}
1628
1629static void noinline
1630periodics_reschedule (EV_P)
1631{
1632 int i;
1633
1634 /* adjust periodics after time jump */
1635 for (i = 1; i <= periodiccnt; ++i)
1636 {
1637 ev_periodic *w = (ev_periodic *)periodics [i];
1638
1639 if (w->reschedule_cb)
1640 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1641 else if (w->interval)
1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1643 }
1644
1645 /* now rebuild the heap */
1646 for (i = periodiccnt >> 1; --i; )
1647 downheap (periodics, periodiccnt, i + HEAP0);
1535} 1648}
1536#endif 1649#endif
1537 1650
1538void inline_speed 1651void inline_speed
1539time_update (EV_P_ ev_tstamp max_block) 1652time_update (EV_P_ ev_tstamp max_block)
1568 */ 1681 */
1569 for (i = 4; --i; ) 1682 for (i = 4; --i; )
1570 { 1683 {
1571 rtmn_diff = ev_rt_now - mn_now; 1684 rtmn_diff = ev_rt_now - mn_now;
1572 1685
1573 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1686 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1574 return; /* all is well */ 1687 return; /* all is well */
1575 1688
1576 ev_rt_now = ev_time (); 1689 ev_rt_now = ev_time ();
1577 mn_now = get_clock (); 1690 mn_now = get_clock ();
1578 now_floor = mn_now; 1691 now_floor = mn_now;
1593 { 1706 {
1594#if EV_PERIODIC_ENABLE 1707#if EV_PERIODIC_ENABLE
1595 periodics_reschedule (EV_A); 1708 periodics_reschedule (EV_A);
1596#endif 1709#endif
1597 /* adjust timers. this is easy, as the offset is the same for all of them */ 1710 /* adjust timers. this is easy, as the offset is the same for all of them */
1598 for (i = 0; i < timercnt; ++i) 1711 for (i = 1; i <= timercnt; ++i)
1599 ((WT)timers [i])->at += ev_rt_now - mn_now; 1712 ev_at (timers [i]) += ev_rt_now - mn_now;
1600 } 1713 }
1601 1714
1602 mn_now = ev_rt_now; 1715 mn_now = ev_rt_now;
1603 } 1716 }
1604} 1717}
1674 1787
1675 waittime = MAX_BLOCKTIME; 1788 waittime = MAX_BLOCKTIME;
1676 1789
1677 if (timercnt) 1790 if (timercnt)
1678 { 1791 {
1679 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1792 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1680 if (waittime > to) waittime = to; 1793 if (waittime > to) waittime = to;
1681 } 1794 }
1682 1795
1683#if EV_PERIODIC_ENABLE 1796#if EV_PERIODIC_ENABLE
1684 if (periodiccnt) 1797 if (periodiccnt)
1685 { 1798 {
1686 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1799 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1687 if (waittime > to) waittime = to; 1800 if (waittime > to) waittime = to;
1688 } 1801 }
1689#endif 1802#endif
1690 1803
1691 if (expect_false (waittime < timeout_blocktime)) 1804 if (expect_false (waittime < timeout_blocktime))
1857ev_timer_start (EV_P_ ev_timer *w) 1970ev_timer_start (EV_P_ ev_timer *w)
1858{ 1971{
1859 if (expect_false (ev_is_active (w))) 1972 if (expect_false (ev_is_active (w)))
1860 return; 1973 return;
1861 1974
1862 ((WT)w)->at += mn_now; 1975 ev_at (w) += mn_now;
1863 1976
1864 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1977 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1865 1978
1866 ev_start (EV_A_ (W)w, ++timercnt); 1979 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1867 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1980 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1868 timers [timercnt - 1] = (WT)w; 1981 timers [ev_active (w)] = (WT)w;
1869 upheap (timers, timercnt - 1); 1982 upheap (timers, ev_active (w));
1870 1983
1871 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1984 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1872} 1985}
1873 1986
1874void noinline 1987void noinline
1875ev_timer_stop (EV_P_ ev_timer *w) 1988ev_timer_stop (EV_P_ ev_timer *w)
1876{ 1989{
1877 clear_pending (EV_A_ (W)w); 1990 clear_pending (EV_A_ (W)w);
1878 if (expect_false (!ev_is_active (w))) 1991 if (expect_false (!ev_is_active (w)))
1879 return; 1992 return;
1880 1993
1881 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1882
1883 { 1994 {
1884 int active = ((W)w)->active; 1995 int active = ev_active (w);
1885 1996
1997 assert (("internal timer heap corruption", timers [active] == (WT)w));
1998
1886 if (expect_true (--active < --timercnt)) 1999 if (expect_true (active < timercnt + HEAP0 - 1))
1887 { 2000 {
1888 timers [active] = timers [timercnt]; 2001 timers [active] = timers [timercnt + HEAP0 - 1];
1889 adjustheap (timers, timercnt, active); 2002 adjustheap (timers, timercnt, active);
1890 } 2003 }
2004
2005 --timercnt;
1891 } 2006 }
1892 2007
1893 ((WT)w)->at -= mn_now; 2008 ev_at (w) -= mn_now;
1894 2009
1895 ev_stop (EV_A_ (W)w); 2010 ev_stop (EV_A_ (W)w);
1896} 2011}
1897 2012
1898void noinline 2013void noinline
1900{ 2015{
1901 if (ev_is_active (w)) 2016 if (ev_is_active (w))
1902 { 2017 {
1903 if (w->repeat) 2018 if (w->repeat)
1904 { 2019 {
1905 ((WT)w)->at = mn_now + w->repeat; 2020 ev_at (w) = mn_now + w->repeat;
1906 adjustheap (timers, timercnt, ((W)w)->active - 1); 2021 adjustheap (timers, timercnt, ev_active (w));
1907 } 2022 }
1908 else 2023 else
1909 ev_timer_stop (EV_A_ w); 2024 ev_timer_stop (EV_A_ w);
1910 } 2025 }
1911 else if (w->repeat) 2026 else if (w->repeat)
1912 { 2027 {
1913 w->at = w->repeat; 2028 ev_at (w) = w->repeat;
1914 ev_timer_start (EV_A_ w); 2029 ev_timer_start (EV_A_ w);
1915 } 2030 }
1916} 2031}
1917 2032
1918#if EV_PERIODIC_ENABLE 2033#if EV_PERIODIC_ENABLE
1921{ 2036{
1922 if (expect_false (ev_is_active (w))) 2037 if (expect_false (ev_is_active (w)))
1923 return; 2038 return;
1924 2039
1925 if (w->reschedule_cb) 2040 if (w->reschedule_cb)
1926 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2041 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1927 else if (w->interval) 2042 else if (w->interval)
1928 { 2043 {
1929 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2044 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1930 /* this formula differs from the one in periodic_reify because we do not always round up */ 2045 /* this formula differs from the one in periodic_reify because we do not always round up */
1931 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2046 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1932 } 2047 }
1933 else 2048 else
1934 ((WT)w)->at = w->offset; 2049 ev_at (w) = w->offset;
1935 2050
1936 ev_start (EV_A_ (W)w, ++periodiccnt); 2051 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1937 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2052 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1938 periodics [periodiccnt - 1] = (WT)w; 2053 periodics [ev_active (w)] = (WT)w;
1939 upheap (periodics, periodiccnt - 1); 2054 upheap (periodics, ev_active (w));
1940 2055
1941 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2056 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1942} 2057}
1943 2058
1944void noinline 2059void noinline
1945ev_periodic_stop (EV_P_ ev_periodic *w) 2060ev_periodic_stop (EV_P_ ev_periodic *w)
1946{ 2061{
1947 clear_pending (EV_A_ (W)w); 2062 clear_pending (EV_A_ (W)w);
1948 if (expect_false (!ev_is_active (w))) 2063 if (expect_false (!ev_is_active (w)))
1949 return; 2064 return;
1950 2065
1951 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1952
1953 { 2066 {
1954 int active = ((W)w)->active; 2067 int active = ev_active (w);
1955 2068
2069 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
2070
1956 if (expect_true (--active < --periodiccnt)) 2071 if (expect_true (active < periodiccnt + HEAP0 - 1))
1957 { 2072 {
1958 periodics [active] = periodics [periodiccnt]; 2073 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1959 adjustheap (periodics, periodiccnt, active); 2074 adjustheap (periodics, periodiccnt, active);
1960 } 2075 }
2076
2077 --periodiccnt;
1961 } 2078 }
1962 2079
1963 ev_stop (EV_A_ (W)w); 2080 ev_stop (EV_A_ (W)w);
1964} 2081}
1965 2082
2081 if (w->wd < 0) 2198 if (w->wd < 0)
2082 { 2199 {
2083 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2200 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2084 2201
2085 /* monitor some parent directory for speedup hints */ 2202 /* monitor some parent directory for speedup hints */
2203 /* note that exceeding the hardcoded limit is not a correctness issue, */
2204 /* but an efficiency issue only */
2086 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2205 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2087 { 2206 {
2088 char path [4096]; 2207 char path [4096];
2089 strcpy (path, w->path); 2208 strcpy (path, w->path);
2090 2209
2335 clear_pending (EV_A_ (W)w); 2454 clear_pending (EV_A_ (W)w);
2336 if (expect_false (!ev_is_active (w))) 2455 if (expect_false (!ev_is_active (w)))
2337 return; 2456 return;
2338 2457
2339 { 2458 {
2340 int active = ((W)w)->active; 2459 int active = ev_active (w);
2341 2460
2342 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2461 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2343 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2462 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2344 2463
2345 ev_stop (EV_A_ (W)w); 2464 ev_stop (EV_A_ (W)w);
2346 --idleall; 2465 --idleall;
2347 } 2466 }
2348} 2467}
2365 clear_pending (EV_A_ (W)w); 2484 clear_pending (EV_A_ (W)w);
2366 if (expect_false (!ev_is_active (w))) 2485 if (expect_false (!ev_is_active (w)))
2367 return; 2486 return;
2368 2487
2369 { 2488 {
2370 int active = ((W)w)->active; 2489 int active = ev_active (w);
2490
2371 prepares [active - 1] = prepares [--preparecnt]; 2491 prepares [active - 1] = prepares [--preparecnt];
2372 ((W)prepares [active - 1])->active = active; 2492 ev_active (prepares [active - 1]) = active;
2373 } 2493 }
2374 2494
2375 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2376} 2496}
2377 2497
2392 clear_pending (EV_A_ (W)w); 2512 clear_pending (EV_A_ (W)w);
2393 if (expect_false (!ev_is_active (w))) 2513 if (expect_false (!ev_is_active (w)))
2394 return; 2514 return;
2395 2515
2396 { 2516 {
2397 int active = ((W)w)->active; 2517 int active = ev_active (w);
2518
2398 checks [active - 1] = checks [--checkcnt]; 2519 checks [active - 1] = checks [--checkcnt];
2399 ((W)checks [active - 1])->active = active; 2520 ev_active (checks [active - 1]) = active;
2400 } 2521 }
2401 2522
2402 ev_stop (EV_A_ (W)w); 2523 ev_stop (EV_A_ (W)w);
2403} 2524}
2404 2525
2500 clear_pending (EV_A_ (W)w); 2621 clear_pending (EV_A_ (W)w);
2501 if (expect_false (!ev_is_active (w))) 2622 if (expect_false (!ev_is_active (w)))
2502 return; 2623 return;
2503 2624
2504 { 2625 {
2505 int active = ((W)w)->active; 2626 int active = ev_active (w);
2627
2506 forks [active - 1] = forks [--forkcnt]; 2628 forks [active - 1] = forks [--forkcnt];
2507 ((W)forks [active - 1])->active = active; 2629 ev_active (forks [active - 1]) = active;
2508 } 2630 }
2509 2631
2510 ev_stop (EV_A_ (W)w); 2632 ev_stop (EV_A_ (W)w);
2511} 2633}
2512#endif 2634#endif
2531 clear_pending (EV_A_ (W)w); 2653 clear_pending (EV_A_ (W)w);
2532 if (expect_false (!ev_is_active (w))) 2654 if (expect_false (!ev_is_active (w)))
2533 return; 2655 return;
2534 2656
2535 { 2657 {
2536 int active = ((W)w)->active; 2658 int active = ev_active (w);
2659
2537 asyncs [active - 1] = asyncs [--asynccnt]; 2660 asyncs [active - 1] = asyncs [--asynccnt];
2538 ((W)asyncs [active - 1])->active = active; 2661 ev_active (asyncs [active - 1]) = active;
2539 } 2662 }
2540 2663
2541 ev_stop (EV_A_ (W)w); 2664 ev_stop (EV_A_ (W)w);
2542} 2665}
2543 2666

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines