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

Comparing libev/ev.c (file contents):
Revision 1.222 by root, Sun Apr 6 12:45:58 2008 UTC vs.
Revision 1.234 by root, Tue May 6 23:42:16 2008 UTC

300# define expect(expr,value) __builtin_expect ((expr),(value)) 300# define expect(expr,value) __builtin_expect ((expr),(value))
301# define noinline __attribute__ ((noinline)) 301# define noinline __attribute__ ((noinline))
302#else 302#else
303# define expect(expr,value) (expr) 303# define expect(expr,value) (expr)
304# define noinline 304# define noinline
305# if __STDC_VERSION__ < 199901L 305# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
306# define inline 306# define inline
307# endif 307# endif
308#endif 308#endif
309 309
310#define expect_false(expr) expect ((expr) != 0, 0) 310#define expect_false(expr) expect ((expr) != 0, 0)
325 325
326typedef ev_watcher *W; 326typedef ev_watcher *W;
327typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
328typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
329 329
330#define ev_active(w) ((W)(w))->active
331#define ev_at(w) ((WT)(w))->at
332
330#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
331/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 334/* sig_atomic_t is used to avoid per-thread variables or locking but still */
332/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
333static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
334#endif 337#endif
360 perror (msg); 363 perror (msg);
361 abort (); 364 abort ();
362 } 365 }
363} 366}
364 367
368static void *
369ev_realloc_emul (void *ptr, long size)
370{
371 /* some systems, notably openbsd and darwin, fail to properly
372 * implement realloc (x, 0) (as required by both ansi c-98 and
373 * the single unix specification, so work around them here.
374 */
375
376 if (size)
377 return realloc (ptr, size);
378
379 free (ptr);
380 return 0;
381}
382
365static void *(*alloc)(void *ptr, long size); 383static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
366 384
367void 385void
368ev_set_allocator (void *(*cb)(void *ptr, long size)) 386ev_set_allocator (void *(*cb)(void *ptr, long size))
369{ 387{
370 alloc = cb; 388 alloc = cb;
371} 389}
372 390
373inline_speed void * 391inline_speed void *
374ev_realloc (void *ptr, long size) 392ev_realloc (void *ptr, long size)
375{ 393{
376 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 394 ptr = alloc (ptr, size);
377 395
378 if (!ptr && size) 396 if (!ptr && size)
379 { 397 {
380 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 398 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
381 abort (); 399 abort ();
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/* towards the root */
744void inline_speed 765void inline_speed
745upheap (WT *heap, int k) 766upheap (WT *heap, int k)
746{ 767{
747 WT w = heap [k]; 768 WT w = heap [k];
748 769
749 while (k) 770 for (;;)
750 { 771 {
751 int p = (k - 1) >> 1; 772 int p = k >> 1;
752 773
774 /* maybe we could use a dummy element at heap [0]? */
753 if (heap [p]->at <= w->at) 775 if (!p || heap [p]->at <= w->at)
754 break; 776 break;
755 777
756 heap [k] = heap [p]; 778 heap [k] = heap [p];
757 ((W)heap [k])->active = k + 1; 779 ev_active (heap [k]) = k;
758 k = p; 780 k = p;
759 } 781 }
760 782
761 heap [k] = w; 783 heap [k] = w;
762 ((W)heap [k])->active = k + 1; 784 ev_active (heap [k]) = k;
763} 785}
764 786
787/* away from the root */
765void inline_speed 788void inline_speed
766downheap (WT *heap, int N, int k) 789downheap (WT *heap, int N, int k)
767{ 790{
768 WT w = heap [k]; 791 WT w = heap [k];
769 792
770 for (;;) 793 for (;;)
771 { 794 {
772 int c = (k << 1) + 1; 795 int c = k << 1;
773 796
774 if (c >= N) 797 if (c > N)
775 break; 798 break;
776 799
777 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 800 c += c < N && heap [c]->at > heap [c + 1]->at
778 ? 1 : 0; 801 ? 1 : 0;
779 802
780 if (w->at <= heap [c]->at) 803 if (w->at <= heap [c]->at)
781 break; 804 break;
782 805
783 heap [k] = heap [c]; 806 heap [k] = heap [c];
784 ((W)heap [k])->active = k + 1; 807 ev_active (heap [k]) = k;
785 808
786 k = c; 809 k = c;
787 } 810 }
788 811
789 heap [k] = w; 812 heap [k] = w;
790 ((W)heap [k])->active = k + 1; 813 ev_active (heap [k]) = k;
791} 814}
792 815
793void inline_size 816void inline_size
794adjustheap (WT *heap, int N, int k) 817adjustheap (WT *heap, int N, int k)
795{ 818{
891pipecb (EV_P_ ev_io *iow, int revents) 914pipecb (EV_P_ ev_io *iow, int revents)
892{ 915{
893#if EV_USE_EVENTFD 916#if EV_USE_EVENTFD
894 if (evfd >= 0) 917 if (evfd >= 0)
895 { 918 {
896 uint64_t counter = 1; 919 uint64_t counter;
897 read (evfd, &counter, sizeof (uint64_t)); 920 read (evfd, &counter, sizeof (uint64_t));
898 } 921 }
899 else 922 else
900#endif 923#endif
901 { 924 {
1170 if (!(flags & EVFLAG_NOENV) 1193 if (!(flags & EVFLAG_NOENV)
1171 && !enable_secure () 1194 && !enable_secure ()
1172 && getenv ("LIBEV_FLAGS")) 1195 && getenv ("LIBEV_FLAGS"))
1173 flags = atoi (getenv ("LIBEV_FLAGS")); 1196 flags = atoi (getenv ("LIBEV_FLAGS"));
1174 1197
1175 if (!(flags & 0x0000ffffUL)) 1198 if (!(flags & 0x0000ffffU))
1176 flags |= ev_recommended_backends (); 1199 flags |= ev_recommended_backends ();
1177 1200
1178#if EV_USE_PORT 1201#if EV_USE_PORT
1179 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1202 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1180#endif 1203#endif
1268#endif 1291#endif
1269 1292
1270 backend = 0; 1293 backend = 0;
1271} 1294}
1272 1295
1296#if EV_USE_INOTIFY
1273void inline_size infy_fork (EV_P); 1297void inline_size infy_fork (EV_P);
1298#endif
1274 1299
1275void inline_size 1300void inline_size
1276loop_fork (EV_P) 1301loop_fork (EV_P)
1277{ 1302{
1278#if EV_USE_PORT 1303#if EV_USE_PORT
1345void 1370void
1346ev_loop_fork (EV_P) 1371ev_loop_fork (EV_P)
1347{ 1372{
1348 postfork = 1; /* must be in line with ev_default_fork */ 1373 postfork = 1; /* must be in line with ev_default_fork */
1349} 1374}
1350
1351#endif 1375#endif
1352 1376
1353#if EV_MULTIPLICITY 1377#if EV_MULTIPLICITY
1354struct ev_loop * 1378struct ev_loop *
1355ev_default_loop_init (unsigned int flags) 1379ev_default_loop_init (unsigned int flags)
1436 EV_CB_INVOKE (p->w, p->events); 1460 EV_CB_INVOKE (p->w, p->events);
1437 } 1461 }
1438 } 1462 }
1439} 1463}
1440 1464
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 1465#if EV_IDLE_ENABLE
1522void inline_size 1466void inline_size
1523idle_reify (EV_P) 1467idle_reify (EV_P)
1524{ 1468{
1525 if (expect_false (idleall)) 1469 if (expect_false (idleall))
1536 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1480 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1537 break; 1481 break;
1538 } 1482 }
1539 } 1483 }
1540 } 1484 }
1485}
1486#endif
1487
1488void inline_size
1489timers_reify (EV_P)
1490{
1491 while (timercnt && ev_at (timers [1]) <= mn_now)
1492 {
1493 ev_timer *w = (ev_timer *)timers [1];
1494
1495 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1496
1497 /* first reschedule or stop timer */
1498 if (w->repeat)
1499 {
1500 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1501
1502 ev_at (w) += w->repeat;
1503 if (ev_at (w) < mn_now)
1504 ev_at (w) = mn_now;
1505
1506 downheap (timers, timercnt, 1);
1507 }
1508 else
1509 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1510
1511 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1512 }
1513}
1514
1515#if EV_PERIODIC_ENABLE
1516void inline_size
1517periodics_reify (EV_P)
1518{
1519 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1520 {
1521 ev_periodic *w = (ev_periodic *)periodics [1];
1522
1523 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1524
1525 /* first reschedule or stop timer */
1526 if (w->reschedule_cb)
1527 {
1528 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1529 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1530 downheap (periodics, periodiccnt, 1);
1531 }
1532 else if (w->interval)
1533 {
1534 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1535 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1536 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1537 downheap (periodics, periodiccnt, 1);
1538 }
1539 else
1540 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1541
1542 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1543 }
1544}
1545
1546static void noinline
1547periodics_reschedule (EV_P)
1548{
1549 int i;
1550
1551 /* adjust periodics after time jump */
1552 for (i = 1; i <= periodiccnt; ++i)
1553 {
1554 ev_periodic *w = (ev_periodic *)periodics [i];
1555
1556 if (w->reschedule_cb)
1557 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1558 else if (w->interval)
1559 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1560 }
1561
1562 /* now rebuild the heap */
1563 for (i = periodiccnt >> 1; i--; )
1564 downheap (periodics, periodiccnt, i);
1541} 1565}
1542#endif 1566#endif
1543 1567
1544void inline_speed 1568void inline_speed
1545time_update (EV_P_ ev_tstamp max_block) 1569time_update (EV_P_ ev_tstamp max_block)
1574 */ 1598 */
1575 for (i = 4; --i; ) 1599 for (i = 4; --i; )
1576 { 1600 {
1577 rtmn_diff = ev_rt_now - mn_now; 1601 rtmn_diff = ev_rt_now - mn_now;
1578 1602
1579 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1603 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1580 return; /* all is well */ 1604 return; /* all is well */
1581 1605
1582 ev_rt_now = ev_time (); 1606 ev_rt_now = ev_time ();
1583 mn_now = get_clock (); 1607 mn_now = get_clock ();
1584 now_floor = mn_now; 1608 now_floor = mn_now;
1599 { 1623 {
1600#if EV_PERIODIC_ENABLE 1624#if EV_PERIODIC_ENABLE
1601 periodics_reschedule (EV_A); 1625 periodics_reschedule (EV_A);
1602#endif 1626#endif
1603 /* adjust timers. this is easy, as the offset is the same for all of them */ 1627 /* adjust timers. this is easy, as the offset is the same for all of them */
1604 for (i = 0; i < timercnt; ++i) 1628 for (i = 1; i <= timercnt; ++i)
1605 ((WT)timers [i])->at += ev_rt_now - mn_now; 1629 ev_at (timers [i]) += ev_rt_now - mn_now;
1606 } 1630 }
1607 1631
1608 mn_now = ev_rt_now; 1632 mn_now = ev_rt_now;
1609 } 1633 }
1610} 1634}
1680 1704
1681 waittime = MAX_BLOCKTIME; 1705 waittime = MAX_BLOCKTIME;
1682 1706
1683 if (timercnt) 1707 if (timercnt)
1684 { 1708 {
1685 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1709 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1686 if (waittime > to) waittime = to; 1710 if (waittime > to) waittime = to;
1687 } 1711 }
1688 1712
1689#if EV_PERIODIC_ENABLE 1713#if EV_PERIODIC_ENABLE
1690 if (periodiccnt) 1714 if (periodiccnt)
1691 { 1715 {
1692 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1716 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge;
1693 if (waittime > to) waittime = to; 1717 if (waittime > to) waittime = to;
1694 } 1718 }
1695#endif 1719#endif
1696 1720
1697 if (expect_false (waittime < timeout_blocktime)) 1721 if (expect_false (waittime < timeout_blocktime))
1863ev_timer_start (EV_P_ ev_timer *w) 1887ev_timer_start (EV_P_ ev_timer *w)
1864{ 1888{
1865 if (expect_false (ev_is_active (w))) 1889 if (expect_false (ev_is_active (w)))
1866 return; 1890 return;
1867 1891
1868 ((WT)w)->at += mn_now; 1892 ev_at (w) += mn_now;
1869 1893
1870 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1894 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1871 1895
1872 ev_start (EV_A_ (W)w, ++timercnt); 1896 ev_start (EV_A_ (W)w, ++timercnt);
1873 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1874 timers [timercnt - 1] = (WT)w; 1898 timers [timercnt] = (WT)w;
1875 upheap (timers, timercnt - 1); 1899 upheap (timers, timercnt);
1876 1900
1877 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1878} 1902}
1879 1903
1880void noinline 1904void noinline
1881ev_timer_stop (EV_P_ ev_timer *w) 1905ev_timer_stop (EV_P_ ev_timer *w)
1882{ 1906{
1883 clear_pending (EV_A_ (W)w); 1907 clear_pending (EV_A_ (W)w);
1884 if (expect_false (!ev_is_active (w))) 1908 if (expect_false (!ev_is_active (w)))
1885 return; 1909 return;
1886 1910
1887 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1888
1889 { 1911 {
1890 int active = ((W)w)->active; 1912 int active = ev_active (w);
1891 1913
1914 assert (("internal timer heap corruption", timers [active] == (WT)w));
1915
1892 if (expect_true (--active < --timercnt)) 1916 if (expect_true (active < timercnt))
1893 { 1917 {
1894 timers [active] = timers [timercnt]; 1918 timers [active] = timers [timercnt];
1895 adjustheap (timers, timercnt, active); 1919 adjustheap (timers, timercnt, active);
1896 } 1920 }
1921
1922 --timercnt;
1897 } 1923 }
1898 1924
1899 ((WT)w)->at -= mn_now; 1925 ev_at (w) -= mn_now;
1900 1926
1901 ev_stop (EV_A_ (W)w); 1927 ev_stop (EV_A_ (W)w);
1902} 1928}
1903 1929
1904void noinline 1930void noinline
1906{ 1932{
1907 if (ev_is_active (w)) 1933 if (ev_is_active (w))
1908 { 1934 {
1909 if (w->repeat) 1935 if (w->repeat)
1910 { 1936 {
1911 ((WT)w)->at = mn_now + w->repeat; 1937 ev_at (w) = mn_now + w->repeat;
1912 adjustheap (timers, timercnt, ((W)w)->active - 1); 1938 adjustheap (timers, timercnt, ev_active (w));
1913 } 1939 }
1914 else 1940 else
1915 ev_timer_stop (EV_A_ w); 1941 ev_timer_stop (EV_A_ w);
1916 } 1942 }
1917 else if (w->repeat) 1943 else if (w->repeat)
1918 { 1944 {
1919 w->at = w->repeat; 1945 ev_at (w) = w->repeat;
1920 ev_timer_start (EV_A_ w); 1946 ev_timer_start (EV_A_ w);
1921 } 1947 }
1922} 1948}
1923 1949
1924#if EV_PERIODIC_ENABLE 1950#if EV_PERIODIC_ENABLE
1927{ 1953{
1928 if (expect_false (ev_is_active (w))) 1954 if (expect_false (ev_is_active (w)))
1929 return; 1955 return;
1930 1956
1931 if (w->reschedule_cb) 1957 if (w->reschedule_cb)
1932 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1958 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1933 else if (w->interval) 1959 else if (w->interval)
1934 { 1960 {
1935 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1961 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 */ 1962 /* 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; 1963 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1938 } 1964 }
1939 else 1965 else
1940 ((WT)w)->at = w->offset; 1966 ev_at (w) = w->offset;
1941 1967
1942 ev_start (EV_A_ (W)w, ++periodiccnt); 1968 ev_start (EV_A_ (W)w, ++periodiccnt);
1943 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1944 periodics [periodiccnt - 1] = (WT)w; 1970 periodics [periodiccnt] = (WT)w;
1945 upheap (periodics, periodiccnt - 1); 1971 upheap (periodics, periodiccnt);
1946 1972
1947 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1948} 1974}
1949 1975
1950void noinline 1976void noinline
1951ev_periodic_stop (EV_P_ ev_periodic *w) 1977ev_periodic_stop (EV_P_ ev_periodic *w)
1952{ 1978{
1953 clear_pending (EV_A_ (W)w); 1979 clear_pending (EV_A_ (W)w);
1954 if (expect_false (!ev_is_active (w))) 1980 if (expect_false (!ev_is_active (w)))
1955 return; 1981 return;
1956 1982
1957 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1958
1959 { 1983 {
1960 int active = ((W)w)->active; 1984 int active = ev_active (w);
1961 1985
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1987
1962 if (expect_true (--active < --periodiccnt)) 1988 if (expect_true (active < periodiccnt))
1963 { 1989 {
1964 periodics [active] = periodics [periodiccnt]; 1990 periodics [active] = periodics [periodiccnt];
1965 adjustheap (periodics, periodiccnt, active); 1991 adjustheap (periodics, periodiccnt, active);
1966 } 1992 }
1993
1994 --periodiccnt;
1967 } 1995 }
1968 1996
1969 ev_stop (EV_A_ (W)w); 1997 ev_stop (EV_A_ (W)w);
1970} 1998}
1971 1999
2087 if (w->wd < 0) 2115 if (w->wd < 0)
2088 { 2116 {
2089 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2117 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2090 2118
2091 /* monitor some parent directory for speedup hints */ 2119 /* monitor some parent directory for speedup hints */
2120 /* note that exceeding the hardcoded limit is not a correctness issue, */
2121 /* but an efficiency issue only */
2092 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2122 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2093 { 2123 {
2094 char path [4096]; 2124 char path [4096];
2095 strcpy (path, w->path); 2125 strcpy (path, w->path);
2096 2126
2341 clear_pending (EV_A_ (W)w); 2371 clear_pending (EV_A_ (W)w);
2342 if (expect_false (!ev_is_active (w))) 2372 if (expect_false (!ev_is_active (w)))
2343 return; 2373 return;
2344 2374
2345 { 2375 {
2346 int active = ((W)w)->active; 2376 int active = ev_active (w);
2347 2377
2348 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2378 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2349 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2379 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2350 2380
2351 ev_stop (EV_A_ (W)w); 2381 ev_stop (EV_A_ (W)w);
2352 --idleall; 2382 --idleall;
2353 } 2383 }
2354} 2384}
2371 clear_pending (EV_A_ (W)w); 2401 clear_pending (EV_A_ (W)w);
2372 if (expect_false (!ev_is_active (w))) 2402 if (expect_false (!ev_is_active (w)))
2373 return; 2403 return;
2374 2404
2375 { 2405 {
2376 int active = ((W)w)->active; 2406 int active = ev_active (w);
2407
2377 prepares [active - 1] = prepares [--preparecnt]; 2408 prepares [active - 1] = prepares [--preparecnt];
2378 ((W)prepares [active - 1])->active = active; 2409 ev_active (prepares [active - 1]) = active;
2379 } 2410 }
2380 2411
2381 ev_stop (EV_A_ (W)w); 2412 ev_stop (EV_A_ (W)w);
2382} 2413}
2383 2414
2398 clear_pending (EV_A_ (W)w); 2429 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2430 if (expect_false (!ev_is_active (w)))
2400 return; 2431 return;
2401 2432
2402 { 2433 {
2403 int active = ((W)w)->active; 2434 int active = ev_active (w);
2435
2404 checks [active - 1] = checks [--checkcnt]; 2436 checks [active - 1] = checks [--checkcnt];
2405 ((W)checks [active - 1])->active = active; 2437 ev_active (checks [active - 1]) = active;
2406 } 2438 }
2407 2439
2408 ev_stop (EV_A_ (W)w); 2440 ev_stop (EV_A_ (W)w);
2409} 2441}
2410 2442
2506 clear_pending (EV_A_ (W)w); 2538 clear_pending (EV_A_ (W)w);
2507 if (expect_false (!ev_is_active (w))) 2539 if (expect_false (!ev_is_active (w)))
2508 return; 2540 return;
2509 2541
2510 { 2542 {
2511 int active = ((W)w)->active; 2543 int active = ev_active (w);
2544
2512 forks [active - 1] = forks [--forkcnt]; 2545 forks [active - 1] = forks [--forkcnt];
2513 ((W)forks [active - 1])->active = active; 2546 ev_active (forks [active - 1]) = active;
2514 } 2547 }
2515 2548
2516 ev_stop (EV_A_ (W)w); 2549 ev_stop (EV_A_ (W)w);
2517} 2550}
2518#endif 2551#endif
2537 clear_pending (EV_A_ (W)w); 2570 clear_pending (EV_A_ (W)w);
2538 if (expect_false (!ev_is_active (w))) 2571 if (expect_false (!ev_is_active (w)))
2539 return; 2572 return;
2540 2573
2541 { 2574 {
2542 int active = ((W)w)->active; 2575 int active = ev_active (w);
2576
2543 asyncs [active - 1] = asyncs [--asynccnt]; 2577 asyncs [active - 1] = asyncs [--asynccnt];
2544 ((W)asyncs [active - 1])->active = active; 2578 ev_active (asyncs [active - 1]) = active;
2545 } 2579 }
2546 2580
2547 ev_stop (EV_A_ (W)w); 2581 ev_stop (EV_A_ (W)w);
2548} 2582}
2549 2583

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines