Merge "libc: provide atomic operations will full barriers for NDK apps."

This commit is contained in:
David 'Digit' Turner
2011-11-22 02:10:06 -08:00
committed by Android (Google) Code Review
7 changed files with 244 additions and 307 deletions

View File

@@ -33,10 +33,48 @@
__BEGIN_DECLS
extern int __atomic_cmpxchg(int old, int _new, volatile int *ptr);
extern int __atomic_swap(int _new, volatile int *ptr);
extern int __atomic_dec(volatile int *ptr);
extern int __atomic_inc(volatile int *ptr);
/* Note: atomic operations that were exported by the C library didn't
* provide any memory barriers, which created potential issues on
* multi-core devices. We now define them as inlined calls to
* GCC sync builtins, which always provide a full barrier.
*
* NOTE: The C library still exports atomic functions by the same
* name to ensure ABI stability for existing NDK machine code.
*
* If you are an NDK developer, we encourage you to rebuild your
* unmodified sources against this header as soon as possible.
*/
#define __ATOMIC_INLINE__ static __inline__ __attribute__((always_inline))
__ATOMIC_INLINE__ int
__atomic_cmpxchg(int old, int _new, volatile int *ptr)
{
/* We must return 0 on success */
return __sync_val_compare_and_swap(ptr, old, _new) != old;
}
__ATOMIC_INLINE__ int
__atomic_swap(int _new, volatile int *ptr)
{
int prev;
do {
prev = *ptr;
} while (__sync_val_compare_and_swap(ptr, prev, _new) != prev);
return prev;
}
__ATOMIC_INLINE__ int
__atomic_dec(volatile int *ptr)
{
return __sync_fetch_and_sub (ptr, 1);
}
__ATOMIC_INLINE__ int
__atomic_inc(volatile int *ptr)
{
return __sync_fetch_and_add (ptr, 1);
}
int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout);
int __futex_wake(volatile void *ftx, int count);