condition_variable 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <__config>
  85. #include <__mutex_base>
  86. #include <memory>
  87. #include <version>
  88. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  89. # pragma GCC system_header
  90. #endif
  91. #ifndef _LIBCPP_HAS_NO_THREADS
  92. _LIBCPP_BEGIN_NAMESPACE_STD
  93. class _LIBCPP_TYPE_VIS condition_variable_any
  94. {
  95. condition_variable __cv_;
  96. shared_ptr<mutex> __mut_;
  97. public:
  98. _LIBCPP_INLINE_VISIBILITY
  99. condition_variable_any();
  100. _LIBCPP_INLINE_VISIBILITY
  101. void notify_one() _NOEXCEPT;
  102. _LIBCPP_INLINE_VISIBILITY
  103. void notify_all() _NOEXCEPT;
  104. template <class _Lock>
  105. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  106. void wait(_Lock& __lock);
  107. template <class _Lock, class _Predicate>
  108. _LIBCPP_INLINE_VISIBILITY
  109. void wait(_Lock& __lock, _Predicate __pred);
  110. template <class _Lock, class _Clock, class _Duration>
  111. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  112. cv_status
  113. wait_until(_Lock& __lock,
  114. const chrono::time_point<_Clock, _Duration>& __t);
  115. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  116. bool
  117. _LIBCPP_INLINE_VISIBILITY
  118. wait_until(_Lock& __lock,
  119. const chrono::time_point<_Clock, _Duration>& __t,
  120. _Predicate __pred);
  121. template <class _Lock, class _Rep, class _Period>
  122. cv_status
  123. _LIBCPP_INLINE_VISIBILITY
  124. wait_for(_Lock& __lock,
  125. const chrono::duration<_Rep, _Period>& __d);
  126. template <class _Lock, class _Rep, class _Period, class _Predicate>
  127. bool
  128. _LIBCPP_INLINE_VISIBILITY
  129. wait_for(_Lock& __lock,
  130. const chrono::duration<_Rep, _Period>& __d,
  131. _Predicate __pred);
  132. };
  133. inline
  134. condition_variable_any::condition_variable_any()
  135. : __mut_(make_shared<mutex>()) {}
  136. inline
  137. void
  138. condition_variable_any::notify_one() _NOEXCEPT
  139. {
  140. {lock_guard<mutex> __lx(*__mut_);}
  141. __cv_.notify_one();
  142. }
  143. inline
  144. void
  145. condition_variable_any::notify_all() _NOEXCEPT
  146. {
  147. {lock_guard<mutex> __lx(*__mut_);}
  148. __cv_.notify_all();
  149. }
  150. struct __lock_external
  151. {
  152. template <class _Lock>
  153. void operator()(_Lock* __m) {__m->lock();}
  154. };
  155. template <class _Lock>
  156. void
  157. condition_variable_any::wait(_Lock& __lock)
  158. {
  159. shared_ptr<mutex> __mut = __mut_;
  160. unique_lock<mutex> __lk(*__mut);
  161. __lock.unlock();
  162. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  163. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
  164. __cv_.wait(__lk);
  165. } // __mut_.unlock(), __lock.lock()
  166. template <class _Lock, class _Predicate>
  167. inline
  168. void
  169. condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
  170. {
  171. while (!__pred())
  172. wait(__lock);
  173. }
  174. template <class _Lock, class _Clock, class _Duration>
  175. cv_status
  176. condition_variable_any::wait_until(_Lock& __lock,
  177. const chrono::time_point<_Clock, _Duration>& __t)
  178. {
  179. shared_ptr<mutex> __mut = __mut_;
  180. unique_lock<mutex> __lk(*__mut);
  181. __lock.unlock();
  182. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  183. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
  184. return __cv_.wait_until(__lk, __t);
  185. } // __mut_.unlock(), __lock.lock()
  186. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  187. inline
  188. bool
  189. condition_variable_any::wait_until(_Lock& __lock,
  190. const chrono::time_point<_Clock, _Duration>& __t,
  191. _Predicate __pred)
  192. {
  193. while (!__pred())
  194. if (wait_until(__lock, __t) == cv_status::timeout)
  195. return __pred();
  196. return true;
  197. }
  198. template <class _Lock, class _Rep, class _Period>
  199. inline
  200. cv_status
  201. condition_variable_any::wait_for(_Lock& __lock,
  202. const chrono::duration<_Rep, _Period>& __d)
  203. {
  204. return wait_until(__lock, chrono::steady_clock::now() + __d);
  205. }
  206. template <class _Lock, class _Rep, class _Period, class _Predicate>
  207. inline
  208. bool
  209. condition_variable_any::wait_for(_Lock& __lock,
  210. const chrono::duration<_Rep, _Period>& __d,
  211. _Predicate __pred)
  212. {
  213. return wait_until(__lock, chrono::steady_clock::now() + __d,
  214. _VSTD::move(__pred));
  215. }
  216. _LIBCPP_FUNC_VIS
  217. void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  218. _LIBCPP_END_NAMESPACE_STD
  219. #endif // !_LIBCPP_HAS_NO_THREADS
  220. #endif // _LIBCPP_CONDITION_VARIABLE