David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef BIONIC_ATOMIC_GCC_BUILTIN_H |
| 17 | #define BIONIC_ATOMIC_GCC_BUILTIN_H |
| 18 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 19 | /* |
| 20 | * This header file is used by default if we don't have optimized atomic |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 21 | * routines for a given platform. See bionic_atomic_arm.h and |
| 22 | * bionic_atomic_x86.h for examples. |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 23 | * |
| 24 | * Note that the GCC builtins include barriers that aren't present in |
| 25 | * the architecture-specific assembler versions. |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
Serban Constantinescu | 845c778 | 2013-12-19 11:57:10 +0000 | [diff] [blame] | 28 | __ATOMIC_INLINE__ void __bionic_memory_barrier() { |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 29 | __sync_synchronize(); |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 32 | __ATOMIC_INLINE__ int __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr) { |
| 33 | /* We must return 0 on success. */ |
| 34 | return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 37 | __ATOMIC_INLINE__ int32_t __bionic_swap(int32_t new_value, volatile int32_t* ptr) { |
| 38 | int32_t old_value; |
| 39 | do { |
| 40 | old_value = *ptr; |
| 41 | } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value); |
| 42 | return old_value; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 45 | __ATOMIC_INLINE__ int32_t __bionic_atomic_dec(volatile int32_t* ptr) { |
| 46 | /* We must return the old value. */ |
| 47 | return __sync_fetch_and_add(ptr, -1); |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | #endif /* BIONIC_ATOMIC_GCC_BUILTIN_H */ |