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
63template <class _ValueType>
64inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange_strong(
65 _ValueType* __val,
66 _ValueType* __expected,
67 _ValueType __after,
68 int __success_order = _AO_Seq,
69 int __fail_order = _AO_Seq) {
70 return __atomic_compare_exchange_n(__val, __expected, __after, false, __success_order, __fail_order);
71}
72
73#else // _LIBCPP_HAS_THREADS
74
75enum __libcpp_atomic_order { _AO_Relaxed, _AO_Consume, _AO_Acquire, _AO_Release, _AO_Acq_Rel, _AO_Seq };
76
77template <class _ValueType, class _FromType>
78inline _LIBCPP_HIDE_FROM_ABI void __libcpp_atomic_store(_ValueType* __dest, _FromType __val, int = 0) {
79 *__dest = __val;
80}
81
82template <class _ValueType, class _FromType>
83inline _LIBCPP_HIDE_FROM_ABI void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val) {
84 *__dest = __val;
85}
86
87template <class _ValueType>
88inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_load(_ValueType const* __val, int = 0) {
89 return *__val;
90}
91
92template <class _ValueType, class _AddType>
93inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a, int = 0) {
94 return *__val += __a;
95}
96
97template <class _ValueType>
98inline _LIBCPP_HIDE_FROM_ABI _ValueType
99__libcpp_atomic_exchange(_ValueType* __target, _ValueType __value, int = _AO_Seq) {
100 _ValueType old = *__target;
101 *__target = __value;
102 return old;
103}
104
105template <class _ValueType>
106inline _LIBCPP_HIDE_FROM_ABI bool
107__libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) {
108 if (*__val == *__expected) {
109 *__val = __after;
110 return true;
111 }
112 *__expected = *__val;
113 return false;
114}
115
116template <class _ValueType>
117inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange_strong(
118 _ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) {
119 if (*__val == *__expected) {
120 *__val = __after;
121 return true;
122 }
123 *__expected = *__val;
124 return false;
125}
126
127#endif // _LIBCPP_HAS_THREADS
128
129_LIBCPP_END_NAMESPACE_STD
130
131#endif // ATOMIC_SUPPORT_H
132