condition_variable 7.4 KB

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