condition_variable.h 8.7 KB

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