condition_variable 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <__availability>
  94. #include <__chrono/duration.h>
  95. #include <__chrono/steady_clock.h>
  96. #include <__chrono/time_point.h>
  97. #include <__condition_variable/condition_variable.h>
  98. #include <__config>
  99. #include <__memory/shared_ptr.h>
  100. #include <__mutex/lock_guard.h>
  101. #include <__mutex/mutex.h>
  102. #include <__mutex/tag_types.h>
  103. #include <__mutex/unique_lock.h>
  104. #include <__stop_token/stop_callback.h>
  105. #include <__stop_token/stop_token.h>
  106. #include <__utility/move.h>
  107. #include <version>
  108. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  109. # pragma GCC system_header
  110. #endif
  111. _LIBCPP_PUSH_MACROS
  112. #include <__undef_macros>
  113. #ifndef _LIBCPP_HAS_NO_THREADS
  114. _LIBCPP_BEGIN_NAMESPACE_STD
  115. class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
  116. condition_variable __cv_;
  117. shared_ptr<mutex> __mut_;
  118. public:
  119. _LIBCPP_HIDE_FROM_ABI condition_variable_any();
  120. _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
  121. _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
  122. template <class _Lock>
  123. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
  124. template <class _Lock, class _Predicate>
  125. _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
  126. template <class _Lock, class _Clock, class _Duration>
  127. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
  128. wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
  129. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  130. bool _LIBCPP_HIDE_FROM_ABI
  131. wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
  132. template <class _Lock, class _Rep, class _Period>
  133. cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
  134. template <class _Lock, class _Rep, class _Period, class _Predicate>
  135. bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
  136. # if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  137. template <class _Lock, class _Predicate>
  138. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
  139. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  140. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
  141. _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
  142. template <class _Lock, class _Rep, class _Period, class _Predicate>
  143. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
  144. wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
  145. # endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  146. };
  147. inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
  148. inline void condition_variable_any::notify_one() _NOEXCEPT {
  149. { lock_guard<mutex> __lx(*__mut_); }
  150. __cv_.notify_one();
  151. }
  152. inline void condition_variable_any::notify_all() _NOEXCEPT {
  153. { lock_guard<mutex> __lx(*__mut_); }
  154. __cv_.notify_all();
  155. }
  156. template <class _Lock>
  157. struct __unlock_guard {
  158. _Lock& __lock_;
  159. _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
  160. _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
  161. {
  162. __lock_.lock();
  163. }
  164. __unlock_guard(const __unlock_guard&) = delete;
  165. __unlock_guard& operator=(const __unlock_guard&) = delete;
  166. };
  167. template <class _Lock>
  168. void condition_variable_any::wait(_Lock& __lock) {
  169. shared_ptr<mutex> __mut = __mut_;
  170. unique_lock<mutex> __lk(*__mut);
  171. __unlock_guard<_Lock> __unlock(__lock);
  172. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
  173. __cv_.wait(__lk);
  174. } // __mut_.unlock(), __lock.lock()
  175. template <class _Lock, class _Predicate>
  176. inline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
  177. while (!__pred())
  178. wait(__lock);
  179. }
  180. template <class _Lock, class _Clock, class _Duration>
  181. cv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
  182. shared_ptr<mutex> __mut = __mut_;
  183. unique_lock<mutex> __lk(*__mut);
  184. __unlock_guard<_Lock> __unlock(__lock);
  185. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
  186. return __cv_.wait_until(__lk, __t);
  187. } // __mut_.unlock(), __lock.lock()
  188. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  189. inline bool
  190. condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
  191. while (!__pred())
  192. if (wait_until(__lock, __t) == cv_status::timeout)
  193. return __pred();
  194. return true;
  195. }
  196. template <class _Lock, class _Rep, class _Period>
  197. inline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
  198. return wait_until(__lock, chrono::steady_clock::now() + __d);
  199. }
  200. template <class _Lock, class _Rep, class _Period, class _Predicate>
  201. inline bool
  202. condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
  203. return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
  204. }
  205. # if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  206. template <class _Lock, class _Predicate>
  207. bool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
  208. if (__stoken.stop_requested())
  209. return __pred();
  210. // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
  211. // we do need to take a copy of the shared pointer __mut_
  212. // This ensures that a thread can call the destructor immediately after calling
  213. // notify_all, without waiting all the wait calls.
  214. // A thread can also safely call the destructor immediately after calling
  215. // request_stop, as the call to request_stop would evaluate the callback,
  216. // which accesses the internal condition variable, immediately on the same thread.
  217. // In this situation, it is OK even without copying a shared ownership the internal
  218. // condition variable. However, this needs the evaluation of stop_callback to
  219. // happen-before the destruction.
  220. // The spec only says "Only the notification to unblock the wait needs to happen
  221. // before destruction". To make this work, we need to copy the shared ownership of
  222. // the internal condition variable inside this function, which is not possible
  223. // with the current ABI.
  224. shared_ptr<mutex> __mut = __mut_;
  225. stop_callback __cb(__stoken, [this] { notify_all(); });
  226. while (true) {
  227. if (__pred())
  228. return true;
  229. // We need to take the internal lock before checking stop_requested,
  230. // so that the notification cannot come in between the stop_requested
  231. // check and entering the wait.
  232. // Note that the stop_callback takes the same internal lock before notifying
  233. unique_lock<mutex> __internal_lock(*__mut);
  234. if (__stoken.stop_requested())
  235. break;
  236. __unlock_guard<_Lock> __unlock(__user_lock);
  237. unique_lock<mutex> __internal_lock2(
  238. std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
  239. __cv_.wait(__internal_lock2);
  240. } // __internal_lock2.unlock(), __user_lock.lock()
  241. return __pred();
  242. }
  243. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  244. bool condition_variable_any::wait_until(
  245. _Lock& __user_lock,
  246. stop_token __stoken,
  247. const chrono::time_point<_Clock, _Duration>& __abs_time,
  248. _Predicate __pred) {
  249. if (__stoken.stop_requested())
  250. return __pred();
  251. shared_ptr<mutex> __mut = __mut_;
  252. stop_callback __cb(__stoken, [this] { notify_all(); });
  253. while (true) {
  254. if (__pred())
  255. return true;
  256. unique_lock<mutex> __internal_lock(*__mut);
  257. if (__stoken.stop_requested())
  258. break;
  259. __unlock_guard<_Lock> __unlock(__user_lock);
  260. unique_lock<mutex> __internal_lock2(
  261. std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
  262. if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
  263. break;
  264. } // __internal_lock2.unlock(), __user_lock.lock()
  265. return __pred();
  266. }
  267. template <class _Lock, class _Rep, class _Period, class _Predicate>
  268. bool condition_variable_any::wait_for(
  269. _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
  270. return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
  271. }
  272. # endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  273. _LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
  274. _LIBCPP_END_NAMESPACE_STD
  275. #endif // !_LIBCPP_HAS_NO_THREADS
  276. _LIBCPP_POP_MACROS
  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 <iosfwd>
  285. # include <new>
  286. # include <stdexcept>
  287. # include <system_error>
  288. # include <type_traits>
  289. # include <typeinfo>
  290. #endif
  291. #endif // _LIBCPP_CONDITION_VARIABLE