… | |
… | |
40 | |
40 | |
41 | #ifndef ECB_H |
41 | #ifndef ECB_H |
42 | #define ECB_H |
42 | #define ECB_H |
43 | |
43 | |
44 | /* 16 bits major, 16 bits minor */ |
44 | /* 16 bits major, 16 bits minor */ |
45 | #define ECB_VERSION 0x00010005 |
45 | #define ECB_VERSION 0x00010006 |
46 | |
46 | |
47 | #ifdef _WIN32 |
47 | #ifdef _WIN32 |
48 | typedef signed char int8_t; |
48 | typedef signed char int8_t; |
49 | typedef unsigned char uint8_t; |
49 | typedef unsigned char uint8_t; |
50 | typedef signed short int16_t; |
50 | typedef signed short int16_t; |
… | |
… | |
115 | #define ECB_CLANG_EXTENSION(x) 0 |
115 | #define ECB_CLANG_EXTENSION(x) 0 |
116 | #endif |
116 | #endif |
117 | |
117 | |
118 | #define ECB_CPP (__cplusplus+0) |
118 | #define ECB_CPP (__cplusplus+0) |
119 | #define ECB_CPP11 (__cplusplus >= 201103L) |
119 | #define ECB_CPP11 (__cplusplus >= 201103L) |
|
|
120 | #define ECB_CPP14 (__cplusplus >= 201402L) |
|
|
121 | #define ECB_CPP17 (__cplusplus >= 201703L) |
120 | |
122 | |
121 | #if ECB_CPP |
123 | #if ECB_CPP |
122 | #define ECB_C 0 |
124 | #define ECB_C 0 |
123 | #define ECB_STDC_VERSION 0 |
125 | #define ECB_STDC_VERSION 0 |
124 | #else |
126 | #else |
… | |
… | |
126 | #define ECB_STDC_VERSION __STDC_VERSION__ |
128 | #define ECB_STDC_VERSION __STDC_VERSION__ |
127 | #endif |
129 | #endif |
128 | |
130 | |
129 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
131 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
130 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
132 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
|
|
133 | #define ECB_C17 (ECB_STDC_VERSION >= 201710L) |
131 | |
134 | |
132 | #if ECB_CPP |
135 | #if ECB_CPP |
133 | #define ECB_EXTERN_C extern "C" |
136 | #define ECB_EXTERN_C extern "C" |
134 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
137 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
135 | #define ECB_EXTERN_C_END } |
138 | #define ECB_EXTERN_C_END } |
… | |
… | |
161 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
164 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
162 | #endif |
165 | #endif |
163 | |
166 | |
164 | #ifndef ECB_MEMORY_FENCE |
167 | #ifndef ECB_MEMORY_FENCE |
165 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
168 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
169 | #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory") |
166 | #if __i386 || __i386__ |
170 | #if __i386 || __i386__ |
167 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
171 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
168 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
172 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
169 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
173 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
170 | #elif ECB_GCC_AMD64 |
174 | #elif ECB_GCC_AMD64 |
… | |
… | |
220 | #if ECB_GCC_VERSION(4,7) |
224 | #if ECB_GCC_VERSION(4,7) |
221 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
225 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
222 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
226 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
223 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
227 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
224 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
228 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
229 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
225 | |
230 | |
226 | #elif ECB_CLANG_EXTENSION(c_atomic) |
231 | #elif ECB_CLANG_EXTENSION(c_atomic) |
227 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
232 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
228 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
233 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
229 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
234 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
230 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
235 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
236 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
231 | |
237 | |
232 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
238 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
233 | #define ECB_MEMORY_FENCE __sync_synchronize () |
239 | #define ECB_MEMORY_FENCE __sync_synchronize () |
234 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
240 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
235 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
241 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
… | |
… | |
245 | #elif defined _WIN32 |
251 | #elif defined _WIN32 |
246 | #include <WinNT.h> |
252 | #include <WinNT.h> |
247 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
253 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
248 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
254 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
249 | #include <mbarrier.h> |
255 | #include <mbarrier.h> |
250 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
256 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
251 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
257 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier () |
252 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
258 | #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier () |
|
|
259 | #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier () |
253 | #elif __xlC__ |
260 | #elif __xlC__ |
254 | #define ECB_MEMORY_FENCE __sync () |
261 | #define ECB_MEMORY_FENCE __sync () |
255 | #endif |
262 | #endif |
256 | #endif |
263 | #endif |
257 | |
264 | |
258 | #ifndef ECB_MEMORY_FENCE |
265 | #ifndef ECB_MEMORY_FENCE |
259 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
266 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
260 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
267 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
261 | /* not just C11 atomics and atomic accesses */ |
268 | /* not just C11 atomics and atomic accesses */ |
262 | #include <stdatomic.h> |
269 | #include <stdatomic.h> |
263 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
|
|
264 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
265 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
266 | /* for most usages, or gcc and clang have a bug */ |
|
|
267 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
268 | /* all three of ecb's fences as a seq_cst fence */ |
|
|
269 | /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ |
|
|
270 | /* for all __atomic_thread_fence's except seq_cst */ |
|
|
271 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
270 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
|
|
271 | #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire) |
|
|
272 | #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release) |
272 | #endif |
273 | #endif |
273 | #endif |
274 | #endif |
274 | |
275 | |
275 | #ifndef ECB_MEMORY_FENCE |
276 | #ifndef ECB_MEMORY_FENCE |
276 | #if !ECB_AVOID_PTHREADS |
277 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
294 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
295 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
295 | #endif |
296 | #endif |
296 | |
297 | |
297 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
298 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
298 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
299 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
300 | #endif |
|
|
301 | |
|
|
302 | #if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE |
|
|
303 | #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */ |
299 | #endif |
304 | #endif |
300 | |
305 | |
301 | /*****************************************************************************/ |
306 | /*****************************************************************************/ |
302 | |
307 | |
303 | #if ECB_CPP |
308 | #if ECB_CPP |