From 958dad705a8c2c191e4352547fc9950c4feaa9fb Mon Sep 17 00:00:00 2001 From: Chris Dearman Date: Mon, 23 Jul 2012 17:30:04 -0700 Subject: [PATCH] Fix implementation of generic atomic operations Change-Id: Ie1ea5aacc561e2d6d40125d2952ed0e9116b7b0d --- libc/include/sys/atomics.h | 14 +++++++------- libc/private/bionic_atomic_gcc_builtin.h | 11 +++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/libc/include/sys/atomics.h b/libc/include/sys/atomics.h index 3ada8def8..143bc4bc2 100644 --- a/libc/include/sys/atomics.h +++ b/libc/include/sys/atomics.h @@ -47,20 +47,20 @@ __BEGIN_DECLS #define __ATOMIC_INLINE__ static __inline__ __attribute__((always_inline)) __ATOMIC_INLINE__ int -__atomic_cmpxchg(int old, int _new, volatile int *ptr) +__atomic_cmpxchg(int old_value, int new_value, volatile int* ptr) { /* We must return 0 on success */ - return __sync_val_compare_and_swap(ptr, old, _new) != old; + return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; } __ATOMIC_INLINE__ int -__atomic_swap(int _new, volatile int *ptr) +__atomic_swap(int new_value, volatile int *ptr) { - int prev; + int old_value; do { - prev = *ptr; - } while (__sync_val_compare_and_swap(ptr, prev, _new) != prev); - return prev; + old_value = *ptr; + } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value); + return old_value; } __ATOMIC_INLINE__ int diff --git a/libc/private/bionic_atomic_gcc_builtin.h b/libc/private/bionic_atomic_gcc_builtin.h index e7c57617d..2919f7fef 100644 --- a/libc/private/bionic_atomic_gcc_builtin.h +++ b/libc/private/bionic_atomic_gcc_builtin.h @@ -31,18 +31,17 @@ __ATOMIC_INLINE__ int __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr) { /* We must return 0 on success */ - return __sync_bool_compare_and_swap(ptr, old_value, new_value) == 0; + return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; } __ATOMIC_INLINE__ int32_t __bionic_swap(int32_t new_value, volatile int32_t* ptr) { - int32_t prev; + int32_t old_value; do { - prev = *ptr; - status = __sync_val_compare_and_swap(ptr, prev, new_value); - } while (status == 0); - return prev; + old_value = *ptr; + } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value); + return old_value; } __ATOMIC_INLINE__ int32_t