condition_variable 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_CONDITION_VARIABLE
  10. #define _LIBCPP_CONDITION_VARIABLE
  11. /*
  12. condition_variable synopsis
  13. namespace std
  14. {
  15. enum class cv_status { no_timeout, timeout };
  16. class condition_variable
  17. {
  18. public:
  19. condition_variable();
  20. ~condition_variable();
  21. condition_variable(const condition_variable&) = delete;
  22. condition_variable& operator=(const condition_variable&) = delete;
  23. void notify_one() noexcept;
  24. void notify_all() noexcept;
  25. void wait(unique_lock<mutex>& lock);
  26. template <class Predicate>
  27. void wait(unique_lock<mutex>& lock, Predicate pred);
  28. template <class Clock, class Duration>
  29. cv_status
  30. wait_until(unique_lock<mutex>& lock,
  31. const chrono::time_point<Clock, Duration>& abs_time);
  32. template <class Clock, class Duration, class Predicate>
  33. bool
  34. wait_until(unique_lock<mutex>& lock,
  35. const chrono::time_point<Clock, Duration>& abs_time,
  36. Predicate pred);
  37. template <class Rep, class Period>
  38. cv_status
  39. wait_for(unique_lock<mutex>& lock,
  40. const chrono::duration<Rep, Period>& rel_time);
  41. template <class Rep, class Period, class Predicate>
  42. bool
  43. wait_for(unique_lock<mutex>& lock,
  44. const chrono::duration<Rep, Period>& rel_time,
  45. Predicate pred);
  46. typedef pthread_cond_t* native_handle_type;
  47. native_handle_type native_handle();
  48. };
  49. void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  50. class condition_variable_any
  51. {
  52. public:
  53. condition_variable_any();
  54. ~condition_variable_any();
  55. condition_variable_any(const condition_variable_any&) = delete;
  56. condition_variable_any& operator=(const condition_variable_any&) = delete;
  57. void notify_one() noexcept;
  58. void notify_all() noexcept;
  59. template <class Lock>
  60. void wait(Lock& lock);
  61. template <class Lock, class Predicate>
  62. void wait(Lock& lock, Predicate pred);
  63. template <class Lock, class Clock, class Duration>
  64. cv_status
  65. wait_until(Lock& lock,
  66. const chrono::time_point<Clock, Duration>& abs_time);
  67. template <class Lock, class Clock, class Duration, class Predicate>
  68. bool
  69. wait_until(Lock& lock,
  70. const chrono::time_point<Clock, Duration>& abs_time,
  71. Predicate pred);
  72. template <class Lock, class Rep, class Period>
  73. cv_status
  74. wait_for(Lock& lock,
  75. const chrono::duration<Rep, Period>& rel_time);
  76. template <class Lock, class Rep, class Period, class Predicate>
  77. bool
  78. wait_for(Lock& lock,
  79. const chrono::duration<Rep, Period>& rel_time,
  80. Predicate pred);
  81. };
  82. } // std
  83. */
  84. #include <__assert> // all public C++ headers provide the assertion handler
  85. #include <__config>
  86. #include <__memory/shared_ptr.h>
  87. #include <__memory/unique_ptr.h>
  88. #include <__mutex_base>
  89. #include <version>
  90. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  91. # pragma GCC system_header
  92. #endif
  93. #ifndef _LIBCPP_HAS_NO_THREADS
  94. _LIBCPP_BEGIN_NAMESPACE_STD
  95. class _LIBCPP_TYPE_VIS condition_variable_any
  96. {
  97. condition_variable __cv_;
  98. shared_ptr<mutex> __mut_;
  99. public:
  100. _LIBCPP_INLINE_VISIBILITY
  101. condition_variable_any();
  102. _LIBCPP_INLINE_VISIBILITY
  103. void notify_one() _NOEXCEPT;
  104. _LIBCPP_INLINE_VISIBILITY
  105. void notify_all() _NOEXCEPT;
  106. template <class _Lock>
  107. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  108. void wait(_Lock& __lock);
  109. template <class _Lock, class _Predicate>
  110. _LIBCPP_INLINE_VISIBILITY
  111. void wait(_Lock& __lock, _Predicate __pred);
  112. template <class _Lock, class _Clock, class _Duration>
  113. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  114. cv_status
  115. wait_until(_Lock& __lock,
  116. const chrono::time_point<_Clock, _Duration>& __t);
  117. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  118. bool
  119. _LIBCPP_INLINE_VISIBILITY
  120. wait_until(_Lock& __lock,
  121. const chrono::time_point<_Clock, _Duration>& __t,
  122. _Predicate __pred);
  123. template <class _Lock, class _Rep, class _Period>
  124. cv_status
  125. _LIBCPP_INLINE_VISIBILITY
  126. wait_for(_Lock& __lock,
  127. const chrono::duration<_Rep, _Period>& __d);
  128. template <class _Lock, class _Rep, class _Period, class _Predicate>
  129. bool
  130. _LIBCPP_INLINE_VISIBILITY
  131. wait_for(_Lock& __lock,
  132. const chrono::duration<_Rep, _Period>& __d,
  133. _Predicate __pred);
  134. };
  135. inline
  136. condition_variable_any::condition_variable_any()
  137. : __mut_(make_shared<mutex>()) {}
  138. inline
  139. void
  140. condition_variable_any::notify_one() _NOEXCEPT
  141. {
  142. {lock_guard<mutex> __lx(*__mut_);}
  143. __cv_.notify_one();
  144. }
  145. inline
  146. void
  147. condition_variable_any::notify_all() _NOEXCEPT
  148. {
  149. {lock_guard<mutex> __lx(*__mut_);}
  150. __cv_.notify_all();
  151. }
  152. struct __lock_external
  153. {
  154. template <class _Lock>
  155. void operator()(_Lock* __m) {__m->lock();}
  156. };
  157. template <class _Lock>
  158. void
  159. condition_variable_any::wait(_Lock& __lock)
  160. {
  161. shared_ptr<mutex> __mut = __mut_;
  162. unique_lock<mutex> __lk(*__mut);
  163. __lock.unlock();
  164. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  165. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
  166. __cv_.wait(__lk);
  167. } // __mut_.unlock(), __lock.lock()
  168. template <class _Lock, class _Predicate>
  169. inline
  170. void
  171. condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
  172. {
  173. while (!__pred())
  174. wait(__lock);
  175. }
  176. template <class _Lock, class _Clock, class _Duration>
  177. cv_status
  178. condition_variable_any::wait_until(_Lock& __lock,
  179. const chrono::time_point<_Clock, _Duration>& __t)
  180. {
  181. shared_ptr<mutex> __mut = __mut_;
  182. unique_lock<mutex> __lk(*__mut);
  183. __lock.unlock();
  184. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  185. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
  186. return __cv_.wait_until(__lk, __t);
  187. } // __mut_.unlock(), __lock.lock()
  188. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  189. inline
  190. bool
  191. condition_variable_any::wait_until(_Lock& __lock,
  192. const chrono::time_point<_Clock, _Duration>& __t,
  193. _Predicate __pred)
  194. {
  195. while (!__pred())
  196. if (wait_until(__lock, __t) == cv_status::timeout)
  197. return __pred();
  198. return true;
  199. }
  200. template <class _Lock, class _Rep, class _Period>
  201. inline
  202. cv_status
  203. condition_variable_any::wait_for(_Lock& __lock,
  204. const chrono::duration<_Rep, _Period>& __d)
  205. {
  206. return wait_until(__lock, chrono::steady_clock::now() + __d);
  207. }
  208. template <class _Lock, class _Rep, class _Period, class _Predicate>
  209. inline
  210. bool
  211. condition_variable_any::wait_for(_Lock& __lock,
  212. const chrono::duration<_Rep, _Period>& __d,
  213. _Predicate __pred)
  214. {
  215. return wait_until(__lock, chrono::steady_clock::now() + __d,
  216. _VSTD::move(__pred));
  217. }
  218. _LIBCPP_FUNC_VIS
  219. void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
  220. _LIBCPP_END_NAMESPACE_STD
  221. #endif // !_LIBCPP_HAS_NO_THREADS
  222. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  223. # include <concepts>
  224. # include <type_traits>
  225. #endif
  226. #endif // _LIBCPP_CONDITION_VARIABLE