condition_variable 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // [thread.condvarany.intwait], interruptible waits
  82. template <class Lock, class Predicate>
  83. bool wait(Lock& lock, stop_token stoken, Predicate pred); // since C++20
  84. template <class Lock, class Clock, class Duration, class Predicate>
  85. bool wait_until(Lock& lock, stop_token stoken,
  86. const chrono::time_point<Clock, Duration>& abs_time, Predicate pred); // since C++20
  87. template <class Lock, class Rep, class Period, class Predicate>
  88. bool wait_for(Lock& lock, stop_token stoken,
  89. const chrono::duration<Rep, Period>& rel_time, Predicate pred); // since C++20
  90. };
  91. } // std
  92. */
  93. #include <__assert> // all public C++ headers provide the assertion handler
  94. #include <__availability>
  95. #include <__chrono/duration.h>
  96. #include <__chrono/steady_clock.h>
  97. #include <__chrono/time_point.h>
  98. #include <__condition_variable/condition_variable.h>
  99. #include <__config>
  100. #include <__memory/shared_ptr.h>
  101. #include <__memory/unique_ptr.h>
  102. #include <__mutex/lock_guard.h>
  103. #include <__mutex/mutex.h>
  104. #include <__mutex/tag_types.h>
  105. #include <__mutex/unique_lock.h>
  106. #include <__stop_token/stop_token.h>
  107. #include <__utility/move.h>
  108. #include <version>
  109. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  110. # pragma GCC system_header
  111. #endif
  112. #ifndef _LIBCPP_HAS_NO_THREADS
  113. _LIBCPP_BEGIN_NAMESPACE_STD
  114. class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any
  115. {
  116. condition_variable __cv_;
  117. shared_ptr<mutex> __mut_;
  118. public:
  119. _LIBCPP_INLINE_VISIBILITY
  120. condition_variable_any();
  121. _LIBCPP_INLINE_VISIBILITY
  122. void notify_one() _NOEXCEPT;
  123. _LIBCPP_INLINE_VISIBILITY
  124. void notify_all() _NOEXCEPT;
  125. template <class _Lock>
  126. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  127. void wait(_Lock& __lock);
  128. template <class _Lock, class _Predicate>
  129. _LIBCPP_INLINE_VISIBILITY
  130. void wait(_Lock& __lock, _Predicate __pred);
  131. template <class _Lock, class _Clock, class _Duration>
  132. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  133. cv_status
  134. wait_until(_Lock& __lock,
  135. const chrono::time_point<_Clock, _Duration>& __t);
  136. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  137. bool
  138. _LIBCPP_INLINE_VISIBILITY
  139. wait_until(_Lock& __lock,
  140. const chrono::time_point<_Clock, _Duration>& __t,
  141. _Predicate __pred);
  142. template <class _Lock, class _Rep, class _Period>
  143. cv_status
  144. _LIBCPP_INLINE_VISIBILITY
  145. wait_for(_Lock& __lock,
  146. const chrono::duration<_Rep, _Period>& __d);
  147. template <class _Lock, class _Rep, class _Period, class _Predicate>
  148. bool
  149. _LIBCPP_INLINE_VISIBILITY
  150. wait_for(_Lock& __lock,
  151. const chrono::duration<_Rep, _Period>& __d,
  152. _Predicate __pred);
  153. #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  154. template <class _Lock, class _Predicate>
  155. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
  156. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  157. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(_Lock& __lock, stop_token __stoken,
  158. const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
  159. template <class _Lock, class _Rep, class _Period, class _Predicate>
  160. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_for(_Lock& __lock, stop_token __stoken,
  161. const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
  162. #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  163. };
  164. inline
  165. condition_variable_any::condition_variable_any()
  166. : __mut_(make_shared<mutex>()) {}
  167. inline
  168. void
  169. condition_variable_any::notify_one() _NOEXCEPT
  170. {
  171. {lock_guard<mutex> __lx(*__mut_);}
  172. __cv_.notify_one();
  173. }
  174. inline
  175. void
  176. condition_variable_any::notify_all() _NOEXCEPT
  177. {
  178. {lock_guard<mutex> __lx(*__mut_);}
  179. __cv_.notify_all();
  180. }
  181. struct __lock_external
  182. {
  183. template <class _Lock>
  184. _LIBCPP_HIDE_FROM_ABI void operator()(_Lock* __m) {__m->lock();}
  185. };
  186. template <class _Lock>
  187. void
  188. condition_variable_any::wait(_Lock& __lock)
  189. {
  190. shared_ptr<mutex> __mut = __mut_;
  191. unique_lock<mutex> __lk(*__mut);
  192. __lock.unlock();
  193. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  194. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
  195. __cv_.wait(__lk);
  196. } // __mut_.unlock(), __lock.lock()
  197. template <class _Lock, class _Predicate>
  198. inline
  199. void
  200. condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
  201. {
  202. while (!__pred())
  203. wait(__lock);
  204. }
  205. template <class _Lock, class _Clock, class _Duration>
  206. cv_status
  207. condition_variable_any::wait_until(_Lock& __lock,
  208. const chrono::time_point<_Clock, _Duration>& __t)
  209. {
  210. shared_ptr<mutex> __mut = __mut_;
  211. unique_lock<mutex> __lk(*__mut);
  212. __lock.unlock();
  213. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  214. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
  215. return __cv_.wait_until(__lk, __t);
  216. } // __mut_.unlock(), __lock.lock()
  217. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  218. inline
  219. bool
  220. condition_variable_any::wait_until(_Lock& __lock,
  221. const chrono::time_point<_Clock, _Duration>& __t,
  222. _Predicate __pred)
  223. {
  224. while (!__pred())
  225. if (wait_until(__lock, __t) == cv_status::timeout)
  226. return __pred();
  227. return true;
  228. }
  229. template <class _Lock, class _Rep, class _Period>
  230. inline
  231. cv_status
  232. condition_variable_any::wait_for(_Lock& __lock,
  233. const chrono::duration<_Rep, _Period>& __d)
  234. {
  235. return wait_until(__lock, chrono::steady_clock::now() + __d);
  236. }
  237. template <class _Lock, class _Rep, class _Period, class _Predicate>
  238. inline
  239. bool
  240. condition_variable_any::wait_for(_Lock& __lock,
  241. const chrono::duration<_Rep, _Period>& __d,
  242. _Predicate __pred)
  243. {
  244. return wait_until(__lock, chrono::steady_clock::now() + __d,
  245. _VSTD::move(__pred));
  246. }
  247. #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  248. template <class _Lock, class _Predicate>
  249. bool condition_variable_any::wait(_Lock& __lock, stop_token __stoken, _Predicate __pred) {
  250. while (!__stoken.stop_requested()) {
  251. if (__pred())
  252. return true;
  253. wait(__lock);
  254. }
  255. return __pred();
  256. }
  257. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  258. bool condition_variable_any::wait_until(
  259. _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred) {
  260. while (!__stoken.stop_requested()) {
  261. if (__pred())
  262. return true;
  263. if (wait_until(__lock, __abs_time) == cv_status::timeout)
  264. return __pred();
  265. }
  266. return __pred();
  267. }
  268. template <class _Lock, class _Rep, class _Period, class _Predicate>
  269. bool condition_variable_any::wait_for(
  270. _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
  271. return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
  272. }
  273. #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  274. _LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
  275. _LIBCPP_END_NAMESPACE_STD
  276. #endif // !_LIBCPP_HAS_NO_THREADS
  277. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  278. # include <atomic>
  279. # include <concepts>
  280. # include <cstdint>
  281. # include <cstdlib>
  282. # include <cstring>
  283. # include <initializer_list>
  284. # include <new>
  285. # include <stdexcept>
  286. # include <system_error>
  287. # include <type_traits>
  288. # include <typeinfo>
  289. #endif
  290. #endif // _LIBCPP_CONDITION_VARIABLE