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

Comparing libev/ev.c (file contents):
Revision 1.249 by root, Wed May 21 23:30:52 2008 UTC vs.
Revision 1.251 by root, Thu May 22 03:42:34 2008 UTC

126# define EV_USE_EVENTFD 1 126# define EV_USE_EVENTFD 1
127# else 127# else
128# define EV_USE_EVENTFD 0 128# define EV_USE_EVENTFD 0
129# endif 129# endif
130# endif 130# endif
131 131
132#endif 132#endif
133 133
134#include <math.h> 134#include <math.h>
135#include <stdlib.h> 135#include <stdlib.h>
136#include <fcntl.h> 136#include <fcntl.h>
236# define EV_USE_EVENTFD 0 236# define EV_USE_EVENTFD 0
237# endif 237# endif
238#endif 238#endif
239 239
240#if 0 /* debugging */ 240#if 0 /* debugging */
241# define EV_VERIFY 1 241# define EV_VERIFY 3
242# define EV_USE_4HEAP 1 242# define EV_USE_4HEAP 1
243# define EV_HEAP_CACHE_AT 1 243# define EV_HEAP_CACHE_AT 1
244#endif
245
246#ifndef EV_VERIFY
247# define EV_VERIFY !EV_MINIMAL
244#endif 248#endif
245 249
246#ifndef EV_USE_4HEAP 250#ifndef EV_USE_4HEAP
247# define EV_USE_4HEAP !EV_MINIMAL 251# define EV_USE_4HEAP !EV_MINIMAL
248#endif 252#endif
294# endif 298# endif
295#endif 299#endif
296 300
297/**/ 301/**/
298 302
299/* EV_VERIFY: enable internal consistency checks
300 * undefined or zero: no verification done or available
301 * 1 or higher: ev_loop_verify function available
302 * 2 or higher: ev_loop_verify is called frequently
303 */
304#if EV_VERIFY >= 1 303#if EV_VERIFY >= 3
305# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 304# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
306#else 305#else
307# define EV_FREQUENT_CHECK do { } while (0) 306# define EV_FREQUENT_CHECK do { } while (0)
308#endif 307#endif
309 308
938/* rebuild the heap: this function is used only once and executed rarely */ 937/* rebuild the heap: this function is used only once and executed rarely */
939void inline_size 938void inline_size
940reheap (ANHE *heap, int N) 939reheap (ANHE *heap, int N)
941{ 940{
942 int i; 941 int i;
942
943 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 943 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
944 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ 944 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
945 for (i = 0; i < N; ++i) 945 for (i = 0; i < N; ++i)
946 upheap (heap, i + HEAP0); 946 upheap (heap, i + HEAP0);
947} 947}
948
949#if EV_VERIFY
950static void
951checkheap (ANHE *heap, int N)
952{
953 int i;
954
955 for (i = HEAP0; i < N + HEAP0; ++i)
956 {
957 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
958 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
959 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
960 }
961}
962#endif
963 948
964/*****************************************************************************/ 949/*****************************************************************************/
965 950
966typedef struct 951typedef struct
967{ 952{
1484 1469
1485 postfork = 0; 1470 postfork = 0;
1486} 1471}
1487 1472
1488#if EV_MULTIPLICITY 1473#if EV_MULTIPLICITY
1474
1489struct ev_loop * 1475struct ev_loop *
1490ev_loop_new (unsigned int flags) 1476ev_loop_new (unsigned int flags)
1491{ 1477{
1492 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1478 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1493 1479
1513{ 1499{
1514 postfork = 1; /* must be in line with ev_default_fork */ 1500 postfork = 1; /* must be in line with ev_default_fork */
1515} 1501}
1516 1502
1517#if EV_VERIFY 1503#if EV_VERIFY
1518static void 1504void noinline
1505verify_watcher (EV_P_ W w)
1506{
1507 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1508
1509 if (w->pending)
1510 assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1511}
1512
1513static void noinline
1514verify_heap (EV_P_ ANHE *heap, int N)
1515{
1516 int i;
1517
1518 for (i = HEAP0; i < N + HEAP0; ++i)
1519 {
1520 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
1521 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
1522 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
1523
1524 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1525 }
1526}
1527
1528static void noinline
1519array_check (W **ws, int cnt) 1529array_verify (EV_P_ W *ws, int cnt)
1520{ 1530{
1521 while (cnt--) 1531 while (cnt--)
1532 {
1522 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 1533 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1534 verify_watcher (EV_A_ ws [cnt]);
1535 }
1523} 1536}
1537#endif
1524 1538
1525static void 1539void
1526ev_loop_verify (EV_P) 1540ev_loop_verify (EV_P)
1527{ 1541{
1542#if EV_VERIFY
1528 int i; 1543 int i;
1544 WL w;
1529 1545
1546 assert (activecnt >= -1);
1547
1548 assert (fdchangemax >= fdchangecnt);
1549 for (i = 0; i < fdchangecnt; ++i)
1550 assert (("negative fd in fdchanges", fdchanges [i] >= 0));
1551
1552 assert (anfdmax >= 0);
1553 for (i = 0; i < anfdmax; ++i)
1554 for (w = anfds [i].head; w; w = w->next)
1555 {
1556 verify_watcher (EV_A_ (W)w);
1557 assert (("inactive fd watcher on anfd list", ev_active (w) == 1));
1558 assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
1559 }
1560
1561 assert (timermax >= timercnt);
1530 checkheap (timers, timercnt); 1562 verify_heap (EV_A_ timers, timercnt);
1563
1531#if EV_PERIODIC_ENABLE 1564#if EV_PERIODIC_ENABLE
1565 assert (periodicmax >= periodiccnt);
1532 checkheap (periodics, periodiccnt); 1566 verify_heap (EV_A_ periodics, periodiccnt);
1533#endif 1567#endif
1534 1568
1569 for (i = NUMPRI; i--; )
1570 {
1571 assert (pendingmax [i] >= pendingcnt [i]);
1535#if EV_IDLE_ENABLE 1572#if EV_IDLE_ENABLE
1536 for (i = NUMPRI; i--; ) 1573 assert (idlemax [i] >= idlecnt [i]);
1537 array_check ((W **)idles [i], idlecnt [i]); 1574 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1538#endif 1575#endif
1576 }
1577
1539#if EV_FORK_ENABLE 1578#if EV_FORK_ENABLE
1579 assert (forkmax >= forkcnt);
1540 array_check ((W **)forks, forkcnt); 1580 array_verify (EV_A_ (W *)forks, forkcnt);
1541#endif 1581#endif
1542 array_check ((W **)prepares, preparecnt); 1582
1543 array_check ((W **)checks, checkcnt);
1544#if EV_ASYNC_ENABLE 1583#if EV_ASYNC_ENABLE
1584 assert (asyncmax >= asynccnt);
1545 array_check ((W **)asyncs, asynccnt); 1585 array_verify (EV_A_ (W *)asyncs, asynccnt);
1586#endif
1587
1588 assert (preparemax >= preparecnt);
1589 array_verify (EV_A_ (W *)prepares, preparecnt);
1590
1591 assert (checkmax >= checkcnt);
1592 array_verify (EV_A_ (W *)checks, checkcnt);
1593
1594# if 0
1595 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1596 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1546#endif 1597# endif
1547}
1548#endif 1598#endif
1599}
1549 1600
1550#endif 1601#endif /* multiplicity */
1551 1602
1552#if EV_MULTIPLICITY 1603#if EV_MULTIPLICITY
1553struct ev_loop * 1604struct ev_loop *
1554ev_default_loop_init (unsigned int flags) 1605ev_default_loop_init (unsigned int flags)
1555#else 1606#else
1620void inline_speed 1671void inline_speed
1621call_pending (EV_P) 1672call_pending (EV_P)
1622{ 1673{
1623 int pri; 1674 int pri;
1624 1675
1625 EV_FREQUENT_CHECK;
1626
1627 for (pri = NUMPRI; pri--; ) 1676 for (pri = NUMPRI; pri--; )
1628 while (pendingcnt [pri]) 1677 while (pendingcnt [pri])
1629 { 1678 {
1630 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1679 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1631 1680
1633 { 1682 {
1634 /*assert (("non-pending watcher on pending list", p->w->pending));*/ 1683 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1635 1684
1636 p->w->pending = 0; 1685 p->w->pending = 0;
1637 EV_CB_INVOKE (p->w, p->events); 1686 EV_CB_INVOKE (p->w, p->events);
1687 EV_FREQUENT_CHECK;
1638 } 1688 }
1639 } 1689 }
1640
1641 EV_FREQUENT_CHECK;
1642} 1690}
1643 1691
1644#if EV_IDLE_ENABLE 1692#if EV_IDLE_ENABLE
1645void inline_size 1693void inline_size
1646idle_reify (EV_P) 1694idle_reify (EV_P)
1698#if EV_PERIODIC_ENABLE 1746#if EV_PERIODIC_ENABLE
1699void inline_size 1747void inline_size
1700periodics_reify (EV_P) 1748periodics_reify (EV_P)
1701{ 1749{
1702 EV_FREQUENT_CHECK; 1750 EV_FREQUENT_CHECK;
1751
1703 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1752 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1704 { 1753 {
1705 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1754 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1706 1755
1707 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1756 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1713 1762
1714 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1763 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1715 1764
1716 ANHE_at_cache (periodics [HEAP0]); 1765 ANHE_at_cache (periodics [HEAP0]);
1717 downheap (periodics, periodiccnt, HEAP0); 1766 downheap (periodics, periodiccnt, HEAP0);
1718 EV_FREQUENT_CHECK;
1719 } 1767 }
1720 else if (w->interval) 1768 else if (w->interval)
1721 { 1769 {
1722 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1770 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1723 /* if next trigger time is not sufficiently in the future, put it there */ 1771 /* if next trigger time is not sufficiently in the future, put it there */
1859 1907
1860 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1908 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1861 1909
1862 do 1910 do
1863 { 1911 {
1912#if EV_VERIFY >= 2
1913 ev_loop_verify (EV_A);
1914#endif
1915
1864#ifndef _WIN32 1916#ifndef _WIN32
1865 if (expect_false (curpid)) /* penalise the forking check even more */ 1917 if (expect_false (curpid)) /* penalise the forking check even more */
1866 if (expect_false (getpid () != curpid)) 1918 if (expect_false (getpid () != curpid))
1867 { 1919 {
1868 curpid = getpid (); 1920 curpid = getpid ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines