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___CONDITION_VARIABLE_CONDITION_VARIABLE_H
10#define _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
11
12#include <__chrono/duration.h>
13#include <__chrono/steady_clock.h>
14#include <__chrono/system_clock.h>
15#include <__chrono/time_point.h>
16#include <__config>
17#include <__mutex/mutex.h>
18#include <__mutex/unique_lock.h>
19#include <__system_error/throw_system_error.h>
20#include <__thread/support.h>
21#include <__type_traits/enable_if.h>
22#include <__type_traits/is_floating_point.h>
23#include <__utility/move.h>
24#include <limits>
25#include <ratio>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_PUSH_MACROS
32#include <__undef_macros>
33
34#if _LIBCPP_HAS_THREADS
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
38
39# if !_LIBCPP_HAS_THREAD_API_PTHREAD
40# define _LIBCPP_HAS_COND_CLOCKWAIT 0
41# elif (defined(__ANDROID__) && __ANDROID_API__ >= 30) || _LIBCPP_GLIBC_PREREQ(2, 30)
42# define _LIBCPP_HAS_COND_CLOCKWAIT 1
43# else
44# define _LIBCPP_HAS_COND_CLOCKWAIT 0
45# endif
46
47// enum class cv_status
48_LIBCPP_DECLARE_STRONG_ENUM(cv_status){no_timeout, timeout};
49_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
50
51template <class _Rep, class _Period, __enable_if_t<is_floating_point<_Rep>::value, int> = 0>
52inline _LIBCPP_HIDE_FROM_ABI chrono::nanoseconds __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
53 using namespace chrono;
54 using __ratio = ratio_divide<_Period, nano>;
55 using __ns_rep = nanoseconds::rep;
56 _Rep __result_float = __d.count() * __ratio::num / __ratio::den;
57
58 _Rep __result_max = numeric_limits<__ns_rep>::max();
59 if (__result_float >= __result_max) {
60 return nanoseconds::max();
61 }
62
63 _Rep __result_min = numeric_limits<__ns_rep>::min();
64 if (__result_float <= __result_min) {
65 return nanoseconds::min();
66 }
67
68 return nanoseconds(static_cast<__ns_rep>(__result_float));
69}
70
71template <class _Rep, class _Period, __enable_if_t<!is_floating_point<_Rep>::value, int> = 0>
72inline _LIBCPP_HIDE_FROM_ABI chrono::nanoseconds __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
73 using namespace chrono;
74 if (__d.count() == 0) {
75 return nanoseconds(0);
76 }
77
78 using __ratio = ratio_divide<_Period, nano>;
79 using __ns_rep = nanoseconds::rep;
80 __ns_rep __result_max = numeric_limits<__ns_rep>::max();
81 if (__d.count() > 0 && __d.count() > __result_max / __ratio::num) {
82 return nanoseconds::max();
83 }
84
85 __ns_rep __result_min = numeric_limits<__ns_rep>::min();
86 if (__d.count() < 0 && __d.count() < __result_min / __ratio::num) {
87 return nanoseconds::min();
88 }
89
90 __ns_rep __result = __d.count() * __ratio::num / __ratio::den;
91 if (__result == 0) {
92 return nanoseconds(1);
93 }
94
95 return nanoseconds(__result);
96}
97
98template <class _Duration>
99_LIBCPP_HIDE_FROM_ABI chrono::steady_clock::time_point __rel_to_abs(const _Duration& __rel_time) {
100 return chrono::steady_clock::now() + chrono::__ceil<chrono::steady_clock::duration>(__rel_time);
101}
102
103// Destroying a condvar is a nop on Windows.
104//
105// This optimization can't be performed on Apple platforms, where
106// pthread_cond_destroy can allow the kernel to release resources.
107// See https://llvm.org/D64298 for details.
108//
109// TODO(EricWF): This is potentially true for some pthread implementations
110// as well.
111# if (_LIBCPP_HAS_THREAD_API_C11 && defined(__Fuchsia__)) || _LIBCPP_HAS_THREAD_API_WIN32
112# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 1
113# else
114# define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 0
115# endif
116
117class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_WARN_UNUSED condition_variable {
118 __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
119
120public:
121 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default;
122
123# if _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
124 ~condition_variable() = default;
125# else
126 ~condition_variable();
127# endif
128
129 condition_variable(const condition_variable&) = delete;
130 condition_variable& operator=(const condition_variable&) = delete;
131
132 void notify_one() _NOEXCEPT;
133 void notify_all() _NOEXCEPT;
134
135 void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
136
137 template <class _Predicate>
138 _LIBCPP_HIDE_FROM_ABI void wait(unique_lock<mutex>& __lk, _Predicate __pred) {
139 while (!__pred())
140 wait(__lk);
141 }
142
143 template <class _Clock, class _Duration>
144 _LIBCPP_HIDE_FROM_ABI cv_status
145 wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t) {
146 using namespace chrono;
147 using __clock_tp_ns = time_point<_Clock, nanoseconds>;
148
149 typename _Clock::time_point __now = _Clock::now();
150 if (__t <= __now)
151 return cv_status::timeout;
152
153 __clock_tp_ns __t_ns = __clock_tp_ns(std::__safe_nanosecond_cast(__t.time_since_epoch()));
154
155 __do_timed_wait(__lk, __t_ns);
156 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
157 }
158
159 template <class _Clock, class _Duration, class _Predicate>
160 _LIBCPP_HIDE_FROM_ABI bool
161 wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
162 while (!__pred()) {
163 if (wait_until(__lk, __t) == cv_status::timeout)
164 return __pred();
165 }
166 return true;
167 }
168
169 template <class _Rep, class _Period>
170 _LIBCPP_HIDE_FROM_ABI cv_status wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d) {
171 return wait_until(__lk, std::__rel_to_abs(__d));
172 }
173
174 template <class _Rep, class _Period, class _Predicate>
175 _LIBCPP_HIDE_FROM_ABI bool
176 wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
177
178 typedef __libcpp_condvar_t* native_handle_type;
179 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__cv_; }
180
181private:
182 void
183 __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
184# if _LIBCPP_HAS_COND_CLOCKWAIT
185 _LIBCPP_HIDE_FROM_ABI void
186 __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds>) _NOEXCEPT;
187# endif
188 template <class _Clock>
189 _LIBCPP_HIDE_FROM_ABI void
190 __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<_Clock, chrono::nanoseconds>) _NOEXCEPT;
191};
192#endif // _LIBCPP_HAS_THREADS
193
194#if _LIBCPP_HAS_THREADS
195
196template <class _Rep, class _Period, class _Predicate>
197inline bool
198condition_variable::wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
199 return wait_until(__lk, std::__rel_to_abs(__d), std::move(__pred));
200}
201
202# if _LIBCPP_HAS_COND_CLOCKWAIT
203inline void condition_variable::__do_timed_wait(
204 unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds> __tp) _NOEXCEPT {
205 using namespace chrono;
206 if (!__lk.owns_lock())
207 std::__throw_system_error(EPERM, what_arg: "condition_variable::timed wait: mutex not locked");
208 nanoseconds __d = __tp.time_since_epoch();
209 timespec __ts;
210 seconds __s = duration_cast<seconds>(fd: __d);
211 using __ts_sec = decltype(__ts.tv_sec);
212 const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
213 if (__s.count() < __ts_sec_max) {
214 __ts.tv_sec = static_cast<__ts_sec>(__s.count());
215 __ts.tv_nsec = (__d - __s).count();
216 } else {
217 __ts.tv_sec = __ts_sec_max;
218 __ts.tv_nsec = giga::num - 1;
219 }
220 int __ec = pthread_cond_clockwait(cond: &__cv_, mutex: __lk.mutex()->native_handle(), CLOCK_MONOTONIC, abstime: &__ts);
221 if (__ec != 0 && __ec != ETIMEDOUT)
222 std::__throw_system_error(ev: __ec, what_arg: "condition_variable timed_wait failed");
223}
224# endif // _LIBCPP_HAS_COND_CLOCKWAIT
225
226template <class _Clock>
227inline void condition_variable::__do_timed_wait(unique_lock<mutex>& __lk,
228 chrono::time_point<_Clock, chrono::nanoseconds> __tp) _NOEXCEPT {
229 using namespace chrono;
230 nanoseconds __d = __tp - _Clock::now();
231 if (__d <= __d.zero())
232 return;
233 using __ns_rep = nanoseconds::rep;
234
235# if _LIBCPP_HAS_COND_CLOCKWAIT
236 steady_clock::time_point __c_now = steady_clock::now();
237 using __clock_tp_ns = time_point<steady_clock, nanoseconds>;
238 __ns_rep __now_count_ns = std::__safe_nanosecond_cast(d: __c_now.time_since_epoch()).count();
239# else
240 using __clock_tp_ns = time_point<system_clock, nanoseconds>;
241 __ns_rep __now_count_ns = std::__safe_nanosecond_cast(system_clock::now().time_since_epoch()).count();
242# endif
243
244 __ns_rep __d_ns_count = std::__safe_nanosecond_cast(__d).count();
245
246 if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) {
247 __do_timed_wait(__lk, tp: __clock_tp_ns::max());
248 } else {
249 __do_timed_wait(__lk, tp: __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count)));
250 }
251}
252
253_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
254_LIBCPP_END_NAMESPACE_STD
255
256#endif // _LIBCPP_HAS_THREADS
257
258_LIBCPP_POP_MACROS
259
260#endif // _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
261