blob: 0cb832f9757ca4621f5cf31d8a61ade505b892e0 [file] [log] [blame]
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001/*
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 Constantinescu845c7782013-12-19 11:57:10 +000019__ATOMIC_INLINE__ void __bionic_memory_barrier() {
Serban Constantinescu845c7782013-12-19 11:57:10 +000020 __asm__ __volatile__ ( "dmb ish" : : : "memory" );
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010021}
Elliott Hughesc54ca402013-12-13 12:17:13 -080022
Elliott Hughes4c186ff2013-12-16 13:02:49 -080023/* Compare-and-swap, without any explicit barriers. Note that this function
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010024 * returns 0 on success, and 1 on failure. The opposite convention is typically
25 * used on other platforms.
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010026 */
Elliott Hughes2b333b92013-12-13 16:54:16 -080027__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' Turnere31bfae2011-11-15 15:47:02 +010034#ifdef __thumb2__
Elliott Hughes2b333b92013-12-13 16:54:16 -080035 "it eq\n"
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010036#endif
Elliott Hughes2b333b92013-12-13 16:54:16 -080037 "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' Turnere31bfae2011-11-15 15:47:02 +010043}
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010044
Elliott Hughes2b333b92013-12-13 16:54:16 -080045/* 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' Turnere31bfae2011-11-15 15:47:02 +010057}
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010058
Elliott Hughes2b333b92013-12-13 16:54:16 -080059/* 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' Turnere31bfae2011-11-15 15:47:02 +010072}
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +010073
74#endif /* SYS_ATOMICS_ARM_H */