condition_variable 8.7 KB

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