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

Comparing libev/ev.c (file contents):
Revision 1.378 by root, Mon Jun 13 09:52:36 2011 UTC vs.
Revision 1.380 by root, Mon Jun 27 19:20:01 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
897 } 951 }
898 952
899 return ncur; 953 return ncur;
900} 954}
901 955
902static noinline void * 956static void * noinline ecb_cold
903array_realloc (int elem, void *base, int *cur, int cnt) 957array_realloc (int elem, void *base, int *cur, int cnt)
904{ 958{
905 *cur = array_nextsize (elem, *cur, cnt); 959 *cur = array_nextsize (elem, *cur, cnt);
906 return ev_realloc (base, elem * *cur); 960 return ev_realloc (base, elem * *cur);
907} 961}
1090 fdchanges [fdchangecnt - 1] = fd; 1144 fdchanges [fdchangecnt - 1] = fd;
1091 } 1145 }
1092} 1146}
1093 1147
1094/* 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 */
1095inline_speed void 1149inline_speed void ecb_cold
1096fd_kill (EV_P_ int fd) 1150fd_kill (EV_P_ int fd)
1097{ 1151{
1098 ev_io *w; 1152 ev_io *w;
1099 1153
1100 while ((w = (ev_io *)anfds [fd].head)) 1154 while ((w = (ev_io *)anfds [fd].head))
1103 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);
1104 } 1158 }
1105} 1159}
1106 1160
1107/* check whether the given fd is actually valid, for error recovery */ 1161/* check whether the given fd is actually valid, for error recovery */
1108inline_size int 1162inline_size int ecb_cold
1109fd_valid (int fd) 1163fd_valid (int fd)
1110{ 1164{
1111#ifdef _WIN32 1165#ifdef _WIN32
1112 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1166 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1113#else 1167#else
1114 return fcntl (fd, F_GETFD) != -1; 1168 return fcntl (fd, F_GETFD) != -1;
1115#endif 1169#endif
1116} 1170}
1117 1171
1118/* called on EBADF to verify fds */ 1172/* called on EBADF to verify fds */
1119static void noinline 1173static void noinline ecb_cold
1120fd_ebadf (EV_P) 1174fd_ebadf (EV_P)
1121{ 1175{
1122 int fd; 1176 int fd;
1123 1177
1124 for (fd = 0; fd < anfdmax; ++fd) 1178 for (fd = 0; fd < anfdmax; ++fd)
1126 if (!fd_valid (fd) && errno == EBADF) 1180 if (!fd_valid (fd) && errno == EBADF)
1127 fd_kill (EV_A_ fd); 1181 fd_kill (EV_A_ fd);
1128} 1182}
1129 1183
1130/* 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 */
1131static void noinline 1185static void noinline ecb_cold
1132fd_enomem (EV_P) 1186fd_enomem (EV_P)
1133{ 1187{
1134 int fd; 1188 int fd;
1135 1189
1136 for (fd = anfdmax; fd--; ) 1190 for (fd = anfdmax; fd--; )
1331 1385
1332/*****************************************************************************/ 1386/*****************************************************************************/
1333 1387
1334#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 1388#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1335 1389
1336static void noinline 1390static void noinline ecb_cold
1337evpipe_init (EV_P) 1391evpipe_init (EV_P)
1338{ 1392{
1339 if (!ev_is_active (&pipe_w)) 1393 if (!ev_is_active (&pipe_w))
1340 { 1394 {
1341# if EV_USE_EVENTFD 1395# if EV_USE_EVENTFD
1363 ev_io_start (EV_A_ &pipe_w); 1417 ev_io_start (EV_A_ &pipe_w);
1364 ev_unref (EV_A); /* watcher should not keep loop alive */ 1418 ev_unref (EV_A); /* watcher should not keep loop alive */
1365 } 1419 }
1366} 1420}
1367 1421
1368inline_size void 1422inline_speed void
1369evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1423evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1370{ 1424{
1371 if (!*flag) 1425 if (!*flag)
1372 { 1426 {
1373 *flag = 1; 1427 *flag = 1;
1374 1428
1375 pipe_write_skipped = 1; 1429 pipe_write_skipped = 1;
1376 1430
1377 if (pipe_write_wanted) 1431 if (pipe_write_wanted)
1378 { 1432 {
1379 int old_errno = errno; /* save errno because write will clobber it */ 1433 int old_errno;
1380 char dummy;
1381 1434
1382 pipe_write_skipped = 0; 1435 pipe_write_skipped = 0;
1436
1437 old_errno = errno; /* save errno because write will clobber it */
1383 1438
1384#if EV_USE_EVENTFD 1439#if EV_USE_EVENTFD
1385 if (evfd >= 0) 1440 if (evfd >= 0)
1386 { 1441 {
1387 uint64_t counter = 1; 1442 uint64_t counter = 1;
1393 /* win32 people keep sending patches that change this write() to send() */ 1448 /* win32 people keep sending patches that change this write() to send() */
1394 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1449 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1395 /* so when you think this write should be a send instead, please find out */ 1450 /* so when you think this write should be a send instead, please find out */
1396 /* where your send() is from - it's definitely not the microsoft send, and */ 1451 /* where your send() is from - it's definitely not the microsoft send, and */
1397 /* tell me. thank you. */ 1452 /* tell me. thank you. */
1398 write (evpipe [1], &dummy, 1); 1453 write (evpipe [1], &(evpipe [1]), 1);
1399 } 1454 }
1400 1455
1401 errno = old_errno; 1456 errno = old_errno;
1402 } 1457 }
1403 } 1458 }
1607#endif 1662#endif
1608#if EV_USE_SELECT 1663#if EV_USE_SELECT
1609# include "ev_select.c" 1664# include "ev_select.c"
1610#endif 1665#endif
1611 1666
1612int 1667int ecb_cold
1613ev_version_major (void) 1668ev_version_major (void)
1614{ 1669{
1615 return EV_VERSION_MAJOR; 1670 return EV_VERSION_MAJOR;
1616} 1671}
1617 1672
1618int 1673int ecb_cold
1619ev_version_minor (void) 1674ev_version_minor (void)
1620{ 1675{
1621 return EV_VERSION_MINOR; 1676 return EV_VERSION_MINOR;
1622} 1677}
1623 1678
1624/* return true if we are running with elevated privileges and should ignore env variables */ 1679/* return true if we are running with elevated privileges and should ignore env variables */
1625int inline_size 1680int inline_size ecb_cold
1626enable_secure (void) 1681enable_secure (void)
1627{ 1682{
1628#ifdef _WIN32 1683#ifdef _WIN32
1629 return 0; 1684 return 0;
1630#else 1685#else
1631 return getuid () != geteuid () 1686 return getuid () != geteuid ()
1632 || getgid () != getegid (); 1687 || getgid () != getegid ();
1633#endif 1688#endif
1634} 1689}
1635 1690
1636unsigned int 1691unsigned int ecb_cold
1637ev_supported_backends (void) 1692ev_supported_backends (void)
1638{ 1693{
1639 unsigned int flags = 0; 1694 unsigned int flags = 0;
1640 1695
1641 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 1696 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
1645 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 1700 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
1646 1701
1647 return flags; 1702 return flags;
1648} 1703}
1649 1704
1650unsigned int 1705unsigned int ecb_cold
1651ev_recommended_backends (void) 1706ev_recommended_backends (void)
1652{ 1707{
1653 unsigned int flags = ev_supported_backends (); 1708 unsigned int flags = ev_supported_backends ();
1654 1709
1655#ifndef __NetBSD__ 1710#ifndef __NetBSD__
1667#endif 1722#endif
1668 1723
1669 return flags; 1724 return flags;
1670} 1725}
1671 1726
1672unsigned int 1727unsigned int ecb_cold
1673ev_embeddable_backends (void) 1728ev_embeddable_backends (void)
1674{ 1729{
1675 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 1730 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1676 1731
1677 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 1732 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1722ev_userdata (EV_P) 1777ev_userdata (EV_P)
1723{ 1778{
1724 return userdata; 1779 return userdata;
1725} 1780}
1726 1781
1782void
1727void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 1783ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1728{ 1784{
1729 invoke_cb = invoke_pending_cb; 1785 invoke_cb = invoke_pending_cb;
1730} 1786}
1731 1787
1788void
1732void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 1789ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1733{ 1790{
1734 release_cb = release; 1791 release_cb = release;
1735 acquire_cb = acquire; 1792 acquire_cb = acquire;
1736} 1793}
1737#endif 1794#endif
1738 1795
1739/* initialise a loop structure, must be zero-initialised */ 1796/* initialise a loop structure, must be zero-initialised */
1740static void noinline 1797static void noinline ecb_cold
1741loop_init (EV_P_ unsigned int flags) 1798loop_init (EV_P_ unsigned int flags)
1742{ 1799{
1743 if (!backend) 1800 if (!backend)
1744 { 1801 {
1745 origflags = flags; 1802 origflags = flags;
1830#endif 1887#endif
1831 } 1888 }
1832} 1889}
1833 1890
1834/* free up a loop structure */ 1891/* free up a loop structure */
1835void 1892void ecb_cold
1836ev_loop_destroy (EV_P) 1893ev_loop_destroy (EV_P)
1837{ 1894{
1838 int i; 1895 int i;
1839 1896
1840#if EV_MULTIPLICITY 1897#if EV_MULTIPLICITY
1998 postfork = 0; 2055 postfork = 0;
1999} 2056}
2000 2057
2001#if EV_MULTIPLICITY 2058#if EV_MULTIPLICITY
2002 2059
2003struct ev_loop * 2060struct ev_loop * ecb_cold
2004ev_loop_new (unsigned int flags) 2061ev_loop_new (unsigned int flags)
2005{ 2062{
2006 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2063 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2007 2064
2008 memset (EV_A, 0, sizeof (struct ev_loop)); 2065 memset (EV_A, 0, sizeof (struct ev_loop));
2016} 2073}
2017 2074
2018#endif /* multiplicity */ 2075#endif /* multiplicity */
2019 2076
2020#if EV_VERIFY 2077#if EV_VERIFY
2021static void noinline 2078static void noinline ecb_cold
2022verify_watcher (EV_P_ W w) 2079verify_watcher (EV_P_ W w)
2023{ 2080{
2024 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 2081 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
2025 2082
2026 if (w->pending) 2083 if (w->pending)
2027 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); 2084 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
2028} 2085}
2029 2086
2030static void noinline 2087static void noinline ecb_cold
2031verify_heap (EV_P_ ANHE *heap, int N) 2088verify_heap (EV_P_ ANHE *heap, int N)
2032{ 2089{
2033 int i; 2090 int i;
2034 2091
2035 for (i = HEAP0; i < N + HEAP0; ++i) 2092 for (i = HEAP0; i < N + HEAP0; ++i)
2040 2097
2041 verify_watcher (EV_A_ (W)ANHE_w (heap [i])); 2098 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
2042 } 2099 }
2043} 2100}
2044 2101
2045static void noinline 2102static void noinline ecb_cold
2046array_verify (EV_P_ W *ws, int cnt) 2103array_verify (EV_P_ W *ws, int cnt)
2047{ 2104{
2048 while (cnt--) 2105 while (cnt--)
2049 { 2106 {
2050 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 2107 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
2052 } 2109 }
2053} 2110}
2054#endif 2111#endif
2055 2112
2056#if EV_FEATURE_API 2113#if EV_FEATURE_API
2057void 2114void ecb_cold
2058ev_verify (EV_P) 2115ev_verify (EV_P)
2059{ 2116{
2060#if EV_VERIFY 2117#if EV_VERIFY
2061 int i; 2118 int i;
2062 WL w; 2119 WL w;
2128#endif 2185#endif
2129} 2186}
2130#endif 2187#endif
2131 2188
2132#if EV_MULTIPLICITY 2189#if EV_MULTIPLICITY
2133struct ev_loop * 2190struct ev_loop * ecb_cold
2134#else 2191#else
2135int 2192int
2136#endif 2193#endif
2137ev_default_loop (unsigned int flags) 2194ev_default_loop (unsigned int flags)
2138{ 2195{
2337 } 2394 }
2338} 2395}
2339 2396
2340/* simply recalculate all periodics */ 2397/* simply recalculate all periodics */
2341/* TODO: maybe ensure that at least one event happens when jumping forward? */ 2398/* TODO: maybe ensure that at least one event happens when jumping forward? */
2342static void noinline 2399static void noinline ecb_cold
2343periodics_reschedule (EV_P) 2400periodics_reschedule (EV_P)
2344{ 2401{
2345 int i; 2402 int i;
2346 2403
2347 /* adjust periodics after time jump */ 2404 /* adjust periodics after time jump */
2360 reheap (periodics, periodiccnt); 2417 reheap (periodics, periodiccnt);
2361} 2418}
2362#endif 2419#endif
2363 2420
2364/* adjust all timers by a given offset */ 2421/* adjust all timers by a given offset */
2365static void noinline 2422static void noinline ecb_cold
2366timers_reschedule (EV_P_ ev_tstamp adjust) 2423timers_reschedule (EV_P_ ev_tstamp adjust)
2367{ 2424{
2368 int i; 2425 int i;
2369 2426
2370 for (i = 0; i < timercnt; ++i) 2427 for (i = 0; i < timercnt; ++i)
3236 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3293 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3237 ofs += sizeof (struct inotify_event) + ev->len; 3294 ofs += sizeof (struct inotify_event) + ev->len;
3238 } 3295 }
3239} 3296}
3240 3297
3241inline_size void 3298inline_size void ecb_cold
3242ev_check_2625 (EV_P) 3299ev_check_2625 (EV_P)
3243{ 3300{
3244 /* kernels < 2.6.25 are borked 3301 /* kernels < 2.6.25 are borked
3245 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 3302 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
3246 */ 3303 */
3871} 3928}
3872 3929
3873/*****************************************************************************/ 3930/*****************************************************************************/
3874 3931
3875#if EV_WALK_ENABLE 3932#if EV_WALK_ENABLE
3876void 3933void ecb_cold
3877ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3934ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3878{ 3935{
3879 int i, j; 3936 int i, j;
3880 ev_watcher_list *wl, *wn; 3937 ev_watcher_list *wl, *wn;
3881 3938

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines