1//===----------------------------------------------------------------------===////
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===////
8
9#ifndef ATOMIC_SUPPORT_H
10#define ATOMIC_SUPPORT_H
11
12#include <__config>
13
14_LIBCPP_BEGIN_NAMESPACE_STD
15
16#if _LIBCPP_HAS_THREADS
17
18enum __libcpp_atomic_order {
19 _AO_Relaxed = __ATOMIC_RELAXED,
20 _AO_Consume = __ATOMIC_CONSUME,
21 _AO_Acquire = __ATOMIC_ACQUIRE,
22 _AO_Release = __ATOMIC_RELEASE,
23 _AO_Acq_Rel = __ATOMIC_ACQ_REL,
24 _AO_Seq = __ATOMIC_SEQ_CST
25};
26
27template <class _ValueType, class _FromType>
28inline _LIBCPP_HIDE_FROM_ABI void __libcpp_atomic_store(_ValueType* __dest, _FromType __val, int __order = _AO_Seq) {
29 __atomic_store_n(__dest, __val, __order);
30}
31
32template <class _ValueType, class _FromType>
33inline _LIBCPP_HIDE_FROM_ABI void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val) {
34 __atomic_store_n(__dest, __val, _AO_Relaxed);
35}
36
37template <class _ValueType>
38inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_load(_ValueType const* __val, int __order = _AO_Seq) {
39 return __atomic_load_n(__val, __order);
40}
41
42template <class _ValueType, class _AddType>
43inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a, int __order = _AO_Seq) {
44 return __atomic_add_fetch(__val, __a, __order);
45}
46
47template <class _ValueType>
48inline _LIBCPP_HIDE_FROM_ABI _ValueType
49__libcpp_atomic_exchange(_ValueType* __target, _ValueType __value, int __order = _AO_Seq) {
50 return __atomic_exchange_n(__target, __value, __order);
51}
52
53template <class _ValueType>
54inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange(
55 _ValueType* __val,
56 _ValueType* __expected,
57 _ValueType __after,
58 int __success_order = _AO_Seq,
59 int __fail_order = _AO_Seq) {
60 return __atomic_compare_exchange_n(__val, __expected, __after, true, __success_order, __fail_order);
61}
62
63#else // _LIBCPP_HAS_THREADS
64
65enum __libcpp_atomic_order { _AO_Relaxed, _AO_Consume, _AO_Acquire, _AO_Release, _AO_Acq_Rel, _AO_Seq };
66
67template <class _ValueType, class _FromType>
68inline _LIBCPP_HIDE_FROM_ABI void __libcpp_atomic_store(_ValueType* __dest, _FromType __val, int = 0) {
69 *__dest = __val;
70}
71
72template <class _ValueType, class _FromType>
73inline _LIBCPP_HIDE_FROM_ABI void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val) {
74 *__dest = __val;
75}
76
77template <class _ValueType>
78inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_load(_ValueType const* __val, int = 0) {
79 return *__val;
80}
81
82template <class _ValueType, class _AddType>
83inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a, int = 0) {
84 return *__val += __a;
85}
86
87template <class _ValueType>
88inline _LIBCPP_HIDE_FROM_ABI _ValueType
89__libcpp_atomic_exchange(_ValueType* __target, _ValueType __value, int = _AO_Seq) {
90 _ValueType old = *__target;
91 *__target = __value;
92 return old;
93}
94
95template <class _ValueType>
96inline _LIBCPP_HIDE_FROM_ABI bool
97__libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) {
98 if (*__val == *__expected) {
99 *__val = __after;
100 return true;
101 }
102 *__expected = *__val;
103 return false;
104}
105
106#endif // _LIBCPP_HAS_THREADS
107
108_LIBCPP_END_NAMESPACE_STD
109
110#endif // ATOMIC_SUPPORT_H
111