… | |
… | |
505 | |
505 | |
506 | #ifndef ECB_H |
506 | #ifndef ECB_H |
507 | #define ECB_H |
507 | #define ECB_H |
508 | |
508 | |
509 | /* 16 bits major, 16 bits minor */ |
509 | /* 16 bits major, 16 bits minor */ |
510 | #define ECB_VERSION 0x00010001 |
510 | #define ECB_VERSION 0x00010002 |
511 | |
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; |
… | |
… | |
530 | #else |
530 | #else |
531 | #define ECB_PTRSIZE 4 |
531 | #define ECB_PTRSIZE 4 |
532 | typedef uint32_t uintptr_t; |
532 | typedef uint32_t uintptr_t; |
533 | typedef int32_t intptr_t; |
533 | typedef int32_t intptr_t; |
534 | #endif |
534 | #endif |
535 | typedef intptr_t ptrdiff_t; |
|
|
536 | #else |
535 | #else |
537 | #include <inttypes.h> |
536 | #include <inttypes.h> |
538 | #if UINTMAX_MAX > 0xffffffffU |
537 | #if UINTMAX_MAX > 0xffffffffU |
539 | #define ECB_PTRSIZE 8 |
538 | #define ECB_PTRSIZE 8 |
540 | #else |
539 | #else |
… | |
… | |
559 | |
558 | |
560 | #define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ |
559 | #define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ |
561 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
560 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
562 | #define ECB_C11 (__STDC_VERSION__ >= 201112L) |
561 | #define ECB_C11 (__STDC_VERSION__ >= 201112L) |
563 | #define ECB_CPP (__cplusplus+0) |
562 | #define ECB_CPP (__cplusplus+0) |
564 | #define ECB_CPP98 (__cplusplus >= 199711L) |
|
|
565 | #define ECB_CPP11 (__cplusplus >= 201103L) |
563 | #define ECB_CPP11 (__cplusplus >= 201103L) |
|
|
564 | |
|
|
565 | #if ECB_CPP |
|
|
566 | #define ECB_EXTERN_C extern "C" |
|
|
567 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
|
|
568 | #define ECB_EXTERN_C_END } |
|
|
569 | #else |
|
|
570 | #define ECB_EXTERN_C extern |
|
|
571 | #define ECB_EXTERN_C_BEG |
|
|
572 | #define ECB_EXTERN_C_END |
|
|
573 | #endif |
566 | |
574 | |
567 | /*****************************************************************************/ |
575 | /*****************************************************************************/ |
568 | |
576 | |
569 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
577 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
570 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
578 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
… | |
… | |
614 | #endif |
622 | #endif |
615 | #endif |
623 | #endif |
616 | |
624 | |
617 | #ifndef ECB_MEMORY_FENCE |
625 | #ifndef ECB_MEMORY_FENCE |
618 | #if ECB_GCC_VERSION(4,7) |
626 | #if ECB_GCC_VERSION(4,7) |
619 | /* see comment below about the C11 memory model. in short - avoid */ |
627 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
620 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
628 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
|
|
629 | |
|
|
630 | /* The __has_feature syntax from clang is so misdesigned that we cannot use it |
|
|
631 | * without risking compile time errors with other compilers. We *could* |
|
|
632 | * define our own ecb_clang_has_feature, but I just can't be bothered to work |
|
|
633 | * around this shit time and again. |
621 | #elif defined __clang && __has_feature (cxx_atomic) |
634 | * #elif defined __clang && __has_feature (cxx_atomic) |
622 | /* see above */ |
635 | * // see comment below (stdatomic.h) about the C11 memory model. |
623 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
636 | * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
|
|
637 | */ |
|
|
638 | |
624 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
639 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
625 | #define ECB_MEMORY_FENCE __sync_synchronize () |
640 | #define ECB_MEMORY_FENCE __sync_synchronize () |
626 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
641 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
627 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
642 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
628 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
643 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
… | |
… | |
644 | #ifndef ECB_MEMORY_FENCE |
659 | #ifndef ECB_MEMORY_FENCE |
645 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
660 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
646 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
661 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
647 | /* not just C11 atomics and atomic accesses */ |
662 | /* not just C11 atomics and atomic accesses */ |
648 | #include <stdatomic.h> |
663 | #include <stdatomic.h> |
649 | /* unfortunately, the C11 memory model seems to be very limited, and unable to express */ |
664 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
650 | /* simple barrier semantics. That means we need to take out thor's hammer. */ |
665 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
666 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
667 | /* for most usages, or gcc and clang have a bug */ |
|
|
668 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
669 | /* all three of ecb's fences as a seq_cst fence */ |
651 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
670 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
652 | #endif |
671 | #endif |
653 | #endif |
672 | #endif |
654 | |
673 | |
655 | #ifndef ECB_MEMORY_FENCE |
674 | #ifndef ECB_MEMORY_FENCE |
… | |
… | |
932 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
951 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
933 | ecb_inline void ecb_unreachable (void) { } |
952 | ecb_inline void ecb_unreachable (void) { } |
934 | #endif |
953 | #endif |
935 | |
954 | |
936 | /* try to tell the compiler that some condition is definitely true */ |
955 | /* try to tell the compiler that some condition is definitely true */ |
937 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
956 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
938 | |
957 | |
939 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
958 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
940 | ecb_inline unsigned char |
959 | ecb_inline unsigned char |
941 | ecb_byteorder_helper (void) |
960 | ecb_byteorder_helper (void) |
942 | { |
961 | { |
943 | const uint32_t u = 0x11223344; |
962 | /* the union code still generates code under pressure in gcc, */ |
944 | return *(unsigned char *)&u; |
963 | /* but less than using pointers, and always seems to */ |
|
|
964 | /* successfully return a constant. */ |
|
|
965 | /* the reason why we have this horrible preprocessor mess */ |
|
|
966 | /* is to avoid it in all cases, at least on common architectures */ |
|
|
967 | /* or when using a recent enough gcc version (>= 4.6) */ |
|
|
968 | #if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64 |
|
|
969 | return 0x44; |
|
|
970 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
|
|
971 | return 0x44; |
|
|
972 | #elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
|
|
973 | return 0x11; |
|
|
974 | #else |
|
|
975 | union |
|
|
976 | { |
|
|
977 | uint32_t i; |
|
|
978 | uint8_t c; |
|
|
979 | } u = { 0x11223344 }; |
|
|
980 | return u.c; |
|
|
981 | #endif |
945 | } |
982 | } |
946 | |
983 | |
947 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
984 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
948 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
985 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
949 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
986 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
… | |
… | |
980 | } |
1017 | } |
981 | #else |
1018 | #else |
982 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
1019 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
983 | #endif |
1020 | #endif |
984 | |
1021 | |
|
|
1022 | /*******************************************************************************/ |
|
|
1023 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
|
|
1024 | |
|
|
1025 | /* basically, everything uses "ieee pure-endian" floating point numbers */ |
|
|
1026 | /* the only noteworthy exception is ancient armle, which uses order 43218765 */ |
|
|
1027 | #if 0 \ |
|
|
1028 | || __i386 || __i386__ \ |
|
|
1029 | || __amd64 || __amd64__ || __x86_64 || __x86_64__ \ |
|
|
1030 | || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \ |
|
|
1031 | || defined __arm__ && defined __ARM_EABI__ \ |
|
|
1032 | || defined __s390__ || defined __s390x__ \ |
|
|
1033 | || defined __mips__ \ |
|
|
1034 | || defined __alpha__ \ |
|
|
1035 | || defined __hppa__ \ |
|
|
1036 | || defined __ia64__ \ |
|
|
1037 | || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64 |
|
|
1038 | #define ECB_STDFP 1 |
|
|
1039 | #include <string.h> /* for memcpy */ |
|
|
1040 | #else |
|
|
1041 | #define ECB_STDFP 0 |
|
|
1042 | #include <math.h> /* for frexp*, ldexp* */ |
|
|
1043 | #endif |
|
|
1044 | |
|
|
1045 | #ifndef ECB_NO_LIBM |
|
|
1046 | |
|
|
1047 | /* convert a float to ieee single/binary32 */ |
|
|
1048 | ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; |
|
|
1049 | ecb_function_ uint32_t |
|
|
1050 | ecb_float_to_binary32 (float x) |
|
|
1051 | { |
|
|
1052 | uint32_t r; |
|
|
1053 | |
|
|
1054 | #if ECB_STDFP |
|
|
1055 | memcpy (&r, &x, 4); |
|
|
1056 | #else |
|
|
1057 | /* slow emulation, works for anything but -0 */ |
|
|
1058 | uint32_t m; |
|
|
1059 | int e; |
|
|
1060 | |
|
|
1061 | if (x == 0e0f ) return 0x00000000U; |
|
|
1062 | if (x > +3.40282346638528860e+38f) return 0x7f800000U; |
|
|
1063 | if (x < -3.40282346638528860e+38f) return 0xff800000U; |
|
|
1064 | if (x != x ) return 0x7fbfffffU; |
|
|
1065 | |
|
|
1066 | m = frexpf (x, &e) * 0x1000000U; |
|
|
1067 | |
|
|
1068 | r = m & 0x80000000U; |
|
|
1069 | |
|
|
1070 | if (r) |
|
|
1071 | m = -m; |
|
|
1072 | |
|
|
1073 | if (e <= -126) |
|
|
1074 | { |
|
|
1075 | m &= 0xffffffU; |
|
|
1076 | m >>= (-125 - e); |
|
|
1077 | e = -126; |
|
|
1078 | } |
|
|
1079 | |
|
|
1080 | r |= (e + 126) << 23; |
|
|
1081 | r |= m & 0x7fffffU; |
|
|
1082 | #endif |
|
|
1083 | |
|
|
1084 | return r; |
|
|
1085 | } |
|
|
1086 | |
|
|
1087 | /* converts an ieee single/binary32 to a float */ |
|
|
1088 | ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const; |
|
|
1089 | ecb_function_ float |
|
|
1090 | ecb_binary32_to_float (uint32_t x) |
|
|
1091 | { |
|
|
1092 | float r; |
|
|
1093 | |
|
|
1094 | #if ECB_STDFP |
|
|
1095 | memcpy (&r, &x, 4); |
|
|
1096 | #else |
|
|
1097 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
1098 | int neg = x >> 31; |
|
|
1099 | int e = (x >> 23) & 0xffU; |
|
|
1100 | |
|
|
1101 | x &= 0x7fffffU; |
|
|
1102 | |
|
|
1103 | if (e) |
|
|
1104 | x |= 0x800000U; |
|
|
1105 | else |
|
|
1106 | e = 1; |
|
|
1107 | |
|
|
1108 | /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */ |
|
|
1109 | r = ldexpf (x * (0.5f / 0x800000U), e - 126); |
|
|
1110 | |
|
|
1111 | r = neg ? -r : r; |
|
|
1112 | #endif |
|
|
1113 | |
|
|
1114 | return r; |
|
|
1115 | } |
|
|
1116 | |
|
|
1117 | /* convert a double to ieee double/binary64 */ |
|
|
1118 | ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const; |
|
|
1119 | ecb_function_ uint64_t |
|
|
1120 | ecb_double_to_binary64 (double x) |
|
|
1121 | { |
|
|
1122 | uint64_t r; |
|
|
1123 | |
|
|
1124 | #if ECB_STDFP |
|
|
1125 | memcpy (&r, &x, 8); |
|
|
1126 | #else |
|
|
1127 | /* slow emulation, works for anything but -0 */ |
|
|
1128 | uint64_t m; |
|
|
1129 | int e; |
|
|
1130 | |
|
|
1131 | if (x == 0e0 ) return 0x0000000000000000U; |
|
|
1132 | if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U; |
|
|
1133 | if (x < -1.79769313486231470e+308) return 0xfff0000000000000U; |
|
|
1134 | if (x != x ) return 0X7ff7ffffffffffffU; |
|
|
1135 | |
|
|
1136 | m = frexp (x, &e) * 0x20000000000000U; |
|
|
1137 | |
|
|
1138 | r = m & 0x8000000000000000;; |
|
|
1139 | |
|
|
1140 | if (r) |
|
|
1141 | m = -m; |
|
|
1142 | |
|
|
1143 | if (e <= -1022) |
|
|
1144 | { |
|
|
1145 | m &= 0x1fffffffffffffU; |
|
|
1146 | m >>= (-1021 - e); |
|
|
1147 | e = -1022; |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | r |= ((uint64_t)(e + 1022)) << 52; |
|
|
1151 | r |= m & 0xfffffffffffffU; |
|
|
1152 | #endif |
|
|
1153 | |
|
|
1154 | return r; |
|
|
1155 | } |
|
|
1156 | |
|
|
1157 | /* converts an ieee double/binary64 to a double */ |
|
|
1158 | ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const; |
|
|
1159 | ecb_function_ double |
|
|
1160 | ecb_binary64_to_double (uint64_t x) |
|
|
1161 | { |
|
|
1162 | double r; |
|
|
1163 | |
|
|
1164 | #if ECB_STDFP |
|
|
1165 | memcpy (&r, &x, 8); |
|
|
1166 | #else |
|
|
1167 | /* emulation, only works for normals and subnormals and +0 */ |
|
|
1168 | int neg = x >> 63; |
|
|
1169 | int e = (x >> 52) & 0x7ffU; |
|
|
1170 | |
|
|
1171 | x &= 0xfffffffffffffU; |
|
|
1172 | |
|
|
1173 | if (e) |
|
|
1174 | x |= 0x10000000000000U; |
|
|
1175 | else |
|
|
1176 | e = 1; |
|
|
1177 | |
|
|
1178 | /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */ |
|
|
1179 | r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022); |
|
|
1180 | |
|
|
1181 | r = neg ? -r : r; |
|
|
1182 | #endif |
|
|
1183 | |
|
|
1184 | return r; |
|
|
1185 | } |
|
|
1186 | |
|
|
1187 | #endif |
|
|
1188 | |
985 | #endif |
1189 | #endif |
986 | |
1190 | |
987 | /* ECB.H END */ |
1191 | /* ECB.H END */ |
988 | |
1192 | |
989 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
1193 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
… | |
… | |
1188 | } |
1392 | } |
1189 | |
1393 | |
1190 | static void * |
1394 | static void * |
1191 | ev_realloc_emul (void *ptr, long size) EV_THROW |
1395 | ev_realloc_emul (void *ptr, long size) EV_THROW |
1192 | { |
1396 | { |
1193 | #if __GLIBC__ |
|
|
1194 | return realloc (ptr, size); |
|
|
1195 | #else |
|
|
1196 | /* some systems, notably openbsd and darwin, fail to properly |
1397 | /* some systems, notably openbsd and darwin, fail to properly |
1197 | * implement realloc (x, 0) (as required by both ansi c-89 and |
1398 | * implement realloc (x, 0) (as required by both ansi c-89 and |
1198 | * the single unix specification, so work around them here. |
1399 | * the single unix specification, so work around them here. |
|
|
1400 | * recently, also (at least) fedora and debian started breaking it, |
|
|
1401 | * despite documenting it otherwise. |
1199 | */ |
1402 | */ |
1200 | |
1403 | |
1201 | if (size) |
1404 | if (size) |
1202 | return realloc (ptr, size); |
1405 | return realloc (ptr, size); |
1203 | |
1406 | |
1204 | free (ptr); |
1407 | free (ptr); |
1205 | return 0; |
1408 | return 0; |
1206 | #endif |
|
|
1207 | } |
1409 | } |
1208 | |
1410 | |
1209 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1411 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1210 | |
1412 | |
1211 | void ecb_cold |
1413 | void ecb_cold |
… | |
… | |
1866 | static void noinline ecb_cold |
2068 | static void noinline ecb_cold |
1867 | evpipe_init (EV_P) |
2069 | evpipe_init (EV_P) |
1868 | { |
2070 | { |
1869 | if (!ev_is_active (&pipe_w)) |
2071 | if (!ev_is_active (&pipe_w)) |
1870 | { |
2072 | { |
|
|
2073 | int fds [2]; |
|
|
2074 | |
1871 | # if EV_USE_EVENTFD |
2075 | # if EV_USE_EVENTFD |
|
|
2076 | fds [0] = -1; |
1872 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
2077 | fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1873 | if (evfd < 0 && errno == EINVAL) |
2078 | if (fds [1] < 0 && errno == EINVAL) |
1874 | evfd = eventfd (0, 0); |
2079 | fds [1] = eventfd (0, 0); |
1875 | |
2080 | |
1876 | if (evfd >= 0) |
2081 | if (fds [1] < 0) |
1877 | { |
|
|
1878 | evpipe [0] = -1; |
|
|
1879 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
|
|
1880 | ev_io_set (&pipe_w, evfd, EV_READ); |
|
|
1881 | } |
|
|
1882 | else |
|
|
1883 | # endif |
2082 | # endif |
1884 | { |
2083 | { |
1885 | while (pipe (evpipe)) |
2084 | while (pipe (fds)) |
1886 | ev_syserr ("(libev) error creating signal/async pipe"); |
2085 | ev_syserr ("(libev) error creating signal/async pipe"); |
1887 | |
2086 | |
1888 | fd_intern (evpipe [0]); |
2087 | fd_intern (fds [0]); |
1889 | fd_intern (evpipe [1]); |
|
|
1890 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
|
|
1891 | } |
2088 | } |
1892 | |
2089 | |
|
|
2090 | fd_intern (fds [1]); |
|
|
2091 | |
|
|
2092 | evpipe [0] = fds [0]; |
|
|
2093 | |
|
|
2094 | if (evpipe [1] < 0) |
|
|
2095 | evpipe [1] = fds [1]; /* first call, set write fd */ |
|
|
2096 | else |
|
|
2097 | { |
|
|
2098 | /* on subsequent calls, do not change evpipe [1] */ |
|
|
2099 | /* so that evpipe_write can always rely on its value. */ |
|
|
2100 | /* this branch does not do anything sensible on windows, */ |
|
|
2101 | /* so must not be executed on windows */ |
|
|
2102 | |
|
|
2103 | dup2 (fds [1], evpipe [1]); |
|
|
2104 | close (fds [1]); |
|
|
2105 | } |
|
|
2106 | |
|
|
2107 | ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ); |
1893 | ev_io_start (EV_A_ &pipe_w); |
2108 | ev_io_start (EV_A_ &pipe_w); |
1894 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
2109 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1895 | } |
2110 | } |
1896 | } |
2111 | } |
1897 | |
2112 | |
… | |
… | |
1918 | ECB_MEMORY_FENCE_RELEASE; |
2133 | ECB_MEMORY_FENCE_RELEASE; |
1919 | |
2134 | |
1920 | old_errno = errno; /* save errno because write will clobber it */ |
2135 | old_errno = errno; /* save errno because write will clobber it */ |
1921 | |
2136 | |
1922 | #if EV_USE_EVENTFD |
2137 | #if EV_USE_EVENTFD |
1923 | if (evfd >= 0) |
2138 | if (evpipe [0] < 0) |
1924 | { |
2139 | { |
1925 | uint64_t counter = 1; |
2140 | uint64_t counter = 1; |
1926 | write (evfd, &counter, sizeof (uint64_t)); |
2141 | write (evpipe [1], &counter, sizeof (uint64_t)); |
1927 | } |
2142 | } |
1928 | else |
2143 | else |
1929 | #endif |
2144 | #endif |
1930 | { |
2145 | { |
1931 | #ifdef _WIN32 |
2146 | #ifdef _WIN32 |
… | |
… | |
1951 | int i; |
2166 | int i; |
1952 | |
2167 | |
1953 | if (revents & EV_READ) |
2168 | if (revents & EV_READ) |
1954 | { |
2169 | { |
1955 | #if EV_USE_EVENTFD |
2170 | #if EV_USE_EVENTFD |
1956 | if (evfd >= 0) |
2171 | if (evpipe [0] < 0) |
1957 | { |
2172 | { |
1958 | uint64_t counter; |
2173 | uint64_t counter; |
1959 | read (evfd, &counter, sizeof (uint64_t)); |
2174 | read (evpipe [1], &counter, sizeof (uint64_t)); |
1960 | } |
2175 | } |
1961 | else |
2176 | else |
1962 | #endif |
2177 | #endif |
1963 | { |
2178 | { |
1964 | char dummy[4]; |
2179 | char dummy[4]; |
… | |
… | |
2014 | |
2229 | |
2015 | void |
2230 | void |
2016 | ev_feed_signal (int signum) EV_THROW |
2231 | ev_feed_signal (int signum) EV_THROW |
2017 | { |
2232 | { |
2018 | #if EV_MULTIPLICITY |
2233 | #if EV_MULTIPLICITY |
|
|
2234 | ECB_MEMORY_FENCE_ACQUIRE; |
2019 | EV_P = signals [signum - 1].loop; |
2235 | EV_P = signals [signum - 1].loop; |
2020 | |
2236 | |
2021 | if (!EV_A) |
2237 | if (!EV_A) |
2022 | return; |
2238 | return; |
2023 | #endif |
2239 | #endif |
2024 | |
2240 | |
2025 | if (!ev_active (&pipe_w)) |
|
|
2026 | return; |
|
|
2027 | |
|
|
2028 | signals [signum - 1].pending = 1; |
2241 | signals [signum - 1].pending = 1; |
2029 | evpipe_write (EV_A_ &sig_pending); |
2242 | evpipe_write (EV_A_ &sig_pending); |
2030 | } |
2243 | } |
2031 | |
2244 | |
2032 | static void |
2245 | static void |
… | |
… | |
2042 | void noinline |
2255 | void noinline |
2043 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
2256 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
2044 | { |
2257 | { |
2045 | WL w; |
2258 | WL w; |
2046 | |
2259 | |
2047 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
2260 | if (expect_false (signum <= 0 || signum >= EV_NSIG)) |
2048 | return; |
2261 | return; |
2049 | |
2262 | |
2050 | --signum; |
2263 | --signum; |
2051 | |
2264 | |
2052 | #if EV_MULTIPLICITY |
2265 | #if EV_MULTIPLICITY |
… | |
… | |
2350 | #if EV_ASYNC_ENABLE |
2563 | #if EV_ASYNC_ENABLE |
2351 | async_pending = 0; |
2564 | async_pending = 0; |
2352 | #endif |
2565 | #endif |
2353 | pipe_write_skipped = 0; |
2566 | pipe_write_skipped = 0; |
2354 | pipe_write_wanted = 0; |
2567 | pipe_write_wanted = 0; |
|
|
2568 | evpipe [0] = -1; |
|
|
2569 | evpipe [1] = -1; |
2355 | #if EV_USE_INOTIFY |
2570 | #if EV_USE_INOTIFY |
2356 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2571 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2357 | #endif |
2572 | #endif |
2358 | #if EV_USE_SIGNALFD |
2573 | #if EV_USE_SIGNALFD |
2359 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
2574 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
… | |
… | |
2422 | if (ev_is_active (&pipe_w)) |
2637 | if (ev_is_active (&pipe_w)) |
2423 | { |
2638 | { |
2424 | /*ev_ref (EV_A);*/ |
2639 | /*ev_ref (EV_A);*/ |
2425 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
2640 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
2426 | |
2641 | |
2427 | #if EV_USE_EVENTFD |
|
|
2428 | if (evfd >= 0) |
|
|
2429 | close (evfd); |
|
|
2430 | #endif |
|
|
2431 | |
|
|
2432 | if (evpipe [0] >= 0) |
|
|
2433 | { |
|
|
2434 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2642 | if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]); |
2435 | EV_WIN32_CLOSE_FD (evpipe [1]); |
2643 | if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]); |
2436 | } |
|
|
2437 | } |
2644 | } |
2438 | |
2645 | |
2439 | #if EV_USE_SIGNALFD |
2646 | #if EV_USE_SIGNALFD |
2440 | if (ev_is_active (&sigfd_w)) |
2647 | if (ev_is_active (&sigfd_w)) |
2441 | close (sigfd); |
2648 | close (sigfd); |
… | |
… | |
2527 | #endif |
2734 | #endif |
2528 | #if EV_USE_INOTIFY |
2735 | #if EV_USE_INOTIFY |
2529 | infy_fork (EV_A); |
2736 | infy_fork (EV_A); |
2530 | #endif |
2737 | #endif |
2531 | |
2738 | |
|
|
2739 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
2532 | if (ev_is_active (&pipe_w)) |
2740 | if (ev_is_active (&pipe_w)) |
2533 | { |
2741 | { |
2534 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
2742 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
2535 | |
2743 | |
2536 | ev_ref (EV_A); |
2744 | ev_ref (EV_A); |
2537 | ev_io_stop (EV_A_ &pipe_w); |
2745 | ev_io_stop (EV_A_ &pipe_w); |
2538 | |
2746 | |
2539 | #if EV_USE_EVENTFD |
|
|
2540 | if (evfd >= 0) |
|
|
2541 | close (evfd); |
|
|
2542 | #endif |
|
|
2543 | |
|
|
2544 | if (evpipe [0] >= 0) |
2747 | if (evpipe [0] >= 0) |
2545 | { |
|
|
2546 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2748 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2547 | EV_WIN32_CLOSE_FD (evpipe [1]); |
|
|
2548 | } |
|
|
2549 | |
2749 | |
2550 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
|
|
2551 | evpipe_init (EV_A); |
2750 | evpipe_init (EV_A); |
2552 | /* now iterate over everything, in case we missed something */ |
2751 | /* iterate over everything, in case we missed something before */ |
2553 | pipecb (EV_A_ &pipe_w, EV_READ); |
2752 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
2554 | #endif |
|
|
2555 | } |
2753 | } |
|
|
2754 | #endif |
2556 | |
2755 | |
2557 | postfork = 0; |
2756 | postfork = 0; |
2558 | } |
2757 | } |
2559 | |
2758 | |
2560 | #if EV_MULTIPLICITY |
2759 | #if EV_MULTIPLICITY |
… | |
… | |
2759 | } |
2958 | } |
2760 | |
2959 | |
2761 | void noinline |
2960 | void noinline |
2762 | ev_invoke_pending (EV_P) |
2961 | ev_invoke_pending (EV_P) |
2763 | { |
2962 | { |
2764 | for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ |
2963 | pendingpri = NUMPRI; |
|
|
2964 | |
|
|
2965 | while (pendingpri) /* pendingpri possibly gets modified in the inner loop */ |
|
|
2966 | { |
|
|
2967 | --pendingpri; |
|
|
2968 | |
2765 | while (pendingcnt [pendingpri]) |
2969 | while (pendingcnt [pendingpri]) |
2766 | { |
2970 | { |
2767 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2971 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2768 | |
2972 | |
2769 | p->w->pending = 0; |
2973 | p->w->pending = 0; |
2770 | EV_CB_INVOKE (p->w, p->events); |
2974 | EV_CB_INVOKE (p->w, p->events); |
2771 | EV_FREQUENT_CHECK; |
2975 | EV_FREQUENT_CHECK; |
2772 | } |
2976 | } |
|
|
2977 | } |
2773 | } |
2978 | } |
2774 | |
2979 | |
2775 | #if EV_IDLE_ENABLE |
2980 | #if EV_IDLE_ENABLE |
2776 | /* make idle watchers pending. this handles the "call-idle */ |
2981 | /* make idle watchers pending. this handles the "call-idle */ |
2777 | /* only when higher priorities are idle" logic */ |
2982 | /* only when higher priorities are idle" logic */ |
… | |
… | |
3135 | backend_poll (EV_A_ waittime); |
3340 | backend_poll (EV_A_ waittime); |
3136 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3341 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3137 | |
3342 | |
3138 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
3343 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
3139 | |
3344 | |
|
|
3345 | ECB_MEMORY_FENCE_ACQUIRE; |
3140 | if (pipe_write_skipped) |
3346 | if (pipe_write_skipped) |
3141 | { |
3347 | { |
3142 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3348 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3143 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3349 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3144 | } |
3350 | } |
… | |
… | |
3522 | #if EV_MULTIPLICITY |
3728 | #if EV_MULTIPLICITY |
3523 | assert (("libev: a signal must not be attached to two different loops", |
3729 | assert (("libev: a signal must not be attached to two different loops", |
3524 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
3730 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
3525 | |
3731 | |
3526 | signals [w->signum - 1].loop = EV_A; |
3732 | signals [w->signum - 1].loop = EV_A; |
|
|
3733 | ECB_MEMORY_FENCE_RELEASE; |
3527 | #endif |
3734 | #endif |
3528 | |
3735 | |
3529 | EV_FREQUENT_CHECK; |
3736 | EV_FREQUENT_CHECK; |
3530 | |
3737 | |
3531 | #if EV_USE_SIGNALFD |
3738 | #if EV_USE_SIGNALFD |