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

Comparing libev/ev.c (file contents):
Revision 1.220 by root, Sun Apr 6 09:53:17 2008 UTC vs.
Revision 1.241 by root, Fri May 9 13:57:00 2008 UTC

268# include <winsock.h> 268# include <winsock.h>
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>
274# ifdef __cplusplus
275extern "C" {
276# endif
273int eventfd (unsigned int initval, int flags); 277int eventfd (unsigned int initval, int flags);
278# ifdef __cplusplus
279}
280# endif
274#endif 281#endif
275 282
276/**/ 283/**/
277 284
278/* 285/*
293# define expect(expr,value) __builtin_expect ((expr),(value)) 300# define expect(expr,value) __builtin_expect ((expr),(value))
294# define noinline __attribute__ ((noinline)) 301# define noinline __attribute__ ((noinline))
295#else 302#else
296# define expect(expr,value) (expr) 303# define expect(expr,value) (expr)
297# define noinline 304# define noinline
298# if __STDC_VERSION__ < 199901L 305# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
299# define inline 306# define inline
300# endif 307# endif
301#endif 308#endif
302 309
303#define expect_false(expr) expect ((expr) != 0, 0) 310#define expect_false(expr) expect ((expr) != 0, 0)
318 325
319typedef ev_watcher *W; 326typedef ev_watcher *W;
320typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
321typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
322 329
330#define ev_active(w) ((W)(w))->active
331#define ev_at(w) ((WT)(w))->at
332
323#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
324/* 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 */
325/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
326static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
327#endif 337#endif
353 perror (msg); 363 perror (msg);
354 abort (); 364 abort ();
355 } 365 }
356} 366}
357 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
358static void *(*alloc)(void *ptr, long size); 383static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
359 384
360void 385void
361ev_set_allocator (void *(*cb)(void *ptr, long size)) 386ev_set_allocator (void *(*cb)(void *ptr, long size))
362{ 387{
363 alloc = cb; 388 alloc = cb;
364} 389}
365 390
366inline_speed void * 391inline_speed void *
367ev_realloc (void *ptr, long size) 392ev_realloc (void *ptr, long size)
368{ 393{
369 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 394 ptr = alloc (ptr, size);
370 395
371 if (!ptr && size) 396 if (!ptr && size)
372 { 397 {
373 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 398 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
374 abort (); 399 abort ();
397 W w; 422 W w;
398 int events; 423 int events;
399} ANPENDING; 424} ANPENDING;
400 425
401#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
402typedef struct 428typedef struct
403{ 429{
404 WL head; 430 WL head;
405} 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)
406#endif 450#endif
407 451
408#if EV_MULTIPLICITY 452#if EV_MULTIPLICITY
409 453
410 struct ev_loop 454 struct ev_loop
495 } 539 }
496} 540}
497 541
498/*****************************************************************************/ 542/*****************************************************************************/
499 543
544#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
545
500int inline_size 546int inline_size
501array_nextsize (int elem, int cur, int cnt) 547array_nextsize (int elem, int cur, int cnt)
502{ 548{
503 int ncur = cur + 1; 549 int ncur = cur + 1;
504 550
505 do 551 do
506 ncur <<= 1; 552 ncur <<= 1;
507 while (cnt > ncur); 553 while (cnt > ncur);
508 554
509 /* 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 */
510 if (elem * ncur > 4096) 556 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
511 { 557 {
512 ncur *= elem; 558 ncur *= elem;
513 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 559 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
514 ncur = ncur - sizeof (void *) * 4; 560 ncur = ncur - sizeof (void *) * 4;
515 ncur /= elem; 561 ncur /= elem;
516 } 562 }
517 563
518 return ncur; 564 return ncur;
732 } 778 }
733} 779}
734 780
735/*****************************************************************************/ 781/*****************************************************************************/
736 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 */
737void inline_speed 802void inline_speed
738upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
739{ 804{
740 WT w = heap [k]; 805 ANHE he = heap [k];
741 806
742 while (k) 807 for (;;)
743 { 808 {
744 int p = (k - 1) >> 1; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
745 810
746 if (heap [p]->at <= w->at) 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
747 break; 812 break;
748 813
749 heap [k] = heap [p]; 814 heap [k] = heap [p];
750 ((W)heap [k])->active = k + 1; 815 ev_active (ANHE_w (heap [k])) = k;
751 k = p; 816 k = p;
752 } 817 }
753 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
754 heap [k] = w; 890 heap [k] = w;
755 ((W)heap [k])->active = k + 1; 891 ev_active (ANHE_w (heap [k])) = k;
756} 892}
757 893
894/* away from the root */
758void inline_speed 895void inline_speed
759downheap (WT *heap, int N, int k) 896downheap (ANHE *heap, int N, int k)
760{ 897{
761 WT w = heap [k]; 898 ANHE he = heap [k];
762 899
763 for (;;) 900 for (;;)
764 { 901 {
765 int c = (k << 1) + 1; 902 int c = k << 1;
766 903
767 if (c >= N) 904 if (c > N)
768 break; 905 break;
769 906
770 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])
771 ? 1 : 0; 908 ? 1 : 0;
772 909
773 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
774 break; 911 break;
775 912
776 heap [k] = heap [c]; 913 heap [k] = heap [c];
777 ((W)heap [k])->active = k + 1; 914 ev_active (ANHE_w (heap [k])) = k;
778 915
779 k = c; 916 k = c;
780 } 917 }
781 918
782 heap [k] = w; 919 heap [k] = he;
783 ((W)heap [k])->active = k + 1; 920 ev_active (ANHE_w (he)) = k;
784} 921}
922#endif
785 923
786void inline_size 924void inline_size
787adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
788{ 926{
789 upheap (heap, k); 927 upheap (heap, k);
790 downheap (heap, N, k); 928 downheap (heap, N, k);
791} 929}
792 930
884pipecb (EV_P_ ev_io *iow, int revents) 1022pipecb (EV_P_ ev_io *iow, int revents)
885{ 1023{
886#if EV_USE_EVENTFD 1024#if EV_USE_EVENTFD
887 if (evfd >= 0) 1025 if (evfd >= 0)
888 { 1026 {
889 uint64_t counter = 1; 1027 uint64_t counter;
890 read (evfd, &counter, sizeof (uint64_t)); 1028 read (evfd, &counter, sizeof (uint64_t));
891 } 1029 }
892 else 1030 else
893#endif 1031#endif
894 { 1032 {
1163 if (!(flags & EVFLAG_NOENV) 1301 if (!(flags & EVFLAG_NOENV)
1164 && !enable_secure () 1302 && !enable_secure ()
1165 && getenv ("LIBEV_FLAGS")) 1303 && getenv ("LIBEV_FLAGS"))
1166 flags = atoi (getenv ("LIBEV_FLAGS")); 1304 flags = atoi (getenv ("LIBEV_FLAGS"));
1167 1305
1168 if (!(flags & 0x0000ffffUL)) 1306 if (!(flags & 0x0000ffffU))
1169 flags |= ev_recommended_backends (); 1307 flags |= ev_recommended_backends ();
1170 1308
1171#if EV_USE_PORT 1309#if EV_USE_PORT
1172 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1310 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1173#endif 1311#endif
1261#endif 1399#endif
1262 1400
1263 backend = 0; 1401 backend = 0;
1264} 1402}
1265 1403
1404#if EV_USE_INOTIFY
1266void inline_size infy_fork (EV_P); 1405void inline_size infy_fork (EV_P);
1406#endif
1267 1407
1268void inline_size 1408void inline_size
1269loop_fork (EV_P) 1409loop_fork (EV_P)
1270{ 1410{
1271#if EV_USE_PORT 1411#if EV_USE_PORT
1338void 1478void
1339ev_loop_fork (EV_P) 1479ev_loop_fork (EV_P)
1340{ 1480{
1341 postfork = 1; /* must be in line with ev_default_fork */ 1481 postfork = 1; /* must be in line with ev_default_fork */
1342} 1482}
1343
1344#endif 1483#endif
1345 1484
1346#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1347struct ev_loop * 1486struct ev_loop *
1348ev_default_loop_init (unsigned int flags) 1487ev_default_loop_init (unsigned int flags)
1429 EV_CB_INVOKE (p->w, p->events); 1568 EV_CB_INVOKE (p->w, p->events);
1430 } 1569 }
1431 } 1570 }
1432} 1571}
1433 1572
1434void inline_size
1435timers_reify (EV_P)
1436{
1437 while (timercnt && ((WT)timers [0])->at <= mn_now)
1438 {
1439 ev_timer *w = (ev_timer *)timers [0];
1440
1441 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1442
1443 /* first reschedule or stop timer */
1444 if (w->repeat)
1445 {
1446 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1447
1448 ((WT)w)->at += w->repeat;
1449 if (((WT)w)->at < mn_now)
1450 ((WT)w)->at = mn_now;
1451
1452 downheap (timers, timercnt, 0);
1453 }
1454 else
1455 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1456
1457 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1458 }
1459}
1460
1461#if EV_PERIODIC_ENABLE
1462void inline_size
1463periodics_reify (EV_P)
1464{
1465 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1466 {
1467 ev_periodic *w = (ev_periodic *)periodics [0];
1468
1469 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1470
1471 /* first reschedule or stop timer */
1472 if (w->reschedule_cb)
1473 {
1474 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1475 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1476 downheap (periodics, periodiccnt, 0);
1477 }
1478 else if (w->interval)
1479 {
1480 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1481 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1482 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1483 downheap (periodics, periodiccnt, 0);
1484 }
1485 else
1486 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1487
1488 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1489 }
1490}
1491
1492static void noinline
1493periodics_reschedule (EV_P)
1494{
1495 int i;
1496
1497 /* adjust periodics after time jump */
1498 for (i = 0; i < periodiccnt; ++i)
1499 {
1500 ev_periodic *w = (ev_periodic *)periodics [i];
1501
1502 if (w->reschedule_cb)
1503 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1504 else if (w->interval)
1505 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1506 }
1507
1508 /* now rebuild the heap */
1509 for (i = periodiccnt >> 1; i--; )
1510 downheap (periodics, periodiccnt, i);
1511}
1512#endif
1513
1514#if EV_IDLE_ENABLE 1573#if EV_IDLE_ENABLE
1515void inline_size 1574void inline_size
1516idle_reify (EV_P) 1575idle_reify (EV_P)
1517{ 1576{
1518 if (expect_false (idleall)) 1577 if (expect_false (idleall))
1529 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1588 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1530 break; 1589 break;
1531 } 1590 }
1532 } 1591 }
1533 } 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);
1534} 1673}
1535#endif 1674#endif
1536 1675
1537void inline_speed 1676void inline_speed
1538time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1567 */ 1706 */
1568 for (i = 4; --i; ) 1707 for (i = 4; --i; )
1569 { 1708 {
1570 rtmn_diff = ev_rt_now - mn_now; 1709 rtmn_diff = ev_rt_now - mn_now;
1571 1710
1572 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1711 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1573 return; /* all is well */ 1712 return; /* all is well */
1574 1713
1575 ev_rt_now = ev_time (); 1714 ev_rt_now = ev_time ();
1576 mn_now = get_clock (); 1715 mn_now = get_clock ();
1577 now_floor = mn_now; 1716 now_floor = mn_now;
1593#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1594 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1595#endif 1734#endif
1596 /* 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 */
1597 for (i = 0; i < timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1737 {
1738 ANHE *he = timers + i + HEAP0;
1598 ((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 }
1599 } 1742 }
1600 1743
1601 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1602 } 1745 }
1603} 1746}
1673 1816
1674 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1675 1818
1676 if (timercnt) 1819 if (timercnt)
1677 { 1820 {
1678 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1679 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1680 } 1823 }
1681 1824
1682#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1683 if (periodiccnt) 1826 if (periodiccnt)
1684 { 1827 {
1685 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;
1686 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1687 } 1830 }
1688#endif 1831#endif
1689 1832
1690 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1856ev_timer_start (EV_P_ ev_timer *w) 1999ev_timer_start (EV_P_ ev_timer *w)
1857{ 2000{
1858 if (expect_false (ev_is_active (w))) 2001 if (expect_false (ev_is_active (w)))
1859 return; 2002 return;
1860 2003
1861 ((WT)w)->at += mn_now; 2004 ev_at (w) += mn_now;
1862 2005
1863 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.));
1864 2007
1865 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1866 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1867 timers [timercnt - 1] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
1868 upheap (timers, timercnt - 1); 2011 ANHE_at_set (timers [ev_active (w)]);
2012 upheap (timers, ev_active (w));
1869 2013
1870 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1871} 2015}
1872 2016
1873void noinline 2017void noinline
1874ev_timer_stop (EV_P_ ev_timer *w) 2018ev_timer_stop (EV_P_ ev_timer *w)
1875{ 2019{
1876 clear_pending (EV_A_ (W)w); 2020 clear_pending (EV_A_ (W)w);
1877 if (expect_false (!ev_is_active (w))) 2021 if (expect_false (!ev_is_active (w)))
1878 return; 2022 return;
1879 2023
1880 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1881
1882 { 2024 {
1883 int active = ((W)w)->active; 2025 int active = ev_active (w);
1884 2026
2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2028
1885 if (expect_true (--active < --timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1886 { 2030 {
1887 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1888 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1889 } 2033 }
2034
2035 --timercnt;
1890 } 2036 }
1891 2037
1892 ((WT)w)->at -= mn_now; 2038 ev_at (w) -= mn_now;
1893 2039
1894 ev_stop (EV_A_ (W)w); 2040 ev_stop (EV_A_ (W)w);
1895} 2041}
1896 2042
1897void noinline 2043void noinline
1899{ 2045{
1900 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1901 { 2047 {
1902 if (w->repeat) 2048 if (w->repeat)
1903 { 2049 {
1904 ((WT)w)->at = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1905 adjustheap (timers, timercnt, ((W)w)->active - 1); 2052 adjustheap (timers, timercnt, ev_active (w));
1906 } 2053 }
1907 else 2054 else
1908 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1909 } 2056 }
1910 else if (w->repeat) 2057 else if (w->repeat)
1911 { 2058 {
1912 w->at = w->repeat; 2059 ev_at (w) = w->repeat;
1913 ev_timer_start (EV_A_ w); 2060 ev_timer_start (EV_A_ w);
1914 } 2061 }
1915} 2062}
1916 2063
1917#if EV_PERIODIC_ENABLE 2064#if EV_PERIODIC_ENABLE
1920{ 2067{
1921 if (expect_false (ev_is_active (w))) 2068 if (expect_false (ev_is_active (w)))
1922 return; 2069 return;
1923 2070
1924 if (w->reschedule_cb) 2071 if (w->reschedule_cb)
1925 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2072 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1926 else if (w->interval) 2073 else if (w->interval)
1927 { 2074 {
1928 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.));
1929 /* 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 */
1930 ((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;
1931 } 2078 }
1932 else 2079 else
1933 ((WT)w)->at = w->offset; 2080 ev_at (w) = w->offset;
1934 2081
1935 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1936 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1937 periodics [periodiccnt - 1] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1938 upheap (periodics, periodiccnt - 1); 2085 upheap (periodics, ev_active (w));
1939 2086
1940 /*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));*/
1941} 2088}
1942 2089
1943void noinline 2090void noinline
1944ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1945{ 2092{
1946 clear_pending (EV_A_ (W)w); 2093 clear_pending (EV_A_ (W)w);
1947 if (expect_false (!ev_is_active (w))) 2094 if (expect_false (!ev_is_active (w)))
1948 return; 2095 return;
1949 2096
1950 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1951
1952 { 2097 {
1953 int active = ((W)w)->active; 2098 int active = ev_active (w);
1954 2099
2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2101
1955 if (expect_true (--active < --periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1956 { 2103 {
1957 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1958 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1959 } 2106 }
2107
2108 --periodiccnt;
1960 } 2109 }
1961 2110
1962 ev_stop (EV_A_ (W)w); 2111 ev_stop (EV_A_ (W)w);
1963} 2112}
1964 2113
2080 if (w->wd < 0) 2229 if (w->wd < 0)
2081 { 2230 {
2082 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 */
2083 2232
2084 /* 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 */
2085 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2236 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2086 { 2237 {
2087 char path [4096]; 2238 char path [4096];
2088 strcpy (path, w->path); 2239 strcpy (path, w->path);
2089 2240
2334 clear_pending (EV_A_ (W)w); 2485 clear_pending (EV_A_ (W)w);
2335 if (expect_false (!ev_is_active (w))) 2486 if (expect_false (!ev_is_active (w)))
2336 return; 2487 return;
2337 2488
2338 { 2489 {
2339 int active = ((W)w)->active; 2490 int active = ev_active (w);
2340 2491
2341 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2492 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2342 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2493 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2343 2494
2344 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2345 --idleall; 2496 --idleall;
2346 } 2497 }
2347} 2498}
2364 clear_pending (EV_A_ (W)w); 2515 clear_pending (EV_A_ (W)w);
2365 if (expect_false (!ev_is_active (w))) 2516 if (expect_false (!ev_is_active (w)))
2366 return; 2517 return;
2367 2518
2368 { 2519 {
2369 int active = ((W)w)->active; 2520 int active = ev_active (w);
2521
2370 prepares [active - 1] = prepares [--preparecnt]; 2522 prepares [active - 1] = prepares [--preparecnt];
2371 ((W)prepares [active - 1])->active = active; 2523 ev_active (prepares [active - 1]) = active;
2372 } 2524 }
2373 2525
2374 ev_stop (EV_A_ (W)w); 2526 ev_stop (EV_A_ (W)w);
2375} 2527}
2376 2528
2391 clear_pending (EV_A_ (W)w); 2543 clear_pending (EV_A_ (W)w);
2392 if (expect_false (!ev_is_active (w))) 2544 if (expect_false (!ev_is_active (w)))
2393 return; 2545 return;
2394 2546
2395 { 2547 {
2396 int active = ((W)w)->active; 2548 int active = ev_active (w);
2549
2397 checks [active - 1] = checks [--checkcnt]; 2550 checks [active - 1] = checks [--checkcnt];
2398 ((W)checks [active - 1])->active = active; 2551 ev_active (checks [active - 1]) = active;
2399 } 2552 }
2400 2553
2401 ev_stop (EV_A_ (W)w); 2554 ev_stop (EV_A_ (W)w);
2402} 2555}
2403 2556
2499 clear_pending (EV_A_ (W)w); 2652 clear_pending (EV_A_ (W)w);
2500 if (expect_false (!ev_is_active (w))) 2653 if (expect_false (!ev_is_active (w)))
2501 return; 2654 return;
2502 2655
2503 { 2656 {
2504 int active = ((W)w)->active; 2657 int active = ev_active (w);
2658
2505 forks [active - 1] = forks [--forkcnt]; 2659 forks [active - 1] = forks [--forkcnt];
2506 ((W)forks [active - 1])->active = active; 2660 ev_active (forks [active - 1]) = active;
2507 } 2661 }
2508 2662
2509 ev_stop (EV_A_ (W)w); 2663 ev_stop (EV_A_ (W)w);
2510} 2664}
2511#endif 2665#endif
2530 clear_pending (EV_A_ (W)w); 2684 clear_pending (EV_A_ (W)w);
2531 if (expect_false (!ev_is_active (w))) 2685 if (expect_false (!ev_is_active (w)))
2532 return; 2686 return;
2533 2687
2534 { 2688 {
2535 int active = ((W)w)->active; 2689 int active = ev_active (w);
2690
2536 asyncs [active - 1] = asyncs [--asynccnt]; 2691 asyncs [active - 1] = asyncs [--asynccnt];
2537 ((W)asyncs [active - 1])->active = active; 2692 ev_active (asyncs [active - 1]) = active;
2538 } 2693 }
2539 2694
2540 ev_stop (EV_A_ (W)w); 2695 ev_stop (EV_A_ (W)w);
2541} 2696}
2542 2697

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines