xref: /freebsd/contrib/llvm-project/libcxx/include/__condition_variable/condition_variable.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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/steady_clock.h>
13 #include <__chrono/system_clock.h>
14 #include <__chrono/time_point.h>
15 #include <__config>
16 #include <__mutex/mutex.h>
17 #include <__mutex/unique_lock.h>
18 #include <__system_error/system_error.h>
19 #include <__threading_support>
20 #include <__type_traits/enable_if.h>
21 #include <__type_traits/is_floating_point.h>
22 #include <__utility/move.h>
23 #include <limits>
24 #include <ratio>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 #ifndef _LIBCPP_HAS_NO_THREADS
36 
37 // enum class cv_status
38 _LIBCPP_DECLARE_STRONG_ENUM(cv_status){no_timeout, timeout};
39 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
40 
41 class _LIBCPP_EXPORTED_FROM_ABI condition_variable {
42   __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
43 
44 public:
45   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default;
46 
47 #  ifdef _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
48   ~condition_variable() = default;
49 #  else
50   ~condition_variable();
51 #  endif
52 
53   condition_variable(const condition_variable&)            = delete;
54   condition_variable& operator=(const condition_variable&) = delete;
55 
56   void notify_one() _NOEXCEPT;
57   void notify_all() _NOEXCEPT;
58 
59   void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
60   template <class _Predicate>
61   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(unique_lock<mutex>& __lk, _Predicate __pred);
62 
63   template <class _Clock, class _Duration>
64   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
65   wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t);
66 
67   template <class _Clock, class _Duration, class _Predicate>
68   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
69   wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
70 
71   template <class _Rep, class _Period>
72   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
73   wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d);
74 
75   template <class _Rep, class _Period, class _Predicate>
76   bool _LIBCPP_HIDE_FROM_ABI
77   wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
78 
79   typedef __libcpp_condvar_t* native_handle_type;
80   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__cv_; }
81 
82 private:
83   void
84   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
85 #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
86   _LIBCPP_HIDE_FROM_ABI void
87   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds>) _NOEXCEPT;
88 #  endif
89   template <class _Clock>
90   _LIBCPP_HIDE_FROM_ABI void
91   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<_Clock, chrono::nanoseconds>) _NOEXCEPT;
92 };
93 #endif // !_LIBCPP_HAS_NO_THREADS
94 
95 template <class _Rep, class _Period>
96 inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<is_floating_point<_Rep>::value, chrono::nanoseconds>
97 __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
98   using namespace chrono;
99   using __ratio       = ratio_divide<_Period, nano>;
100   using __ns_rep      = nanoseconds::rep;
101   _Rep __result_float = __d.count() * __ratio::num / __ratio::den;
102 
103   _Rep __result_max = numeric_limits<__ns_rep>::max();
104   if (__result_float >= __result_max) {
105     return nanoseconds::max();
106   }
107 
108   _Rep __result_min = numeric_limits<__ns_rep>::min();
109   if (__result_float <= __result_min) {
110     return nanoseconds::min();
111   }
112 
113   return nanoseconds(static_cast<__ns_rep>(__result_float));
114 }
115 
116 template <class _Rep, class _Period>
117 inline _LIBCPP_HIDE_FROM_ABI __enable_if_t<!is_floating_point<_Rep>::value, chrono::nanoseconds>
118 __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
119   using namespace chrono;
120   if (__d.count() == 0) {
121     return nanoseconds(0);
122   }
123 
124   using __ratio         = ratio_divide<_Period, nano>;
125   using __ns_rep        = nanoseconds::rep;
126   __ns_rep __result_max = numeric_limits<__ns_rep>::max();
127   if (__d.count() > 0 && __d.count() > __result_max / __ratio::num) {
128     return nanoseconds::max();
129   }
130 
131   __ns_rep __result_min = numeric_limits<__ns_rep>::min();
132   if (__d.count() < 0 && __d.count() < __result_min / __ratio::num) {
133     return nanoseconds::min();
134   }
135 
136   __ns_rep __result = __d.count() * __ratio::num / __ratio::den;
137   if (__result == 0) {
138     return nanoseconds(1);
139   }
140 
141   return nanoseconds(__result);
142 }
143 
144 #ifndef _LIBCPP_HAS_NO_THREADS
145 template <class _Predicate>
146 void condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) {
147   while (!__pred())
148     wait(__lk);
149 }
150 
151 template <class _Clock, class _Duration>
152 cv_status condition_variable::wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t) {
153   using namespace chrono;
154   using __clock_tp_ns = time_point<_Clock, nanoseconds>;
155 
156   typename _Clock::time_point __now = _Clock::now();
157   if (__t <= __now)
158     return cv_status::timeout;
159 
160   __clock_tp_ns __t_ns = __clock_tp_ns(std::__safe_nanosecond_cast(__t.time_since_epoch()));
161 
162   __do_timed_wait(__lk, __t_ns);
163   return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
164 }
165 
166 template <class _Clock, class _Duration, class _Predicate>
167 bool condition_variable::wait_until(
168     unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
169   while (!__pred()) {
170     if (wait_until(__lk, __t) == cv_status::timeout)
171       return __pred();
172   }
173   return true;
174 }
175 
176 template <class _Rep, class _Period>
177 cv_status condition_variable::wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d) {
178   using namespace chrono;
179   if (__d <= __d.zero())
180     return cv_status::timeout;
181   using __ns_rep                   = nanoseconds::rep;
182   steady_clock::time_point __c_now = steady_clock::now();
183 
184 #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
185   using __clock_tp_ns     = time_point<steady_clock, nanoseconds>;
186   __ns_rep __now_count_ns = std::__safe_nanosecond_cast(__c_now.time_since_epoch()).count();
187 #  else
188   using __clock_tp_ns     = time_point<system_clock, nanoseconds>;
189   __ns_rep __now_count_ns = std::__safe_nanosecond_cast(system_clock::now().time_since_epoch()).count();
190 #  endif
191 
192   __ns_rep __d_ns_count = std::__safe_nanosecond_cast(__d).count();
193 
194   if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) {
195     __do_timed_wait(__lk, __clock_tp_ns::max());
196   } else {
197     __do_timed_wait(__lk, __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count)));
198   }
199 
200   return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : cv_status::timeout;
201 }
202 
203 template <class _Rep, class _Period, class _Predicate>
204 inline bool
205 condition_variable::wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
206   return wait_until(__lk, chrono::steady_clock::now() + __d, std::move(__pred));
207 }
208 
209 #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
210 inline void condition_variable::__do_timed_wait(
211     unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds> __tp) _NOEXCEPT {
212   using namespace chrono;
213   if (!__lk.owns_lock())
214     __throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked");
215   nanoseconds __d = __tp.time_since_epoch();
216   timespec __ts;
217   seconds __s                 = duration_cast<seconds>(__d);
218   using __ts_sec              = decltype(__ts.tv_sec);
219   const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
220   if (__s.count() < __ts_sec_max) {
221     __ts.tv_sec  = static_cast<__ts_sec>(__s.count());
222     __ts.tv_nsec = (__d - __s).count();
223   } else {
224     __ts.tv_sec  = __ts_sec_max;
225     __ts.tv_nsec = giga::num - 1;
226   }
227   int __ec = pthread_cond_clockwait(&__cv_, __lk.mutex()->native_handle(), CLOCK_MONOTONIC, &__ts);
228   if (__ec != 0 && __ec != ETIMEDOUT)
229     __throw_system_error(__ec, "condition_variable timed_wait failed");
230 }
231 #  endif // _LIBCPP_HAS_COND_CLOCKWAIT
232 
233 template <class _Clock>
234 inline void condition_variable::__do_timed_wait(unique_lock<mutex>& __lk,
235                                                 chrono::time_point<_Clock, chrono::nanoseconds> __tp) _NOEXCEPT {
236   wait_for(__lk, __tp - _Clock::now());
237 }
238 
239 #endif // _LIBCPP_HAS_NO_THREADS
240 
241 _LIBCPP_END_NAMESPACE_STD
242 
243 _LIBCPP_POP_MACROS
244 
245 #endif // _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
246