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

Comparing libev/ev.c (file contents):
Revision 1.374 by root, Sat Feb 26 15:21:01 2011 UTC vs.
Revision 1.379 by root, Sun Jun 19 17:55:13 2011 UTC

464#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 464#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
465 465
466#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) 466#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
467#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) 467#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
468 468
469#if __GNUC__ >= 4 469/* the following are taken from libecb */
470# define expect(expr,value) __builtin_expect ((expr),(value)) 470/* ecb.h start */
471# define noinline __attribute__ ((noinline)) 471
472/* many compilers define _GNUC_ to some versions but then only implement
473 * what their idiot authors think are the "more important" extensions,
474 * causing enourmous grief in return for some better fake benchmark numbers.
475 * or so.
476 * we try to detect these and simply assume they are not gcc - if they have
477 * an issue with that they should have done it right in the first place.
478 */
479#ifndef ECB_GCC_VERSION
480 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__)
481 #define ECB_GCC_VERSION(major,minor) 0
482 #else
483 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
484 #endif
485#endif
486
487#if __cplusplus
488 #define ecb_inline static inline
489#elif ECB_GCC_VERSION(2,5)
490 #define ecb_inline static __inline__
491#elif ECB_C99
492 #define ecb_inline static inline
472#else 493#else
473# define expect(expr,value) (expr) 494 #define ecb_inline static
474# define noinline
475# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
476# define inline
477# endif 495#endif
478#endif
479 496
497#if ECB_GCC_VERSION(3,1)
498 #define ecb_attribute(attrlist) __attribute__(attrlist)
499 #define ecb_is_constant(expr) __builtin_constant_p (expr)
500 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
501 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
502#else
503 #define ecb_attribute(attrlist)
504 #define ecb_is_constant(expr) 0
505 #define ecb_expect(expr,value) (expr)
506 #define ecb_prefetch(addr,rw,locality)
507#endif
508
509#define ecb_noinline ecb_attribute ((__noinline__))
510#define ecb_noreturn ecb_attribute ((__noreturn__))
511#define ecb_unused ecb_attribute ((__unused__))
512#define ecb_const ecb_attribute ((__const__))
513#define ecb_pure ecb_attribute ((__pure__))
514
515#if ECB_GCC_VERSION(4,3)
516 #define ecb_artificial ecb_attribute ((__artificial__))
517 #define ecb_hot ecb_attribute ((__hot__))
518 #define ecb_cold ecb_attribute ((__cold__))
519#else
520 #define ecb_artificial
521 #define ecb_hot
522 #define ecb_cold
523#endif
524
525/* put around conditional expressions if you are very sure that the */
526/* expression is mostly true or mostly false. note that these return */
527/* booleans, not the expression. */
480#define expect_false(expr) expect ((expr) != 0, 0) 528#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
481#define expect_true(expr) expect ((expr) != 0, 1) 529#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
530/* ecb.h end */
531
532#define expect_false(cond) ecb_expect_false (cond)
533#define expect_true(cond) ecb_expect_true (cond)
534#define noinline ecb_noinline
535
482#define inline_size static inline 536#define inline_size ecb_inline
483 537
484#if EV_FEATURE_CODE 538#if EV_FEATURE_CODE
485# define inline_speed static inline 539# define inline_speed ecb_inline
486#else 540#else
487# define inline_speed static noinline 541# define inline_speed static noinline
488#endif 542#endif
489 543
490#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 544#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
581 635
582#ifdef __linux 636#ifdef __linux
583# include <sys/utsname.h> 637# include <sys/utsname.h>
584#endif 638#endif
585 639
586static unsigned int noinline 640static unsigned int noinline ecb_cold
587ev_linux_version (void) 641ev_linux_version (void)
588{ 642{
589#ifdef __linux 643#ifdef __linux
590 unsigned int v = 0; 644 unsigned int v = 0;
591 struct utsname buf; 645 struct utsname buf;
620} 674}
621 675
622/*****************************************************************************/ 676/*****************************************************************************/
623 677
624#if EV_AVOID_STDIO 678#if EV_AVOID_STDIO
625static void noinline 679static void noinline ecb_cold
626ev_printerr (const char *msg) 680ev_printerr (const char *msg)
627{ 681{
628 write (STDERR_FILENO, msg, strlen (msg)); 682 write (STDERR_FILENO, msg, strlen (msg));
629} 683}
630#endif 684#endif
631 685
632static void (*syserr_cb)(const char *msg); 686static void (*syserr_cb)(const char *msg);
633 687
634void 688void ecb_cold
635ev_set_syserr_cb (void (*cb)(const char *msg)) 689ev_set_syserr_cb (void (*cb)(const char *msg))
636{ 690{
637 syserr_cb = cb; 691 syserr_cb = cb;
638} 692}
639 693
640static void noinline 694static void noinline ecb_cold
641ev_syserr (const char *msg) 695ev_syserr (const char *msg)
642{ 696{
643 if (!msg) 697 if (!msg)
644 msg = "(libev) system error"; 698 msg = "(libev) system error";
645 699
678#endif 732#endif
679} 733}
680 734
681static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 735static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
682 736
683void 737void ecb_cold
684ev_set_allocator (void *(*cb)(void *ptr, long size)) 738ev_set_allocator (void *(*cb)(void *ptr, long size))
685{ 739{
686 alloc = cb; 740 alloc = cb;
687} 741}
688 742
870 select (0, 0, 0, 0, &tv); 924 select (0, 0, 0, 0, &tv);
871#endif 925#endif
872 } 926 }
873} 927}
874 928
875inline_speed int
876ev_timeout_to_ms (ev_tstamp timeout)
877{
878 int ms = timeout * 1000. + .999999;
879
880 return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1;
881}
882
883/*****************************************************************************/ 929/*****************************************************************************/
884 930
885#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 931#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
886 932
887/* find a suitable new size for the given array, */ 933/* find a suitable new size for the given array, */
905 } 951 }
906 952
907 return ncur; 953 return ncur;
908} 954}
909 955
910static noinline void * 956static void * noinline ecb_cold
911array_realloc (int elem, void *base, int *cur, int cnt) 957array_realloc (int elem, void *base, int *cur, int cnt)
912{ 958{
913 *cur = array_nextsize (elem, *cur, cnt); 959 *cur = array_nextsize (elem, *cur, cnt);
914 return ev_realloc (base, elem * *cur); 960 return ev_realloc (base, elem * *cur);
915} 961}
1098 fdchanges [fdchangecnt - 1] = fd; 1144 fdchanges [fdchangecnt - 1] = fd;
1099 } 1145 }
1100} 1146}
1101 1147
1102/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ 1148/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
1103inline_speed void 1149inline_speed void ecb_cold
1104fd_kill (EV_P_ int fd) 1150fd_kill (EV_P_ int fd)
1105{ 1151{
1106 ev_io *w; 1152 ev_io *w;
1107 1153
1108 while ((w = (ev_io *)anfds [fd].head)) 1154 while ((w = (ev_io *)anfds [fd].head))
1111 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 1157 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
1112 } 1158 }
1113} 1159}
1114 1160
1115/* check whether the given fd is actually valid, for error recovery */ 1161/* check whether the given fd is actually valid, for error recovery */
1116inline_size int 1162inline_size int ecb_cold
1117fd_valid (int fd) 1163fd_valid (int fd)
1118{ 1164{
1119#ifdef _WIN32 1165#ifdef _WIN32
1120 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1166 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1121#else 1167#else
1122 return fcntl (fd, F_GETFD) != -1; 1168 return fcntl (fd, F_GETFD) != -1;
1123#endif 1169#endif
1124} 1170}
1125 1171
1126/* called on EBADF to verify fds */ 1172/* called on EBADF to verify fds */
1127static void noinline 1173static void noinline ecb_cold
1128fd_ebadf (EV_P) 1174fd_ebadf (EV_P)
1129{ 1175{
1130 int fd; 1176 int fd;
1131 1177
1132 for (fd = 0; fd < anfdmax; ++fd) 1178 for (fd = 0; fd < anfdmax; ++fd)
1134 if (!fd_valid (fd) && errno == EBADF) 1180 if (!fd_valid (fd) && errno == EBADF)
1135 fd_kill (EV_A_ fd); 1181 fd_kill (EV_A_ fd);
1136} 1182}
1137 1183
1138/* called on ENOMEM in select/poll to kill some fds and retry */ 1184/* called on ENOMEM in select/poll to kill some fds and retry */
1139static void noinline 1185static void noinline ecb_cold
1140fd_enomem (EV_P) 1186fd_enomem (EV_P)
1141{ 1187{
1142 int fd; 1188 int fd;
1143 1189
1144 for (fd = anfdmax; fd--; ) 1190 for (fd = anfdmax; fd--; )
1339 1385
1340/*****************************************************************************/ 1386/*****************************************************************************/
1341 1387
1342#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 1388#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1343 1389
1344static void noinline 1390static void noinline ecb_cold
1345evpipe_init (EV_P) 1391evpipe_init (EV_P)
1346{ 1392{
1347 if (!ev_is_active (&pipe_w)) 1393 if (!ev_is_active (&pipe_w))
1348 { 1394 {
1349# if EV_USE_EVENTFD 1395# if EV_USE_EVENTFD
1376inline_size void 1422inline_size void
1377evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1423evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1378{ 1424{
1379 if (!*flag) 1425 if (!*flag)
1380 { 1426 {
1381 int old_errno = errno; /* save errno because write might clobber it */
1382 char dummy;
1383
1384 *flag = 1; 1427 *flag = 1;
1385 1428
1429 pipe_write_skipped = 1;
1430
1431 if (pipe_write_wanted)
1432 {
1433 int old_errno = errno; /* save errno because write will clobber it */
1434 char dummy;
1435
1436 pipe_write_skipped = 0;
1437
1386#if EV_USE_EVENTFD 1438#if EV_USE_EVENTFD
1387 if (evfd >= 0) 1439 if (evfd >= 0)
1388 { 1440 {
1389 uint64_t counter = 1; 1441 uint64_t counter = 1;
1390 write (evfd, &counter, sizeof (uint64_t)); 1442 write (evfd, &counter, sizeof (uint64_t));
1443 }
1444 else
1445#endif
1446 {
1447 /* win32 people keep sending patches that change this write() to send() */
1448 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1449 /* so when you think this write should be a send instead, please find out */
1450 /* where your send() is from - it's definitely not the microsoft send, and */
1451 /* tell me. thank you. */
1452 write (evpipe [1], &dummy, 1);
1453 }
1454
1455 errno = old_errno;
1391 } 1456 }
1392 else
1393#endif
1394 /* win32 people keep sending patches that change this write() to send() */
1395 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1396 /* so when you think this write should be a send instead, please find out */
1397 /* where your send() is from - it's definitely not the microsoft send, and */
1398 /* tell me. thank you. */
1399 write (evpipe [1], &dummy, 1);
1400
1401 errno = old_errno;
1402 } 1457 }
1403} 1458}
1404 1459
1405/* called whenever the libev signal pipe */ 1460/* called whenever the libev signal pipe */
1406/* got some events (signal, async) */ 1461/* got some events (signal, async) */
1407static void 1462static void
1408pipecb (EV_P_ ev_io *iow, int revents) 1463pipecb (EV_P_ ev_io *iow, int revents)
1409{ 1464{
1410 int i; 1465 int i;
1411 1466
1467 if (revents & EV_READ)
1468 {
1412#if EV_USE_EVENTFD 1469#if EV_USE_EVENTFD
1413 if (evfd >= 0) 1470 if (evfd >= 0)
1414 { 1471 {
1415 uint64_t counter; 1472 uint64_t counter;
1416 read (evfd, &counter, sizeof (uint64_t)); 1473 read (evfd, &counter, sizeof (uint64_t));
1417 } 1474 }
1418 else 1475 else
1419#endif 1476#endif
1420 { 1477 {
1421 char dummy; 1478 char dummy;
1422 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1479 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1423 read (evpipe [0], &dummy, 1); 1480 read (evpipe [0], &dummy, 1);
1481 }
1424 } 1482 }
1483
1484 pipe_write_skipped = 0;
1425 1485
1426#if EV_SIGNAL_ENABLE 1486#if EV_SIGNAL_ENABLE
1427 if (sig_pending) 1487 if (sig_pending)
1428 { 1488 {
1429 sig_pending = 0; 1489 sig_pending = 0;
1458 EV_P = signals [signum - 1].loop; 1518 EV_P = signals [signum - 1].loop;
1459 1519
1460 if (!EV_A) 1520 if (!EV_A)
1461 return; 1521 return;
1462#endif 1522#endif
1523
1524 evpipe_init (EV_A);
1463 1525
1464 signals [signum - 1].pending = 1; 1526 signals [signum - 1].pending = 1;
1465 evpipe_write (EV_A_ &sig_pending); 1527 evpipe_write (EV_A_ &sig_pending);
1466} 1528}
1467 1529
1599#endif 1661#endif
1600#if EV_USE_SELECT 1662#if EV_USE_SELECT
1601# include "ev_select.c" 1663# include "ev_select.c"
1602#endif 1664#endif
1603 1665
1604int 1666int ecb_cold
1605ev_version_major (void) 1667ev_version_major (void)
1606{ 1668{
1607 return EV_VERSION_MAJOR; 1669 return EV_VERSION_MAJOR;
1608} 1670}
1609 1671
1610int 1672int ecb_cold
1611ev_version_minor (void) 1673ev_version_minor (void)
1612{ 1674{
1613 return EV_VERSION_MINOR; 1675 return EV_VERSION_MINOR;
1614} 1676}
1615 1677
1616/* return true if we are running with elevated privileges and should ignore env variables */ 1678/* return true if we are running with elevated privileges and should ignore env variables */
1617int inline_size 1679int inline_size ecb_cold
1618enable_secure (void) 1680enable_secure (void)
1619{ 1681{
1620#ifdef _WIN32 1682#ifdef _WIN32
1621 return 0; 1683 return 0;
1622#else 1684#else
1623 return getuid () != geteuid () 1685 return getuid () != geteuid ()
1624 || getgid () != getegid (); 1686 || getgid () != getegid ();
1625#endif 1687#endif
1626} 1688}
1627 1689
1628unsigned int 1690unsigned int ecb_cold
1629ev_supported_backends (void) 1691ev_supported_backends (void)
1630{ 1692{
1631 unsigned int flags = 0; 1693 unsigned int flags = 0;
1632 1694
1633 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 1695 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
1637 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 1699 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
1638 1700
1639 return flags; 1701 return flags;
1640} 1702}
1641 1703
1642unsigned int 1704unsigned int ecb_cold
1643ev_recommended_backends (void) 1705ev_recommended_backends (void)
1644{ 1706{
1645 unsigned int flags = ev_supported_backends (); 1707 unsigned int flags = ev_supported_backends ();
1646 1708
1647#ifndef __NetBSD__ 1709#ifndef __NetBSD__
1659#endif 1721#endif
1660 1722
1661 return flags; 1723 return flags;
1662} 1724}
1663 1725
1664unsigned int 1726unsigned int ecb_cold
1665ev_embeddable_backends (void) 1727ev_embeddable_backends (void)
1666{ 1728{
1667 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 1729 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1668 1730
1669 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 1731 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1714ev_userdata (EV_P) 1776ev_userdata (EV_P)
1715{ 1777{
1716 return userdata; 1778 return userdata;
1717} 1779}
1718 1780
1781void
1719void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 1782ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1720{ 1783{
1721 invoke_cb = invoke_pending_cb; 1784 invoke_cb = invoke_pending_cb;
1722} 1785}
1723 1786
1787void
1724void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 1788ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1725{ 1789{
1726 release_cb = release; 1790 release_cb = release;
1727 acquire_cb = acquire; 1791 acquire_cb = acquire;
1728} 1792}
1729#endif 1793#endif
1730 1794
1731/* initialise a loop structure, must be zero-initialised */ 1795/* initialise a loop structure, must be zero-initialised */
1732static void noinline 1796static void noinline ecb_cold
1733loop_init (EV_P_ unsigned int flags) 1797loop_init (EV_P_ unsigned int flags)
1734{ 1798{
1735 if (!backend) 1799 if (!backend)
1736 { 1800 {
1737 origflags = flags; 1801 origflags = flags;
1765 if (!(flags & EVFLAG_NOENV) 1829 if (!(flags & EVFLAG_NOENV)
1766 && !enable_secure () 1830 && !enable_secure ()
1767 && getenv ("LIBEV_FLAGS")) 1831 && getenv ("LIBEV_FLAGS"))
1768 flags = atoi (getenv ("LIBEV_FLAGS")); 1832 flags = atoi (getenv ("LIBEV_FLAGS"));
1769 1833
1770 ev_rt_now = ev_time (); 1834 ev_rt_now = ev_time ();
1771 mn_now = get_clock (); 1835 mn_now = get_clock ();
1772 now_floor = mn_now; 1836 now_floor = mn_now;
1773 rtmn_diff = ev_rt_now - mn_now; 1837 rtmn_diff = ev_rt_now - mn_now;
1774#if EV_FEATURE_API 1838#if EV_FEATURE_API
1775 invoke_cb = ev_invoke_pending; 1839 invoke_cb = ev_invoke_pending;
1776#endif 1840#endif
1777 1841
1778 io_blocktime = 0.; 1842 io_blocktime = 0.;
1779 timeout_blocktime = 0.; 1843 timeout_blocktime = 0.;
1780 backend = 0; 1844 backend = 0;
1781 backend_fd = -1; 1845 backend_fd = -1;
1782 sig_pending = 0; 1846 sig_pending = 0;
1783#if EV_ASYNC_ENABLE 1847#if EV_ASYNC_ENABLE
1784 async_pending = 0; 1848 async_pending = 0;
1785#endif 1849#endif
1850 pipe_write_skipped = 0;
1851 pipe_write_wanted = 0;
1786#if EV_USE_INOTIFY 1852#if EV_USE_INOTIFY
1787 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1853 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1788#endif 1854#endif
1789#if EV_USE_SIGNALFD 1855#if EV_USE_SIGNALFD
1790 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1856 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1791#endif 1857#endif
1792 1858
1793 if (!(flags & EVBACKEND_MASK)) 1859 if (!(flags & EVBACKEND_MASK))
1794 flags |= ev_recommended_backends (); 1860 flags |= ev_recommended_backends ();
1795 1861
1820#endif 1886#endif
1821 } 1887 }
1822} 1888}
1823 1889
1824/* free up a loop structure */ 1890/* free up a loop structure */
1825void 1891void ecb_cold
1826ev_loop_destroy (EV_P) 1892ev_loop_destroy (EV_P)
1827{ 1893{
1828 int i; 1894 int i;
1829 1895
1830#if EV_MULTIPLICITY 1896#if EV_MULTIPLICITY
1960 infy_fork (EV_A); 2026 infy_fork (EV_A);
1961#endif 2027#endif
1962 2028
1963 if (ev_is_active (&pipe_w)) 2029 if (ev_is_active (&pipe_w))
1964 { 2030 {
1965 /* this "locks" the handlers against writing to the pipe */ 2031 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
1966 /* while we modify the fd vars */
1967 sig_pending = 1;
1968#if EV_ASYNC_ENABLE
1969 async_pending = 1;
1970#endif
1971 2032
1972 ev_ref (EV_A); 2033 ev_ref (EV_A);
1973 ev_io_stop (EV_A_ &pipe_w); 2034 ev_io_stop (EV_A_ &pipe_w);
1974 2035
1975#if EV_USE_EVENTFD 2036#if EV_USE_EVENTFD
1993 postfork = 0; 2054 postfork = 0;
1994} 2055}
1995 2056
1996#if EV_MULTIPLICITY 2057#if EV_MULTIPLICITY
1997 2058
1998struct ev_loop * 2059struct ev_loop * ecb_cold
1999ev_loop_new (unsigned int flags) 2060ev_loop_new (unsigned int flags)
2000{ 2061{
2001 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2062 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2002 2063
2003 memset (EV_A, 0, sizeof (struct ev_loop)); 2064 memset (EV_A, 0, sizeof (struct ev_loop));
2011} 2072}
2012 2073
2013#endif /* multiplicity */ 2074#endif /* multiplicity */
2014 2075
2015#if EV_VERIFY 2076#if EV_VERIFY
2016static void noinline 2077static void noinline ecb_cold
2017verify_watcher (EV_P_ W w) 2078verify_watcher (EV_P_ W w)
2018{ 2079{
2019 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 2080 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
2020 2081
2021 if (w->pending) 2082 if (w->pending)
2022 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); 2083 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
2023} 2084}
2024 2085
2025static void noinline 2086static void noinline ecb_cold
2026verify_heap (EV_P_ ANHE *heap, int N) 2087verify_heap (EV_P_ ANHE *heap, int N)
2027{ 2088{
2028 int i; 2089 int i;
2029 2090
2030 for (i = HEAP0; i < N + HEAP0; ++i) 2091 for (i = HEAP0; i < N + HEAP0; ++i)
2035 2096
2036 verify_watcher (EV_A_ (W)ANHE_w (heap [i])); 2097 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
2037 } 2098 }
2038} 2099}
2039 2100
2040static void noinline 2101static void noinline ecb_cold
2041array_verify (EV_P_ W *ws, int cnt) 2102array_verify (EV_P_ W *ws, int cnt)
2042{ 2103{
2043 while (cnt--) 2104 while (cnt--)
2044 { 2105 {
2045 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 2106 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
2047 } 2108 }
2048} 2109}
2049#endif 2110#endif
2050 2111
2051#if EV_FEATURE_API 2112#if EV_FEATURE_API
2052void 2113void ecb_cold
2053ev_verify (EV_P) 2114ev_verify (EV_P)
2054{ 2115{
2055#if EV_VERIFY 2116#if EV_VERIFY
2056 int i; 2117 int i;
2057 WL w; 2118 WL w;
2123#endif 2184#endif
2124} 2185}
2125#endif 2186#endif
2126 2187
2127#if EV_MULTIPLICITY 2188#if EV_MULTIPLICITY
2128struct ev_loop * 2189struct ev_loop * ecb_cold
2129#else 2190#else
2130int 2191int
2131#endif 2192#endif
2132ev_default_loop (unsigned int flags) 2193ev_default_loop (unsigned int flags)
2133{ 2194{
2332 } 2393 }
2333} 2394}
2334 2395
2335/* simply recalculate all periodics */ 2396/* simply recalculate all periodics */
2336/* TODO: maybe ensure that at least one event happens when jumping forward? */ 2397/* TODO: maybe ensure that at least one event happens when jumping forward? */
2337static void noinline 2398static void noinline ecb_cold
2338periodics_reschedule (EV_P) 2399periodics_reschedule (EV_P)
2339{ 2400{
2340 int i; 2401 int i;
2341 2402
2342 /* adjust periodics after time jump */ 2403 /* adjust periodics after time jump */
2355 reheap (periodics, periodiccnt); 2416 reheap (periodics, periodiccnt);
2356} 2417}
2357#endif 2418#endif
2358 2419
2359/* adjust all timers by a given offset */ 2420/* adjust all timers by a given offset */
2360static void noinline 2421static void noinline ecb_cold
2361timers_reschedule (EV_P_ ev_tstamp adjust) 2422timers_reschedule (EV_P_ ev_tstamp adjust)
2362{ 2423{
2363 int i; 2424 int i;
2364 2425
2365 for (i = 0; i < timercnt; ++i) 2426 for (i = 0; i < timercnt; ++i)
2507 ev_tstamp prev_mn_now = mn_now; 2568 ev_tstamp prev_mn_now = mn_now;
2508 2569
2509 /* update time to cancel out callback processing overhead */ 2570 /* update time to cancel out callback processing overhead */
2510 time_update (EV_A_ 1e100); 2571 time_update (EV_A_ 1e100);
2511 2572
2573 /* from now on, we want a pipe-wake-up */
2574 pipe_write_wanted = 1;
2575
2512 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) 2576 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2513 { 2577 {
2514 waittime = MAX_BLOCKTIME; 2578 waittime = MAX_BLOCKTIME;
2515 2579
2516 if (timercnt) 2580 if (timercnt)
2517 { 2581 {
2518 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2582 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
2519 if (waittime > to) waittime = to; 2583 if (waittime > to) waittime = to;
2520 } 2584 }
2521 2585
2522#if EV_PERIODIC_ENABLE 2586#if EV_PERIODIC_ENABLE
2523 if (periodiccnt) 2587 if (periodiccnt)
2524 { 2588 {
2525 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2589 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
2526 if (waittime > to) waittime = to; 2590 if (waittime > to) waittime = to;
2527 } 2591 }
2528#endif 2592#endif
2529 2593
2530 /* don't let timeouts decrease the waittime below timeout_blocktime */ 2594 /* don't let timeouts decrease the waittime below timeout_blocktime */
2531 if (expect_false (waittime < timeout_blocktime)) 2595 if (expect_false (waittime < timeout_blocktime))
2532 waittime = timeout_blocktime; 2596 waittime = timeout_blocktime;
2597
2598 /* at this point, we NEED to wait, so we have to ensure */
2599 /* to pass a minimum nonzero value to the backend */
2600 if (expect_false (waittime < backend_mintime))
2601 waittime = backend_mintime;
2533 2602
2534 /* extra check because io_blocktime is commonly 0 */ 2603 /* extra check because io_blocktime is commonly 0 */
2535 if (expect_false (io_blocktime)) 2604 if (expect_false (io_blocktime))
2536 { 2605 {
2537 sleeptime = io_blocktime - (mn_now - prev_mn_now); 2606 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2538 2607
2539 if (sleeptime > waittime - backend_fudge) 2608 if (sleeptime > waittime - backend_mintime)
2540 sleeptime = waittime - backend_fudge; 2609 sleeptime = waittime - backend_mintime;
2541 2610
2542 if (expect_true (sleeptime > 0.)) 2611 if (expect_true (sleeptime > 0.))
2543 { 2612 {
2544 ev_sleep (sleeptime); 2613 ev_sleep (sleeptime);
2545 waittime -= sleeptime; 2614 waittime -= sleeptime;
2551 ++loop_count; 2620 ++loop_count;
2552#endif 2621#endif
2553 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 2622 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2554 backend_poll (EV_A_ waittime); 2623 backend_poll (EV_A_ waittime);
2555 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 2624 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2625
2626 pipe_write_wanted = 0;
2627
2628 if (pipe_write_skipped)
2629 {
2630 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2631 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2632 }
2633
2556 2634
2557 /* update ev_rt_now, do magic */ 2635 /* update ev_rt_now, do magic */
2558 time_update (EV_A_ waittime + sleeptime); 2636 time_update (EV_A_ waittime + sleeptime);
2559 } 2637 }
2560 2638
3214 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3292 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3215 ofs += sizeof (struct inotify_event) + ev->len; 3293 ofs += sizeof (struct inotify_event) + ev->len;
3216 } 3294 }
3217} 3295}
3218 3296
3219inline_size void 3297inline_size void ecb_cold
3220ev_check_2625 (EV_P) 3298ev_check_2625 (EV_P)
3221{ 3299{
3222 /* kernels < 2.6.25 are borked 3300 /* kernels < 2.6.25 are borked
3223 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 3301 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
3224 */ 3302 */
3849} 3927}
3850 3928
3851/*****************************************************************************/ 3929/*****************************************************************************/
3852 3930
3853#if EV_WALK_ENABLE 3931#if EV_WALK_ENABLE
3854void 3932void ecb_cold
3855ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3933ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3856{ 3934{
3857 int i, j; 3935 int i, j;
3858 ev_watcher_list *wl, *wn; 3936 ev_watcher_list *wl, *wn;
3859 3937

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines