blob: 6215621e967c92487dff54cb12025219e958c3ee [file] [log] [blame]
Duane Sand84c3e992014-04-29 11:19:26 -07001/*
2 * Copyright (C) 2010 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
17#ifndef ANDROID_CUTILS_ATOMIC_MIPS64_H
18#define ANDROID_CUTILS_ATOMIC_MIPS64_H
19
20#include <stdint.h>
21
22#ifndef ANDROID_ATOMIC_INLINE
23#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
24#endif
25
26extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
27{
28 __asm__ __volatile__ ("" : : : "memory");
29}
30
Duane Sand84c3e992014-04-29 11:19:26 -070031extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
32{
33 __asm__ __volatile__ ("sync" : : : "memory");
34}
35extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
36{
37 __asm__ __volatile__ ("sync" : : : "memory");
38}
Duane Sand84c3e992014-04-29 11:19:26 -070039
40extern ANDROID_ATOMIC_INLINE
41int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
42{
43 int32_t value = *ptr;
44 android_memory_barrier();
45 return value;
46}
47
48extern ANDROID_ATOMIC_INLINE
49int64_t android_atomic_acquire_load64(volatile const int64_t *ptr)
50{
51 int64_t value = *ptr;
52 android_memory_barrier();
53 return value;
54}
55
56extern ANDROID_ATOMIC_INLINE
57int32_t android_atomic_release_load(volatile const int32_t *ptr)
58{
59 android_memory_barrier();
60 return *ptr;
61}
62
63extern ANDROID_ATOMIC_INLINE
64int64_t android_atomic_release_load64(volatile const int64_t *ptr)
65{
66 android_memory_barrier();
67 return *ptr;
68}
69
70extern ANDROID_ATOMIC_INLINE
71void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
72{
73 *ptr = value;
74 android_memory_barrier();
75}
76
77extern ANDROID_ATOMIC_INLINE
78void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr)
79{
80 *ptr = value;
81 android_memory_barrier();
82}
83
84extern ANDROID_ATOMIC_INLINE
85void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
86{
87 android_memory_barrier();
88 *ptr = value;
89}
90
91extern ANDROID_ATOMIC_INLINE
92void android_atomic_release_store64(int64_t value, volatile int64_t *ptr)
93{
94 android_memory_barrier();
95 *ptr = value;
96}
97
98extern ANDROID_ATOMIC_INLINE
99int android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
100{
101 int32_t prev, status;
102 do {
103 __asm__ __volatile__ (
104 " ll %[prev], (%[ptr])\n"
105 " li %[status], 1\n"
106 " bne %[prev], %[old], 9f\n"
107 " move %[status], %[new_value]\n"
108 " sc %[status], (%[ptr])\n"
109 "9:\n"
110 : [prev] "=&r" (prev), [status] "=&r" (status)
111 : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value)
112 );
113 } while (__builtin_expect(status == 0, 0));
114 return prev != old_value;
115}
116
117extern ANDROID_ATOMIC_INLINE
118int64_t android_atomic_cas64(int64_t old_value, int64_t new_value,
119 volatile int64_t *ptr)
120{
121 return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
122}
123
124extern ANDROID_ATOMIC_INLINE
125int android_atomic_acquire_cas(int32_t old_value,
126 int32_t new_value,
127 volatile int32_t *ptr)
128{
129 int status = android_atomic_cas(old_value, new_value, ptr);
130 android_memory_barrier();
131 return status;
132}
133
134extern ANDROID_ATOMIC_INLINE
135int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
136 volatile int64_t *ptr)
137{
138 int status = android_atomic_cas64(old_value, new_value, ptr);
139 android_memory_barrier();
140 return status;
141}
142
143extern ANDROID_ATOMIC_INLINE
144int android_atomic_release_cas(int32_t old_value,
145 int32_t new_value,
146 volatile int32_t *ptr)
147{
148 android_memory_barrier();
149 return android_atomic_cas(old_value, new_value, ptr);
150}
151
152extern ANDROID_ATOMIC_INLINE
153int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
154 volatile int64_t *ptr)
155{
156 android_memory_barrier();
157 return android_atomic_cas64(old_value, new_value, ptr);
158}
159
160extern ANDROID_ATOMIC_INLINE
Duane Sand84c3e992014-04-29 11:19:26 -0700161int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
162{
163 int32_t prev, status;
164 android_memory_barrier();
165 do {
166 __asm__ __volatile__ (
167 " ll %[prev], (%[ptr])\n"
168 " addu %[status], %[prev], %[inc]\n"
169 " sc %[status], (%[ptr])\n"
170 : [status] "=&r" (status), [prev] "=&r" (prev)
171 : [ptr] "r" (ptr), [inc] "Ir" (increment)
172 );
173 } while (__builtin_expect(status == 0, 0));
174 return prev;
175}
176
177extern ANDROID_ATOMIC_INLINE int32_t
178android_atomic_inc(volatile int32_t *addr)
179{
180 return android_atomic_add(1, addr);
181}
182
183extern ANDROID_ATOMIC_INLINE int32_t
184android_atomic_dec(volatile int32_t *addr)
185{
186 return android_atomic_add(-1, addr);
187}
188
189extern ANDROID_ATOMIC_INLINE int32_t
190android_atomic_and(int32_t value, volatile int32_t *ptr)
191{
192 int32_t prev, status;
193 android_memory_barrier();
194 do {
195 __asm__ __volatile__ (
196 " ll %[prev], (%[ptr])\n"
197 " and %[status], %[prev], %[value]\n"
198 " sc %[status], (%[ptr])\n"
199 : [prev] "=&r" (prev), [status] "=&r" (status)
200 : [ptr] "r" (ptr), [value] "Ir" (value)
201 );
202 } while (__builtin_expect(status == 0, 0));
203 return prev;
204}
205
206extern ANDROID_ATOMIC_INLINE int32_t
207android_atomic_or(int32_t value, volatile int32_t *ptr)
208{
209 int32_t prev, status;
210 android_memory_barrier();
211 do {
212 __asm__ __volatile__ (
213 " ll %[prev], (%[ptr])\n"
214 " or %[status], %[prev], %[value]\n"
215 " sc %[status], (%[ptr])\n"
216 : [prev] "=&r" (prev), [status] "=&r" (status)
217 : [ptr] "r" (ptr), [value] "Ir" (value)
218 );
219 } while (__builtin_expect(status == 0, 0));
220 return prev;
221}
222
223#endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */