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