You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
13KB

  1. // <condition_variable> -*- C++ -*-
  2. // Copyright (C) 2008-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/condition_variable
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_CONDITION_VARIABLE
  24. #define _GLIBCXX_CONDITION_VARIABLE 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <chrono>
  30. #include <bits/std_mutex.h>
  31. #include <bits/unique_lock.h>
  32. #include <ext/concurrence.h>
  33. #include <bits/alloc_traits.h>
  34. #include <bits/allocator.h>
  35. #include <bits/unique_ptr.h>
  36. #include <bits/shared_ptr.h>
  37. #include <bits/cxxabi_forced.h>
  38. #if __cplusplus > 201703L
  39. # include <stop_token>
  40. #endif
  41. #if defined(_GLIBCXX_HAS_GTHREADS)
  42. namespace std _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. /**
  46. * @defgroup condition_variables Condition Variables
  47. * @ingroup concurrency
  48. *
  49. * Classes for condition_variable support.
  50. * @{
  51. */
  52. /// cv_status
  53. enum class cv_status { no_timeout, timeout };
  54. /// condition_variable
  55. class condition_variable
  56. {
  57. using steady_clock = chrono::steady_clock;
  58. using system_clock = chrono::system_clock;
  59. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  60. using __clock_t = steady_clock;
  61. #else
  62. using __clock_t = system_clock;
  63. #endif
  64. typedef __gthread_cond_t __native_type;
  65. #ifdef __GTHREAD_COND_INIT
  66. __native_type _M_cond = __GTHREAD_COND_INIT;
  67. #else
  68. __native_type _M_cond;
  69. #endif
  70. public:
  71. typedef __native_type* native_handle_type;
  72. condition_variable() noexcept;
  73. ~condition_variable() noexcept;
  74. condition_variable(const condition_variable&) = delete;
  75. condition_variable& operator=(const condition_variable&) = delete;
  76. void
  77. notify_one() noexcept;
  78. void
  79. notify_all() noexcept;
  80. void
  81. wait(unique_lock<mutex>& __lock) noexcept;
  82. template<typename _Predicate>
  83. void
  84. wait(unique_lock<mutex>& __lock, _Predicate __p)
  85. {
  86. while (!__p())
  87. wait(__lock);
  88. }
  89. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  90. template<typename _Duration>
  91. cv_status
  92. wait_until(unique_lock<mutex>& __lock,
  93. const chrono::time_point<steady_clock, _Duration>& __atime)
  94. { return __wait_until_impl(__lock, __atime); }
  95. #endif
  96. template<typename _Duration>
  97. cv_status
  98. wait_until(unique_lock<mutex>& __lock,
  99. const chrono::time_point<system_clock, _Duration>& __atime)
  100. { return __wait_until_impl(__lock, __atime); }
  101. template<typename _Clock, typename _Duration>
  102. cv_status
  103. wait_until(unique_lock<mutex>& __lock,
  104. const chrono::time_point<_Clock, _Duration>& __atime)
  105. {
  106. #if __cplusplus > 201703L
  107. static_assert(chrono::is_clock_v<_Clock>);
  108. #endif
  109. const typename _Clock::time_point __c_entry = _Clock::now();
  110. const __clock_t::time_point __s_entry = __clock_t::now();
  111. const auto __delta = __atime - __c_entry;
  112. const auto __s_atime = __s_entry + __delta;
  113. if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
  114. return cv_status::no_timeout;
  115. // We got a timeout when measured against __clock_t but
  116. // we need to check against the caller-supplied clock
  117. // to tell whether we should return a timeout.
  118. if (_Clock::now() < __atime)
  119. return cv_status::no_timeout;
  120. return cv_status::timeout;
  121. }
  122. template<typename _Clock, typename _Duration, typename _Predicate>
  123. bool
  124. wait_until(unique_lock<mutex>& __lock,
  125. const chrono::time_point<_Clock, _Duration>& __atime,
  126. _Predicate __p)
  127. {
  128. while (!__p())
  129. if (wait_until(__lock, __atime) == cv_status::timeout)
  130. return __p();
  131. return true;
  132. }
  133. template<typename _Rep, typename _Period>
  134. cv_status
  135. wait_for(unique_lock<mutex>& __lock,
  136. const chrono::duration<_Rep, _Period>& __rtime)
  137. {
  138. using __dur = typename steady_clock::duration;
  139. auto __reltime = chrono::duration_cast<__dur>(__rtime);
  140. if (__reltime < __rtime)
  141. ++__reltime;
  142. return wait_until(__lock, steady_clock::now() + __reltime);
  143. }
  144. template<typename _Rep, typename _Period, typename _Predicate>
  145. bool
  146. wait_for(unique_lock<mutex>& __lock,
  147. const chrono::duration<_Rep, _Period>& __rtime,
  148. _Predicate __p)
  149. {
  150. using __dur = typename steady_clock::duration;
  151. auto __reltime = chrono::duration_cast<__dur>(__rtime);
  152. if (__reltime < __rtime)
  153. ++__reltime;
  154. return wait_until(__lock, steady_clock::now() + __reltime,
  155. std::move(__p));
  156. }
  157. native_handle_type
  158. native_handle()
  159. { return &_M_cond; }
  160. private:
  161. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  162. template<typename _Dur>
  163. cv_status
  164. __wait_until_impl(unique_lock<mutex>& __lock,
  165. const chrono::time_point<steady_clock, _Dur>& __atime)
  166. {
  167. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  168. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  169. __gthread_time_t __ts =
  170. {
  171. static_cast<std::time_t>(__s.time_since_epoch().count()),
  172. static_cast<long>(__ns.count())
  173. };
  174. pthread_cond_clockwait(&_M_cond, __lock.mutex()->native_handle(),
  175. CLOCK_MONOTONIC,
  176. &__ts);
  177. return (steady_clock::now() < __atime
  178. ? cv_status::no_timeout : cv_status::timeout);
  179. }
  180. #endif
  181. template<typename _Dur>
  182. cv_status
  183. __wait_until_impl(unique_lock<mutex>& __lock,
  184. const chrono::time_point<system_clock, _Dur>& __atime)
  185. {
  186. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  187. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  188. __gthread_time_t __ts =
  189. {
  190. static_cast<std::time_t>(__s.time_since_epoch().count()),
  191. static_cast<long>(__ns.count())
  192. };
  193. __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
  194. &__ts);
  195. return (system_clock::now() < __atime
  196. ? cv_status::no_timeout : cv_status::timeout);
  197. }
  198. };
  199. void
  200. notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
  201. struct __at_thread_exit_elt
  202. {
  203. __at_thread_exit_elt* _M_next;
  204. void (*_M_cb)(void*);
  205. };
  206. inline namespace _V2 {
  207. /// condition_variable_any
  208. // Like above, but mutex is not required to have try_lock.
  209. class condition_variable_any
  210. {
  211. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  212. using __clock_t = chrono::steady_clock;
  213. #else
  214. using __clock_t = chrono::system_clock;
  215. #endif
  216. condition_variable _M_cond;
  217. shared_ptr<mutex> _M_mutex;
  218. // scoped unlock - unlocks in ctor, re-locks in dtor
  219. template<typename _Lock>
  220. struct _Unlock
  221. {
  222. explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
  223. ~_Unlock() noexcept(false)
  224. {
  225. if (uncaught_exception())
  226. {
  227. __try
  228. { _M_lock.lock(); }
  229. __catch(const __cxxabiv1::__forced_unwind&)
  230. { __throw_exception_again; }
  231. __catch(...)
  232. { }
  233. }
  234. else
  235. _M_lock.lock();
  236. }
  237. _Unlock(const _Unlock&) = delete;
  238. _Unlock& operator=(const _Unlock&) = delete;
  239. _Lock& _M_lock;
  240. };
  241. public:
  242. condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
  243. ~condition_variable_any() = default;
  244. condition_variable_any(const condition_variable_any&) = delete;
  245. condition_variable_any& operator=(const condition_variable_any&) = delete;
  246. void
  247. notify_one() noexcept
  248. {
  249. lock_guard<mutex> __lock(*_M_mutex);
  250. _M_cond.notify_one();
  251. }
  252. void
  253. notify_all() noexcept
  254. {
  255. lock_guard<mutex> __lock(*_M_mutex);
  256. _M_cond.notify_all();
  257. }
  258. template<typename _Lock>
  259. void
  260. wait(_Lock& __lock)
  261. {
  262. shared_ptr<mutex> __mutex = _M_mutex;
  263. unique_lock<mutex> __my_lock(*__mutex);
  264. _Unlock<_Lock> __unlock(__lock);
  265. // *__mutex must be unlocked before re-locking __lock so move
  266. // ownership of *__mutex lock to an object with shorter lifetime.
  267. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  268. _M_cond.wait(__my_lock2);
  269. }
  270. template<typename _Lock, typename _Predicate>
  271. void
  272. wait(_Lock& __lock, _Predicate __p)
  273. {
  274. while (!__p())
  275. wait(__lock);
  276. }
  277. template<typename _Lock, typename _Clock, typename _Duration>
  278. cv_status
  279. wait_until(_Lock& __lock,
  280. const chrono::time_point<_Clock, _Duration>& __atime)
  281. {
  282. shared_ptr<mutex> __mutex = _M_mutex;
  283. unique_lock<mutex> __my_lock(*__mutex);
  284. _Unlock<_Lock> __unlock(__lock);
  285. // *__mutex must be unlocked before re-locking __lock so move
  286. // ownership of *__mutex lock to an object with shorter lifetime.
  287. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  288. return _M_cond.wait_until(__my_lock2, __atime);
  289. }
  290. template<typename _Lock, typename _Clock,
  291. typename _Duration, typename _Predicate>
  292. bool
  293. wait_until(_Lock& __lock,
  294. const chrono::time_point<_Clock, _Duration>& __atime,
  295. _Predicate __p)
  296. {
  297. while (!__p())
  298. if (wait_until(__lock, __atime) == cv_status::timeout)
  299. return __p();
  300. return true;
  301. }
  302. template<typename _Lock, typename _Rep, typename _Period>
  303. cv_status
  304. wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
  305. { return wait_until(__lock, __clock_t::now() + __rtime); }
  306. template<typename _Lock, typename _Rep,
  307. typename _Period, typename _Predicate>
  308. bool
  309. wait_for(_Lock& __lock,
  310. const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
  311. { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
  312. #ifdef __cpp_lib_jthread
  313. template <class _Lock, class _Predicate>
  314. bool wait(_Lock& __lock,
  315. stop_token __stoken,
  316. _Predicate __p)
  317. {
  318. if (__stoken.stop_requested())
  319. {
  320. return __p();
  321. }
  322. std::stop_callback __cb(__stoken, [this] { notify_all(); });
  323. shared_ptr<mutex> __mutex = _M_mutex;
  324. while (!__p())
  325. {
  326. unique_lock<mutex> __my_lock(*__mutex);
  327. if (__stoken.stop_requested())
  328. {
  329. return false;
  330. }
  331. // *__mutex must be unlocked before re-locking __lock so move
  332. // ownership of *__mutex lock to an object with shorter lifetime.
  333. _Unlock<_Lock> __unlock(__lock);
  334. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  335. _M_cond.wait(__my_lock2);
  336. }
  337. return true;
  338. }
  339. template <class _Lock, class _Clock, class _Duration, class _Predicate>
  340. bool wait_until(_Lock& __lock,
  341. stop_token __stoken,
  342. const chrono::time_point<_Clock, _Duration>& __abs_time,
  343. _Predicate __p)
  344. {
  345. if (__stoken.stop_requested())
  346. {
  347. return __p();
  348. }
  349. std::stop_callback __cb(__stoken, [this] { notify_all(); });
  350. shared_ptr<mutex> __mutex = _M_mutex;
  351. while (!__p())
  352. {
  353. bool __stop;
  354. {
  355. unique_lock<mutex> __my_lock(*__mutex);
  356. if (__stoken.stop_requested())
  357. {
  358. return false;
  359. }
  360. _Unlock<_Lock> __u(__lock);
  361. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  362. const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
  363. __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
  364. }
  365. if (__stop)
  366. {
  367. return __p();
  368. }
  369. }
  370. return true;
  371. }
  372. template <class _Lock, class _Rep, class _Period, class _Predicate>
  373. bool wait_for(_Lock& __lock,
  374. stop_token __stoken,
  375. const chrono::duration<_Rep, _Period>& __rel_time,
  376. _Predicate __p)
  377. {
  378. auto __abst = std::chrono::steady_clock::now() + __rel_time;
  379. return wait_until(__lock,
  380. std::move(__stoken),
  381. __abst,
  382. std::move(__p));
  383. }
  384. #endif
  385. };
  386. } // end inline namespace
  387. // @} group condition_variables
  388. _GLIBCXX_END_NAMESPACE_VERSION
  389. } // namespace
  390. #endif // _GLIBCXX_HAS_GTHREADS
  391. #endif // C++11
  392. #endif // _GLIBCXX_CONDITION_VARIABLE