blob: 5e081161d3bc5eac3fb71d2f7a906985f54e0dc0 [file] [log] [blame]
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -07001/*
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_MIPS_H
17#define BIONIC_ATOMIC_MIPS_H
18
19/* Define a full memory barrier, this is only needed if we build the
20 * platform for a multi-core device.
21 */
Elliott Hughes2b333b92013-12-13 16:54:16 -080022
23__ATOMIC_INLINE__ void __bionic_memory_barrier() {
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070024#if defined(ANDROID_SMP) && ANDROID_SMP == 1
Elliott Hughes2b333b92013-12-13 16:54:16 -080025 __asm__ __volatile__ ( "sync" : : : "memory" );
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070026#else
Elliott Hughes2b333b92013-12-13 16:54:16 -080027 /* A simple compiler barrier. */
28 __asm__ __volatile__ ( "" : : : "memory" );
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070029#endif
Elliott Hughes2b333b92013-12-13 16:54:16 -080030}
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070031
32/* Compare-and-swap, without any explicit barriers. Note that this function
33 * returns 0 on success, and 1 on failure. The opposite convention is typically
34 * used on other platforms.
35 */
Elliott Hughes2b333b92013-12-13 16:54:16 -080036__ATOMIC_INLINE__ int __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr) {
37 int32_t prev, status;
38 __asm__ __volatile__ ("1: move %[status], %[new_value] \n"
39 " ll %[prev], 0(%[ptr]) \n"
40 " bne %[old_value], %[prev], 2f \n"
41 " sc %[status], 0(%[ptr]) \n"
42 " beqz %[status], 1b \n"
43 "2: \n"
44 : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
45 : [new_value]"r"(new_value), [old_value]"r"(old_value), [ptr]"r"(ptr)
46 : "memory");
47 return prev != old_value;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070048}
49
Elliott Hughes2b333b92013-12-13 16:54:16 -080050/* 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 __asm__ __volatile__ ("1: move %[status], %[new_value] \n"
54 " ll %[prev], 0(%[ptr]) \n"
55 " sc %[status], 0(%[ptr]) \n"
56 " beqz %[status], 1b \n"
57 : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
58 : [ptr]"r"(ptr), [new_value]"r"(new_value)
59 : "memory");
60 return prev;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070061}
62
Elliott Hughes2b333b92013-12-13 16:54:16 -080063/* Atomic decrement, without explicit barriers. */
64__ATOMIC_INLINE__ int32_t __bionic_atomic_dec(volatile int32_t* ptr) {
65 int32_t prev, status;
66 __asm__ __volatile__ ("1: ll %[prev], 0(%[ptr]) \n"
67 " addiu %[status], %[prev], -1 \n"
68 " sc %[status], 0(%[ptr]) \n"
69 " beqz %[status], 1b \n"
70 : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
71 : [ptr]"r"(ptr)
72 : "memory");
73 return prev;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070074}
75
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070076#endif /* BIONIC_ATOMIC_MIPS_H */