| 1 | #ifndef V_PREALLOC_ATOMICS_H |
| 2 | #define V_PREALLOC_ATOMICS_H |
| 3 | |
| 4 | #if defined(_MSC_VER) |
| 5 | #include <intrin.h> |
| 6 | static inline int v_prealloc_atomic_add_i32(int *ptr, int delta) { |
| 7 | return (int)_InterlockedExchangeAdd((volatile long*)ptr, (long)delta) + delta; |
| 8 | } |
| 9 | static inline int v_prealloc_atomic_load_i32(int *ptr) { |
| 10 | return (int)_InterlockedCompareExchange((volatile long*)ptr, 0, 0); |
| 11 | } |
| 12 | static inline int v_prealloc_atomic_store_i32(int *ptr, int val) { |
| 13 | _InterlockedExchange((volatile long*)ptr, (long)val); |
| 14 | return val; |
| 15 | } |
| 16 | static inline int v_prealloc_atomic_cas_i32(int *ptr, int expected, int desired) { |
| 17 | return _InterlockedCompareExchange((volatile long*)ptr, (long)desired, (long)expected) == expected; |
| 18 | } |
| 19 | #else |
| 20 | static inline int v_prealloc_atomic_add_i32(int *ptr, int delta) { |
| 21 | return __sync_add_and_fetch(ptr, delta); |
| 22 | } |
| 23 | static inline int v_prealloc_atomic_load_i32(int *ptr) { |
| 24 | return __sync_add_and_fetch(ptr, 0); |
| 25 | } |
| 26 | static inline int v_prealloc_atomic_store_i32(int *ptr, int val) { |
| 27 | return __sync_lock_test_and_set(ptr, val); |
| 28 | } |
| 29 | static inline int v_prealloc_atomic_cas_i32(int *ptr, int expected, int desired) { |
| 30 | return __sync_bool_compare_and_swap(ptr, expected, desired); |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | #endif |
| 35 | |