xref: /aosp_15_r20/art/libartbase/base/atomic.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_ATOMIC_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_ATOMIC_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
21*795d594fSAndroid Build Coastguard Worker #include <atomic>
22*795d594fSAndroid Build Coastguard Worker #include <limits>
23*795d594fSAndroid Build Coastguard Worker #include <vector>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "macros.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker enum class CASMode {
32*795d594fSAndroid Build Coastguard Worker   kStrong,
33*795d594fSAndroid Build Coastguard Worker   kWeak,
34*795d594fSAndroid Build Coastguard Worker };
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker template<typename T>
PACKED(sizeof (T))37*795d594fSAndroid Build Coastguard Worker class PACKED(sizeof(T)) Atomic : public std::atomic<T> {
38*795d594fSAndroid Build Coastguard Worker  public:
39*795d594fSAndroid Build Coastguard Worker   Atomic<T>() : std::atomic<T>(T()) { }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker   explicit Atomic<T>(T value) : std::atomic<T>(value) { }
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker   // Load data from an atomic variable with Java data memory order semantics.
44*795d594fSAndroid Build Coastguard Worker   //
45*795d594fSAndroid Build Coastguard Worker   // Promises memory access semantics of ordinary Java data.
46*795d594fSAndroid Build Coastguard Worker   // Does not order other memory accesses.
47*795d594fSAndroid Build Coastguard Worker   // Long and double accesses may be performed 32 bits at a time.
48*795d594fSAndroid Build Coastguard Worker   // There are no "cache coherence" guarantees; e.g. loads from the same location may be reordered.
49*795d594fSAndroid Build Coastguard Worker   // In contrast to normal C++ accesses, racing accesses are allowed.
50*795d594fSAndroid Build Coastguard Worker   T LoadJavaData() const {
51*795d594fSAndroid Build Coastguard Worker     return this->load(std::memory_order_relaxed);
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // Store data in an atomic variable with Java data memory ordering semantics.
55*795d594fSAndroid Build Coastguard Worker   //
56*795d594fSAndroid Build Coastguard Worker   // Promises memory access semantics of ordinary Java data.
57*795d594fSAndroid Build Coastguard Worker   // Does not order other memory accesses.
58*795d594fSAndroid Build Coastguard Worker   // Long and double accesses may be performed 32 bits at a time.
59*795d594fSAndroid Build Coastguard Worker   // There are no "cache coherence" guarantees; e.g. loads from the same location may be reordered.
60*795d594fSAndroid Build Coastguard Worker   // In contrast to normal C++ accesses, racing accesses are allowed.
61*795d594fSAndroid Build Coastguard Worker   void StoreJavaData(T desired_value) {
62*795d594fSAndroid Build Coastguard Worker     this->store(desired_value, std::memory_order_relaxed);
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value.
66*795d594fSAndroid Build Coastguard Worker   // Participates in total ordering of atomic operations.
67*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetStrongSequentiallyConsistent(T expected_value, T desired_value) {
68*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_seq_cst);
69*795d594fSAndroid Build Coastguard Worker   }
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker   // The same, except it may fail spuriously.
72*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetWeakSequentiallyConsistent(T expected_value, T desired_value) {
73*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_seq_cst);
74*795d594fSAndroid Build Coastguard Worker   }
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value. Doesn't
77*795d594fSAndroid Build Coastguard Worker   // imply ordering or synchronization constraints.
78*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetStrongRelaxed(T expected_value, T desired_value) {
79*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_relaxed);
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value. Prior writes
83*795d594fSAndroid Build Coastguard Worker   // to other memory locations become visible to the threads that do a consume or an acquire on the
84*795d594fSAndroid Build Coastguard Worker   // same location.
85*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetStrongRelease(T expected_value, T desired_value) {
86*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_release);
87*795d594fSAndroid Build Coastguard Worker   }
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker   // The same, except it may fail spuriously.
90*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetWeakRelaxed(T expected_value, T desired_value) {
91*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_relaxed);
92*795d594fSAndroid Build Coastguard Worker   }
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value. Prior writes
95*795d594fSAndroid Build Coastguard Worker   // made to other memory locations by the thread that did the release become visible in this
96*795d594fSAndroid Build Coastguard Worker   // thread.
97*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetWeakAcquire(T expected_value, T desired_value) {
98*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_acquire);
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value. Prior writes
102*795d594fSAndroid Build Coastguard Worker   // to other memory locations become visible to the threads that do a consume or an acquire on the
103*795d594fSAndroid Build Coastguard Worker   // same location.
104*795d594fSAndroid Build Coastguard Worker   bool CompareAndSetWeakRelease(T expected_value, T desired_value) {
105*795d594fSAndroid Build Coastguard Worker     return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_release);
106*795d594fSAndroid Build Coastguard Worker   }
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker   // Atomically replace the value with desired_value if it matches the expected_value.
109*795d594fSAndroid Build Coastguard Worker   // Participates in total ordering of atomic operations.
110*795d594fSAndroid Build Coastguard Worker   // Returns the existing value before the exchange. In other words, if the returned value is the
111*795d594fSAndroid Build Coastguard Worker   // same as expected_value, as passed to this method, the exchange has completed successfully.
112*795d594fSAndroid Build Coastguard Worker   // Otherwise the value was left unchanged.
113*795d594fSAndroid Build Coastguard Worker   T CompareAndExchangeStrongSequentiallyConsistent(T expected_value, T desired_value) {
114*795d594fSAndroid Build Coastguard Worker     // compare_exchange_strong() modifies expected_value if the actual value found is different from
115*795d594fSAndroid Build Coastguard Worker     // what was expected. In other words expected_value is changed if compare_exchange_strong
116*795d594fSAndroid Build Coastguard Worker     // returns false.
117*795d594fSAndroid Build Coastguard Worker     this->compare_exchange_strong(expected_value, desired_value, std::memory_order_seq_cst);
118*795d594fSAndroid Build Coastguard Worker     return expected_value;
119*795d594fSAndroid Build Coastguard Worker   }
120*795d594fSAndroid Build Coastguard Worker 
121*795d594fSAndroid Build Coastguard Worker   bool CompareAndSet(T expected_value,
122*795d594fSAndroid Build Coastguard Worker                      T desired_value,
123*795d594fSAndroid Build Coastguard Worker                      CASMode mode,
124*795d594fSAndroid Build Coastguard Worker                      std::memory_order memory_order) {
125*795d594fSAndroid Build Coastguard Worker     return mode == CASMode::kStrong
126*795d594fSAndroid Build Coastguard Worker         ? this->compare_exchange_strong(expected_value, desired_value, memory_order)
127*795d594fSAndroid Build Coastguard Worker         : this->compare_exchange_weak(expected_value, desired_value, memory_order);
128*795d594fSAndroid Build Coastguard Worker   }
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // Returns the address of the current atomic variable. This is only used by futex() which is
131*795d594fSAndroid Build Coastguard Worker   // declared to take a volatile address (see base/mutex-inl.h).
132*795d594fSAndroid Build Coastguard Worker   volatile T* Address() {
133*795d594fSAndroid Build Coastguard Worker     return reinterpret_cast<T*>(this);
134*795d594fSAndroid Build Coastguard Worker   }
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker   static T MaxValue() {
137*795d594fSAndroid Build Coastguard Worker     return std::numeric_limits<T>::max();
138*795d594fSAndroid Build Coastguard Worker   }
139*795d594fSAndroid Build Coastguard Worker };
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker // Increment a debug- or statistics-only counter when there is a single writer, especially if
142*795d594fSAndroid Build Coastguard Worker // concurrent reads are uncommon. Usually appreciably faster in this case.
143*795d594fSAndroid Build Coastguard Worker // NOT suitable as an approximate counter with multiple writers.
144*795d594fSAndroid Build Coastguard Worker template <typename T>
IncrementStatsCounter(std::atomic<T> * a)145*795d594fSAndroid Build Coastguard Worker void IncrementStatsCounter(std::atomic<T>* a) {
146*795d594fSAndroid Build Coastguard Worker   a->store(a->load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
147*795d594fSAndroid Build Coastguard Worker }
148*795d594fSAndroid Build Coastguard Worker 
149*795d594fSAndroid Build Coastguard Worker using AtomicInteger = Atomic<int32_t>;
150*795d594fSAndroid Build Coastguard Worker 
151*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(AtomicInteger) == sizeof(int32_t), "Weird AtomicInteger size");
152*795d594fSAndroid Build Coastguard Worker static_assert(alignof(AtomicInteger) == alignof(int32_t),
153*795d594fSAndroid Build Coastguard Worker               "AtomicInteger alignment differs from that of underlyingtype");
154*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(Atomic<int64_t>) == sizeof(int64_t), "Weird Atomic<int64> size");
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker // Assert the alignment of 64-bit integers is 64-bit. This isn't true on certain 32-bit
157*795d594fSAndroid Build Coastguard Worker // architectures (e.g. x86-32) but we know that 64-bit integers here are arranged to be 8-byte
158*795d594fSAndroid Build Coastguard Worker // aligned.
159*795d594fSAndroid Build Coastguard Worker #if defined(__LP64__)
160*795d594fSAndroid Build Coastguard Worker   static_assert(alignof(Atomic<int64_t>) == alignof(int64_t),
161*795d594fSAndroid Build Coastguard Worker                 "Atomic<int64> alignment differs from that of underlying type");
162*795d594fSAndroid Build Coastguard Worker #endif
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker }  // namespace art
165*795d594fSAndroid Build Coastguard Worker 
166*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_ATOMIC_H_
167