1 /*
2 * Copyright (C) 2022 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 ART_RUNTIME_BASE_ATOMIC_PAIR_H_
18 #define ART_RUNTIME_BASE_ATOMIC_PAIR_H_
19
20 #include <android-base/logging.h>
21
22 #include <atomic>
23 #include <type_traits>
24
25 #include "base/macros.h"
26 #include "base/time_utils.h"
27
28 namespace art HIDDEN {
29
30 // Implement 16-byte atomic pair using the seq-lock synchronization algorithm.
31 // This is currently only used for DexCache.
32 //
33 // This uses top 4-bytes of the key as version counter and lock bit,
34 // which means the stored pair key can not use those bytes.
35 //
36 // This allows us to read the cache without exclusive access to the cache line.
37 //
38 // The 8-byte atomic pair uses the normal single-instruction implementation.
39 //
40 static constexpr uint64_t kSeqMask = (0xFFFFFFFFull << 32);
41 static constexpr uint64_t kSeqLock = (0x80000000ull << 32);
42 static constexpr uint64_t kSeqIncr = (0x00000001ull << 32);
43 static constexpr uint kAtomicPairMaxSpins = 10'000u;
44 static constexpr uint kAtomicPairSleepNanos = 5'000u;
45
46 // std::pair<> is not trivially copyable and as such it is unsuitable for atomic operations.
47 template <typename IntType>
48 struct PACKED(2 * sizeof(IntType)) AtomicPair {
49 static_assert(std::is_integral_v<IntType>);
50
AtomicPairAtomicPair51 AtomicPair(IntType f, IntType s) : key(f), val(s) {}
52
53 IntType key;
54 IntType val;
55 };
56
57 template <typename IntType>
AtomicPairLoadAcquire(AtomicPair<IntType> * pair)58 ALWAYS_INLINE static inline AtomicPair<IntType> AtomicPairLoadAcquire(AtomicPair<IntType>* pair) {
59 static_assert(std::is_trivially_copyable<AtomicPair<IntType>>::value);
60 auto* target = reinterpret_cast<std::atomic<AtomicPair<IntType>>*>(pair);
61 return target->load(std::memory_order_acquire);
62 }
63
64 template <typename IntType>
AtomicPairStoreRelease(AtomicPair<IntType> * pair,AtomicPair<IntType> value)65 ALWAYS_INLINE static inline void AtomicPairStoreRelease(AtomicPair<IntType>* pair,
66 AtomicPair<IntType> value) {
67 static_assert(std::is_trivially_copyable<AtomicPair<IntType>>::value);
68 auto* target = reinterpret_cast<std::atomic<AtomicPair<IntType>>*>(pair);
69 target->store(value, std::memory_order_release);
70 }
71
AtomicPairLoadAcquire(AtomicPair<uint64_t> * pair)72 ALWAYS_INLINE static inline AtomicPair<uint64_t> AtomicPairLoadAcquire(AtomicPair<uint64_t>* pair) {
73 auto* key_ptr = reinterpret_cast<std::atomic_uint64_t*>(&pair->key);
74 auto* val_ptr = reinterpret_cast<std::atomic_uint64_t*>(&pair->val);
75 for (uint i = 0;; ++i) {
76 uint64_t key0 = key_ptr->load(std::memory_order_acquire);
77 uint64_t val = val_ptr->load(std::memory_order_acquire);
78 uint64_t key1 = key_ptr->load(std::memory_order_relaxed);
79 uint64_t key = key0 & ~kSeqMask;
80 if (LIKELY((key0 & kSeqLock) == 0 && key0 == key1)) {
81 return {key, val};
82 }
83 if (UNLIKELY(i > kAtomicPairMaxSpins)) {
84 NanoSleep(kAtomicPairSleepNanos);
85 }
86 }
87 }
88
AtomicPairStoreRelease(AtomicPair<uint64_t> * pair,AtomicPair<uint64_t> value)89 ALWAYS_INLINE static inline void AtomicPairStoreRelease(AtomicPair<uint64_t>* pair,
90 AtomicPair<uint64_t> value) {
91 DCHECK((value.key & kSeqMask) == 0) << "Key=0x" << std::hex << value.key;
92 auto* key_ptr = reinterpret_cast<std::atomic_uint64_t*>(&pair->key);
93 auto* val_ptr = reinterpret_cast<std::atomic_uint64_t*>(&pair->val);
94 uint64_t key = key_ptr->load(std::memory_order_relaxed);
95 for (uint i = 0;; ++i) {
96 key &= ~kSeqLock; // Ensure that the CAS below fails if the lock bit is already set.
97 if (LIKELY(key_ptr->compare_exchange_weak(key, key | kSeqLock))) {
98 break;
99 }
100 if (UNLIKELY(i > kAtomicPairMaxSpins)) {
101 NanoSleep(kAtomicPairSleepNanos);
102 }
103 }
104 key = (((key & kSeqMask) + kSeqIncr) & ~kSeqLock) | (value.key & ~kSeqMask);
105 val_ptr->store(value.val, std::memory_order_release);
106 key_ptr->store(key, std::memory_order_release);
107 }
108
109 } // namespace art
110
111 #endif // ART_RUNTIME_BASE_ATOMIC_PAIR_H_
112