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 "base/macros.h"
21
22 #include <type_traits>
23
24 namespace art {
25
26 // std::pair<> is not trivially copyable and as such it is unsuitable for atomic operations.
27 template <typename IntType>
28 struct PACKED(2 * sizeof(IntType)) AtomicPair {
29 static_assert(std::is_integral_v<IntType>);
30
AtomicPairAtomicPair31 constexpr AtomicPair() : first(0), second(0) { }
AtomicPairAtomicPair32 AtomicPair(IntType f, IntType s) : first(f), second(s) { }
33 AtomicPair(const AtomicPair&) = default;
34 AtomicPair& operator=(const AtomicPair&) = default;
35
36 IntType first;
37 IntType second;
38 };
39
40 template <typename IntType>
AtomicPairLoadAcquire(std::atomic<AtomicPair<IntType>> * target)41 ALWAYS_INLINE static inline AtomicPair<IntType> AtomicPairLoadAcquire(
42 std::atomic<AtomicPair<IntType>>* target) {
43 return target->load(std::memory_order_acquire);
44 }
45
46 template <typename IntType>
AtomicPairStoreRelease(std::atomic<AtomicPair<IntType>> * target,AtomicPair<IntType> value)47 ALWAYS_INLINE static inline void AtomicPairStoreRelease(std::atomic<AtomicPair<IntType>>* target,
48 AtomicPair<IntType> value) {
49 target->store(value, std::memory_order_release);
50 }
51
52 // LLVM uses generic lock-based implementation for x86_64, we can do better with CMPXCHG16B.
53 #if defined(__x86_64__)
AtomicPairLoadAcquire(std::atomic<AtomicPair<uint64_t>> * target)54 ALWAYS_INLINE static inline AtomicPair<uint64_t> AtomicPairLoadAcquire(
55 std::atomic<AtomicPair<uint64_t>>* target) {
56 uint64_t first, second;
57 __asm__ __volatile__(
58 "lock cmpxchg16b (%2)"
59 : "=&a"(first), "=&d"(second)
60 : "r"(target), "a"(0), "d"(0), "b"(0), "c"(0)
61 : "cc");
62 return {first, second};
63 }
64
AtomicPairStoreRelease(std::atomic<AtomicPair<uint64_t>> * target,AtomicPair<uint64_t> value)65 ALWAYS_INLINE static inline void AtomicPairStoreRelease(
66 std::atomic<AtomicPair<uint64_t>>* target, AtomicPair<uint64_t> value) {
67 uint64_t first, second;
68 __asm__ __volatile__ (
69 "movq (%2), %%rax\n\t"
70 "movq 8(%2), %%rdx\n\t"
71 "1:\n\t"
72 "lock cmpxchg16b (%2)\n\t"
73 "jnz 1b"
74 : "=&a"(first), "=&d"(second)
75 : "r"(target), "b"(value.first), "c"(value.second)
76 : "cc");
77 }
78 #endif // defined(__x86_64__)
79
80 } // namespace art
81
82 #endif // ART_RUNTIME_BASE_ATOMIC_PAIR_H_
83