From 4ce81fcda86e372d9d7241d3201162eca057bdd6 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Wed, 6 Aug 2014 15:48:31 -0700 Subject: [PATCH] CAS should return a bool. --- src/poly/atomic.h | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/poly/atomic.h b/src/poly/atomic.h index 51c0ec9de..721db37dc 100644 --- a/src/poly/atomic.h +++ b/src/poly/atomic.h @@ -40,11 +40,16 @@ inline int64_t atomic_exchange(int64_t new_value, volatile int64_t* value) { return OSAtomicCompareAndSwap64Barrier(*value, new_value, value); } -inline int32_t atomic_cas(int32_t old_value, int32_t new_value, - volatile int32_t* value) { +inline bool atomic_cas(int32_t old_value, int32_t new_value, + volatile int32_t* value) { return OSAtomicCompareAndSwap32Barrier( old_value, new_value, reinterpret_cast(value)); } +inline bool atomic_cas(int64_t old_value, int64_t new_value, + volatile int32_t* value) { + return OSAtomicCompareAndSwap64Barrier( + old_value, new_value, reinterpret_cast(value)); +} #elif XE_LIKE_WIN32 @@ -64,11 +69,16 @@ inline int64_t atomic_exchange(int64_t new_value, volatile int64_t* value) { new_value); } -inline int32_t atomic_cas(int32_t old_value, int32_t new_value, - volatile int32_t* value) { +inline bool atomic_cas(int32_t old_value, int32_t new_value, + volatile int32_t* value) { return InterlockedCompareExchange(reinterpret_cast(value), new_value, old_value) == old_value; } +inline bool atomic_cas(int64_t old_value, int64_t new_value, + volatile int64_t* value) { + return InterlockedCompareExchange64(reinterpret_cast(value), + new_value, old_value) == old_value; +} #elif XE_LIKE_POSIX @@ -86,11 +96,16 @@ inline int64_t atomic_exchange(int64_t new_value, volatile int64_t* value) { return __sync_val_compare_and_swap(*value, value, new_value); } -inline int32_t atomic_cas(int32_t old_value, int32_t new_value, - volatile int32_t* value) { +inline bool atomic_cas(int32_t old_value, int32_t new_value, + volatile int32_t* value) { return __sync_bool_compare_and_swap( reinterpret_cast(value), old_value, new_value); } +inline bool atomic_cas(int64_t old_value, int64_t new_value, + volatile int64_t* value) { + return __sync_bool_compare_and_swap( + reinterpret_cast(value), old_value, new_value); +} #else @@ -118,11 +133,17 @@ inline uint64_t atomic_exchange(uint64_t new_value, volatile uint64_t* value) { reinterpret_cast(value))); } -inline uint32_t atomic_cas(uint32_t old_value, uint32_t new_value, - volatile uint32_t* value) { - return static_cast(atomic_cas( - static_cast(old_value), static_cast(new_value), - reinterpret_cast(value))); +inline bool atomic_cas(uint32_t old_value, uint32_t new_value, + volatile uint32_t* value) { + return atomic_cas(static_cast(old_value), + static_cast(new_value), + reinterpret_cast(value)); +} +inline bool atomic_cas(uint64_t old_value, uint64_t new_value, + volatile uint64_t* value) { + return atomic_cas(static_cast(old_value), + static_cast(new_value), + reinterpret_cast(value)); } } // namespace poly