blob: 83f75fe86b9759b351876a99de9a45a3bd4e3364 [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() {
Elliott Hughes2b333b92013-12-13 16:54:16 -080024 __asm__ __volatile__ ( "sync" : : : "memory" );
Elliott Hughes2b333b92013-12-13 16:54:16 -080025}
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070026
27/* Compare-and-swap, without any explicit barriers. Note that this function
28 * returns 0 on success, and 1 on failure. The opposite convention is typically
29 * used on other platforms.
30 */
Elliott Hughes2b333b92013-12-13 16:54:16 -080031__ATOMIC_INLINE__ int __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr) {
32 int32_t prev, status;
33 __asm__ __volatile__ ("1: move %[status], %[new_value] \n"
34 " ll %[prev], 0(%[ptr]) \n"
35 " bne %[old_value], %[prev], 2f \n"
36 " sc %[status], 0(%[ptr]) \n"
37 " beqz %[status], 1b \n"
38 "2: \n"
39 : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
40 : [new_value]"r"(new_value), [old_value]"r"(old_value), [ptr]"r"(ptr)
41 : "memory");
42 return prev != old_value;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070043}
44
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 __asm__ __volatile__ ("1: move %[status], %[new_value] \n"
49 " ll %[prev], 0(%[ptr]) \n"
50 " sc %[status], 0(%[ptr]) \n"
51 " beqz %[status], 1b \n"
52 : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
53 : [ptr]"r"(ptr), [new_value]"r"(new_value)
54 : "memory");
55 return prev;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070056}
57
Elliott Hughes2b333b92013-12-13 16:54:16 -080058/* Atomic decrement, without explicit barriers. */
59__ATOMIC_INLINE__ int32_t __bionic_atomic_dec(volatile int32_t* ptr) {
60 int32_t prev, status;
61 __asm__ __volatile__ ("1: ll %[prev], 0(%[ptr]) \n"
62 " addiu %[status], %[prev], -1 \n"
63 " sc %[status], 0(%[ptr]) \n"
64 " beqz %[status], 1b \n"
65 : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
66 : [ptr]"r"(ptr)
67 : "memory");
68 return prev;
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070069}
70
Raghu Gandhamf7fb9e12012-06-29 15:52:55 -070071#endif /* BIONIC_ATOMIC_MIPS_H */