xref: /freebsd-src/contrib/llvm-project/libcxx/include/__condition_variable/condition_variable.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
106c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric #ifndef _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
1006c3fb27SDimitry Andric #define _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
1106c3fb27SDimitry Andric 
12*0fca6ea1SDimitry Andric #include <__chrono/duration.h>
1306c3fb27SDimitry Andric #include <__chrono/steady_clock.h>
1406c3fb27SDimitry Andric #include <__chrono/system_clock.h>
1506c3fb27SDimitry Andric #include <__chrono/time_point.h>
1606c3fb27SDimitry Andric #include <__config>
1706c3fb27SDimitry Andric #include <__mutex/mutex.h>
1806c3fb27SDimitry Andric #include <__mutex/unique_lock.h>
1906c3fb27SDimitry Andric #include <__system_error/system_error.h>
20*0fca6ea1SDimitry Andric #include <__thread/support.h>
2106c3fb27SDimitry Andric #include <__type_traits/enable_if.h>
2206c3fb27SDimitry Andric #include <__type_traits/is_floating_point.h>
2306c3fb27SDimitry Andric #include <__utility/move.h>
2406c3fb27SDimitry Andric #include <limits>
2506c3fb27SDimitry Andric #include <ratio>
2606c3fb27SDimitry Andric 
2706c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2806c3fb27SDimitry Andric #  pragma GCC system_header
2906c3fb27SDimitry Andric #endif
3006c3fb27SDimitry Andric 
3106c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
3206c3fb27SDimitry Andric #include <__undef_macros>
3306c3fb27SDimitry Andric 
3406c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3506c3fb27SDimitry Andric 
3606c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
3706c3fb27SDimitry Andric 
3806c3fb27SDimitry Andric // enum class cv_status
3906c3fb27SDimitry Andric _LIBCPP_DECLARE_STRONG_ENUM(cv_status){no_timeout, timeout};
4006c3fb27SDimitry Andric _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
4106c3fb27SDimitry Andric 
4206c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI condition_variable {
4306c3fb27SDimitry Andric   __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
4406c3fb27SDimitry Andric 
4506c3fb27SDimitry Andric public:
4606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default;
4706c3fb27SDimitry Andric 
4806c3fb27SDimitry Andric #  ifdef _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
4906c3fb27SDimitry Andric   ~condition_variable() = default;
5006c3fb27SDimitry Andric #  else
5106c3fb27SDimitry Andric   ~condition_variable();
5206c3fb27SDimitry Andric #  endif
5306c3fb27SDimitry Andric 
5406c3fb27SDimitry Andric   condition_variable(const condition_variable&)            = delete;
5506c3fb27SDimitry Andric   condition_variable& operator=(const condition_variable&) = delete;
5606c3fb27SDimitry Andric 
5706c3fb27SDimitry Andric   void notify_one() _NOEXCEPT;
5806c3fb27SDimitry Andric   void notify_all() _NOEXCEPT;
5906c3fb27SDimitry Andric 
6006c3fb27SDimitry Andric   void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
6106c3fb27SDimitry Andric   template <class _Predicate>
6206c3fb27SDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(unique_lock<mutex>& __lk, _Predicate __pred);
6306c3fb27SDimitry Andric 
6406c3fb27SDimitry Andric   template <class _Clock, class _Duration>
6506c3fb27SDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
6606c3fb27SDimitry Andric   wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t);
6706c3fb27SDimitry Andric 
6806c3fb27SDimitry Andric   template <class _Clock, class _Duration, class _Predicate>
6906c3fb27SDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
7006c3fb27SDimitry Andric   wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
7106c3fb27SDimitry Andric 
7206c3fb27SDimitry Andric   template <class _Rep, class _Period>
7306c3fb27SDimitry Andric   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
7406c3fb27SDimitry Andric   wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d);
7506c3fb27SDimitry Andric 
7606c3fb27SDimitry Andric   template <class _Rep, class _Period, class _Predicate>
7706c3fb27SDimitry Andric   bool _LIBCPP_HIDE_FROM_ABI
7806c3fb27SDimitry Andric   wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
7906c3fb27SDimitry Andric 
8006c3fb27SDimitry Andric   typedef __libcpp_condvar_t* native_handle_type;
8106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__cv_; }
8206c3fb27SDimitry Andric 
8306c3fb27SDimitry Andric private:
8406c3fb27SDimitry Andric   void
8506c3fb27SDimitry Andric   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
8606c3fb27SDimitry Andric #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
8706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void
8806c3fb27SDimitry Andric   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds>) _NOEXCEPT;
8906c3fb27SDimitry Andric #  endif
9006c3fb27SDimitry Andric   template <class _Clock>
9106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void
9206c3fb27SDimitry Andric   __do_timed_wait(unique_lock<mutex>& __lk, chrono::time_point<_Clock, chrono::nanoseconds>) _NOEXCEPT;
9306c3fb27SDimitry Andric };
9406c3fb27SDimitry Andric #endif // !_LIBCPP_HAS_NO_THREADS
9506c3fb27SDimitry Andric 
965f757f3fSDimitry Andric template <class _Rep, class _Period, __enable_if_t<is_floating_point<_Rep>::value, int> = 0>
975f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI chrono::nanoseconds __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
9806c3fb27SDimitry Andric   using namespace chrono;
9906c3fb27SDimitry Andric   using __ratio       = ratio_divide<_Period, nano>;
10006c3fb27SDimitry Andric   using __ns_rep      = nanoseconds::rep;
10106c3fb27SDimitry Andric   _Rep __result_float = __d.count() * __ratio::num / __ratio::den;
10206c3fb27SDimitry Andric 
10306c3fb27SDimitry Andric   _Rep __result_max = numeric_limits<__ns_rep>::max();
10406c3fb27SDimitry Andric   if (__result_float >= __result_max) {
10506c3fb27SDimitry Andric     return nanoseconds::max();
10606c3fb27SDimitry Andric   }
10706c3fb27SDimitry Andric 
10806c3fb27SDimitry Andric   _Rep __result_min = numeric_limits<__ns_rep>::min();
10906c3fb27SDimitry Andric   if (__result_float <= __result_min) {
11006c3fb27SDimitry Andric     return nanoseconds::min();
11106c3fb27SDimitry Andric   }
11206c3fb27SDimitry Andric 
11306c3fb27SDimitry Andric   return nanoseconds(static_cast<__ns_rep>(__result_float));
11406c3fb27SDimitry Andric }
11506c3fb27SDimitry Andric 
1165f757f3fSDimitry Andric template <class _Rep, class _Period, __enable_if_t<!is_floating_point<_Rep>::value, int> = 0>
1175f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI chrono::nanoseconds __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) {
11806c3fb27SDimitry Andric   using namespace chrono;
11906c3fb27SDimitry Andric   if (__d.count() == 0) {
12006c3fb27SDimitry Andric     return nanoseconds(0);
12106c3fb27SDimitry Andric   }
12206c3fb27SDimitry Andric 
12306c3fb27SDimitry Andric   using __ratio         = ratio_divide<_Period, nano>;
12406c3fb27SDimitry Andric   using __ns_rep        = nanoseconds::rep;
12506c3fb27SDimitry Andric   __ns_rep __result_max = numeric_limits<__ns_rep>::max();
12606c3fb27SDimitry Andric   if (__d.count() > 0 && __d.count() > __result_max / __ratio::num) {
12706c3fb27SDimitry Andric     return nanoseconds::max();
12806c3fb27SDimitry Andric   }
12906c3fb27SDimitry Andric 
13006c3fb27SDimitry Andric   __ns_rep __result_min = numeric_limits<__ns_rep>::min();
13106c3fb27SDimitry Andric   if (__d.count() < 0 && __d.count() < __result_min / __ratio::num) {
13206c3fb27SDimitry Andric     return nanoseconds::min();
13306c3fb27SDimitry Andric   }
13406c3fb27SDimitry Andric 
13506c3fb27SDimitry Andric   __ns_rep __result = __d.count() * __ratio::num / __ratio::den;
13606c3fb27SDimitry Andric   if (__result == 0) {
13706c3fb27SDimitry Andric     return nanoseconds(1);
13806c3fb27SDimitry Andric   }
13906c3fb27SDimitry Andric 
14006c3fb27SDimitry Andric   return nanoseconds(__result);
14106c3fb27SDimitry Andric }
14206c3fb27SDimitry Andric 
14306c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
14406c3fb27SDimitry Andric template <class _Predicate>
14506c3fb27SDimitry Andric void condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) {
14606c3fb27SDimitry Andric   while (!__pred())
14706c3fb27SDimitry Andric     wait(__lk);
14806c3fb27SDimitry Andric }
14906c3fb27SDimitry Andric 
15006c3fb27SDimitry Andric template <class _Clock, class _Duration>
15106c3fb27SDimitry Andric cv_status condition_variable::wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t) {
15206c3fb27SDimitry Andric   using namespace chrono;
15306c3fb27SDimitry Andric   using __clock_tp_ns = time_point<_Clock, nanoseconds>;
15406c3fb27SDimitry Andric 
15506c3fb27SDimitry Andric   typename _Clock::time_point __now = _Clock::now();
15606c3fb27SDimitry Andric   if (__t <= __now)
15706c3fb27SDimitry Andric     return cv_status::timeout;
15806c3fb27SDimitry Andric 
15906c3fb27SDimitry Andric   __clock_tp_ns __t_ns = __clock_tp_ns(std::__safe_nanosecond_cast(__t.time_since_epoch()));
16006c3fb27SDimitry Andric 
16106c3fb27SDimitry Andric   __do_timed_wait(__lk, __t_ns);
16206c3fb27SDimitry Andric   return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
16306c3fb27SDimitry Andric }
16406c3fb27SDimitry Andric 
16506c3fb27SDimitry Andric template <class _Clock, class _Duration, class _Predicate>
16606c3fb27SDimitry Andric bool condition_variable::wait_until(
16706c3fb27SDimitry Andric     unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
16806c3fb27SDimitry Andric   while (!__pred()) {
16906c3fb27SDimitry Andric     if (wait_until(__lk, __t) == cv_status::timeout)
17006c3fb27SDimitry Andric       return __pred();
17106c3fb27SDimitry Andric   }
17206c3fb27SDimitry Andric   return true;
17306c3fb27SDimitry Andric }
17406c3fb27SDimitry Andric 
17506c3fb27SDimitry Andric template <class _Rep, class _Period>
17606c3fb27SDimitry Andric cv_status condition_variable::wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d) {
17706c3fb27SDimitry Andric   using namespace chrono;
17806c3fb27SDimitry Andric   if (__d <= __d.zero())
17906c3fb27SDimitry Andric     return cv_status::timeout;
18006c3fb27SDimitry Andric   using __ns_rep                   = nanoseconds::rep;
18106c3fb27SDimitry Andric   steady_clock::time_point __c_now = steady_clock::now();
18206c3fb27SDimitry Andric 
18306c3fb27SDimitry Andric #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
18406c3fb27SDimitry Andric   using __clock_tp_ns     = time_point<steady_clock, nanoseconds>;
18506c3fb27SDimitry Andric   __ns_rep __now_count_ns = std::__safe_nanosecond_cast(__c_now.time_since_epoch()).count();
18606c3fb27SDimitry Andric #  else
18706c3fb27SDimitry Andric   using __clock_tp_ns     = time_point<system_clock, nanoseconds>;
18806c3fb27SDimitry Andric   __ns_rep __now_count_ns = std::__safe_nanosecond_cast(system_clock::now().time_since_epoch()).count();
18906c3fb27SDimitry Andric #  endif
19006c3fb27SDimitry Andric 
19106c3fb27SDimitry Andric   __ns_rep __d_ns_count = std::__safe_nanosecond_cast(__d).count();
19206c3fb27SDimitry Andric 
19306c3fb27SDimitry Andric   if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) {
19406c3fb27SDimitry Andric     __do_timed_wait(__lk, __clock_tp_ns::max());
19506c3fb27SDimitry Andric   } else {
19606c3fb27SDimitry Andric     __do_timed_wait(__lk, __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count)));
19706c3fb27SDimitry Andric   }
19806c3fb27SDimitry Andric 
19906c3fb27SDimitry Andric   return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : cv_status::timeout;
20006c3fb27SDimitry Andric }
20106c3fb27SDimitry Andric 
20206c3fb27SDimitry Andric template <class _Rep, class _Period, class _Predicate>
20306c3fb27SDimitry Andric inline bool
20406c3fb27SDimitry Andric condition_variable::wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
20506c3fb27SDimitry Andric   return wait_until(__lk, chrono::steady_clock::now() + __d, std::move(__pred));
20606c3fb27SDimitry Andric }
20706c3fb27SDimitry Andric 
20806c3fb27SDimitry Andric #  if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
20906c3fb27SDimitry Andric inline void condition_variable::__do_timed_wait(
21006c3fb27SDimitry Andric     unique_lock<mutex>& __lk, chrono::time_point<chrono::steady_clock, chrono::nanoseconds> __tp) _NOEXCEPT {
21106c3fb27SDimitry Andric   using namespace chrono;
21206c3fb27SDimitry Andric   if (!__lk.owns_lock())
21306c3fb27SDimitry Andric     __throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked");
21406c3fb27SDimitry Andric   nanoseconds __d = __tp.time_since_epoch();
21506c3fb27SDimitry Andric   timespec __ts;
21606c3fb27SDimitry Andric   seconds __s                 = duration_cast<seconds>(__d);
21706c3fb27SDimitry Andric   using __ts_sec              = decltype(__ts.tv_sec);
21806c3fb27SDimitry Andric   const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
21906c3fb27SDimitry Andric   if (__s.count() < __ts_sec_max) {
22006c3fb27SDimitry Andric     __ts.tv_sec  = static_cast<__ts_sec>(__s.count());
22106c3fb27SDimitry Andric     __ts.tv_nsec = (__d - __s).count();
22206c3fb27SDimitry Andric   } else {
22306c3fb27SDimitry Andric     __ts.tv_sec  = __ts_sec_max;
22406c3fb27SDimitry Andric     __ts.tv_nsec = giga::num - 1;
22506c3fb27SDimitry Andric   }
22606c3fb27SDimitry Andric   int __ec = pthread_cond_clockwait(&__cv_, __lk.mutex()->native_handle(), CLOCK_MONOTONIC, &__ts);
22706c3fb27SDimitry Andric   if (__ec != 0 && __ec != ETIMEDOUT)
22806c3fb27SDimitry Andric     __throw_system_error(__ec, "condition_variable timed_wait failed");
22906c3fb27SDimitry Andric }
23006c3fb27SDimitry Andric #  endif // _LIBCPP_HAS_COND_CLOCKWAIT
23106c3fb27SDimitry Andric 
23206c3fb27SDimitry Andric template <class _Clock>
23306c3fb27SDimitry Andric inline void condition_variable::__do_timed_wait(unique_lock<mutex>& __lk,
23406c3fb27SDimitry Andric                                                 chrono::time_point<_Clock, chrono::nanoseconds> __tp) _NOEXCEPT {
23506c3fb27SDimitry Andric   wait_for(__lk, __tp - _Clock::now());
23606c3fb27SDimitry Andric }
23706c3fb27SDimitry Andric 
23806c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_THREADS
23906c3fb27SDimitry Andric 
24006c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
24106c3fb27SDimitry Andric 
24206c3fb27SDimitry Andric _LIBCPP_POP_MACROS
24306c3fb27SDimitry Andric 
24406c3fb27SDimitry Andric #endif // _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
245