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