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

Comparing libev/ev.c (file contents):
Revision 1.424 by root, Tue May 1 22:01:40 2012 UTC vs.
Revision 1.449 by root, Sun Sep 23 21:21:58 2012 UTC

201# include <sys/wait.h> 201# include <sys/wait.h>
202# include <unistd.h> 202# include <unistd.h>
203#else 203#else
204# include <io.h> 204# include <io.h>
205# define WIN32_LEAN_AND_MEAN 205# define WIN32_LEAN_AND_MEAN
206# include <winsock2.h>
206# include <windows.h> 207# include <windows.h>
207# ifndef EV_SELECT_IS_WINSOCKET 208# ifndef EV_SELECT_IS_WINSOCKET
208# define EV_SELECT_IS_WINSOCKET 1 209# define EV_SELECT_IS_WINSOCKET 1
209# endif 210# endif
210# undef EV_AVOID_STDIO 211# undef EV_AVOID_STDIO
408/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 409/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
409# ifndef IN_DONT_FOLLOW 410# ifndef IN_DONT_FOLLOW
410# undef EV_USE_INOTIFY 411# undef EV_USE_INOTIFY
411# define EV_USE_INOTIFY 0 412# define EV_USE_INOTIFY 0
412# endif 413# endif
413#endif
414
415#if EV_SELECT_IS_WINSOCKET
416# include <winsock.h>
417#endif 414#endif
418 415
419#if EV_USE_EVENTFD 416#if EV_USE_EVENTFD
420/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 417/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
421# include <stdint.h> 418# include <stdint.h>
507 */ 504 */
508 505
509#ifndef ECB_H 506#ifndef ECB_H
510#define ECB_H 507#define ECB_H
511 508
509/* 16 bits major, 16 bits minor */
510#define ECB_VERSION 0x00010001
511
512#ifdef _WIN32 512#ifdef _WIN32
513 typedef signed char int8_t; 513 typedef signed char int8_t;
514 typedef unsigned char uint8_t; 514 typedef unsigned char uint8_t;
515 typedef signed short int16_t; 515 typedef signed short int16_t;
516 typedef unsigned short uint16_t; 516 typedef unsigned short uint16_t;
521 typedef unsigned long long uint64_t; 521 typedef unsigned long long uint64_t;
522 #else /* _MSC_VER || __BORLANDC__ */ 522 #else /* _MSC_VER || __BORLANDC__ */
523 typedef signed __int64 int64_t; 523 typedef signed __int64 int64_t;
524 typedef unsigned __int64 uint64_t; 524 typedef unsigned __int64 uint64_t;
525 #endif 525 #endif
526 #ifdef _WIN64
527 #define ECB_PTRSIZE 8
528 typedef uint64_t uintptr_t;
529 typedef int64_t intptr_t;
530 #else
531 #define ECB_PTRSIZE 4
532 typedef uint32_t uintptr_t;
533 typedef int32_t intptr_t;
534 #endif
535 typedef intptr_t ptrdiff_t;
526#else 536#else
527 #include <inttypes.h> 537 #include <inttypes.h>
538 #if UINTMAX_MAX > 0xffffffffU
539 #define ECB_PTRSIZE 8
540 #else
541 #define ECB_PTRSIZE 4
542 #endif
528#endif 543#endif
529 544
530/* many compilers define _GNUC_ to some versions but then only implement 545/* many compilers define _GNUC_ to some versions but then only implement
531 * what their idiot authors think are the "more important" extensions, 546 * what their idiot authors think are the "more important" extensions,
532 * causing enormous grief in return for some better fake benchmark numbers. 547 * causing enormous grief in return for some better fake benchmark numbers.
540 #else 555 #else
541 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 556 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
542 #endif 557 #endif
543#endif 558#endif
544 559
560#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
561#define ECB_C99 (__STDC_VERSION__ >= 199901L)
562#define ECB_C11 (__STDC_VERSION__ >= 201112L)
563#define ECB_CPP (__cplusplus+0)
564#define ECB_CPP11 (__cplusplus >= 201103L)
565
545/*****************************************************************************/ 566/*****************************************************************************/
546 567
547/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 568/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
548/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 569/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
549 570
550#if ECB_NO_THREADS 571#if ECB_NO_THREADS
551# define ECB_NO_SMP 1 572 #define ECB_NO_SMP 1
552#endif 573#endif
553 574
554#if ECB_NO_THREADS || ECB_NO_SMP 575#if ECB_NO_SMP
555 #define ECB_MEMORY_FENCE do { } while (0) 576 #define ECB_MEMORY_FENCE do { } while (0)
556#endif 577#endif
557 578
558#ifndef ECB_MEMORY_FENCE 579#ifndef ECB_MEMORY_FENCE
559 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 580 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
560 #if __i386 || __i386__ 581 #if __i386 || __i386__
561 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 582 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
562 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ 583 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
563 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 584 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
564 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ 585 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
565 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 586 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
566 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 587 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
567 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ 588 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
568 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 589 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
569 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 590 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
570 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ 591 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
571 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ 592 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
572 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 593 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
573 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ 594 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
574 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ 595 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
575 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 596 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
576 #elif __sparc || __sparc__ 597 #elif __sparc || __sparc__
577 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") 598 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
578 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") 599 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
579 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 600 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
580 #elif defined __s390__ || defined __s390x__ 601 #elif defined __s390__ || defined __s390x__
581 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 602 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
582 #elif defined __mips__ 603 #elif defined __mips__
583 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 604 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
584 #elif defined __alpha__ 605 #elif defined __alpha__
585 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") 606 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
607 #elif defined __hppa__
608 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
609 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
610 #elif defined __ia64__
611 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
586 #endif 612 #endif
587 #endif 613 #endif
588#endif 614#endif
589 615
590#ifndef ECB_MEMORY_FENCE 616#ifndef ECB_MEMORY_FENCE
617 #if ECB_GCC_VERSION(4,7)
618 /* see comment below (stdatomic.h) about the C11 memory model. */
619 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
620 #elif defined __clang && __has_feature (cxx_atomic)
621 /* see comment below (stdatomic.h) about the C11 memory model. */
622 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
591 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 623 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
592 #define ECB_MEMORY_FENCE __sync_synchronize () 624 #define ECB_MEMORY_FENCE __sync_synchronize ()
593 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
594 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
595 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 625 #elif _MSC_VER >= 1400 /* VC++ 2005 */
596 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 626 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
597 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 627 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
598 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 628 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
599 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 629 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
609 #define ECB_MEMORY_FENCE __sync () 639 #define ECB_MEMORY_FENCE __sync ()
610 #endif 640 #endif
611#endif 641#endif
612 642
613#ifndef ECB_MEMORY_FENCE 643#ifndef ECB_MEMORY_FENCE
644 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
645 /* we assume that these memory fences work on all variables/all memory accesses, */
646 /* not just C11 atomics and atomic accesses */
647 #include <stdatomic.h>
648 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
649 /* any fence other than seq_cst, which isn't very efficient for us. */
650 /* Why that is, we don't know - either the C11 memory model is quite useless */
651 /* for most usages, or gcc and clang have a bug */
652 /* I *currently* lean towards the latter, and inefficiently implement */
653 /* all three of ecb's fences as a seq_cst fence */
654 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
655 #endif
656#endif
657
658#ifndef ECB_MEMORY_FENCE
614 #if !ECB_AVOID_PTHREADS 659 #if !ECB_AVOID_PTHREADS
615 /* 660 /*
616 * if you get undefined symbol references to pthread_mutex_lock, 661 * if you get undefined symbol references to pthread_mutex_lock,
617 * or failure to find pthread.h, then you should implement 662 * or failure to find pthread.h, then you should implement
618 * the ECB_MEMORY_FENCE operations for your cpu/compiler 663 * the ECB_MEMORY_FENCE operations for your cpu/compiler
636 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 681 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
637#endif 682#endif
638 683
639/*****************************************************************************/ 684/*****************************************************************************/
640 685
641#define ECB_C99 (__STDC_VERSION__ >= 199901L)
642
643#if __cplusplus 686#if __cplusplus
644 #define ecb_inline static inline 687 #define ecb_inline static inline
645#elif ECB_GCC_VERSION(2,5) 688#elif ECB_GCC_VERSION(2,5)
646 #define ecb_inline static __inline__ 689 #define ecb_inline static __inline__
647#elif ECB_C99 690#elif ECB_C99
685#elif ECB_GCC_VERSION(3,0) 728#elif ECB_GCC_VERSION(3,0)
686 #define ecb_decltype(x) __typeof(x) 729 #define ecb_decltype(x) __typeof(x)
687#endif 730#endif
688 731
689#define ecb_noinline ecb_attribute ((__noinline__)) 732#define ecb_noinline ecb_attribute ((__noinline__))
690#define ecb_noreturn ecb_attribute ((__noreturn__))
691#define ecb_unused ecb_attribute ((__unused__)) 733#define ecb_unused ecb_attribute ((__unused__))
692#define ecb_const ecb_attribute ((__const__)) 734#define ecb_const ecb_attribute ((__const__))
693#define ecb_pure ecb_attribute ((__pure__)) 735#define ecb_pure ecb_attribute ((__pure__))
736
737#if ECB_C11
738 #define ecb_noreturn _Noreturn
739#else
740 #define ecb_noreturn ecb_attribute ((__noreturn__))
741#endif
694 742
695#if ECB_GCC_VERSION(4,3) 743#if ECB_GCC_VERSION(4,3)
696 #define ecb_artificial ecb_attribute ((__artificial__)) 744 #define ecb_artificial ecb_attribute ((__artificial__))
697 #define ecb_hot ecb_attribute ((__hot__)) 745 #define ecb_hot ecb_attribute ((__hot__))
698 #define ecb_cold ecb_attribute ((__cold__)) 746 #define ecb_cold ecb_attribute ((__cold__))
789 837
790 return r + ecb_ld32 (x); 838 return r + ecb_ld32 (x);
791 } 839 }
792#endif 840#endif
793 841
842ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
843ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
844ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
845ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
846
794ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; 847ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
795ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) 848ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
796{ 849{
797 return ( (x * 0x0802U & 0x22110U) 850 return ( (x * 0x0802U & 0x22110U)
798 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; 851 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
1108#endif 1161#endif
1109 1162
1110static void (*syserr_cb)(const char *msg) EV_THROW; 1163static void (*syserr_cb)(const char *msg) EV_THROW;
1111 1164
1112void ecb_cold 1165void ecb_cold
1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW 1166ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1114{ 1167{
1115 syserr_cb = cb; 1168 syserr_cb = cb;
1116} 1169}
1117 1170
1118static void noinline ecb_cold 1171static void noinline ecb_cold
1136 abort (); 1189 abort ();
1137 } 1190 }
1138} 1191}
1139 1192
1140static void * 1193static void *
1141ev_realloc_emul (void *ptr, long size) 1194ev_realloc_emul (void *ptr, long size) EV_THROW
1142{ 1195{
1143#if __GLIBC__
1144 return realloc (ptr, size);
1145#else
1146 /* some systems, notably openbsd and darwin, fail to properly 1196 /* some systems, notably openbsd and darwin, fail to properly
1147 * implement realloc (x, 0) (as required by both ansi c-89 and 1197 * implement realloc (x, 0) (as required by both ansi c-89 and
1148 * the single unix specification, so work around them here. 1198 * the single unix specification, so work around them here.
1199 * recently, also (at least) fedora and debian started breaking it,
1200 * despite documenting it otherwise.
1149 */ 1201 */
1150 1202
1151 if (size) 1203 if (size)
1152 return realloc (ptr, size); 1204 return realloc (ptr, size);
1153 1205
1154 free (ptr); 1206 free (ptr);
1155 return 0; 1207 return 0;
1156#endif
1157} 1208}
1158 1209
1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; 1210static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1160 1211
1161void ecb_cold 1212void ecb_cold
1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW 1213ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1163{ 1214{
1164 alloc = cb; 1215 alloc = cb;
1165} 1216}
1166 1217
1167inline_speed void * 1218inline_speed void *
1430 w_->pending = ++pendingcnt [pri]; 1481 w_->pending = ++pendingcnt [pri];
1431 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1482 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1432 pendings [pri][w_->pending - 1].w = w_; 1483 pendings [pri][w_->pending - 1].w = w_;
1433 pendings [pri][w_->pending - 1].events = revents; 1484 pendings [pri][w_->pending - 1].events = revents;
1434 } 1485 }
1486
1487 pendingpri = NUMPRI - 1;
1435} 1488}
1436 1489
1437inline_speed void 1490inline_speed void
1438feed_reverse (EV_P_ W w) 1491feed_reverse (EV_P_ W w)
1439{ 1492{
1814static void noinline ecb_cold 1867static void noinline ecb_cold
1815evpipe_init (EV_P) 1868evpipe_init (EV_P)
1816{ 1869{
1817 if (!ev_is_active (&pipe_w)) 1870 if (!ev_is_active (&pipe_w))
1818 { 1871 {
1872 int fds [2];
1873
1819# if EV_USE_EVENTFD 1874# if EV_USE_EVENTFD
1875 fds [0] = -1;
1820 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1876 fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1821 if (evfd < 0 && errno == EINVAL) 1877 if (fds [1] < 0 && errno == EINVAL)
1822 evfd = eventfd (0, 0); 1878 fds [1] = eventfd (0, 0);
1823 1879
1824 if (evfd >= 0) 1880 if (fds [1] < 0)
1881# endif
1825 { 1882 {
1883 while (pipe (fds))
1884 ev_syserr ("(libev) error creating signal/async pipe");
1885
1886 fd_intern (fds [0]);
1887 }
1888
1889 fd_intern (fds [1]);
1890
1826 evpipe [0] = -1; 1891 evpipe [0] = fds [0];
1827 fd_intern (evfd); /* doing it twice doesn't hurt */ 1892
1828 ev_io_set (&pipe_w, evfd, EV_READ); 1893 if (evpipe [1] < 0)
1894 evpipe [1] = fds [1]; /* first call, set write fd */
1895 else
1896 {
1897 /* on subsequent calls, do not change evpipe [1] */
1898 /* so that evpipe_write can always rely on its value. */
1899 /* this branch does not do anything sensible on windows, */
1900 /* so must not be executed on windows */
1901
1902 dup2 (fds [1], evpipe [1]);
1903 close (fds [1]);
1904 }
1905
1906 ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
1907 ev_io_start (EV_A_ &pipe_w);
1908 ev_unref (EV_A); /* watcher should not keep loop alive */
1909 }
1910}
1911
1912inline_speed void
1913evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1914{
1915 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1916
1917 if (expect_true (*flag))
1918 return;
1919
1920 *flag = 1;
1921 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1922
1923 pipe_write_skipped = 1;
1924
1925 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1926
1927 if (pipe_write_wanted)
1928 {
1929 int old_errno;
1930
1931 pipe_write_skipped = 0;
1932 ECB_MEMORY_FENCE_RELEASE;
1933
1934 old_errno = errno; /* save errno because write will clobber it */
1935
1936#if EV_USE_EVENTFD
1937 if (evpipe [0] < 0)
1938 {
1939 uint64_t counter = 1;
1940 write (evpipe [1], &counter, sizeof (uint64_t));
1829 } 1941 }
1830 else 1942 else
1831# endif 1943#endif
1832 { 1944 {
1833 while (pipe (evpipe)) 1945#ifdef _WIN32
1834 ev_syserr ("(libev) error creating signal/async pipe"); 1946 WSABUF buf;
1835 1947 DWORD sent;
1836 fd_intern (evpipe [0]); 1948 buf.buf = &buf;
1837 fd_intern (evpipe [1]); 1949 buf.len = 1;
1838 ev_io_set (&pipe_w, evpipe [0], EV_READ); 1950 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1839 } 1951#else
1840
1841 ev_io_start (EV_A_ &pipe_w);
1842 ev_unref (EV_A); /* watcher should not keep loop alive */
1843 }
1844}
1845
1846inline_speed void
1847evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1848{
1849 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1850
1851 if (expect_true (*flag))
1852 return;
1853
1854 *flag = 1;
1855
1856 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1857
1858 pipe_write_skipped = 1;
1859
1860 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1861
1862 if (pipe_write_wanted)
1863 {
1864 int old_errno;
1865
1866 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1867
1868 old_errno = errno; /* save errno because write will clobber it */
1869
1870#if EV_USE_EVENTFD
1871 if (evfd >= 0)
1872 {
1873 uint64_t counter = 1;
1874 write (evfd, &counter, sizeof (uint64_t));
1875 }
1876 else
1877#endif
1878 {
1879 /* win32 people keep sending patches that change this write() to send() */
1880 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1881 /* so when you think this write should be a send instead, please find out */
1882 /* where your send() is from - it's definitely not the microsoft send, and */
1883 /* tell me. thank you. */
1884 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1885 /* check the ev documentation on how to use this flag */
1886 write (evpipe [1], &(evpipe [1]), 1); 1952 write (evpipe [1], &(evpipe [1]), 1);
1953#endif
1887 } 1954 }
1888 1955
1889 errno = old_errno; 1956 errno = old_errno;
1890 } 1957 }
1891} 1958}
1898 int i; 1965 int i;
1899 1966
1900 if (revents & EV_READ) 1967 if (revents & EV_READ)
1901 { 1968 {
1902#if EV_USE_EVENTFD 1969#if EV_USE_EVENTFD
1903 if (evfd >= 0) 1970 if (evpipe [0] < 0)
1904 { 1971 {
1905 uint64_t counter; 1972 uint64_t counter;
1906 read (evfd, &counter, sizeof (uint64_t)); 1973 read (evpipe [1], &counter, sizeof (uint64_t));
1907 } 1974 }
1908 else 1975 else
1909#endif 1976#endif
1910 { 1977 {
1911 char dummy; 1978 char dummy[4];
1912 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1979#ifdef _WIN32
1980 WSABUF buf;
1981 DWORD recvd;
1982 DWORD flags = 0;
1983 buf.buf = dummy;
1984 buf.len = sizeof (dummy);
1985 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
1986#else
1913 read (evpipe [0], &dummy, 1); 1987 read (evpipe [0], &dummy, sizeof (dummy));
1988#endif
1914 } 1989 }
1915 } 1990 }
1916 1991
1917 pipe_write_skipped = 0; 1992 pipe_write_skipped = 0;
1918 1993
1921#if EV_SIGNAL_ENABLE 1996#if EV_SIGNAL_ENABLE
1922 if (sig_pending) 1997 if (sig_pending)
1923 { 1998 {
1924 sig_pending = 0; 1999 sig_pending = 0;
1925 2000
1926 ECB_MEMORY_FENCE_RELEASE; 2001 ECB_MEMORY_FENCE;
1927 2002
1928 for (i = EV_NSIG - 1; i--; ) 2003 for (i = EV_NSIG - 1; i--; )
1929 if (expect_false (signals [i].pending)) 2004 if (expect_false (signals [i].pending))
1930 ev_feed_signal_event (EV_A_ i + 1); 2005 ev_feed_signal_event (EV_A_ i + 1);
1931 } 2006 }
1934#if EV_ASYNC_ENABLE 2009#if EV_ASYNC_ENABLE
1935 if (async_pending) 2010 if (async_pending)
1936 { 2011 {
1937 async_pending = 0; 2012 async_pending = 0;
1938 2013
1939 ECB_MEMORY_FENCE_RELEASE; 2014 ECB_MEMORY_FENCE;
1940 2015
1941 for (i = asynccnt; i--; ) 2016 for (i = asynccnt; i--; )
1942 if (asyncs [i]->sent) 2017 if (asyncs [i]->sent)
1943 { 2018 {
1944 asyncs [i]->sent = 0; 2019 asyncs [i]->sent = 0;
2020 ECB_MEMORY_FENCE_RELEASE;
1945 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2021 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1946 } 2022 }
1947 } 2023 }
1948#endif 2024#endif
1949} 2025}
1952 2028
1953void 2029void
1954ev_feed_signal (int signum) EV_THROW 2030ev_feed_signal (int signum) EV_THROW
1955{ 2031{
1956#if EV_MULTIPLICITY 2032#if EV_MULTIPLICITY
2033 ECB_MEMORY_FENCE_ACQUIRE;
1957 EV_P = signals [signum - 1].loop; 2034 EV_P = signals [signum - 1].loop;
1958 2035
1959 if (!EV_A) 2036 if (!EV_A)
1960 return; 2037 return;
1961#endif 2038#endif
1962 2039
1963 if (!ev_active (&pipe_w))
1964 return;
1965
1966 signals [signum - 1].pending = 1; 2040 signals [signum - 1].pending = 1;
1967 evpipe_write (EV_A_ &sig_pending); 2041 evpipe_write (EV_A_ &sig_pending);
1968} 2042}
1969 2043
1970static void 2044static void
1980void noinline 2054void noinline
1981ev_feed_signal_event (EV_P_ int signum) EV_THROW 2055ev_feed_signal_event (EV_P_ int signum) EV_THROW
1982{ 2056{
1983 WL w; 2057 WL w;
1984 2058
1985 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2059 if (expect_false (signum <= 0 || signum >= EV_NSIG))
1986 return; 2060 return;
1987 2061
1988 --signum; 2062 --signum;
1989 2063
1990#if EV_MULTIPLICITY 2064#if EV_MULTIPLICITY
1994 if (expect_false (signals [signum].loop != EV_A)) 2068 if (expect_false (signals [signum].loop != EV_A))
1995 return; 2069 return;
1996#endif 2070#endif
1997 2071
1998 signals [signum].pending = 0; 2072 signals [signum].pending = 0;
2073 ECB_MEMORY_FENCE_RELEASE;
1999 2074
2000 for (w = signals [signum].head; w; w = w->next) 2075 for (w = signals [signum].head; w; w = w->next)
2001 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2076 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
2002} 2077}
2003 2078
2287#if EV_ASYNC_ENABLE 2362#if EV_ASYNC_ENABLE
2288 async_pending = 0; 2363 async_pending = 0;
2289#endif 2364#endif
2290 pipe_write_skipped = 0; 2365 pipe_write_skipped = 0;
2291 pipe_write_wanted = 0; 2366 pipe_write_wanted = 0;
2367 evpipe [0] = -1;
2368 evpipe [1] = -1;
2292#if EV_USE_INOTIFY 2369#if EV_USE_INOTIFY
2293 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 2370 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
2294#endif 2371#endif
2295#if EV_USE_SIGNALFD 2372#if EV_USE_SIGNALFD
2296 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 2373 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
2347 EV_INVOKE_PENDING; 2424 EV_INVOKE_PENDING;
2348 } 2425 }
2349#endif 2426#endif
2350 2427
2351#if EV_CHILD_ENABLE 2428#if EV_CHILD_ENABLE
2352 if (ev_is_active (&childev)) 2429 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2353 { 2430 {
2354 ev_ref (EV_A); /* child watcher */ 2431 ev_ref (EV_A); /* child watcher */
2355 ev_signal_stop (EV_A_ &childev); 2432 ev_signal_stop (EV_A_ &childev);
2356 } 2433 }
2357#endif 2434#endif
2359 if (ev_is_active (&pipe_w)) 2436 if (ev_is_active (&pipe_w))
2360 { 2437 {
2361 /*ev_ref (EV_A);*/ 2438 /*ev_ref (EV_A);*/
2362 /*ev_io_stop (EV_A_ &pipe_w);*/ 2439 /*ev_io_stop (EV_A_ &pipe_w);*/
2363 2440
2364#if EV_USE_EVENTFD
2365 if (evfd >= 0)
2366 close (evfd);
2367#endif
2368
2369 if (evpipe [0] >= 0)
2370 {
2371 EV_WIN32_CLOSE_FD (evpipe [0]); 2441 if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
2372 EV_WIN32_CLOSE_FD (evpipe [1]); 2442 if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
2373 }
2374 } 2443 }
2375 2444
2376#if EV_USE_SIGNALFD 2445#if EV_USE_SIGNALFD
2377 if (ev_is_active (&sigfd_w)) 2446 if (ev_is_active (&sigfd_w))
2378 close (sigfd); 2447 close (sigfd);
2464#endif 2533#endif
2465#if EV_USE_INOTIFY 2534#if EV_USE_INOTIFY
2466 infy_fork (EV_A); 2535 infy_fork (EV_A);
2467#endif 2536#endif
2468 2537
2538#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2469 if (ev_is_active (&pipe_w)) 2539 if (ev_is_active (&pipe_w))
2470 { 2540 {
2471 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ 2541 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
2472 2542
2473 ev_ref (EV_A); 2543 ev_ref (EV_A);
2474 ev_io_stop (EV_A_ &pipe_w); 2544 ev_io_stop (EV_A_ &pipe_w);
2475 2545
2476#if EV_USE_EVENTFD
2477 if (evfd >= 0)
2478 close (evfd);
2479#endif
2480
2481 if (evpipe [0] >= 0) 2546 if (evpipe [0] >= 0)
2482 {
2483 EV_WIN32_CLOSE_FD (evpipe [0]); 2547 EV_WIN32_CLOSE_FD (evpipe [0]);
2484 EV_WIN32_CLOSE_FD (evpipe [1]);
2485 }
2486 2548
2487#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2488 evpipe_init (EV_A); 2549 evpipe_init (EV_A);
2489 /* now iterate over everything, in case we missed something */ 2550 /* iterate over everything, in case we missed something before */
2490 pipecb (EV_A_ &pipe_w, EV_READ); 2551 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2491#endif
2492 } 2552 }
2553#endif
2493 2554
2494 postfork = 0; 2555 postfork = 0;
2495} 2556}
2496 2557
2497#if EV_MULTIPLICITY 2558#if EV_MULTIPLICITY
2553void ecb_cold 2614void ecb_cold
2554ev_verify (EV_P) EV_THROW 2615ev_verify (EV_P) EV_THROW
2555{ 2616{
2556#if EV_VERIFY 2617#if EV_VERIFY
2557 int i; 2618 int i;
2558 WL w; 2619 WL w, w2;
2559 2620
2560 assert (activecnt >= -1); 2621 assert (activecnt >= -1);
2561 2622
2562 assert (fdchangemax >= fdchangecnt); 2623 assert (fdchangemax >= fdchangecnt);
2563 for (i = 0; i < fdchangecnt; ++i) 2624 for (i = 0; i < fdchangecnt; ++i)
2564 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2625 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2565 2626
2566 assert (anfdmax >= 0); 2627 assert (anfdmax >= 0);
2567 for (i = 0; i < anfdmax; ++i) 2628 for (i = 0; i < anfdmax; ++i)
2629 {
2630 int j = 0;
2631
2568 for (w = anfds [i].head; w; w = w->next) 2632 for (w = w2 = anfds [i].head; w; w = w->next)
2569 { 2633 {
2570 verify_watcher (EV_A_ (W)w); 2634 verify_watcher (EV_A_ (W)w);
2635
2636 if (j++ & 1)
2637 {
2638 assert (("libev: io watcher list contains a loop", w != w2));
2639 w2 = w2->next;
2640 }
2641
2571 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2642 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2572 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2643 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2573 } 2644 }
2645 }
2574 2646
2575 assert (timermax >= timercnt); 2647 assert (timermax >= timercnt);
2576 verify_heap (EV_A_ timers, timercnt); 2648 verify_heap (EV_A_ timers, timercnt);
2577 2649
2578#if EV_PERIODIC_ENABLE 2650#if EV_PERIODIC_ENABLE
2659} 2731}
2660 2732
2661void 2733void
2662ev_loop_fork (EV_P) EV_THROW 2734ev_loop_fork (EV_P) EV_THROW
2663{ 2735{
2664 postfork = 1; /* must be in line with ev_default_fork */ 2736 postfork = 1;
2665} 2737}
2666 2738
2667/*****************************************************************************/ 2739/*****************************************************************************/
2668 2740
2669void 2741void
2685} 2757}
2686 2758
2687void noinline 2759void noinline
2688ev_invoke_pending (EV_P) 2760ev_invoke_pending (EV_P)
2689{ 2761{
2690 int pri; 2762 pendingpri = NUMPRI;
2691 2763
2692 for (pri = NUMPRI; pri--; ) 2764 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2765 {
2766 --pendingpri;
2767
2693 while (pendingcnt [pri]) 2768 while (pendingcnt [pendingpri])
2694 { 2769 {
2695 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2770 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2696 2771
2697 p->w->pending = 0; 2772 p->w->pending = 0;
2698 EV_CB_INVOKE (p->w, p->events); 2773 EV_CB_INVOKE (p->w, p->events);
2699 EV_FREQUENT_CHECK; 2774 EV_FREQUENT_CHECK;
2700 } 2775 }
2776 }
2701} 2777}
2702 2778
2703#if EV_IDLE_ENABLE 2779#if EV_IDLE_ENABLE
2704/* make idle watchers pending. this handles the "call-idle */ 2780/* make idle watchers pending. this handles the "call-idle */
2705/* only when higher priorities are idle" logic */ 2781/* only when higher priorities are idle" logic */
2795{ 2871{
2796 EV_FREQUENT_CHECK; 2872 EV_FREQUENT_CHECK;
2797 2873
2798 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 2874 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2799 { 2875 {
2800 int feed_count = 0;
2801
2802 do 2876 do
2803 { 2877 {
2804 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 2878 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2805 2879
2806 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 2880 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
3065 backend_poll (EV_A_ waittime); 3139 backend_poll (EV_A_ waittime);
3066 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3140 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3067 3141
3068 pipe_write_wanted = 0; /* just an optimisation, no fence needed */ 3142 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3069 3143
3144 ECB_MEMORY_FENCE_ACQUIRE;
3070 if (pipe_write_skipped) 3145 if (pipe_write_skipped)
3071 { 3146 {
3072 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3147 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3073 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3148 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3074 } 3149 }
3249 EV_FREQUENT_CHECK; 3324 EV_FREQUENT_CHECK;
3250 3325
3251 ev_start (EV_A_ (W)w, 1); 3326 ev_start (EV_A_ (W)w, 1);
3252 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3327 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3253 wlist_add (&anfds[fd].head, (WL)w); 3328 wlist_add (&anfds[fd].head, (WL)w);
3329
3330 /* common bug, apparently */
3331 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3254 3332
3255 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3333 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3256 w->events &= ~EV__IOFDSET; 3334 w->events &= ~EV__IOFDSET;
3257 3335
3258 EV_FREQUENT_CHECK; 3336 EV_FREQUENT_CHECK;
3449#if EV_MULTIPLICITY 3527#if EV_MULTIPLICITY
3450 assert (("libev: a signal must not be attached to two different loops", 3528 assert (("libev: a signal must not be attached to two different loops",
3451 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 3529 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
3452 3530
3453 signals [w->signum - 1].loop = EV_A; 3531 signals [w->signum - 1].loop = EV_A;
3532 ECB_MEMORY_FENCE_RELEASE;
3454#endif 3533#endif
3455 3534
3456 EV_FREQUENT_CHECK; 3535 EV_FREQUENT_CHECK;
3457 3536
3458#if EV_USE_SIGNALFD 3537#if EV_USE_SIGNALFD

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines