• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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     // Atomic with parameterized order reason: memory order passed as argument
26     reinterpret_cast<std::atomic<T> *>(addr)->store(val, order);
27 }
28 
29 template <typename T>
AtomicLoad(T * addr,std::memory_order order)30 ALWAYS_INLINE inline T AtomicLoad(T *addr, std::memory_order order)
31 {
32     // Atomic with parameterized order reason: memory order passed as argument
33     return reinterpret_cast<const std::atomic<T> *>(addr)->load(order);
34 }
35 
36 template <typename T>
AtomicCmpxchgStrong(T * addr,T expected,T newValue,std::memory_order order)37 ALWAYS_INLINE inline T AtomicCmpxchgStrong(T *addr, T expected, T newValue, std::memory_order order)
38 {
39     // Atomic with parameterized order reason: memory order passed as argument
40     reinterpret_cast<std::atomic<T> *>(addr)->compare_exchange_strong(expected, newValue, order);
41     return expected;
42 }
43 }  // namespace ark
44 
45 #endif  // PANDA_LIBPANDABASE_UTILS_ATOMIC_H
46