condition_variable 8.4 KB

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