blob: ba5a218aee93f7ac9d87a43d3701532f9cd7144d [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
Ashok Bhatc15d2ce2013-11-13 16:20:49 +000049extern ANDROID_ATOMIC_INLINE
50void android_memory_barrier(void)
51{
52 __asm__ __volatile__ ("dmb ish" : : : "memory");
53}
54extern ANDROID_ATOMIC_INLINE
55void android_memory_store_barrier(void)
56{
Colin Cross35106372014-01-22 19:04:28 -080057 __asm__ __volatile__ ("dmb ishst" : : : "memory");
Ashok Bhatc15d2ce2013-11-13 16:20:49 +000058}
Ashok Bhatc15d2ce2013-11-13 16:20:49 +000059
60extern ANDROID_ATOMIC_INLINE
61int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
62{
63 int32_t value = *ptr;
64 android_memory_barrier();
65 return value;
66}
67
68extern ANDROID_ATOMIC_INLINE
69int64_t android_atomic_acquire_load64(volatile const int64_t *ptr)
70{
71 int64_t value = *ptr;
72 android_memory_barrier();
73 return value;
74}
75
76extern ANDROID_ATOMIC_INLINE
77int32_t android_atomic_release_load(volatile const int32_t *ptr)
78{
79 android_memory_barrier();
80 return *ptr;
81}
82
83extern ANDROID_ATOMIC_INLINE
84int64_t android_atomic_release_load64(volatile const int64_t *ptr)
85{
86 android_memory_barrier();
87 return *ptr;
88}
89
90extern ANDROID_ATOMIC_INLINE
91void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
92{
93 *ptr = value;
94 android_memory_barrier();
95}
96
97extern ANDROID_ATOMIC_INLINE
98void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr)
99{
100 *ptr = value;
101 android_memory_barrier();
102}
103
104extern ANDROID_ATOMIC_INLINE
105void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
106{
107 android_memory_barrier();
108 *ptr = value;
109}
110
111extern ANDROID_ATOMIC_INLINE
112void android_atomic_release_store64(int64_t value, volatile int64_t *ptr)
113{
114 android_memory_barrier();
115 *ptr = value;
116}
117
118extern ANDROID_ATOMIC_INLINE
119int android_atomic_cas(int32_t old_value, int32_t new_value,
120 volatile int32_t *ptr)
121{
122 return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
123}
124
125extern ANDROID_ATOMIC_INLINE
126int64_t android_atomic_cas64(int64_t old_value, int64_t new_value,
127 volatile int64_t *ptr)
128{
129 return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
130}
131
132extern ANDROID_ATOMIC_INLINE
133int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
134 volatile int32_t *ptr)
135{
136 int status = android_atomic_cas(old_value, new_value, ptr);
137 android_memory_barrier();
138 return status;
139}
140
141extern ANDROID_ATOMIC_INLINE
142int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
143 volatile int64_t *ptr)
144{
145 int status = android_atomic_cas64(old_value, new_value, ptr);
146 android_memory_barrier();
147 return status;
148}
149
150extern ANDROID_ATOMIC_INLINE
151int android_atomic_release_cas(int32_t old_value, int32_t new_value,
152 volatile int32_t *ptr)
153{
154 android_memory_barrier();
155 return android_atomic_cas(old_value, new_value, ptr);
156}
157
158extern ANDROID_ATOMIC_INLINE
159int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
160 volatile int64_t *ptr)
161{
162 android_memory_barrier();
163 return android_atomic_cas64(old_value, new_value, ptr);
164}
165
166extern ANDROID_ATOMIC_INLINE
167int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
168{
169 int32_t prev, status;
170 android_memory_barrier();
171 do {
172 prev = *ptr;
173 status = android_atomic_cas(prev, prev + increment, ptr);
174 } while (__builtin_expect(status != 0, 0));
175 return prev;
176}
177
178extern ANDROID_ATOMIC_INLINE
179int32_t android_atomic_inc(volatile int32_t *addr)
180{
181 return android_atomic_add(1, addr);
182}
183
184extern ANDROID_ATOMIC_INLINE
185int32_t android_atomic_dec(volatile int32_t *addr)
186{
187 return android_atomic_add(-1, addr);
188}
189
190extern ANDROID_ATOMIC_INLINE
191int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
192{
193 int32_t prev, status;
194 android_memory_barrier();
195 do {
196 prev = *ptr;
197 status = android_atomic_cas(prev, prev & value, ptr);
198 } while (__builtin_expect(status != 0, 0));
199 return prev;
200}
201
202extern ANDROID_ATOMIC_INLINE
203int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
204{
205 int32_t prev, status;
206 android_memory_barrier();
207 do {
208 prev = *ptr;
209 status = android_atomic_cas(prev, prev | value, ptr);
210 } while (__builtin_expect(status != 0, 0));
211 return prev;
212}
213
214#endif /* ANDROID_CUTILS_ATOMIC_AARCH64_H */