1 /**
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef PANDA_LIBPANDABASE_UTILS_ATOMIC_H
16 #define PANDA_LIBPANDABASE_UTILS_ATOMIC_H
17
18 #include <atomic>
19 #include "libpandabase/macros.h"
20
21 namespace ark {
22 template <typename T>
AtomicStore(T * addr,T val,std::memory_order order)23 ALWAYS_INLINE inline void AtomicStore(T *addr, T val, std::memory_order order)
24 {
25 ASSERT(addr != nullptr);
26 // Atomic with parameterized order reason: memory order passed as argument
27 reinterpret_cast<std::atomic<T> *>(addr)->store(val, order);
28 }
29
30 template <typename T>
AtomicLoad(const T * addr,std::memory_order order)31 ALWAYS_INLINE inline T AtomicLoad(const T *addr, std::memory_order order)
32 {
33 ASSERT(addr != nullptr);
34 // Atomic with parameterized order reason: memory order passed as argument
35 return reinterpret_cast<const std::atomic<T> *>(addr)->load(order);
36 }
37
38 template <typename T>
AtomicCmpxchgStrong(T * addr,T expected,T newValue,std::memory_order order)39 ALWAYS_INLINE inline T AtomicCmpxchgStrong(T *addr, T expected, T newValue, std::memory_order order)
40 {
41 ASSERT(addr != nullptr);
42 // Atomic with parameterized order reason: memory order passed as argument
43 reinterpret_cast<std::atomic<T> *>(addr)->compare_exchange_strong(expected, newValue, order);
44 return expected;
45 }
46
47 template <typename T>
AtomicCmpxchgWeak(T * addr,T & expected,T newValue,std::memory_order orderSuccess,std::memory_order orderFailure)48 ALWAYS_INLINE inline bool AtomicCmpxchgWeak(T *addr, T &expected, T newValue, std::memory_order orderSuccess,
49 std::memory_order orderFailure)
50 {
51 ASSERT(addr != nullptr);
52 // Atomic with parameterized order reason: memory order passed as argument
53 return reinterpret_cast<std::atomic<T> *>(addr)->compare_exchange_weak(expected, newValue, orderSuccess,
54 orderFailure);
55 }
56 } // namespace ark
57
58 #endif // PANDA_LIBPANDABASE_UTILS_ATOMIC_H
59