… | |
… | |
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 0x00010007 |
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; |
… | |
… | |
77 | #endif |
77 | #endif |
78 | |
78 | |
79 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
79 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
80 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
80 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
81 | |
81 | |
|
|
82 | #ifndef ECB_OPTIMIZE_SIZE |
|
|
83 | #if __OPTIMIZE_SIZE__ |
|
|
84 | #define ECB_OPTIMIZE_SIZE 1 |
|
|
85 | #else |
|
|
86 | #define ECB_OPTIMIZE_SIZE 0 |
|
|
87 | #endif |
|
|
88 | #endif |
|
|
89 | |
82 | /* work around x32 idiocy by defining proper macros */ |
90 | /* work around x32 idiocy by defining proper macros */ |
83 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
91 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
84 | #if _ILP32 |
92 | #if _ILP32 |
85 | #define ECB_AMD64_X32 1 |
93 | #define ECB_AMD64_X32 1 |
86 | #else |
94 | #else |
… | |
… | |
164 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
172 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
165 | #endif |
173 | #endif |
166 | |
174 | |
167 | #ifndef ECB_MEMORY_FENCE |
175 | #ifndef ECB_MEMORY_FENCE |
168 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
176 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
177 | #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory") |
169 | #if __i386 || __i386__ |
178 | #if __i386 || __i386__ |
170 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
179 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
171 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
180 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
172 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
181 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
173 | #elif ECB_GCC_AMD64 |
182 | #elif ECB_GCC_AMD64 |
… | |
… | |
223 | #if ECB_GCC_VERSION(4,7) |
232 | #if ECB_GCC_VERSION(4,7) |
224 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
233 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
225 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
234 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
226 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
235 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
227 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
236 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
237 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
228 | |
238 | |
229 | #elif ECB_CLANG_EXTENSION(c_atomic) |
239 | #elif ECB_CLANG_EXTENSION(c_atomic) |
230 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
240 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
231 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
241 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
232 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
242 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
233 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
243 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
244 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
234 | |
245 | |
235 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
246 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
236 | #define ECB_MEMORY_FENCE __sync_synchronize () |
247 | #define ECB_MEMORY_FENCE __sync_synchronize () |
237 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
248 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
238 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
249 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
… | |
… | |
248 | #elif defined _WIN32 |
259 | #elif defined _WIN32 |
249 | #include <WinNT.h> |
260 | #include <WinNT.h> |
250 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
261 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
251 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
262 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
252 | #include <mbarrier.h> |
263 | #include <mbarrier.h> |
253 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
264 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
254 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
265 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier () |
255 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
266 | #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier () |
|
|
267 | #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier () |
256 | #elif __xlC__ |
268 | #elif __xlC__ |
257 | #define ECB_MEMORY_FENCE __sync () |
269 | #define ECB_MEMORY_FENCE __sync () |
258 | #endif |
270 | #endif |
259 | #endif |
271 | #endif |
260 | |
272 | |
261 | #ifndef ECB_MEMORY_FENCE |
273 | #ifndef ECB_MEMORY_FENCE |
262 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
274 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
263 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
275 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
264 | /* not just C11 atomics and atomic accesses */ |
276 | /* not just C11 atomics and atomic accesses */ |
265 | #include <stdatomic.h> |
277 | #include <stdatomic.h> |
266 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
|
|
267 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
268 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
269 | /* for most usages, or gcc and clang have a bug */ |
|
|
270 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
271 | /* all three of ecb's fences as a seq_cst fence */ |
|
|
272 | /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ |
|
|
273 | /* for all __atomic_thread_fence's except seq_cst */ |
|
|
274 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
278 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
|
|
279 | #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire) |
|
|
280 | #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release) |
275 | #endif |
281 | #endif |
276 | #endif |
282 | #endif |
277 | |
283 | |
278 | #ifndef ECB_MEMORY_FENCE |
284 | #ifndef ECB_MEMORY_FENCE |
279 | #if !ECB_AVOID_PTHREADS |
285 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
297 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
303 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
298 | #endif |
304 | #endif |
299 | |
305 | |
300 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
306 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
301 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
307 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
308 | #endif |
|
|
309 | |
|
|
310 | #if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE |
|
|
311 | #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */ |
302 | #endif |
312 | #endif |
303 | |
313 | |
304 | /*****************************************************************************/ |
314 | /*****************************************************************************/ |
305 | |
315 | |
306 | #if ECB_CPP |
316 | #if ECB_CPP |