Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / wdlatomic.h
blob93f59ff1b8d33320704e73ff582c749575677f33
1 #ifndef _WDL_ATOMIC_H_
2 #define _WDL_ATOMIC_H_
4 #ifdef _WIN32
6 static int wdl_atomic_incr(int *v) { return (int) InterlockedIncrement((LONG *)v); }
7 static int wdl_atomic_decr(int *v) { return (int) InterlockedDecrement((LONG *)v); }
8 static int wdl_atomic_incr(volatile int *v) { return (int) InterlockedIncrement((LONG *)v); }
9 static int wdl_atomic_decr(volatile int *v) { return (int) InterlockedDecrement((LONG *)v); }
11 #elif (!defined(__APPLE__) || !defined(__ppc__)) && (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))))
13 static int wdl_atomic_incr(int *v) { return __sync_add_and_fetch(v,1); }
14 static int wdl_atomic_decr(int *v) { return __sync_add_and_fetch(v,~0); }
15 static int wdl_atomic_incr(volatile int *v) { return __sync_add_and_fetch(v,1); }
16 static int wdl_atomic_decr(volatile int *v) { return __sync_add_and_fetch(v,~0); }
18 #elif defined(__APPLE__)
19 // used by GCC < 4.2 on OSX
20 #include <libkern/OSAtomic.h>
22 static int wdl_atomic_incr(int *v) { return (int) OSAtomicIncrement32Barrier((int32_t*)v); }
23 static int wdl_atomic_decr(int *v) { return (int) OSAtomicDecrement32Barrier((int32_t*)v); }
24 static int wdl_atomic_incr(volatile int *v) { return (int) OSAtomicIncrement32Barrier((int32_t*)v); }
25 static int wdl_atomic_decr(volatile int *v) { return (int) OSAtomicDecrement32Barrier((int32_t*)v); }
26 #else
28 // unsupported!
29 #pragma message("Need win32 or apple or gcc 4.2+ for wdlatomic.h, doh")
31 #endif
33 #endif