blob: 927379f647a35aab2730815acfb4fdc7196d3a32 [file] [log] [blame]
Ashok Bhatc15d2ce2013-11-13 16:20:49 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef ANDROID_CUTILS_ATOMIC_AARCH64_H
30#define ANDROID_CUTILS_ATOMIC_AARCH64_H
31
32#include <stdint.h>
33
34#ifndef ANDROID_ATOMIC_INLINE
35#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
36#endif
37
38/*
39 TODOAArch64: Revisit the below functions and check for potential
40 optimizations using assembly code or otherwise.
41*/
42
43extern ANDROID_ATOMIC_INLINE
44void android_compiler_barrier(void)
45{
46 __asm__ __volatile__ ("" : : : "memory");
47}
48
49#if ANDROID_SMP == 0
50extern ANDROID_ATOMIC_INLINE
51void android_memory_barrier(void)
52{
53 android_compiler_barrier();
54}
55extern ANDROID_ATOMIC_INLINE
56void android_memory_store_barrier(void)
57{
58 android_compiler_barrier();
59}
60#else
61extern ANDROID_ATOMIC_INLINE
62void android_memory_barrier(void)
63{
64 __asm__ __volatile__ ("dmb ish" : : : "memory");
65}
66extern ANDROID_ATOMIC_INLINE
67void android_memory_store_barrier(void)
68{
69 __asm__ __volatile__ ("dmb isht" : : : "memory");
70}
71#endif
72
73extern ANDROID_ATOMIC_INLINE
74int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
75{
76 int32_t value = *ptr;
77 android_memory_barrier();
78 return value;
79}
80
81extern ANDROID_ATOMIC_INLINE
82int64_t android_atomic_acquire_load64(volatile const int64_t *ptr)
83{
84 int64_t value = *ptr;
85 android_memory_barrier();
86 return value;
87}
88
89extern ANDROID_ATOMIC_INLINE
90int32_t android_atomic_release_load(volatile const int32_t *ptr)
91{
92 android_memory_barrier();
93 return *ptr;
94}
95
96extern ANDROID_ATOMIC_INLINE
97int64_t android_atomic_release_load64(volatile const int64_t *ptr)
98{
99 android_memory_barrier();
100 return *ptr;
101}
102
103extern ANDROID_ATOMIC_INLINE
104void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
105{
106 *ptr = value;
107 android_memory_barrier();
108}
109
110extern ANDROID_ATOMIC_INLINE
111void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr)
112{
113 *ptr = value;
114 android_memory_barrier();
115}
116
117extern ANDROID_ATOMIC_INLINE
118void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
119{
120 android_memory_barrier();
121 *ptr = value;
122}
123
124extern ANDROID_ATOMIC_INLINE
125void android_atomic_release_store64(int64_t value, volatile int64_t *ptr)
126{
127 android_memory_barrier();
128 *ptr = value;
129}
130
131extern ANDROID_ATOMIC_INLINE
132int android_atomic_cas(int32_t old_value, int32_t new_value,
133 volatile int32_t *ptr)
134{
135 return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
136}
137
138extern ANDROID_ATOMIC_INLINE
139int64_t android_atomic_cas64(int64_t old_value, int64_t new_value,
140 volatile int64_t *ptr)
141{
142 return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
143}
144
145extern ANDROID_ATOMIC_INLINE
146int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
147 volatile int32_t *ptr)
148{
149 int status = android_atomic_cas(old_value, new_value, ptr);
150 android_memory_barrier();
151 return status;
152}
153
154extern ANDROID_ATOMIC_INLINE
155int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
156 volatile int64_t *ptr)
157{
158 int status = android_atomic_cas64(old_value, new_value, ptr);
159 android_memory_barrier();
160 return status;
161}
162
163extern ANDROID_ATOMIC_INLINE
164int android_atomic_release_cas(int32_t old_value, int32_t new_value,
165 volatile int32_t *ptr)
166{
167 android_memory_barrier();
168 return android_atomic_cas(old_value, new_value, ptr);
169}
170
171extern ANDROID_ATOMIC_INLINE
172int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
173 volatile int64_t *ptr)
174{
175 android_memory_barrier();
176 return android_atomic_cas64(old_value, new_value, ptr);
177}
178
179extern ANDROID_ATOMIC_INLINE
180int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
181{
182 int32_t prev, status;
183 android_memory_barrier();
184 do {
185 prev = *ptr;
186 status = android_atomic_cas(prev, prev + increment, ptr);
187 } while (__builtin_expect(status != 0, 0));
188 return prev;
189}
190
191extern ANDROID_ATOMIC_INLINE
192int32_t android_atomic_inc(volatile int32_t *addr)
193{
194 return android_atomic_add(1, addr);
195}
196
197extern ANDROID_ATOMIC_INLINE
198int32_t android_atomic_dec(volatile int32_t *addr)
199{
200 return android_atomic_add(-1, addr);
201}
202
203extern ANDROID_ATOMIC_INLINE
204int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
205{
206 int32_t prev, status;
207 android_memory_barrier();
208 do {
209 prev = *ptr;
210 status = android_atomic_cas(prev, prev & value, ptr);
211 } while (__builtin_expect(status != 0, 0));
212 return prev;
213}
214
215extern ANDROID_ATOMIC_INLINE
216int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
217{
218 int32_t prev, status;
219 android_memory_barrier();
220 do {
221 prev = *ptr;
222 status = android_atomic_cas(prev, prev | value, ptr);
223 } while (__builtin_expect(status != 0, 0));
224 return prev;
225}
226
227#endif /* ANDROID_CUTILS_ATOMIC_AARCH64_H */