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_TIMED_H
10#define _LIBCPP___ATOMIC_ATOMIC_SYNC_TIMED_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 <__thread/timed_backoff_policy.h>
21#include <__type_traits/conjunction.h>
22#include <__type_traits/decay.h>
23#include <__type_traits/has_unique_object_representation.h>
24#include <__type_traits/invoke.h>
25#include <__type_traits/is_same.h>
26#include <__type_traits/is_trivially_copyable.h>
27#include <__type_traits/void_t.h>
28#include <__utility/declval.h>
29#include <cstdint>
30#include <cstring>
31
32#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33# pragma GCC system_header
34#endif
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38#if _LIBCPP_STD_VER >= 20
39# if _LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
40
41_LIBCPP_AVAILABILITY_NEW_SYNC
42_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __atomic_monitor_global(void const* __address) _NOEXCEPT;
43
44// wait on the global contention state to be changed from the given value for the address
45_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void __atomic_wait_global_table_with_timeout(
46 void const* __address, __cxx_contention_t __monitor_value, uint64_t __timeout_ns) _NOEXCEPT;
47
48// wait on the address directly with the native platform wait
49template <std::size_t _Size>
50_LIBCPP_AVAILABILITY_NEW_SYNC _LIBCPP_EXPORTED_FROM_ABI void
51__atomic_wait_native_with_timeout(void const* __address, void const* __old_value, uint64_t __timeout_ns) _NOEXCEPT;
52
53template <class _AtomicWaitable, class _Poll, class _Rep, class _Period>
54struct __atomic_wait_timed_backoff_impl {
55 const _AtomicWaitable& __a_;
56 _Poll __poll_;
57 memory_order __order_;
58 chrono::duration<_Rep, _Period> __rel_time_;
59
60 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
61 using __value_type _LIBCPP_NODEBUG = typename __waitable_traits::__value_type;
62
63 _LIBCPP_HIDE_FROM_ABI __backoff_results operator()(chrono::nanoseconds __elapsed) const {
64 if (__elapsed > chrono::microseconds(4)) {
65 auto __contention_address = const_cast<const void*>(
66 static_cast<const volatile void*>(__waitable_traits::__atomic_contention_address(__a_)));
67
68 uint64_t __timeout_ns =
69 static_cast<uint64_t>((chrono::duration_cast<chrono::nanoseconds>(__rel_time_) - __elapsed).count());
70
71 if constexpr (__has_native_atomic_wait<__value_type>) {
72 auto __atomic_value = __waitable_traits::__atomic_load(__a_, __order_);
73 if (__poll_(__atomic_value))
74 return __backoff_results::__poll_success;
75 std::__atomic_wait_native_with_timeout<sizeof(__value_type)>(
76 __contention_address, std::addressof(__atomic_value), __timeout_ns);
77 } else {
78 __cxx_contention_t __monitor_val = std::__atomic_monitor_global(__contention_address);
79 auto __atomic_value = __waitable_traits::__atomic_load(__a_, __order_);
80 if (__poll_(__atomic_value))
81 return __backoff_results::__poll_success;
82 std::__atomic_wait_global_table_with_timeout(__contention_address, __monitor_val, __timeout_ns);
83 }
84 } else {
85 } // poll
86 return __backoff_results::__continue_poll;
87 }
88};
89
90// The semantics of this function are similar to `atomic`'s
91// `.wait(T old, std::memory_order order)` with a timeout, but instead of having a hardcoded
92// predicate (is the loaded value unequal to `old`?), the predicate function is
93// specified as an argument. The loaded value is given as an in-out argument to
94// the predicate. If the predicate function returns `true`,
95// `__atomic_wait_unless_with_timeout` will return. If the predicate function returns
96// `false`, it must set the argument to its current understanding of the atomic
97// value. The predicate function must not return `false` spuriously.
98template <class _AtomicWaitable, class _Poll, class _Rep, class _Period>
99_LIBCPP_HIDE_FROM_ABI bool __atomic_wait_unless_with_timeout(
100 const _AtomicWaitable& __a,
101 memory_order __order,
102 _Poll&& __poll,
103 chrono::duration<_Rep, _Period> const& __rel_time) {
104 static_assert(__atomic_waitable<_AtomicWaitable>, "");
105 __atomic_wait_timed_backoff_impl<_AtomicWaitable, __decay_t<_Poll>, _Rep, _Period> __backoff_fn = {
106 __a, __poll, __order, __rel_time};
107 auto __poll_result = std::__libcpp_thread_poll_with_backoff(
108 /* poll */
109 [&]() {
110 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);
111 return __poll(__current_val);
112 },
113 /* backoff */ __backoff_fn,
114 __rel_time);
115
116 return __poll_result == __poll_with_backoff_results::__poll_success;
117}
118
119# elif _LIBCPP_HAS_THREADS // _LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
120
121template <class _AtomicWaitable, class _Poll, class _Rep, class _Period>
122_LIBCPP_HIDE_FROM_ABI bool __atomic_wait_unless_with_timeout(
123 const _AtomicWaitable& __a,
124 memory_order __order,
125 _Poll&& __poll,
126 chrono::duration<_Rep, _Period> const& __rel_time) {
127 auto __res = std::__libcpp_thread_poll_with_backoff(
128 /* poll */
129 [&]() {
130 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);
131 return __poll(__current_val);
132 },
133 /* backoff */ __libcpp_timed_backoff_policy(),
134 __rel_time);
135 return __res == __poll_with_backoff_results::__poll_success;
136}
137
138# endif // _LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
139
140#endif // C++20
141
142_LIBCPP_END_NAMESPACE_STD
143
144#endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_TIMED_H