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.242 by root, Fri May 9 14:07:19 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 ();
398 W w; 422 W w;
399 int events; 423 int events;
400} ANPENDING; 424} ANPENDING;
401 425
402#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
403typedef struct 428typedef struct
404{ 429{
405 WL head; 430 WL head;
406} ANFS; 431} ANFS;
432#endif
433
434/* Heap Entry */
435#define EV_HEAP_CACHE_AT 0
436#if EV_HEAP_CACHE_AT
437 typedef struct {
438 WT w;
439 ev_tstamp at;
440 } ANHE;
441
442 #define ANHE_w(he) (he).w /* access watcher, read-write */
443 #define ANHE_at(he) (he).at /* access cached at, read-only */
444 #define ANHE_at_set(he) (he).at = (he).w->at /* update at from watcher */
445#else
446 typedef WT ANHE;
447
448 #define ANHE_w(he) (he)
449 #define ANHE_at(he) (he)->at
450 #define ANHE_at_set(he)
407#endif 451#endif
408 452
409#if EV_MULTIPLICITY 453#if EV_MULTIPLICITY
410 454
411 struct ev_loop 455 struct ev_loop
496 } 540 }
497} 541}
498 542
499/*****************************************************************************/ 543/*****************************************************************************/
500 544
545#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
546
501int inline_size 547int inline_size
502array_nextsize (int elem, int cur, int cnt) 548array_nextsize (int elem, int cur, int cnt)
503{ 549{
504 int ncur = cur + 1; 550 int ncur = cur + 1;
505 551
506 do 552 do
507 ncur <<= 1; 553 ncur <<= 1;
508 while (cnt > ncur); 554 while (cnt > ncur);
509 555
510 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 556 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
511 if (elem * ncur > 4096) 557 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
512 { 558 {
513 ncur *= elem; 559 ncur *= elem;
514 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 560 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
515 ncur = ncur - sizeof (void *) * 4; 561 ncur = ncur - sizeof (void *) * 4;
516 ncur /= elem; 562 ncur /= elem;
517 } 563 }
518 564
519 return ncur; 565 return ncur;
733 } 779 }
734} 780}
735 781
736/*****************************************************************************/ 782/*****************************************************************************/
737 783
784/*
785 * the heap functions want a real array index. array index 0 uis guaranteed to not
786 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
787 * the branching factor of the d-tree.
788 */
789
790/*
791 * at the moment we allow libev the luxury of two heaps,
792 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
793 * which is more cache-efficient.
794 * the difference is about 5% with 50000+ watchers.
795 */
796#define EV_USE_4HEAP !EV_MINIMAL
797#if EV_USE_4HEAP
798
799#define DHEAP 4
800#define HEAP0 (DHEAP - 1) /* index of first element in heap */
801
802/* towards the root */
738void inline_speed 803void inline_speed
739upheap (WT *heap, int k) 804upheap (ANHE *heap, int k)
740{ 805{
741 WT w = heap [k]; 806 ANHE he = heap [k];
742 807
743 while (k) 808 for (;;)
744 { 809 {
745 int p = (k - 1) >> 1; 810 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
746 811
747 if (heap [p]->at <= w->at) 812 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
748 break; 813 break;
749 814
750 heap [k] = heap [p]; 815 heap [k] = heap [p];
751 ((W)heap [k])->active = k + 1; 816 ev_active (ANHE_w (heap [k])) = k;
752 k = p; 817 k = p;
753 } 818 }
754 819
820 ev_active (ANHE_w (he)) = k;
821 heap [k] = he;
822}
823
824/* away from the root */
825void inline_speed
826downheap (ANHE *heap, int N, int k)
827{
828 ANHE he = heap [k];
829 ANHE *E = heap + N + HEAP0;
830
831 for (;;)
832 {
833 ev_tstamp minat;
834 ANHE *minpos;
835 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
836
837 // find minimum child
838 if (expect_true (pos + DHEAP - 1 < E))
839 {
840 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
843 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
844 }
845 else if (pos < E)
846 {
847 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
848 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
849 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
850 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
851 }
852 else
853 break;
854
855 if (ANHE_at (he) <= minat)
856 break;
857
858 ev_active (ANHE_w (*minpos)) = k;
859 heap [k] = *minpos;
860
861 k = minpos - heap;
862 }
863
864 ev_active (ANHE_w (he)) = k;
865 heap [k] = he;
866}
867
868#else // 4HEAP
869
870#define HEAP0 1
871
872/* towards the root */
873void inline_speed
874upheap (ANHE *heap, int k)
875{
876 ANHE he = heap [k];
877
878 for (;;)
879 {
880 int p = k >> 1;
881
882 /* maybe we could use a dummy element at heap [0]? */
883 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
884 break;
885
886 heap [k] = heap [p];
887 ev_active (ANHE_w (heap [k])) = k;
888 k = p;
889 }
890
755 heap [k] = w; 891 heap [k] = w;
756 ((W)heap [k])->active = k + 1; 892 ev_active (ANHE_w (heap [k])) = k;
757} 893}
758 894
895/* away from the root */
759void inline_speed 896void inline_speed
760downheap (WT *heap, int N, int k) 897downheap (ANHE *heap, int N, int k)
761{ 898{
762 WT w = heap [k]; 899 ANHE he = heap [k];
763 900
764 for (;;) 901 for (;;)
765 { 902 {
766 int c = (k << 1) + 1; 903 int c = k << 1;
767 904
768 if (c >= N) 905 if (c > N)
769 break; 906 break;
770 907
771 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 908 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
772 ? 1 : 0; 909 ? 1 : 0;
773 910
774 if (w->at <= heap [c]->at) 911 if (w->at <= ANHE_at (heap [c]))
775 break; 912 break;
776 913
777 heap [k] = heap [c]; 914 heap [k] = heap [c];
778 ((W)heap [k])->active = k + 1; 915 ev_active (ANHE_w (heap [k])) = k;
779 916
780 k = c; 917 k = c;
781 } 918 }
782 919
783 heap [k] = w; 920 heap [k] = he;
784 ((W)heap [k])->active = k + 1; 921 ev_active (ANHE_w (he)) = k;
785} 922}
923#endif
786 924
787void inline_size 925void inline_size
788adjustheap (WT *heap, int N, int k) 926adjustheap (ANHE *heap, int N, int k)
789{ 927{
790 upheap (heap, k); 928 upheap (heap, k);
791 downheap (heap, N, k); 929 downheap (heap, N, k);
792} 930}
793 931
885pipecb (EV_P_ ev_io *iow, int revents) 1023pipecb (EV_P_ ev_io *iow, int revents)
886{ 1024{
887#if EV_USE_EVENTFD 1025#if EV_USE_EVENTFD
888 if (evfd >= 0) 1026 if (evfd >= 0)
889 { 1027 {
890 uint64_t counter = 1; 1028 uint64_t counter;
891 read (evfd, &counter, sizeof (uint64_t)); 1029 read (evfd, &counter, sizeof (uint64_t));
892 } 1030 }
893 else 1031 else
894#endif 1032#endif
895 { 1033 {
1164 if (!(flags & EVFLAG_NOENV) 1302 if (!(flags & EVFLAG_NOENV)
1165 && !enable_secure () 1303 && !enable_secure ()
1166 && getenv ("LIBEV_FLAGS")) 1304 && getenv ("LIBEV_FLAGS"))
1167 flags = atoi (getenv ("LIBEV_FLAGS")); 1305 flags = atoi (getenv ("LIBEV_FLAGS"));
1168 1306
1169 if (!(flags & 0x0000ffffUL)) 1307 if (!(flags & 0x0000ffffU))
1170 flags |= ev_recommended_backends (); 1308 flags |= ev_recommended_backends ();
1171 1309
1172#if EV_USE_PORT 1310#if EV_USE_PORT
1173 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1311 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1174#endif 1312#endif
1262#endif 1400#endif
1263 1401
1264 backend = 0; 1402 backend = 0;
1265} 1403}
1266 1404
1405#if EV_USE_INOTIFY
1267void inline_size infy_fork (EV_P); 1406void inline_size infy_fork (EV_P);
1407#endif
1268 1408
1269void inline_size 1409void inline_size
1270loop_fork (EV_P) 1410loop_fork (EV_P)
1271{ 1411{
1272#if EV_USE_PORT 1412#if EV_USE_PORT
1339void 1479void
1340ev_loop_fork (EV_P) 1480ev_loop_fork (EV_P)
1341{ 1481{
1342 postfork = 1; /* must be in line with ev_default_fork */ 1482 postfork = 1; /* must be in line with ev_default_fork */
1343} 1483}
1344
1345#endif 1484#endif
1346 1485
1347#if EV_MULTIPLICITY 1486#if EV_MULTIPLICITY
1348struct ev_loop * 1487struct ev_loop *
1349ev_default_loop_init (unsigned int flags) 1488ev_default_loop_init (unsigned int flags)
1430 EV_CB_INVOKE (p->w, p->events); 1569 EV_CB_INVOKE (p->w, p->events);
1431 } 1570 }
1432 } 1571 }
1433} 1572}
1434 1573
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 1574#if EV_IDLE_ENABLE
1516void inline_size 1575void inline_size
1517idle_reify (EV_P) 1576idle_reify (EV_P)
1518{ 1577{
1519 if (expect_false (idleall)) 1578 if (expect_false (idleall))
1530 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1589 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1531 break; 1590 break;
1532 } 1591 }
1533 } 1592 }
1534 } 1593 }
1594}
1595#endif
1596
1597void inline_size
1598timers_reify (EV_P)
1599{
1600 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1601 {
1602 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1603
1604 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1605
1606 /* first reschedule or stop timer */
1607 if (w->repeat)
1608 {
1609 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1610
1611 ev_at (w) += w->repeat;
1612 if (ev_at (w) < mn_now)
1613 ev_at (w) = mn_now;
1614
1615 ANHE_at_set (timers [HEAP0]);
1616 downheap (timers, timercnt, HEAP0);
1617 }
1618 else
1619 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1620
1621 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1622 }
1623}
1624
1625#if EV_PERIODIC_ENABLE
1626void inline_size
1627periodics_reify (EV_P)
1628{
1629 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1630 {
1631 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1632
1633 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1634
1635 /* first reschedule or stop timer */
1636 if (w->reschedule_cb)
1637 {
1638 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1639 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1640 ANHE_at_set (periodics [HEAP0]);
1641 downheap (periodics, periodiccnt, HEAP0);
1642 }
1643 else if (w->interval)
1644 {
1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1646 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1647 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1648 ANHE_at_set (periodics [HEAP0]);
1649 downheap (periodics, periodiccnt, HEAP0);
1650 }
1651 else
1652 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1653
1654 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1655 }
1656}
1657
1658static void noinline
1659periodics_reschedule (EV_P)
1660{
1661 int i;
1662
1663 /* adjust periodics after time jump */
1664 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1665 {
1666 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1667
1668 if (w->reschedule_cb)
1669 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1670 else if (w->interval)
1671 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1672
1673 ANHE_at_set (periodics [i]);
1674 }
1675
1676 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1677 for (i = periodiccnt >> 1; --i; )
1678 downheap (periodics, periodiccnt, i + HEAP0);
1535} 1679}
1536#endif 1680#endif
1537 1681
1538void inline_speed 1682void inline_speed
1539time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1568 */ 1712 */
1569 for (i = 4; --i; ) 1713 for (i = 4; --i; )
1570 { 1714 {
1571 rtmn_diff = ev_rt_now - mn_now; 1715 rtmn_diff = ev_rt_now - mn_now;
1572 1716
1573 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1717 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1574 return; /* all is well */ 1718 return; /* all is well */
1575 1719
1576 ev_rt_now = ev_time (); 1720 ev_rt_now = ev_time ();
1577 mn_now = get_clock (); 1721 mn_now = get_clock ();
1578 now_floor = mn_now; 1722 now_floor = mn_now;
1594#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1595 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1596#endif 1740#endif
1597 /* adjust timers. this is easy, as the offset is the same for all of them */ 1741 /* adjust timers. this is easy, as the offset is the same for all of them */
1598 for (i = 0; i < timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1743 {
1744 ANHE *he = timers + i + HEAP0;
1599 ((WT)timers [i])->at += ev_rt_now - mn_now; 1745 ANHE_w (*he)->at += ev_rt_now - mn_now;
1746 ANHE_at_set (*he);
1747 }
1600 } 1748 }
1601 1749
1602 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1603 } 1751 }
1604} 1752}
1674 1822
1675 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1676 1824
1677 if (timercnt) 1825 if (timercnt)
1678 { 1826 {
1679 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1680 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1681 } 1829 }
1682 1830
1683#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1684 if (periodiccnt) 1832 if (periodiccnt)
1685 { 1833 {
1686 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1834 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1687 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1688 } 1836 }
1689#endif 1837#endif
1690 1838
1691 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1843{ 1991{
1844 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1845 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1846 return; 1994 return;
1847 1995
1848 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1996 assert (("ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1849 1997
1850 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1851 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1852 2000
1853 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1857ev_timer_start (EV_P_ ev_timer *w) 2005ev_timer_start (EV_P_ ev_timer *w)
1858{ 2006{
1859 if (expect_false (ev_is_active (w))) 2007 if (expect_false (ev_is_active (w)))
1860 return; 2008 return;
1861 2009
1862 ((WT)w)->at += mn_now; 2010 ev_at (w) += mn_now;
1863 2011
1864 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2012 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1865 2013
1866 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1867 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1868 timers [timercnt - 1] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
1869 upheap (timers, timercnt - 1); 2017 ANHE_at_set (timers [ev_active (w)]);
2018 upheap (timers, ev_active (w));
1870 2019
1871 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1872} 2021}
1873 2022
1874void noinline 2023void noinline
1875ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1876{ 2025{
1877 clear_pending (EV_A_ (W)w); 2026 clear_pending (EV_A_ (W)w);
1878 if (expect_false (!ev_is_active (w))) 2027 if (expect_false (!ev_is_active (w)))
1879 return; 2028 return;
1880 2029
1881 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1882
1883 { 2030 {
1884 int active = ((W)w)->active; 2031 int active = ev_active (w);
1885 2032
2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2034
1886 if (expect_true (--active < --timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1887 { 2036 {
1888 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1889 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1890 } 2039 }
2040
2041 --timercnt;
1891 } 2042 }
1892 2043
1893 ((WT)w)->at -= mn_now; 2044 ev_at (w) -= mn_now;
1894 2045
1895 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1896} 2047}
1897 2048
1898void noinline 2049void noinline
1900{ 2051{
1901 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1902 { 2053 {
1903 if (w->repeat) 2054 if (w->repeat)
1904 { 2055 {
1905 ((WT)w)->at = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1906 adjustheap (timers, timercnt, ((W)w)->active - 1); 2058 adjustheap (timers, timercnt, ev_active (w));
1907 } 2059 }
1908 else 2060 else
1909 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1910 } 2062 }
1911 else if (w->repeat) 2063 else if (w->repeat)
1912 { 2064 {
1913 w->at = w->repeat; 2065 ev_at (w) = w->repeat;
1914 ev_timer_start (EV_A_ w); 2066 ev_timer_start (EV_A_ w);
1915 } 2067 }
1916} 2068}
1917 2069
1918#if EV_PERIODIC_ENABLE 2070#if EV_PERIODIC_ENABLE
1921{ 2073{
1922 if (expect_false (ev_is_active (w))) 2074 if (expect_false (ev_is_active (w)))
1923 return; 2075 return;
1924 2076
1925 if (w->reschedule_cb) 2077 if (w->reschedule_cb)
1926 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2078 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1927 else if (w->interval) 2079 else if (w->interval)
1928 { 2080 {
1929 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2081 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1930 /* this formula differs from the one in periodic_reify because we do not always round up */ 2082 /* this formula differs from the one in periodic_reify because we do not always round up */
1931 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2083 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1932 } 2084 }
1933 else 2085 else
1934 ((WT)w)->at = w->offset; 2086 ev_at (w) = w->offset;
1935 2087
1936 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1937 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1938 periodics [periodiccnt - 1] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1939 upheap (periodics, periodiccnt - 1); 2091 upheap (periodics, ev_active (w));
1940 2092
1941 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2093 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1942} 2094}
1943 2095
1944void noinline 2096void noinline
1945ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1946{ 2098{
1947 clear_pending (EV_A_ (W)w); 2099 clear_pending (EV_A_ (W)w);
1948 if (expect_false (!ev_is_active (w))) 2100 if (expect_false (!ev_is_active (w)))
1949 return; 2101 return;
1950 2102
1951 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1952
1953 { 2103 {
1954 int active = ((W)w)->active; 2104 int active = ev_active (w);
1955 2105
2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2107
1956 if (expect_true (--active < --periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1957 { 2109 {
1958 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1959 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1960 } 2112 }
2113
2114 --periodiccnt;
1961 } 2115 }
1962 2116
1963 ev_stop (EV_A_ (W)w); 2117 ev_stop (EV_A_ (W)w);
1964} 2118}
1965 2119
2081 if (w->wd < 0) 2235 if (w->wd < 0)
2082 { 2236 {
2083 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2237 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2084 2238
2085 /* monitor some parent directory for speedup hints */ 2239 /* monitor some parent directory for speedup hints */
2240 /* note that exceeding the hardcoded limit is not a correctness issue, */
2241 /* but an efficiency issue only */
2086 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2242 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2087 { 2243 {
2088 char path [4096]; 2244 char path [4096];
2089 strcpy (path, w->path); 2245 strcpy (path, w->path);
2090 2246
2335 clear_pending (EV_A_ (W)w); 2491 clear_pending (EV_A_ (W)w);
2336 if (expect_false (!ev_is_active (w))) 2492 if (expect_false (!ev_is_active (w)))
2337 return; 2493 return;
2338 2494
2339 { 2495 {
2340 int active = ((W)w)->active; 2496 int active = ev_active (w);
2341 2497
2342 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2498 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2343 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2499 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2344 2500
2345 ev_stop (EV_A_ (W)w); 2501 ev_stop (EV_A_ (W)w);
2346 --idleall; 2502 --idleall;
2347 } 2503 }
2348} 2504}
2365 clear_pending (EV_A_ (W)w); 2521 clear_pending (EV_A_ (W)w);
2366 if (expect_false (!ev_is_active (w))) 2522 if (expect_false (!ev_is_active (w)))
2367 return; 2523 return;
2368 2524
2369 { 2525 {
2370 int active = ((W)w)->active; 2526 int active = ev_active (w);
2527
2371 prepares [active - 1] = prepares [--preparecnt]; 2528 prepares [active - 1] = prepares [--preparecnt];
2372 ((W)prepares [active - 1])->active = active; 2529 ev_active (prepares [active - 1]) = active;
2373 } 2530 }
2374 2531
2375 ev_stop (EV_A_ (W)w); 2532 ev_stop (EV_A_ (W)w);
2376} 2533}
2377 2534
2392 clear_pending (EV_A_ (W)w); 2549 clear_pending (EV_A_ (W)w);
2393 if (expect_false (!ev_is_active (w))) 2550 if (expect_false (!ev_is_active (w)))
2394 return; 2551 return;
2395 2552
2396 { 2553 {
2397 int active = ((W)w)->active; 2554 int active = ev_active (w);
2555
2398 checks [active - 1] = checks [--checkcnt]; 2556 checks [active - 1] = checks [--checkcnt];
2399 ((W)checks [active - 1])->active = active; 2557 ev_active (checks [active - 1]) = active;
2400 } 2558 }
2401 2559
2402 ev_stop (EV_A_ (W)w); 2560 ev_stop (EV_A_ (W)w);
2403} 2561}
2404 2562
2500 clear_pending (EV_A_ (W)w); 2658 clear_pending (EV_A_ (W)w);
2501 if (expect_false (!ev_is_active (w))) 2659 if (expect_false (!ev_is_active (w)))
2502 return; 2660 return;
2503 2661
2504 { 2662 {
2505 int active = ((W)w)->active; 2663 int active = ev_active (w);
2664
2506 forks [active - 1] = forks [--forkcnt]; 2665 forks [active - 1] = forks [--forkcnt];
2507 ((W)forks [active - 1])->active = active; 2666 ev_active (forks [active - 1]) = active;
2508 } 2667 }
2509 2668
2510 ev_stop (EV_A_ (W)w); 2669 ev_stop (EV_A_ (W)w);
2511} 2670}
2512#endif 2671#endif
2531 clear_pending (EV_A_ (W)w); 2690 clear_pending (EV_A_ (W)w);
2532 if (expect_false (!ev_is_active (w))) 2691 if (expect_false (!ev_is_active (w)))
2533 return; 2692 return;
2534 2693
2535 { 2694 {
2536 int active = ((W)w)->active; 2695 int active = ev_active (w);
2696
2537 asyncs [active - 1] = asyncs [--asynccnt]; 2697 asyncs [active - 1] = asyncs [--asynccnt];
2538 ((W)asyncs [active - 1])->active = active; 2698 ev_active (asyncs [active - 1]) = active;
2539 } 2699 }
2540 2700
2541 ev_stop (EV_A_ (W)w); 2701 ev_stop (EV_A_ (W)w);
2542} 2702}
2543 2703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines