condition_variable 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // <condition_variable> -*- C++ -*-
  2. // Copyright (C) 2008-2019 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 defined(_GLIBCXX_HAS_GTHREADS)
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. /**
  43. * @defgroup condition_variables Condition Variables
  44. * @ingroup concurrency
  45. *
  46. * Classes for condition_variable support.
  47. * @{
  48. */
  49. /// cv_status
  50. enum class cv_status { no_timeout, timeout };
  51. /// condition_variable
  52. class condition_variable
  53. {
  54. typedef chrono::system_clock __clock_t;
  55. typedef chrono::steady_clock __steady_clock_t;
  56. typedef __gthread_cond_t __native_type;
  57. #ifdef __GTHREAD_COND_INIT
  58. __native_type _M_cond = __GTHREAD_COND_INIT;
  59. #else
  60. __native_type _M_cond;
  61. #endif
  62. public:
  63. typedef __native_type* native_handle_type;
  64. condition_variable() noexcept;
  65. ~condition_variable() noexcept;
  66. condition_variable(const condition_variable&) = delete;
  67. condition_variable& operator=(const condition_variable&) = delete;
  68. void
  69. notify_one() noexcept;
  70. void
  71. notify_all() noexcept;
  72. void
  73. wait(unique_lock<mutex>& __lock) noexcept;
  74. template<typename _Predicate>
  75. void
  76. wait(unique_lock<mutex>& __lock, _Predicate __p)
  77. {
  78. while (!__p())
  79. wait(__lock);
  80. }
  81. template<typename _Duration>
  82. cv_status
  83. wait_until(unique_lock<mutex>& __lock,
  84. const chrono::time_point<__clock_t, _Duration>& __atime)
  85. { return __wait_until_impl(__lock, __atime); }
  86. template<typename _Clock, typename _Duration>
  87. cv_status
  88. wait_until(unique_lock<mutex>& __lock,
  89. const chrono::time_point<_Clock, _Duration>& __atime)
  90. {
  91. // DR 887 - Sync unknown clock to known clock.
  92. const typename _Clock::time_point __c_entry = _Clock::now();
  93. const __clock_t::time_point __s_entry = __clock_t::now();
  94. const auto __delta = __atime - __c_entry;
  95. const auto __s_atime = __s_entry + __delta;
  96. if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
  97. return cv_status::no_timeout;
  98. // We got a timeout when measured against __clock_t but
  99. // we need to check against the caller-supplied clock
  100. // to tell whether we should return a timeout.
  101. if (_Clock::now() < __atime)
  102. return cv_status::no_timeout;
  103. return cv_status::timeout;
  104. }
  105. template<typename _Clock, typename _Duration, typename _Predicate>
  106. bool
  107. wait_until(unique_lock<mutex>& __lock,
  108. const chrono::time_point<_Clock, _Duration>& __atime,
  109. _Predicate __p)
  110. {
  111. while (!__p())
  112. if (wait_until(__lock, __atime) == cv_status::timeout)
  113. return __p();
  114. return true;
  115. }
  116. template<typename _Rep, typename _Period>
  117. cv_status
  118. wait_for(unique_lock<mutex>& __lock,
  119. const chrono::duration<_Rep, _Period>& __rtime)
  120. {
  121. using __dur = typename __steady_clock_t::duration;
  122. auto __reltime = chrono::duration_cast<__dur>(__rtime);
  123. if (__reltime < __rtime)
  124. ++__reltime;
  125. return wait_until(__lock, __steady_clock_t::now() + __reltime);
  126. }
  127. template<typename _Rep, typename _Period, typename _Predicate>
  128. bool
  129. wait_for(unique_lock<mutex>& __lock,
  130. const chrono::duration<_Rep, _Period>& __rtime,
  131. _Predicate __p)
  132. {
  133. using __dur = typename __steady_clock_t::duration;
  134. auto __reltime = chrono::duration_cast<__dur>(__rtime);
  135. if (__reltime < __rtime)
  136. ++__reltime;
  137. return wait_until(__lock, __steady_clock_t::now() + __reltime,
  138. std::move(__p));
  139. }
  140. native_handle_type
  141. native_handle()
  142. { return &_M_cond; }
  143. private:
  144. template<typename _Dur>
  145. cv_status
  146. __wait_until_impl(unique_lock<mutex>& __lock,
  147. const chrono::time_point<__clock_t, _Dur>& __atime)
  148. {
  149. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  150. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  151. __gthread_time_t __ts =
  152. {
  153. static_cast<std::time_t>(__s.time_since_epoch().count()),
  154. static_cast<long>(__ns.count())
  155. };
  156. __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
  157. &__ts);
  158. return (__clock_t::now() < __atime
  159. ? cv_status::no_timeout : cv_status::timeout);
  160. }
  161. };
  162. void
  163. notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
  164. struct __at_thread_exit_elt
  165. {
  166. __at_thread_exit_elt* _M_next;
  167. void (*_M_cb)(void*);
  168. };
  169. inline namespace _V2 {
  170. /// condition_variable_any
  171. // Like above, but mutex is not required to have try_lock.
  172. class condition_variable_any
  173. {
  174. typedef chrono::system_clock __clock_t;
  175. condition_variable _M_cond;
  176. shared_ptr<mutex> _M_mutex;
  177. // scoped unlock - unlocks in ctor, re-locks in dtor
  178. template<typename _Lock>
  179. struct _Unlock
  180. {
  181. explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
  182. ~_Unlock() noexcept(false)
  183. {
  184. if (uncaught_exception())
  185. {
  186. __try
  187. { _M_lock.lock(); }
  188. __catch(const __cxxabiv1::__forced_unwind&)
  189. { __throw_exception_again; }
  190. __catch(...)
  191. { }
  192. }
  193. else
  194. _M_lock.lock();
  195. }
  196. _Unlock(const _Unlock&) = delete;
  197. _Unlock& operator=(const _Unlock&) = delete;
  198. _Lock& _M_lock;
  199. };
  200. public:
  201. condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
  202. ~condition_variable_any() = default;
  203. condition_variable_any(const condition_variable_any&) = delete;
  204. condition_variable_any& operator=(const condition_variable_any&) = delete;
  205. void
  206. notify_one() noexcept
  207. {
  208. lock_guard<mutex> __lock(*_M_mutex);
  209. _M_cond.notify_one();
  210. }
  211. void
  212. notify_all() noexcept
  213. {
  214. lock_guard<mutex> __lock(*_M_mutex);
  215. _M_cond.notify_all();
  216. }
  217. template<typename _Lock>
  218. void
  219. wait(_Lock& __lock)
  220. {
  221. shared_ptr<mutex> __mutex = _M_mutex;
  222. unique_lock<mutex> __my_lock(*__mutex);
  223. _Unlock<_Lock> __unlock(__lock);
  224. // *__mutex must be unlocked before re-locking __lock so move
  225. // ownership of *__mutex lock to an object with shorter lifetime.
  226. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  227. _M_cond.wait(__my_lock2);
  228. }
  229. template<typename _Lock, typename _Predicate>
  230. void
  231. wait(_Lock& __lock, _Predicate __p)
  232. {
  233. while (!__p())
  234. wait(__lock);
  235. }
  236. template<typename _Lock, typename _Clock, typename _Duration>
  237. cv_status
  238. wait_until(_Lock& __lock,
  239. const chrono::time_point<_Clock, _Duration>& __atime)
  240. {
  241. shared_ptr<mutex> __mutex = _M_mutex;
  242. unique_lock<mutex> __my_lock(*__mutex);
  243. _Unlock<_Lock> __unlock(__lock);
  244. // *__mutex must be unlocked before re-locking __lock so move
  245. // ownership of *__mutex lock to an object with shorter lifetime.
  246. unique_lock<mutex> __my_lock2(std::move(__my_lock));
  247. return _M_cond.wait_until(__my_lock2, __atime);
  248. }
  249. template<typename _Lock, typename _Clock,
  250. typename _Duration, typename _Predicate>
  251. bool
  252. wait_until(_Lock& __lock,
  253. const chrono::time_point<_Clock, _Duration>& __atime,
  254. _Predicate __p)
  255. {
  256. while (!__p())
  257. if (wait_until(__lock, __atime) == cv_status::timeout)
  258. return __p();
  259. return true;
  260. }
  261. template<typename _Lock, typename _Rep, typename _Period>
  262. cv_status
  263. wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
  264. { return wait_until(__lock, __clock_t::now() + __rtime); }
  265. template<typename _Lock, typename _Rep,
  266. typename _Period, typename _Predicate>
  267. bool
  268. wait_for(_Lock& __lock,
  269. const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
  270. { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
  271. };
  272. } // end inline namespace
  273. // @} group condition_variables
  274. _GLIBCXX_END_NAMESPACE_VERSION
  275. } // namespace
  276. #endif // _GLIBCXX_HAS_GTHREADS
  277. #endif // C++11
  278. #endif // _GLIBCXX_CONDITION_VARIABLE