Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_ATOMIC_H_ |
| 18 | #define ART_LIBARTBASE_BASE_ATOMIC_H_ |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 21 | #include <atomic> |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 22 | #include <limits> |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 23 | #include <vector> |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 27 | #include "macros.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 31 | enum class CASMode { |
| 32 | kStrong, |
| 33 | kWeak, |
| 34 | }; |
| 35 | |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 36 | template<typename T> |
Dan Albert | aab0f86 | 2014-08-11 16:38:02 -0700 | [diff] [blame] | 37 | class PACKED(sizeof(T)) Atomic : public std::atomic<T> { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 38 | public: |
Mathieu Chartier | 1a088d4 | 2017-07-18 11:43:57 -0700 | [diff] [blame] | 39 | Atomic<T>() : std::atomic<T>(T()) { } |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 40 | |
| 41 | explicit Atomic<T>(T value) : std::atomic<T>(value) { } |
| 42 | |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 43 | // Load data from an atomic variable with Java data memory order semantics. |
| 44 | // |
| 45 | // Promises memory access semantics of ordinary Java data. |
| 46 | // Does not order other memory accesses. |
| 47 | // Long and double accesses may be performed 32 bits at a time. |
| 48 | // There are no "cache coherence" guarantees; e.g. loads from the same location may be reordered. |
| 49 | // In contrast to normal C++ accesses, racing accesses are allowed. |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 50 | T LoadJavaData() const { |
| 51 | return this->load(std::memory_order_relaxed); |
| 52 | } |
| 53 | |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 54 | // Store data in an atomic variable with Java data memory ordering semantics. |
| 55 | // |
| 56 | // Promises memory access semantics of ordinary Java data. |
| 57 | // Does not order other memory accesses. |
| 58 | // Long and double accesses may be performed 32 bits at a time. |
| 59 | // There are no "cache coherence" guarantees; e.g. loads from the same location may be reordered. |
| 60 | // In contrast to normal C++ accesses, racing accesses are allowed. |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 61 | void StoreJavaData(T desired_value) { |
| 62 | this->store(desired_value, std::memory_order_relaxed); |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 65 | // Atomically replace the value with desired_value if it matches the expected_value. |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 66 | // Participates in total ordering of atomic operations. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 67 | bool CompareAndSetStrongSequentiallyConsistent(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 68 | return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_seq_cst); |
| 69 | } |
| 70 | |
| 71 | // The same, except it may fail spuriously. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 72 | bool CompareAndSetWeakSequentiallyConsistent(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 73 | return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_seq_cst); |
| 74 | } |
| 75 | |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 76 | // Atomically replace the value with desired_value if it matches the expected_value. Doesn't |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 77 | // imply ordering or synchronization constraints. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 78 | bool CompareAndSetStrongRelaxed(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 79 | return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_relaxed); |
| 80 | } |
| 81 | |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 82 | // Atomically replace the value with desired_value if it matches the expected_value. Prior writes |
Mathieu Chartier | fdd513d | 2017-06-01 11:26:50 -0700 | [diff] [blame] | 83 | // to other memory locations become visible to the threads that do a consume or an acquire on the |
| 84 | // same location. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 85 | bool CompareAndSetStrongRelease(T expected_value, T desired_value) { |
Mathieu Chartier | fdd513d | 2017-06-01 11:26:50 -0700 | [diff] [blame] | 86 | return this->compare_exchange_strong(expected_value, desired_value, std::memory_order_release); |
| 87 | } |
| 88 | |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 89 | // The same, except it may fail spuriously. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 90 | bool CompareAndSetWeakRelaxed(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 91 | return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_relaxed); |
| 92 | } |
| 93 | |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 94 | // Atomically replace the value with desired_value if it matches the expected_value. Prior writes |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 95 | // made to other memory locations by the thread that did the release become visible in this |
| 96 | // thread. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 97 | bool CompareAndSetWeakAcquire(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 98 | return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_acquire); |
| 99 | } |
| 100 | |
Orion Hodson | 4131d10 | 2018-01-03 14:04:42 +0000 | [diff] [blame] | 101 | // Atomically replace the value with desired_value if it matches the expected_value. Prior writes |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 102 | // to other memory locations become visible to the threads that do a consume or an acquire on the |
| 103 | // same location. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 104 | bool CompareAndSetWeakRelease(T expected_value, T desired_value) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 105 | return this->compare_exchange_weak(expected_value, desired_value, std::memory_order_release); |
| 106 | } |
| 107 | |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 108 | bool CompareAndSet(T expected_value, |
| 109 | T desired_value, |
| 110 | CASMode mode, |
| 111 | std::memory_order memory_order) { |
| 112 | return mode == CASMode::kStrong |
| 113 | ? this->compare_exchange_strong(expected_value, desired_value, memory_order) |
| 114 | : this->compare_exchange_weak(expected_value, desired_value, memory_order); |
| 115 | } |
| 116 | |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 117 | // Returns the address of the current atomic variable. This is only used by futex() which is |
| 118 | // declared to take a volatile address (see base/mutex-inl.h). |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 119 | volatile T* Address() { |
| 120 | return reinterpret_cast<T*>(this); |
| 121 | } |
| 122 | |
| 123 | static T MaxValue() { |
| 124 | return std::numeric_limits<T>::max(); |
| 125 | } |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 128 | typedef Atomic<int32_t> AtomicInteger; |
| 129 | |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 130 | static_assert(sizeof(AtomicInteger) == sizeof(int32_t), "Weird AtomicInteger size"); |
| 131 | static_assert(alignof(AtomicInteger) == alignof(int32_t), |
| 132 | "AtomicInteger alignment differs from that of underlyingtype"); |
| 133 | static_assert(sizeof(Atomic<int64_t>) == sizeof(int64_t), "Weird Atomic<int64> size"); |
Dan Albert | aab0f86 | 2014-08-11 16:38:02 -0700 | [diff] [blame] | 134 | |
| 135 | // Assert the alignment of 64-bit integers is 64-bit. This isn't true on certain 32-bit |
| 136 | // architectures (e.g. x86-32) but we know that 64-bit integers here are arranged to be 8-byte |
| 137 | // aligned. |
Hans Boehm | 2f4a2ed | 2014-06-06 18:17:43 -0700 | [diff] [blame] | 138 | #if defined(__LP64__) |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 139 | static_assert(alignof(Atomic<int64_t>) == alignof(int64_t), |
| 140 | "Atomic<int64> alignment differs from that of underlying type"); |
Hans Boehm | 2f4a2ed | 2014-06-06 18:17:43 -0700 | [diff] [blame] | 141 | #endif |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 142 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 143 | } // namespace art |
| 144 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 145 | #endif // ART_LIBARTBASE_BASE_ATOMIC_H_ |