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.234 by root, Tue May 6 23:42:16 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 ();
495 } 520 }
496} 521}
497 522
498/*****************************************************************************/ 523/*****************************************************************************/
499 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
500int inline_size 527int inline_size
501array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
502{ 529{
503 int ncur = cur + 1; 530 int ncur = cur + 1;
504 531
505 do 532 do
506 ncur <<= 1; 533 ncur <<= 1;
507 while (cnt > ncur); 534 while (cnt > ncur);
508 535
509 /* 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 */
510 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
511 { 538 {
512 ncur *= elem; 539 ncur *= elem;
513 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
514 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
515 ncur /= elem; 542 ncur /= elem;
516 } 543 }
517 544
518 return ncur; 545 return ncur;
732 } 759 }
733} 760}
734 761
735/*****************************************************************************/ 762/*****************************************************************************/
736 763
764/* towards the root */
737void inline_speed 765void inline_speed
738upheap (WT *heap, int k) 766upheap (WT *heap, int k)
739{ 767{
740 WT w = heap [k]; 768 WT w = heap [k];
741 769
742 while (k) 770 for (;;)
743 { 771 {
744 int p = (k - 1) >> 1; 772 int p = k >> 1;
745 773
774 /* maybe we could use a dummy element at heap [0]? */
746 if (heap [p]->at <= w->at) 775 if (!p || heap [p]->at <= w->at)
747 break; 776 break;
748 777
749 heap [k] = heap [p]; 778 heap [k] = heap [p];
750 ((W)heap [k])->active = k + 1; 779 ev_active (heap [k]) = k;
751 k = p; 780 k = p;
752 } 781 }
753 782
754 heap [k] = w; 783 heap [k] = w;
755 ((W)heap [k])->active = k + 1; 784 ev_active (heap [k]) = k;
756} 785}
757 786
787/* away from the root */
758void inline_speed 788void inline_speed
759downheap (WT *heap, int N, int k) 789downheap (WT *heap, int N, int k)
760{ 790{
761 WT w = heap [k]; 791 WT w = heap [k];
762 792
763 for (;;) 793 for (;;)
764 { 794 {
765 int c = (k << 1) + 1; 795 int c = k << 1;
766 796
767 if (c >= N) 797 if (c > N)
768 break; 798 break;
769 799
770 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 800 c += c < N && heap [c]->at > heap [c + 1]->at
771 ? 1 : 0; 801 ? 1 : 0;
772 802
773 if (w->at <= heap [c]->at) 803 if (w->at <= heap [c]->at)
774 break; 804 break;
775 805
776 heap [k] = heap [c]; 806 heap [k] = heap [c];
777 ((W)heap [k])->active = k + 1; 807 ev_active (heap [k]) = k;
778 808
779 k = c; 809 k = c;
780 } 810 }
781 811
782 heap [k] = w; 812 heap [k] = w;
783 ((W)heap [k])->active = k + 1; 813 ev_active (heap [k]) = k;
784} 814}
785 815
786void inline_size 816void inline_size
787adjustheap (WT *heap, int N, int k) 817adjustheap (WT *heap, int N, int k)
788{ 818{
884pipecb (EV_P_ ev_io *iow, int revents) 914pipecb (EV_P_ ev_io *iow, int revents)
885{ 915{
886#if EV_USE_EVENTFD 916#if EV_USE_EVENTFD
887 if (evfd >= 0) 917 if (evfd >= 0)
888 { 918 {
889 uint64_t counter = 1; 919 uint64_t counter;
890 read (evfd, &counter, sizeof (uint64_t)); 920 read (evfd, &counter, sizeof (uint64_t));
891 } 921 }
892 else 922 else
893#endif 923#endif
894 { 924 {
1163 if (!(flags & EVFLAG_NOENV) 1193 if (!(flags & EVFLAG_NOENV)
1164 && !enable_secure () 1194 && !enable_secure ()
1165 && getenv ("LIBEV_FLAGS")) 1195 && getenv ("LIBEV_FLAGS"))
1166 flags = atoi (getenv ("LIBEV_FLAGS")); 1196 flags = atoi (getenv ("LIBEV_FLAGS"));
1167 1197
1168 if (!(flags & 0x0000ffffUL)) 1198 if (!(flags & 0x0000ffffU))
1169 flags |= ev_recommended_backends (); 1199 flags |= ev_recommended_backends ();
1170 1200
1171#if EV_USE_PORT 1201#if EV_USE_PORT
1172 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1202 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1173#endif 1203#endif
1261#endif 1291#endif
1262 1292
1263 backend = 0; 1293 backend = 0;
1264} 1294}
1265 1295
1296#if EV_USE_INOTIFY
1266void inline_size infy_fork (EV_P); 1297void inline_size infy_fork (EV_P);
1298#endif
1267 1299
1268void inline_size 1300void inline_size
1269loop_fork (EV_P) 1301loop_fork (EV_P)
1270{ 1302{
1271#if EV_USE_PORT 1303#if EV_USE_PORT
1338void 1370void
1339ev_loop_fork (EV_P) 1371ev_loop_fork (EV_P)
1340{ 1372{
1341 postfork = 1; /* must be in line with ev_default_fork */ 1373 postfork = 1; /* must be in line with ev_default_fork */
1342} 1374}
1343
1344#endif 1375#endif
1345 1376
1346#if EV_MULTIPLICITY 1377#if EV_MULTIPLICITY
1347struct ev_loop * 1378struct ev_loop *
1348ev_default_loop_init (unsigned int flags) 1379ev_default_loop_init (unsigned int flags)
1429 EV_CB_INVOKE (p->w, p->events); 1460 EV_CB_INVOKE (p->w, p->events);
1430 } 1461 }
1431 } 1462 }
1432} 1463}
1433 1464
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 1465#if EV_IDLE_ENABLE
1515void inline_size 1466void inline_size
1516idle_reify (EV_P) 1467idle_reify (EV_P)
1517{ 1468{
1518 if (expect_false (idleall)) 1469 if (expect_false (idleall))
1529 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1480 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1530 break; 1481 break;
1531 } 1482 }
1532 } 1483 }
1533 } 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);
1534} 1565}
1535#endif 1566#endif
1536 1567
1537void inline_speed 1568void inline_speed
1538time_update (EV_P_ ev_tstamp max_block) 1569time_update (EV_P_ ev_tstamp max_block)
1567 */ 1598 */
1568 for (i = 4; --i; ) 1599 for (i = 4; --i; )
1569 { 1600 {
1570 rtmn_diff = ev_rt_now - mn_now; 1601 rtmn_diff = ev_rt_now - mn_now;
1571 1602
1572 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1603 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1573 return; /* all is well */ 1604 return; /* all is well */
1574 1605
1575 ev_rt_now = ev_time (); 1606 ev_rt_now = ev_time ();
1576 mn_now = get_clock (); 1607 mn_now = get_clock ();
1577 now_floor = mn_now; 1608 now_floor = mn_now;
1592 { 1623 {
1593#if EV_PERIODIC_ENABLE 1624#if EV_PERIODIC_ENABLE
1594 periodics_reschedule (EV_A); 1625 periodics_reschedule (EV_A);
1595#endif 1626#endif
1596 /* 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 */
1597 for (i = 0; i < timercnt; ++i) 1628 for (i = 1; i <= timercnt; ++i)
1598 ((WT)timers [i])->at += ev_rt_now - mn_now; 1629 ev_at (timers [i]) += ev_rt_now - mn_now;
1599 } 1630 }
1600 1631
1601 mn_now = ev_rt_now; 1632 mn_now = ev_rt_now;
1602 } 1633 }
1603} 1634}
1673 1704
1674 waittime = MAX_BLOCKTIME; 1705 waittime = MAX_BLOCKTIME;
1675 1706
1676 if (timercnt) 1707 if (timercnt)
1677 { 1708 {
1678 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1709 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1679 if (waittime > to) waittime = to; 1710 if (waittime > to) waittime = to;
1680 } 1711 }
1681 1712
1682#if EV_PERIODIC_ENABLE 1713#if EV_PERIODIC_ENABLE
1683 if (periodiccnt) 1714 if (periodiccnt)
1684 { 1715 {
1685 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;
1686 if (waittime > to) waittime = to; 1717 if (waittime > to) waittime = to;
1687 } 1718 }
1688#endif 1719#endif
1689 1720
1690 if (expect_false (waittime < timeout_blocktime)) 1721 if (expect_false (waittime < timeout_blocktime))
1856ev_timer_start (EV_P_ ev_timer *w) 1887ev_timer_start (EV_P_ ev_timer *w)
1857{ 1888{
1858 if (expect_false (ev_is_active (w))) 1889 if (expect_false (ev_is_active (w)))
1859 return; 1890 return;
1860 1891
1861 ((WT)w)->at += mn_now; 1892 ev_at (w) += mn_now;
1862 1893
1863 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.));
1864 1895
1865 ev_start (EV_A_ (W)w, ++timercnt); 1896 ev_start (EV_A_ (W)w, ++timercnt);
1866 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1867 timers [timercnt - 1] = (WT)w; 1898 timers [timercnt] = (WT)w;
1868 upheap (timers, timercnt - 1); 1899 upheap (timers, timercnt);
1869 1900
1870 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1871} 1902}
1872 1903
1873void noinline 1904void noinline
1874ev_timer_stop (EV_P_ ev_timer *w) 1905ev_timer_stop (EV_P_ ev_timer *w)
1875{ 1906{
1876 clear_pending (EV_A_ (W)w); 1907 clear_pending (EV_A_ (W)w);
1877 if (expect_false (!ev_is_active (w))) 1908 if (expect_false (!ev_is_active (w)))
1878 return; 1909 return;
1879 1910
1880 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1881
1882 { 1911 {
1883 int active = ((W)w)->active; 1912 int active = ev_active (w);
1884 1913
1914 assert (("internal timer heap corruption", timers [active] == (WT)w));
1915
1885 if (expect_true (--active < --timercnt)) 1916 if (expect_true (active < timercnt))
1886 { 1917 {
1887 timers [active] = timers [timercnt]; 1918 timers [active] = timers [timercnt];
1888 adjustheap (timers, timercnt, active); 1919 adjustheap (timers, timercnt, active);
1889 } 1920 }
1921
1922 --timercnt;
1890 } 1923 }
1891 1924
1892 ((WT)w)->at -= mn_now; 1925 ev_at (w) -= mn_now;
1893 1926
1894 ev_stop (EV_A_ (W)w); 1927 ev_stop (EV_A_ (W)w);
1895} 1928}
1896 1929
1897void noinline 1930void noinline
1899{ 1932{
1900 if (ev_is_active (w)) 1933 if (ev_is_active (w))
1901 { 1934 {
1902 if (w->repeat) 1935 if (w->repeat)
1903 { 1936 {
1904 ((WT)w)->at = mn_now + w->repeat; 1937 ev_at (w) = mn_now + w->repeat;
1905 adjustheap (timers, timercnt, ((W)w)->active - 1); 1938 adjustheap (timers, timercnt, ev_active (w));
1906 } 1939 }
1907 else 1940 else
1908 ev_timer_stop (EV_A_ w); 1941 ev_timer_stop (EV_A_ w);
1909 } 1942 }
1910 else if (w->repeat) 1943 else if (w->repeat)
1911 { 1944 {
1912 w->at = w->repeat; 1945 ev_at (w) = w->repeat;
1913 ev_timer_start (EV_A_ w); 1946 ev_timer_start (EV_A_ w);
1914 } 1947 }
1915} 1948}
1916 1949
1917#if EV_PERIODIC_ENABLE 1950#if EV_PERIODIC_ENABLE
1920{ 1953{
1921 if (expect_false (ev_is_active (w))) 1954 if (expect_false (ev_is_active (w)))
1922 return; 1955 return;
1923 1956
1924 if (w->reschedule_cb) 1957 if (w->reschedule_cb)
1925 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1958 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1926 else if (w->interval) 1959 else if (w->interval)
1927 { 1960 {
1928 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.));
1929 /* 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 */
1930 ((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;
1931 } 1964 }
1932 else 1965 else
1933 ((WT)w)->at = w->offset; 1966 ev_at (w) = w->offset;
1934 1967
1935 ev_start (EV_A_ (W)w, ++periodiccnt); 1968 ev_start (EV_A_ (W)w, ++periodiccnt);
1936 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1937 periodics [periodiccnt - 1] = (WT)w; 1970 periodics [periodiccnt] = (WT)w;
1938 upheap (periodics, periodiccnt - 1); 1971 upheap (periodics, periodiccnt);
1939 1972
1940 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1941} 1974}
1942 1975
1943void noinline 1976void noinline
1944ev_periodic_stop (EV_P_ ev_periodic *w) 1977ev_periodic_stop (EV_P_ ev_periodic *w)
1945{ 1978{
1946 clear_pending (EV_A_ (W)w); 1979 clear_pending (EV_A_ (W)w);
1947 if (expect_false (!ev_is_active (w))) 1980 if (expect_false (!ev_is_active (w)))
1948 return; 1981 return;
1949 1982
1950 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1951
1952 { 1983 {
1953 int active = ((W)w)->active; 1984 int active = ev_active (w);
1954 1985
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1987
1955 if (expect_true (--active < --periodiccnt)) 1988 if (expect_true (active < periodiccnt))
1956 { 1989 {
1957 periodics [active] = periodics [periodiccnt]; 1990 periodics [active] = periodics [periodiccnt];
1958 adjustheap (periodics, periodiccnt, active); 1991 adjustheap (periodics, periodiccnt, active);
1959 } 1992 }
1993
1994 --periodiccnt;
1960 } 1995 }
1961 1996
1962 ev_stop (EV_A_ (W)w); 1997 ev_stop (EV_A_ (W)w);
1963} 1998}
1964 1999
2080 if (w->wd < 0) 2115 if (w->wd < 0)
2081 { 2116 {
2082 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 */
2083 2118
2084 /* 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 */
2085 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2122 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2086 { 2123 {
2087 char path [4096]; 2124 char path [4096];
2088 strcpy (path, w->path); 2125 strcpy (path, w->path);
2089 2126
2334 clear_pending (EV_A_ (W)w); 2371 clear_pending (EV_A_ (W)w);
2335 if (expect_false (!ev_is_active (w))) 2372 if (expect_false (!ev_is_active (w)))
2336 return; 2373 return;
2337 2374
2338 { 2375 {
2339 int active = ((W)w)->active; 2376 int active = ev_active (w);
2340 2377
2341 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2378 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2342 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2379 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2343 2380
2344 ev_stop (EV_A_ (W)w); 2381 ev_stop (EV_A_ (W)w);
2345 --idleall; 2382 --idleall;
2346 } 2383 }
2347} 2384}
2364 clear_pending (EV_A_ (W)w); 2401 clear_pending (EV_A_ (W)w);
2365 if (expect_false (!ev_is_active (w))) 2402 if (expect_false (!ev_is_active (w)))
2366 return; 2403 return;
2367 2404
2368 { 2405 {
2369 int active = ((W)w)->active; 2406 int active = ev_active (w);
2407
2370 prepares [active - 1] = prepares [--preparecnt]; 2408 prepares [active - 1] = prepares [--preparecnt];
2371 ((W)prepares [active - 1])->active = active; 2409 ev_active (prepares [active - 1]) = active;
2372 } 2410 }
2373 2411
2374 ev_stop (EV_A_ (W)w); 2412 ev_stop (EV_A_ (W)w);
2375} 2413}
2376 2414
2391 clear_pending (EV_A_ (W)w); 2429 clear_pending (EV_A_ (W)w);
2392 if (expect_false (!ev_is_active (w))) 2430 if (expect_false (!ev_is_active (w)))
2393 return; 2431 return;
2394 2432
2395 { 2433 {
2396 int active = ((W)w)->active; 2434 int active = ev_active (w);
2435
2397 checks [active - 1] = checks [--checkcnt]; 2436 checks [active - 1] = checks [--checkcnt];
2398 ((W)checks [active - 1])->active = active; 2437 ev_active (checks [active - 1]) = active;
2399 } 2438 }
2400 2439
2401 ev_stop (EV_A_ (W)w); 2440 ev_stop (EV_A_ (W)w);
2402} 2441}
2403 2442
2499 clear_pending (EV_A_ (W)w); 2538 clear_pending (EV_A_ (W)w);
2500 if (expect_false (!ev_is_active (w))) 2539 if (expect_false (!ev_is_active (w)))
2501 return; 2540 return;
2502 2541
2503 { 2542 {
2504 int active = ((W)w)->active; 2543 int active = ev_active (w);
2544
2505 forks [active - 1] = forks [--forkcnt]; 2545 forks [active - 1] = forks [--forkcnt];
2506 ((W)forks [active - 1])->active = active; 2546 ev_active (forks [active - 1]) = active;
2507 } 2547 }
2508 2548
2509 ev_stop (EV_A_ (W)w); 2549 ev_stop (EV_A_ (W)w);
2510} 2550}
2511#endif 2551#endif
2530 clear_pending (EV_A_ (W)w); 2570 clear_pending (EV_A_ (W)w);
2531 if (expect_false (!ev_is_active (w))) 2571 if (expect_false (!ev_is_active (w)))
2532 return; 2572 return;
2533 2573
2534 { 2574 {
2535 int active = ((W)w)->active; 2575 int active = ev_active (w);
2576
2536 asyncs [active - 1] = asyncs [--asynccnt]; 2577 asyncs [active - 1] = asyncs [--asynccnt];
2537 ((W)asyncs [active - 1])->active = active; 2578 ev_active (asyncs [active - 1]) = active;
2538 } 2579 }
2539 2580
2540 ev_stop (EV_A_ (W)w); 2581 ev_stop (EV_A_ (W)w);
2541} 2582}
2542 2583

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines