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 _LIBCPP___ATOMIC_ATOMIC_SYNC_H
10#define _LIBCPP___ATOMIC_ATOMIC_SYNC_H
11
12#include <__atomic/atomic_waitable_traits.h>
13#include <__atomic/contention_t.h>
14#include <__atomic/memory_order.h>
15#include <__atomic/to_gcc_order.h>
16#include <__chrono/duration.h>
17#include <__config>
18#include <__memory/addressof.h>
19#include <__thread/poll_with_backoff.h>
20#include <__type_traits/conjunction.h>
21#include <__type_traits/decay.h>
22#include <__type_traits/invoke.h>
23#include <__type_traits/is_same.h>
24#include <__type_traits/void_t.h>
25#include <__utility/declval.h>
26#include <cstring>
27
28#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29# pragma GCC system_header
30#endif
31
32_LIBCPP_BEGIN_NAMESPACE_STD
33
34#if _LIBCPP_STD_VER >= 20
35# if _LIBCPP_HAS_THREADS
36
37# if !_LIBCPP_AVAILABILITY_HAS_NEW_SYNC
38
39// old dylib interface kept for backwards compatibility
40_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*) _NOEXCEPT;
41_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*) _NOEXCEPT;
42_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile*) _NOEXCEPT;
43_LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile*, __cxx_contention_t) _NOEXCEPT;
44
45_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;
46_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;
47_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t
48__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;
49_LIBCPP_EXPORTED_FROM_ABI void
50__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t) _NOEXCEPT;
51# endif // !_LIBCPP_AVAILABILITY_HAS_NEW_SYNC
52
53// new dylib interface
54
55// return the global contention state's current value for the address
56_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t
57__atomic_monitor_global(void const* __address) _NOEXCEPT;
58
59// wait on the global contention state to be changed from the given value for the address
60_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void
61__atomic_wait_global_table(void const* __address, __cxx_contention_t __monitor_value) _NOEXCEPT;
62
63// notify one waiter waiting on the global contention state for the address
64_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void __atomic_notify_one_global_table(void const*) _NOEXCEPT;
65
66// notify all waiters waiting on the global contention state for the address
67_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void __atomic_notify_all_global_table(void const*) _NOEXCEPT;
68
69// wait on the address directly with the native platform wait
70template <std::size_t _Size>
71_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void
72__atomic_wait_native(void const* __address, void const* __old_value) _NOEXCEPT;
73
74// notify one waiter waiting on the address directly with the native platform wait
75template <std::size_t _Size>
76_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void __atomic_notify_one_native(const void*) _NOEXCEPT;
77
78// notify all waiters waiting on the address directly with the native platform wait
79template <std::size_t _Size>
80_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void __atomic_notify_all_native(const void*) _NOEXCEPT;
81
82# if _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
83
84template <class _AtomicWaitable, class _Poll>
85struct __atomic_wait_backoff_impl {
86 const _AtomicWaitable& __a_;
87 _Poll __poll_;
88 memory_order __order_;
89
90 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
91 using __value_type _LIBCPP_NODEBUG = typename __waitable_traits::__value_type;
92
93 _LIBCPP_HIDE_FROM_ABI __backoff_results operator()(chrono::nanoseconds __elapsed) const {
94 if (__elapsed > chrono::microseconds(4)) {
95 auto __contention_address = const_cast<const void*>(
96 static_cast<const volatile void*>(__waitable_traits::__atomic_contention_address(__a_)));
97
98 if constexpr (__has_native_atomic_wait<__value_type>) {
99 auto __atomic_value = __waitable_traits::__atomic_load(__a_, __order_);
100 if (__poll_(__atomic_value))
101 return __backoff_results::__poll_success;
102 std::__atomic_wait_native<sizeof(__value_type)>(__contention_address, std::addressof(__atomic_value));
103 } else {
104 __cxx_contention_t __monitor_val = std::__atomic_monitor_global(__contention_address);
105 auto __atomic_value = __waitable_traits::__atomic_load(__a_, __order_);
106 if (__poll_(__atomic_value))
107 return __backoff_results::__poll_success;
108 std::__atomic_wait_global_table(__contention_address, __monitor_val);
109 }
110 } else {
111 } // poll
112 return __backoff_results::__continue_poll;
113 }
114};
115
116# else // _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
117
118template <class _AtomicWaitable, class _Poll>
119struct __atomic_wait_backoff_impl {
120 const _AtomicWaitable& __a_;
121 _Poll __poll_;
122 memory_order __order_;
123
124 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
125
126 _LIBCPP_HIDE_FROM_ABI bool
127 __update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {
128 // In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,
129 // the platform wait is directly monitoring the atomic value itself.
130 // `__poll_` takes the current value of the atomic as an in-out argument
131 // to potentially modify it. After it returns, `__monitor` has a value
132 // which can be safely waited on by `std::__libcpp_atomic_wait` without any
133 // ABA style issues.
134 __monitor_val = __waitable_traits::__atomic_load(__a_, __order_);
135 return __poll_(__monitor_val);
136 }
137
138 _LIBCPP_HIDE_FROM_ABI bool
139 __update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {
140 // In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t
141 // from the global pool, the monitor comes from __libcpp_atomic_monitor
142 __monitor_val = std::__libcpp_atomic_monitor(__contention_address);
143 auto __current_val = __waitable_traits::__atomic_load(__a_, __order_);
144 return __poll_(__current_val);
145 }
146
147 _LIBCPP_HIDE_FROM_ABI __backoff_results operator()(chrono::nanoseconds __elapsed) const {
148 if (__elapsed > chrono::microseconds(4)) {
149 auto __contention_address = __waitable_traits::__atomic_contention_address(__a_);
150 __cxx_contention_t __monitor_val;
151 if (__update_monitor_val_and_poll(__contention_address, __monitor_val))
152 return __backoff_results::__poll_success;
153 std::__libcpp_atomic_wait(__contention_address, __monitor_val);
154 } else {
155 } // poll
156 return __backoff_results::__continue_poll;
157 }
158};
159
160# endif // _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
161
162// The semantics of this function are similar to `atomic`'s
163// `.wait(T old, std::memory_order order)`, but instead of having a hardcoded
164// predicate (is the loaded value unequal to `old`?), the predicate function is
165// specified as an argument. The loaded value is given as an in-out argument to
166// the predicate. If the predicate function returns `true`,
167// `__atomic_wait_unless` will return. If the predicate function returns
168// `false`, it must set the argument to its current understanding of the atomic
169// value. The predicate function must not return `false` spuriously.
170template <class _AtomicWaitable, class _Poll>
171_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, memory_order __order, _Poll&& __poll) {
172 static_assert(__atomic_waitable<_AtomicWaitable>);
173 __atomic_wait_backoff_impl<_AtomicWaitable, __decay_t<_Poll> > __backoff_fn = {__a, __poll, __order};
174 std::__libcpp_thread_poll_with_backoff(
175 /* poll */
176 [&]() {
177 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);
178 return __poll(__current_val);
179 },
180 /* backoff */ __backoff_fn);
181}
182
183# if _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
184
185template <class _AtomicWaitable>
186_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {
187 static_assert(__atomic_waitable<_AtomicWaitable>);
188 using __value_type _LIBCPP_NODEBUG = typename __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__value_type;
189 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
190 auto __contention_address =
191 const_cast<const void*>(static_cast<const volatile void*>(__waitable_traits::__atomic_contention_address(__a)));
192 if constexpr (__has_native_atomic_wait<__value_type>) {
193 std::__atomic_notify_one_native<sizeof(__value_type)>(__contention_address);
194 } else {
195 std::__atomic_notify_one_global_table(__contention_address);
196 }
197}
198
199template <class _AtomicWaitable>
200_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {
201 static_assert(__atomic_waitable<_AtomicWaitable>);
202 using __value_type _LIBCPP_NODEBUG = typename __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__value_type;
203 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
204 auto __contention_address =
205 const_cast<const void*>(static_cast<const volatile void*>(__waitable_traits::__atomic_contention_address(__a)));
206 if constexpr (__has_native_atomic_wait<__value_type>) {
207 std::__atomic_notify_all_native<sizeof(__value_type)>(__contention_address);
208 } else {
209 std::__atomic_notify_all_global_table(__contention_address);
210 }
211}
212
213# else // _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
214
215template <class _AtomicWaitable>
216_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {
217 static_assert(__atomic_waitable<_AtomicWaitable>);
218 std::__cxx_atomic_notify_one(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));
219}
220
221template <class _AtomicWaitable>
222_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {
223 static_assert(__atomic_waitable<_AtomicWaitable>);
224 std::__cxx_atomic_notify_all(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));
225}
226
227# endif
228
229# else // _LIBCPP_HAS_THREADS
230
231template <class _AtomicWaitable, class _Poll>
232_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, memory_order __order, _Poll&& __poll) {
233 std::__libcpp_thread_poll_with_backoff(
234 /* poll */
235 [&]() {
236 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);
237 return __poll(__current_val);
238 },
239 /* backoff */ __spinning_backoff_policy());
240}
241
242template <class _AtomicWaitable>
243_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {}
244
245template <class _AtomicWaitable>
246_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {}
247
248# endif // _LIBCPP_HAS_THREADS
249
250template <typename _Tp>
251_LIBCPP_HIDE_FROM_ABI bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {
252 return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;
253}
254
255template <class _AtomicWaitable, class _Tp>
256_LIBCPP_HIDE_FROM_ABI void __atomic_wait(_AtomicWaitable& __a, _Tp __val, memory_order __order) {
257 static_assert(__atomic_waitable<_AtomicWaitable>);
258 std::__atomic_wait_unless(__a, __order, [&](_Tp const& __current) {
259 return !std::__cxx_nonatomic_compare_equal(__current, __val);
260 });
261}
262
263#endif // C++20
264
265_LIBCPP_END_NAMESPACE_STD
266
267#endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_H