Commit 3fcef2fc authored by David Reid's avatar David Reid

Add atomic compare and swap.

parent fda0c076
......@@ -7494,25 +7494,31 @@ Atomics
#endif
#if defined(_WIN32) && !defined(__GNUC__) && !defined(__clang__)
#define ma_memory_barrier() MemoryBarrier()
#define ma_atomic_exchange_32(a, b) InterlockedExchange((LONG*)a, (LONG)b)
#define ma_atomic_exchange_64(a, b) InterlockedExchange64((LONGLONG*)a, (LONGLONG)b)
#define ma_atomic_increment_32(a) InterlockedIncrement((LONG*)a)
#define ma_atomic_decrement_32(a) InterlockedDecrement((LONG*)a)
#define ma_memory_barrier() MemoryBarrier()
#define ma_atomic_exchange_32(a, b) InterlockedExchange((LONG*)a, (LONG)b)
#define ma_atomic_exchange_64(a, b) InterlockedExchange64((LONGLONG*)a, (LONGLONG)b)
#define ma_atomic_increment_32(a) InterlockedIncrement((LONG*)a)
#define ma_atomic_decrement_32(a) InterlockedDecrement((LONG*)a)
#define ma_compare_and_swap_32(d, e, c) _InterlockedCompareExchange((LONG*)d, (LONG)e, (LONG)c)
#define ma_compare_and_swap_64(d, e, c) _InterlockedCompareExchange64((LONGLONG*)d, (LONGLONG)e, (LONGLONG)c)
#define ma_compare_and_swap_ptr(d, e, c) _InterlockedCompareExchangePointer((void*volatile*)d, (void*)e, (void*)c)
#else
#define ma_memory_barrier() __sync_synchronize()
#define ma_memory_barrier() __sync_synchronize()
#if defined(MA_HAS_SYNC_SWAP)
#define ma_atomic_exchange_32(a, b) __sync_swap(a, b)
#define ma_atomic_exchange_64(a, b) __sync_swap(a, b)
#define ma_atomic_exchange_32(a, b) __sync_swap(a, b)
#define ma_atomic_exchange_64(a, b) __sync_swap(a, b)
#elif defined(MA_HAS_GNUC_ATOMICS)
#define ma_atomic_exchange_32(a, b) (void)__atomic_exchange_n(a, b, __ATOMIC_ACQ_REL)
#define ma_atomic_exchange_64(a, b) (void)__atomic_exchange_n(a, b, __ATOMIC_ACQ_REL)
#define ma_atomic_exchange_32(a, b) (void)__atomic_exchange_n(a, b, __ATOMIC_ACQ_REL)
#define ma_atomic_exchange_64(a, b) (void)__atomic_exchange_n(a, b, __ATOMIC_ACQ_REL)
#else
#define ma_atomic_exchange_32(a, b) __sync_synchronize(); (void)__sync_lock_test_and_set(a, b)
#define ma_atomic_exchange_64(a, b) __sync_synchronize(); (void)__sync_lock_test_and_set(a, b)
#endif
#define ma_atomic_increment_32(a) __sync_add_and_fetch(a, 1)
#define ma_atomic_decrement_32(a) __sync_sub_and_fetch(a, 1)
#define ma_atomic_exchange_32(a, b) __sync_synchronize(); (void)__sync_lock_test_and_set(a, b)
#define ma_atomic_exchange_64(a, b) __sync_synchronize(); (void)__sync_lock_test_and_set(a, b)
#endif
#define ma_atomic_increment_32(a) __sync_add_and_fetch(a, 1)
#define ma_atomic_decrement_32(a) __sync_sub_and_fetch(a, 1)
#define ma_compare_and_swap_32(d, e, c) __sync_val_compare_and_swap(d, e, c)
#define ma_compare_and_swap_64(d, e, c) __sync_val_compare_and_swap(d, e, c)
#define ma_compare_and_swap_ptr(d, e, c) __sync_val_compare_and_swap(d, e, c)
#endif
#ifdef MA_64BIT
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment