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_ARM_H |
| 17 | #define BIONIC_ATOMIC_ARM_H |
| 18 | |
Serban Constantinescu | 845c778 | 2013-12-19 11:57:10 +0000 | [diff] [blame] | 19 | __ATOMIC_INLINE__ void __bionic_memory_barrier() { |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 20 | #if defined(ANDROID_SMP) && ANDROID_SMP == 1 |
Serban Constantinescu | 845c778 | 2013-12-19 11:57:10 +0000 | [diff] [blame] | 21 | __asm__ __volatile__ ( "dmb ish" : : : "memory" ); |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 22 | #else |
| 23 | /* A simple compiler barrier. */ |
| 24 | __asm__ __volatile__ ( "" : : : "memory" ); |
| 25 | #endif |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 26 | } |
Elliott Hughes | c54ca40 | 2013-12-13 12:17:13 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | 4c186ff | 2013-12-16 13:02:49 -0800 | [diff] [blame] | 28 | /* Compare-and-swap, without any explicit barriers. Note that this function |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 29 | * returns 0 on success, and 1 on failure. The opposite convention is typically |
| 30 | * used on other platforms. |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 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 | int32_t prev, status; |
| 34 | do { |
| 35 | __asm__ __volatile__ ( |
| 36 | "ldrex %0, [%3]\n" |
| 37 | "mov %1, #0\n" |
| 38 | "teq %0, %4\n" |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 39 | #ifdef __thumb2__ |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 40 | "it eq\n" |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 41 | #endif |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 42 | "strexeq %1, %5, [%3]" |
| 43 | : "=&r" (prev), "=&r" (status), "+m"(*ptr) |
| 44 | : "r" (ptr), "Ir" (old_value), "r" (new_value) |
| 45 | : "cc"); |
| 46 | } while (__builtin_expect(status != 0, 0)); |
| 47 | return prev != old_value; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 48 | } |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 49 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 50 | /* Swap, without any explicit barriers. */ |
| 51 | __ATOMIC_INLINE__ int32_t __bionic_swap(int32_t new_value, volatile int32_t* ptr) { |
| 52 | int32_t prev, status; |
| 53 | do { |
| 54 | __asm__ __volatile__ ( |
| 55 | "ldrex %0, [%3]\n" |
| 56 | "strex %1, %4, [%3]" |
| 57 | : "=&r" (prev), "=&r" (status), "+m" (*ptr) |
| 58 | : "r" (ptr), "r" (new_value) |
| 59 | : "cc"); |
| 60 | } while (__builtin_expect(status != 0, 0)); |
| 61 | return prev; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 62 | } |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 63 | |
Elliott Hughes | 2b333b9 | 2013-12-13 16:54:16 -0800 | [diff] [blame] | 64 | /* Atomic decrement, without explicit barriers. */ |
| 65 | __ATOMIC_INLINE__ int32_t __bionic_atomic_dec(volatile int32_t* ptr) { |
| 66 | int32_t prev, tmp, status; |
| 67 | do { |
| 68 | __asm__ __volatile__ ( |
| 69 | "ldrex %0, [%4]\n" |
| 70 | "sub %1, %0, #1\n" |
| 71 | "strex %2, %1, [%4]" |
| 72 | : "=&r" (prev), "=&r" (tmp), "=&r" (status), "+m"(*ptr) |
| 73 | : "r" (ptr) |
| 74 | : "cc"); |
| 75 | } while (__builtin_expect(status != 0, 0)); |
| 76 | return prev; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 77 | } |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 78 | |
| 79 | #endif /* SYS_ATOMICS_ARM_H */ |