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

Comparing libev/ev.c (file contents):
Revision 1.373 by root, Sun Feb 20 02:56:23 2011 UTC vs.
Revision 1.385 by root, Wed Jul 20 01:04:03 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
496
497#ifndef ECB_MEMORY_FENCE
498 #if ECB_GCC_VERSION(2,5)
499 #if __x86
500 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
501 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
502 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */
503 #elif __amd64
504 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
505 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
506 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence")
507 #endif
478#endif 508 #endif
509#endif
479 510
511#ifndef ECB_MEMORY_FENCE
512 #if ECB_GCC_VERSION(4,4)
513 #define ECB_MEMORY_FENCE __sync_synchronize ()
514 #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); })
515 #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); })
516 #elif defined(_WIN32) && defined(MemoryBarrier)
517 #define ECB_MEMORY_FENCE MemoryBarrier ()
518 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
519 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
520 #endif
521#endif
522
523#ifndef ECB_MEMORY_FENCE
524 #include <pthread.h>
525
526 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
527 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
528 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
529 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
530#endif
531
532#if ECB_GCC_VERSION(3,1)
533 #define ecb_attribute(attrlist) __attribute__(attrlist)
534 #define ecb_is_constant(expr) __builtin_constant_p (expr)
535 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
536 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
537#else
538 #define ecb_attribute(attrlist)
539 #define ecb_is_constant(expr) 0
540 #define ecb_expect(expr,value) (expr)
541 #define ecb_prefetch(addr,rw,locality)
542#endif
543
544#define ecb_noinline ecb_attribute ((__noinline__))
545#define ecb_noreturn ecb_attribute ((__noreturn__))
546#define ecb_unused ecb_attribute ((__unused__))
547#define ecb_const ecb_attribute ((__const__))
548#define ecb_pure ecb_attribute ((__pure__))
549
550#if ECB_GCC_VERSION(4,3)
551 #define ecb_artificial ecb_attribute ((__artificial__))
552 #define ecb_hot ecb_attribute ((__hot__))
553 #define ecb_cold ecb_attribute ((__cold__))
554#else
555 #define ecb_artificial
556 #define ecb_hot
557 #define ecb_cold
558#endif
559
560/* put around conditional expressions if you are very sure that the */
561/* expression is mostly true or mostly false. note that these return */
562/* booleans, not the expression. */
480#define expect_false(expr) expect ((expr) != 0, 0) 563#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
481#define expect_true(expr) expect ((expr) != 0, 1) 564#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
565/* ecb.h end */
566
567#define expect_false(cond) ecb_expect_false (cond)
568#define expect_true(cond) ecb_expect_true (cond)
569#define noinline ecb_noinline
570
482#define inline_size static inline 571#define inline_size ecb_inline
483 572
484#if EV_FEATURE_CODE 573#if EV_FEATURE_CODE
485# define inline_speed static inline 574# define inline_speed ecb_inline
486#else 575#else
487# define inline_speed static noinline 576# define inline_speed static noinline
488#endif 577#endif
489 578
490#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 579#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
581 670
582#ifdef __linux 671#ifdef __linux
583# include <sys/utsname.h> 672# include <sys/utsname.h>
584#endif 673#endif
585 674
586static unsigned int noinline 675static unsigned int noinline ecb_cold
587ev_linux_version (void) 676ev_linux_version (void)
588{ 677{
589#ifdef __linux 678#ifdef __linux
590 unsigned int v = 0; 679 unsigned int v = 0;
591 struct utsname buf; 680 struct utsname buf;
620} 709}
621 710
622/*****************************************************************************/ 711/*****************************************************************************/
623 712
624#if EV_AVOID_STDIO 713#if EV_AVOID_STDIO
625static void noinline 714static void noinline ecb_cold
626ev_printerr (const char *msg) 715ev_printerr (const char *msg)
627{ 716{
628 write (STDERR_FILENO, msg, strlen (msg)); 717 write (STDERR_FILENO, msg, strlen (msg));
629} 718}
630#endif 719#endif
631 720
632static void (*syserr_cb)(const char *msg); 721static void (*syserr_cb)(const char *msg);
633 722
634void 723void ecb_cold
635ev_set_syserr_cb (void (*cb)(const char *msg)) 724ev_set_syserr_cb (void (*cb)(const char *msg))
636{ 725{
637 syserr_cb = cb; 726 syserr_cb = cb;
638} 727}
639 728
640static void noinline 729static void noinline ecb_cold
641ev_syserr (const char *msg) 730ev_syserr (const char *msg)
642{ 731{
643 if (!msg) 732 if (!msg)
644 msg = "(libev) system error"; 733 msg = "(libev) system error";
645 734
678#endif 767#endif
679} 768}
680 769
681static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 770static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
682 771
683void 772void ecb_cold
684ev_set_allocator (void *(*cb)(void *ptr, long size)) 773ev_set_allocator (void *(*cb)(void *ptr, long size))
685{ 774{
686 alloc = cb; 775 alloc = cb;
687} 776}
688 777
870 select (0, 0, 0, 0, &tv); 959 select (0, 0, 0, 0, &tv);
871#endif 960#endif
872 } 961 }
873} 962}
874 963
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/*****************************************************************************/ 964/*****************************************************************************/
884 965
885#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 966#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
886 967
887/* find a suitable new size for the given array, */ 968/* find a suitable new size for the given array, */
905 } 986 }
906 987
907 return ncur; 988 return ncur;
908} 989}
909 990
910static noinline void * 991static void * noinline ecb_cold
911array_realloc (int elem, void *base, int *cur, int cnt) 992array_realloc (int elem, void *base, int *cur, int cnt)
912{ 993{
913 *cur = array_nextsize (elem, *cur, cnt); 994 *cur = array_nextsize (elem, *cur, cnt);
914 return ev_realloc (base, elem * *cur); 995 return ev_realloc (base, elem * *cur);
915} 996}
918 memset ((void *)(base), 0, sizeof (*(base)) * (count)) 999 memset ((void *)(base), 0, sizeof (*(base)) * (count))
919 1000
920#define array_needsize(type,base,cur,cnt,init) \ 1001#define array_needsize(type,base,cur,cnt,init) \
921 if (expect_false ((cnt) > (cur))) \ 1002 if (expect_false ((cnt) > (cur))) \
922 { \ 1003 { \
923 int ocur_ = (cur); \ 1004 int ecb_unused ocur_ = (cur); \
924 (base) = (type *)array_realloc \ 1005 (base) = (type *)array_realloc \
925 (sizeof (type), (base), &(cur), (cnt)); \ 1006 (sizeof (type), (base), &(cur), (cnt)); \
926 init ((base) + (ocur_), (cur) - ocur_); \ 1007 init ((base) + (ocur_), (cur) - ocur_); \
927 } 1008 }
928 1009
1034 for (i = 0; i < fdchangecnt; ++i) 1115 for (i = 0; i < fdchangecnt; ++i)
1035 { 1116 {
1036 int fd = fdchanges [i]; 1117 int fd = fdchanges [i];
1037 ANFD *anfd = anfds + fd; 1118 ANFD *anfd = anfds + fd;
1038 1119
1039 if (anfd->reify & EV__IOFDSET) 1120 if (anfd->reify & EV__IOFDSET && anfd->head)
1040 { 1121 {
1041 SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); 1122 SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
1042 1123
1043 if (handle != anfd->handle) 1124 if (handle != anfd->handle)
1044 { 1125 {
1098 fdchanges [fdchangecnt - 1] = fd; 1179 fdchanges [fdchangecnt - 1] = fd;
1099 } 1180 }
1100} 1181}
1101 1182
1102/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ 1183/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
1103inline_speed void 1184inline_speed void ecb_cold
1104fd_kill (EV_P_ int fd) 1185fd_kill (EV_P_ int fd)
1105{ 1186{
1106 ev_io *w; 1187 ev_io *w;
1107 1188
1108 while ((w = (ev_io *)anfds [fd].head)) 1189 while ((w = (ev_io *)anfds [fd].head))
1111 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 1192 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
1112 } 1193 }
1113} 1194}
1114 1195
1115/* check whether the given fd is actually valid, for error recovery */ 1196/* check whether the given fd is actually valid, for error recovery */
1116inline_size int 1197inline_size int ecb_cold
1117fd_valid (int fd) 1198fd_valid (int fd)
1118{ 1199{
1119#ifdef _WIN32 1200#ifdef _WIN32
1120 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1201 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1121#else 1202#else
1122 return fcntl (fd, F_GETFD) != -1; 1203 return fcntl (fd, F_GETFD) != -1;
1123#endif 1204#endif
1124} 1205}
1125 1206
1126/* called on EBADF to verify fds */ 1207/* called on EBADF to verify fds */
1127static void noinline 1208static void noinline ecb_cold
1128fd_ebadf (EV_P) 1209fd_ebadf (EV_P)
1129{ 1210{
1130 int fd; 1211 int fd;
1131 1212
1132 for (fd = 0; fd < anfdmax; ++fd) 1213 for (fd = 0; fd < anfdmax; ++fd)
1134 if (!fd_valid (fd) && errno == EBADF) 1215 if (!fd_valid (fd) && errno == EBADF)
1135 fd_kill (EV_A_ fd); 1216 fd_kill (EV_A_ fd);
1136} 1217}
1137 1218
1138/* called on ENOMEM in select/poll to kill some fds and retry */ 1219/* called on ENOMEM in select/poll to kill some fds and retry */
1139static void noinline 1220static void noinline ecb_cold
1140fd_enomem (EV_P) 1221fd_enomem (EV_P)
1141{ 1222{
1142 int fd; 1223 int fd;
1143 1224
1144 for (fd = anfdmax; fd--; ) 1225 for (fd = anfdmax; fd--; )
1339 1420
1340/*****************************************************************************/ 1421/*****************************************************************************/
1341 1422
1342#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 1423#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1343 1424
1344static void noinline 1425static void noinline ecb_cold
1345evpipe_init (EV_P) 1426evpipe_init (EV_P)
1346{ 1427{
1347 if (!ev_is_active (&pipe_w)) 1428 if (!ev_is_active (&pipe_w))
1348 { 1429 {
1349# if EV_USE_EVENTFD 1430# if EV_USE_EVENTFD
1371 ev_io_start (EV_A_ &pipe_w); 1452 ev_io_start (EV_A_ &pipe_w);
1372 ev_unref (EV_A); /* watcher should not keep loop alive */ 1453 ev_unref (EV_A); /* watcher should not keep loop alive */
1373 } 1454 }
1374} 1455}
1375 1456
1376inline_size void 1457inline_speed void
1377evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1458evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1378{ 1459{
1379 if (!*flag) 1460 if (expect_true (*flag))
1461 /*return*//*D*/;
1462
1463 *flag = 1;
1464
1465 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1466
1467 pipe_write_skipped = 1;
1468
1469 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1470
1471 if (pipe_write_wanted)
1380 { 1472 {
1473 int old_errno;
1474
1475 pipe_write_skipped = 0; /* just an optimsiation, no fence needed */
1476
1381 int old_errno = errno; /* save errno because write might clobber it */ 1477 old_errno = errno; /* save errno because write will clobber it */
1382 char dummy;
1383
1384 *flag = 1;
1385 1478
1386#if EV_USE_EVENTFD 1479#if EV_USE_EVENTFD
1387 if (evfd >= 0) 1480 if (evfd >= 0)
1388 { 1481 {
1389 uint64_t counter = 1; 1482 uint64_t counter = 1;
1390 write (evfd, &counter, sizeof (uint64_t)); 1483 write (evfd, &counter, sizeof (uint64_t));
1391 } 1484 }
1392 else 1485 else
1393#endif 1486#endif
1487 {
1394 /* win32 people keep sending patches that change this write() to send() */ 1488 /* 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 */ 1489 /* 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 */ 1490 /* 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 */ 1491 /* where your send() is from - it's definitely not the microsoft send, and */
1398 /* tell me. thank you. */ 1492 /* tell me. thank you. */
1399 write (evpipe [1], &dummy, 1); 1493 write (evpipe [1], &(evpipe [1]), 1);
1494 }
1400 1495
1401 errno = old_errno; 1496 errno = old_errno;
1402 } 1497 }
1403} 1498}
1404 1499
1407static void 1502static void
1408pipecb (EV_P_ ev_io *iow, int revents) 1503pipecb (EV_P_ ev_io *iow, int revents)
1409{ 1504{
1410 int i; 1505 int i;
1411 1506
1507 if (revents & EV_READ)
1508 {
1412#if EV_USE_EVENTFD 1509#if EV_USE_EVENTFD
1413 if (evfd >= 0) 1510 if (evfd >= 0)
1414 { 1511 {
1415 uint64_t counter; 1512 uint64_t counter;
1416 read (evfd, &counter, sizeof (uint64_t)); 1513 read (evfd, &counter, sizeof (uint64_t));
1417 } 1514 }
1418 else 1515 else
1419#endif 1516#endif
1420 { 1517 {
1421 char dummy; 1518 char dummy;
1422 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1519 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1423 read (evpipe [0], &dummy, 1); 1520 read (evpipe [0], &dummy, 1);
1521 }
1424 } 1522 }
1523
1524 pipe_write_skipped = 0;
1425 1525
1426#if EV_SIGNAL_ENABLE 1526#if EV_SIGNAL_ENABLE
1427 if (sig_pending) 1527 if (sig_pending)
1428 { 1528 {
1429 sig_pending = 0; 1529 sig_pending = 0;
1458 EV_P = signals [signum - 1].loop; 1558 EV_P = signals [signum - 1].loop;
1459 1559
1460 if (!EV_A) 1560 if (!EV_A)
1461 return; 1561 return;
1462#endif 1562#endif
1563
1564 if (!ev_active (&pipe_w))
1565 return;
1463 1566
1464 signals [signum - 1].pending = 1; 1567 signals [signum - 1].pending = 1;
1465 evpipe_write (EV_A_ &sig_pending); 1568 evpipe_write (EV_A_ &sig_pending);
1466} 1569}
1467 1570
1599#endif 1702#endif
1600#if EV_USE_SELECT 1703#if EV_USE_SELECT
1601# include "ev_select.c" 1704# include "ev_select.c"
1602#endif 1705#endif
1603 1706
1604int 1707int ecb_cold
1605ev_version_major (void) 1708ev_version_major (void)
1606{ 1709{
1607 return EV_VERSION_MAJOR; 1710 return EV_VERSION_MAJOR;
1608} 1711}
1609 1712
1610int 1713int ecb_cold
1611ev_version_minor (void) 1714ev_version_minor (void)
1612{ 1715{
1613 return EV_VERSION_MINOR; 1716 return EV_VERSION_MINOR;
1614} 1717}
1615 1718
1616/* return true if we are running with elevated privileges and should ignore env variables */ 1719/* return true if we are running with elevated privileges and should ignore env variables */
1617int inline_size 1720int inline_size ecb_cold
1618enable_secure (void) 1721enable_secure (void)
1619{ 1722{
1620#ifdef _WIN32 1723#ifdef _WIN32
1621 return 0; 1724 return 0;
1622#else 1725#else
1623 return getuid () != geteuid () 1726 return getuid () != geteuid ()
1624 || getgid () != getegid (); 1727 || getgid () != getegid ();
1625#endif 1728#endif
1626} 1729}
1627 1730
1628unsigned int 1731unsigned int ecb_cold
1629ev_supported_backends (void) 1732ev_supported_backends (void)
1630{ 1733{
1631 unsigned int flags = 0; 1734 unsigned int flags = 0;
1632 1735
1633 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 1736 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
1637 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 1740 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
1638 1741
1639 return flags; 1742 return flags;
1640} 1743}
1641 1744
1642unsigned int 1745unsigned int ecb_cold
1643ev_recommended_backends (void) 1746ev_recommended_backends (void)
1644{ 1747{
1645 unsigned int flags = ev_supported_backends (); 1748 unsigned int flags = ev_supported_backends ();
1646 1749
1647#ifndef __NetBSD__ 1750#ifndef __NetBSD__
1659#endif 1762#endif
1660 1763
1661 return flags; 1764 return flags;
1662} 1765}
1663 1766
1664unsigned int 1767unsigned int ecb_cold
1665ev_embeddable_backends (void) 1768ev_embeddable_backends (void)
1666{ 1769{
1667 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 1770 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1668 1771
1669 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 1772 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1714ev_userdata (EV_P) 1817ev_userdata (EV_P)
1715{ 1818{
1716 return userdata; 1819 return userdata;
1717} 1820}
1718 1821
1822void
1719void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 1823ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1720{ 1824{
1721 invoke_cb = invoke_pending_cb; 1825 invoke_cb = invoke_pending_cb;
1722} 1826}
1723 1827
1828void
1724void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 1829ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1725{ 1830{
1726 release_cb = release; 1831 release_cb = release;
1727 acquire_cb = acquire; 1832 acquire_cb = acquire;
1728} 1833}
1729#endif 1834#endif
1730 1835
1731/* initialise a loop structure, must be zero-initialised */ 1836/* initialise a loop structure, must be zero-initialised */
1732static void noinline 1837static void noinline ecb_cold
1733loop_init (EV_P_ unsigned int flags) 1838loop_init (EV_P_ unsigned int flags)
1734{ 1839{
1735 if (!backend) 1840 if (!backend)
1736 { 1841 {
1737 origflags = flags; 1842 origflags = flags;
1765 if (!(flags & EVFLAG_NOENV) 1870 if (!(flags & EVFLAG_NOENV)
1766 && !enable_secure () 1871 && !enable_secure ()
1767 && getenv ("LIBEV_FLAGS")) 1872 && getenv ("LIBEV_FLAGS"))
1768 flags = atoi (getenv ("LIBEV_FLAGS")); 1873 flags = atoi (getenv ("LIBEV_FLAGS"));
1769 1874
1770 ev_rt_now = ev_time (); 1875 ev_rt_now = ev_time ();
1771 mn_now = get_clock (); 1876 mn_now = get_clock ();
1772 now_floor = mn_now; 1877 now_floor = mn_now;
1773 rtmn_diff = ev_rt_now - mn_now; 1878 rtmn_diff = ev_rt_now - mn_now;
1774#if EV_FEATURE_API 1879#if EV_FEATURE_API
1775 invoke_cb = ev_invoke_pending; 1880 invoke_cb = ev_invoke_pending;
1776#endif 1881#endif
1777 1882
1778 io_blocktime = 0.; 1883 io_blocktime = 0.;
1779 timeout_blocktime = 0.; 1884 timeout_blocktime = 0.;
1780 backend = 0; 1885 backend = 0;
1781 backend_fd = -1; 1886 backend_fd = -1;
1782 sig_pending = 0; 1887 sig_pending = 0;
1783#if EV_ASYNC_ENABLE 1888#if EV_ASYNC_ENABLE
1784 async_pending = 0; 1889 async_pending = 0;
1785#endif 1890#endif
1891 pipe_write_skipped = 0;
1892 pipe_write_wanted = 0;
1786#if EV_USE_INOTIFY 1893#if EV_USE_INOTIFY
1787 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1894 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1788#endif 1895#endif
1789#if EV_USE_SIGNALFD 1896#if EV_USE_SIGNALFD
1790 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1897 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1791#endif 1898#endif
1792 1899
1793 if (!(flags & EVBACKEND_MASK)) 1900 if (!(flags & EVBACKEND_MASK))
1794 flags |= ev_recommended_backends (); 1901 flags |= ev_recommended_backends ();
1795 1902
1820#endif 1927#endif
1821 } 1928 }
1822} 1929}
1823 1930
1824/* free up a loop structure */ 1931/* free up a loop structure */
1825void 1932void ecb_cold
1826ev_loop_destroy (EV_P) 1933ev_loop_destroy (EV_P)
1827{ 1934{
1828 int i; 1935 int i;
1829 1936
1830#if EV_MULTIPLICITY 1937#if EV_MULTIPLICITY
1960 infy_fork (EV_A); 2067 infy_fork (EV_A);
1961#endif 2068#endif
1962 2069
1963 if (ev_is_active (&pipe_w)) 2070 if (ev_is_active (&pipe_w))
1964 { 2071 {
1965 /* this "locks" the handlers against writing to the pipe */ 2072 /* 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 2073
1972 ev_ref (EV_A); 2074 ev_ref (EV_A);
1973 ev_io_stop (EV_A_ &pipe_w); 2075 ev_io_stop (EV_A_ &pipe_w);
1974 2076
1975#if EV_USE_EVENTFD 2077#if EV_USE_EVENTFD
1993 postfork = 0; 2095 postfork = 0;
1994} 2096}
1995 2097
1996#if EV_MULTIPLICITY 2098#if EV_MULTIPLICITY
1997 2099
1998struct ev_loop * 2100struct ev_loop * ecb_cold
1999ev_loop_new (unsigned int flags) 2101ev_loop_new (unsigned int flags)
2000{ 2102{
2001 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2103 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2002 2104
2003 memset (EV_A, 0, sizeof (struct ev_loop)); 2105 memset (EV_A, 0, sizeof (struct ev_loop));
2011} 2113}
2012 2114
2013#endif /* multiplicity */ 2115#endif /* multiplicity */
2014 2116
2015#if EV_VERIFY 2117#if EV_VERIFY
2016static void noinline 2118static void noinline ecb_cold
2017verify_watcher (EV_P_ W w) 2119verify_watcher (EV_P_ W w)
2018{ 2120{
2019 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 2121 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
2020 2122
2021 if (w->pending) 2123 if (w->pending)
2022 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); 2124 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
2023} 2125}
2024 2126
2025static void noinline 2127static void noinline ecb_cold
2026verify_heap (EV_P_ ANHE *heap, int N) 2128verify_heap (EV_P_ ANHE *heap, int N)
2027{ 2129{
2028 int i; 2130 int i;
2029 2131
2030 for (i = HEAP0; i < N + HEAP0; ++i) 2132 for (i = HEAP0; i < N + HEAP0; ++i)
2035 2137
2036 verify_watcher (EV_A_ (W)ANHE_w (heap [i])); 2138 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
2037 } 2139 }
2038} 2140}
2039 2141
2040static void noinline 2142static void noinline ecb_cold
2041array_verify (EV_P_ W *ws, int cnt) 2143array_verify (EV_P_ W *ws, int cnt)
2042{ 2144{
2043 while (cnt--) 2145 while (cnt--)
2044 { 2146 {
2045 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 2147 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
2047 } 2149 }
2048} 2150}
2049#endif 2151#endif
2050 2152
2051#if EV_FEATURE_API 2153#if EV_FEATURE_API
2052void 2154void ecb_cold
2053ev_verify (EV_P) 2155ev_verify (EV_P)
2054{ 2156{
2055#if EV_VERIFY 2157#if EV_VERIFY
2056 int i; 2158 int i;
2057 WL w; 2159 WL w;
2123#endif 2225#endif
2124} 2226}
2125#endif 2227#endif
2126 2228
2127#if EV_MULTIPLICITY 2229#if EV_MULTIPLICITY
2128struct ev_loop * 2230struct ev_loop * ecb_cold
2129#else 2231#else
2130int 2232int
2131#endif 2233#endif
2132ev_default_loop (unsigned int flags) 2234ev_default_loop (unsigned int flags)
2133{ 2235{
2332 } 2434 }
2333} 2435}
2334 2436
2335/* simply recalculate all periodics */ 2437/* simply recalculate all periodics */
2336/* TODO: maybe ensure that at least one event happens when jumping forward? */ 2438/* TODO: maybe ensure that at least one event happens when jumping forward? */
2337static void noinline 2439static void noinline ecb_cold
2338periodics_reschedule (EV_P) 2440periodics_reschedule (EV_P)
2339{ 2441{
2340 int i; 2442 int i;
2341 2443
2342 /* adjust periodics after time jump */ 2444 /* adjust periodics after time jump */
2355 reheap (periodics, periodiccnt); 2457 reheap (periodics, periodiccnt);
2356} 2458}
2357#endif 2459#endif
2358 2460
2359/* adjust all timers by a given offset */ 2461/* adjust all timers by a given offset */
2360static void noinline 2462static void noinline ecb_cold
2361timers_reschedule (EV_P_ ev_tstamp adjust) 2463timers_reschedule (EV_P_ ev_tstamp adjust)
2362{ 2464{
2363 int i; 2465 int i;
2364 2466
2365 for (i = 0; i < timercnt; ++i) 2467 for (i = 0; i < timercnt; ++i)
2507 ev_tstamp prev_mn_now = mn_now; 2609 ev_tstamp prev_mn_now = mn_now;
2508 2610
2509 /* update time to cancel out callback processing overhead */ 2611 /* update time to cancel out callback processing overhead */
2510 time_update (EV_A_ 1e100); 2612 time_update (EV_A_ 1e100);
2511 2613
2614 /* from now on, we want a pipe-wake-up */
2615 pipe_write_wanted = 1;
2616
2617 ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */
2618
2512 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) 2619 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2513 { 2620 {
2514 waittime = MAX_BLOCKTIME; 2621 waittime = MAX_BLOCKTIME;
2515 2622
2516 if (timercnt) 2623 if (timercnt)
2517 { 2624 {
2518 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2625 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
2519 if (waittime > to) waittime = to; 2626 if (waittime > to) waittime = to;
2520 } 2627 }
2521 2628
2522#if EV_PERIODIC_ENABLE 2629#if EV_PERIODIC_ENABLE
2523 if (periodiccnt) 2630 if (periodiccnt)
2524 { 2631 {
2525 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2632 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
2526 if (waittime > to) waittime = to; 2633 if (waittime > to) waittime = to;
2527 } 2634 }
2528#endif 2635#endif
2529 2636
2530 /* don't let timeouts decrease the waittime below timeout_blocktime */ 2637 /* don't let timeouts decrease the waittime below timeout_blocktime */
2531 if (expect_false (waittime < timeout_blocktime)) 2638 if (expect_false (waittime < timeout_blocktime))
2532 waittime = timeout_blocktime; 2639 waittime = timeout_blocktime;
2640
2641 /* at this point, we NEED to wait, so we have to ensure */
2642 /* to pass a minimum nonzero value to the backend */
2643 if (expect_false (waittime < backend_mintime))
2644 waittime = backend_mintime;
2533 2645
2534 /* extra check because io_blocktime is commonly 0 */ 2646 /* extra check because io_blocktime is commonly 0 */
2535 if (expect_false (io_blocktime)) 2647 if (expect_false (io_blocktime))
2536 { 2648 {
2537 sleeptime = io_blocktime - (mn_now - prev_mn_now); 2649 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2538 2650
2539 if (sleeptime > waittime - backend_fudge) 2651 if (sleeptime > waittime - backend_mintime)
2540 sleeptime = waittime - backend_fudge; 2652 sleeptime = waittime - backend_mintime;
2541 2653
2542 if (expect_true (sleeptime > 0.)) 2654 if (expect_true (sleeptime > 0.))
2543 { 2655 {
2544 ev_sleep (sleeptime); 2656 ev_sleep (sleeptime);
2545 waittime -= sleeptime; 2657 waittime -= sleeptime;
2551 ++loop_count; 2663 ++loop_count;
2552#endif 2664#endif
2553 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 2665 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2554 backend_poll (EV_A_ waittime); 2666 backend_poll (EV_A_ waittime);
2555 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 2667 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2668
2669 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */
2670
2671 if (pipe_write_skipped)
2672 {
2673 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2674 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2675 }
2676
2556 2677
2557 /* update ev_rt_now, do magic */ 2678 /* update ev_rt_now, do magic */
2558 time_update (EV_A_ waittime + sleeptime); 2679 time_update (EV_A_ waittime + sleeptime);
2559 } 2680 }
2560 2681
3214 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3335 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3215 ofs += sizeof (struct inotify_event) + ev->len; 3336 ofs += sizeof (struct inotify_event) + ev->len;
3216 } 3337 }
3217} 3338}
3218 3339
3219inline_size void 3340inline_size void ecb_cold
3220ev_check_2625 (EV_P) 3341ev_check_2625 (EV_P)
3221{ 3342{
3222 /* kernels < 2.6.25 are borked 3343 /* kernels < 2.6.25 are borked
3223 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 3344 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
3224 */ 3345 */
3849} 3970}
3850 3971
3851/*****************************************************************************/ 3972/*****************************************************************************/
3852 3973
3853#if EV_WALK_ENABLE 3974#if EV_WALK_ENABLE
3854void 3975void ecb_cold
3855ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3976ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3856{ 3977{
3857 int i, j; 3978 int i, j;
3858 ev_watcher_list *wl, *wn; 3979 ev_watcher_list *wl, *wn;
3859 3980

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines