blob: 0c88bfedd3a2a987a21e8daddebf3f3fbf3ed92c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 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_H
18#define ANDROID_CUTILS_ATOMIC_H
19
20#include <stdint.h>
21#include <sys/types.h>
Hans Boehmc5664892014-10-05 16:04:54 +000022#include <stdatomic.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
Hans Boehmc5664892014-10-05 16:04:54 +000024#ifndef ANDROID_ATOMIC_INLINE
25#define ANDROID_ATOMIC_INLINE static inline
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#endif
27
28/*
Hans Boehm5af78772014-07-11 18:30:13 -070029 * A handful of basic atomic operations.
30 * THESE ARE HERE FOR LEGACY REASONS ONLY. AVOID.
31 *
32 * PREFERRED ALTERNATIVES:
33 * - Use C++/C/pthread locks/mutexes whenever there is not a
34 * convincing reason to do otherwise. Note that very clever and
35 * complicated, but correct, lock-free code is often slower than
36 * using locks, especially where nontrivial data structures
37 * are involved.
38 * - C11 stdatomic.h.
39 * - Where supported, C++11 std::atomic<T> .
40 *
41 * PLEASE STOP READING HERE UNLESS YOU ARE TRYING TO UNDERSTAND
42 * OR UPDATE OLD CODE.
Andy McFadden8dfa47d2010-05-27 10:10:18 -070043 *
44 * The "acquire" and "release" terms can be defined intuitively in terms
45 * of the placement of memory barriers in a simple lock implementation:
46 * - wait until compare-and-swap(lock-is-free --> lock-is-held) succeeds
47 * - barrier
48 * - [do work]
49 * - barrier
50 * - store(lock-is-free)
51 * In very crude terms, the initial (acquire) barrier prevents any of the
52 * "work" from happening before the lock is held, and the later (release)
53 * barrier ensures that all of the work happens before the lock is released.
54 * (Think of cached writes, cache read-ahead, and instruction reordering
55 * around the CAS and store instructions.)
56 *
57 * The barriers must apply to both the compiler and the CPU. Note it is
58 * legal for instructions that occur before an "acquire" barrier to be
59 * moved down below it, and for instructions that occur after a "release"
60 * barrier to be moved up above it.
61 *
62 * The ARM-driven implementation we use here is short on subtlety,
63 * and actually requests a full barrier from the compiler and the CPU.
64 * The only difference between acquire and release is in whether they
65 * are issued before or after the atomic operation with which they
66 * are associated. To ease the transition to C/C++ atomic intrinsics,
67 * you should not rely on this, and instead assume that only the minimal
68 * acquire/release protection is provided.
69 *
70 * NOTE: all int32_t* values are expected to be aligned on 32-bit boundaries.
71 * If they are not, atomicity is not guaranteed.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 */
73
Colin Crossec69c052016-09-15 18:01:41 -070074ANDROID_ATOMIC_INLINE
75volatile atomic_int_least32_t* to_atomic_int_least32_t(volatile const int32_t* addr) {
76#ifdef __cplusplus
77 return reinterpret_cast<volatile atomic_int_least32_t*>(const_cast<volatile int32_t*>(addr));
78#else
79 return (volatile atomic_int_least32_t*)addr;
80#endif
81}
82
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083/*
Andy McFadden8dfa47d2010-05-27 10:10:18 -070084 * Basic arithmetic and bitwise operations. These all provide a
85 * barrier with "release" ordering, and return the previous value.
86 *
87 * These have the same characteristics (e.g. what happens on overflow)
88 * as the equivalent non-atomic C operations.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 */
Hans Boehmc5664892014-10-05 16:04:54 +000090ANDROID_ATOMIC_INLINE
91int32_t android_atomic_inc(volatile int32_t* addr)
92{
Colin Crossec69c052016-09-15 18:01:41 -070093 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +000094 /* Int32_t, if it exists, is the same as int_least32_t. */
95 return atomic_fetch_add_explicit(a, 1, memory_order_release);
96}
97
98ANDROID_ATOMIC_INLINE
99int32_t android_atomic_dec(volatile int32_t* addr)
100{
Colin Crossec69c052016-09-15 18:01:41 -0700101 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000102 return atomic_fetch_sub_explicit(a, 1, memory_order_release);
103}
104
105ANDROID_ATOMIC_INLINE
106int32_t android_atomic_add(int32_t value, volatile int32_t* addr)
107{
Colin Crossec69c052016-09-15 18:01:41 -0700108 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000109 return atomic_fetch_add_explicit(a, value, memory_order_release);
110}
111
112ANDROID_ATOMIC_INLINE
113int32_t android_atomic_and(int32_t value, volatile int32_t* addr)
114{
Colin Crossec69c052016-09-15 18:01:41 -0700115 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000116 return atomic_fetch_and_explicit(a, value, memory_order_release);
117}
118
119ANDROID_ATOMIC_INLINE
120int32_t android_atomic_or(int32_t value, volatile int32_t* addr)
121{
Colin Crossec69c052016-09-15 18:01:41 -0700122 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000123 return atomic_fetch_or_explicit(a, value, memory_order_release);
124}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126/*
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700127 * Perform an atomic load with "acquire" or "release" ordering.
Andy McFaddenac322da2010-05-19 22:33:28 -0700128 *
Hans Boehm5af78772014-07-11 18:30:13 -0700129 * Note that the notion of a "release" ordering for a load does not
130 * really fit into the C11 or C++11 memory model. The extra ordering
131 * is normally observable only by code using memory_order_relaxed
132 * atomics, or data races. In the rare cases in which such ordering
133 * is called for, use memory_order_relaxed atomics and a leading
134 * atomic_thread_fence (typically with memory_order_acquire,
135 * not memory_order_release!) instead. If you do not understand
136 * this comment, you are in the vast majority, and should not be
137 * using release loads or replacing them with anything other than
138 * locks or default sequentially consistent atomics.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 */
Hans Boehmc5664892014-10-05 16:04:54 +0000140ANDROID_ATOMIC_INLINE
141int32_t android_atomic_acquire_load(volatile const int32_t* addr)
142{
Colin Crossec69c052016-09-15 18:01:41 -0700143 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000144 return atomic_load_explicit(a, memory_order_acquire);
145}
146
147ANDROID_ATOMIC_INLINE
148int32_t android_atomic_release_load(volatile const int32_t* addr)
149{
Colin Crossec69c052016-09-15 18:01:41 -0700150 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000151 atomic_thread_fence(memory_order_seq_cst);
152 /* Any reasonable clients of this interface would probably prefer */
153 /* something weaker. But some remaining clients seem to be */
154 /* abusing this API in strange ways, e.g. by using it as a fence. */
155 /* Thus we are conservative until we can get rid of remaining */
156 /* clients (and this function). */
157 return atomic_load_explicit(a, memory_order_relaxed);
158}
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700159
160/*
161 * Perform an atomic store with "acquire" or "release" ordering.
162 *
Hans Boehmc5664892014-10-05 16:04:54 +0000163 * Note that the notion of an "acquire" ordering for a store does not
Hans Boehm5af78772014-07-11 18:30:13 -0700164 * really fit into the C11 or C++11 memory model. The extra ordering
165 * is normally observable only by code using memory_order_relaxed
166 * atomics, or data races. In the rare cases in which such ordering
167 * is called for, use memory_order_relaxed atomics and a trailing
168 * atomic_thread_fence (typically with memory_order_release,
169 * not memory_order_acquire!) instead.
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700170 */
Hans Boehmc5664892014-10-05 16:04:54 +0000171ANDROID_ATOMIC_INLINE
172void android_atomic_acquire_store(int32_t value, volatile int32_t* addr)
173{
Colin Crossec69c052016-09-15 18:01:41 -0700174 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000175 atomic_store_explicit(a, value, memory_order_relaxed);
176 atomic_thread_fence(memory_order_seq_cst);
177 /* Again overly conservative to accomodate weird clients. */
178}
179
180ANDROID_ATOMIC_INLINE
181void android_atomic_release_store(int32_t value, volatile int32_t* addr)
182{
Colin Crossec69c052016-09-15 18:01:41 -0700183 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
Hans Boehmc5664892014-10-05 16:04:54 +0000184 atomic_store_explicit(a, value, memory_order_release);
185}
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700186
187/*
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700188 * Compare-and-set operation with "acquire" or "release" ordering.
189 *
190 * This returns zero if the new value was successfully stored, which will
191 * only happen when *addr == oldvalue.
192 *
193 * (The return value is inverted from implementations on other platforms,
194 * but matches the ARM ldrex/strex result.)
195 *
196 * Implementations that use the release CAS in a loop may be less efficient
197 * than possible, because we re-issue the memory barrier on each iteration.
198 */
Hans Boehmc5664892014-10-05 16:04:54 +0000199ANDROID_ATOMIC_INLINE
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700200int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue,
Hans Boehmc5664892014-10-05 16:04:54 +0000201 volatile int32_t* addr)
202{
Colin Crossec69c052016-09-15 18:01:41 -0700203 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
204 return !atomic_compare_exchange_strong_explicit(
Hans Boehmc5664892014-10-05 16:04:54 +0000205 a, &oldvalue, newvalue,
206 memory_order_acquire,
Colin Crossec69c052016-09-15 18:01:41 -0700207 memory_order_acquire);
Hans Boehmc5664892014-10-05 16:04:54 +0000208}
209
210ANDROID_ATOMIC_INLINE
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700211int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
Hans Boehmc5664892014-10-05 16:04:54 +0000212 volatile int32_t* addr)
213{
Colin Crossec69c052016-09-15 18:01:41 -0700214 volatile atomic_int_least32_t* a = to_atomic_int_least32_t(addr);
215 return !atomic_compare_exchange_strong_explicit(
Hans Boehmc5664892014-10-05 16:04:54 +0000216 a, &oldvalue, newvalue,
217 memory_order_release,
Colin Crossec69c052016-09-15 18:01:41 -0700218 memory_order_relaxed);
Hans Boehmc5664892014-10-05 16:04:54 +0000219}
220
221/*
222 * Fence primitives.
223 */
224ANDROID_ATOMIC_INLINE
225void android_compiler_barrier(void)
226{
227 __asm__ __volatile__ ("" : : : "memory");
228 /* Could probably also be: */
229 /* atomic_signal_fence(memory_order_seq_cst); */
230}
231
232ANDROID_ATOMIC_INLINE
233void android_memory_barrier(void)
234{
235 atomic_thread_fence(memory_order_seq_cst);
236}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237
Andy McFaddenac322da2010-05-19 22:33:28 -0700238/*
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700239 * Aliases for code using an older version of this header. These are now
240 * deprecated and should not be used. The definitions will be removed
241 * in a future release.
Andy McFaddenac322da2010-05-19 22:33:28 -0700242 */
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700243#define android_atomic_write android_atomic_release_store
244#define android_atomic_cmpxchg android_atomic_release_cas
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246#endif // ANDROID_CUTILS_ATOMIC_H