condition_variable 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // <condition_variable> -*- C++ -*-
  2. // Copyright (C) 2008, 2009 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 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. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  27. # include <c++0x_warning.h>
  28. #else
  29. #include <chrono>
  30. #include <mutex> // unique_lock
  31. #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
  32. namespace std
  33. {
  34. /**
  35. * @defgroup condition_variables Condition Variables
  36. * @ingroup concurrency
  37. *
  38. * Classes for condition_variable support.
  39. * @{
  40. */
  41. /// condition_variable
  42. class condition_variable
  43. {
  44. typedef chrono::system_clock __clock_t;
  45. typedef __gthread_cond_t __native_type;
  46. __native_type _M_cond;
  47. public:
  48. typedef __native_type* native_handle_type;
  49. condition_variable();
  50. ~condition_variable();
  51. condition_variable(const condition_variable&) = delete;
  52. condition_variable& operator=(const condition_variable&) = delete;
  53. void
  54. notify_one();
  55. void
  56. notify_all();
  57. void
  58. wait(unique_lock<mutex>& __lock);
  59. template<typename _Predicate>
  60. void
  61. wait(unique_lock<mutex>& __lock, _Predicate __p)
  62. {
  63. while (!__p())
  64. wait(__lock);
  65. }
  66. template<typename _Duration>
  67. bool
  68. wait_until(unique_lock<mutex>& __lock,
  69. const chrono::time_point<__clock_t, _Duration>& __atime)
  70. { return __wait_until_impl(__lock, __atime); }
  71. template<typename _Clock, typename _Duration>
  72. bool
  73. wait_until(unique_lock<mutex>& __lock,
  74. const chrono::time_point<_Clock, _Duration>& __atime)
  75. {
  76. // DR 887 - Sync unknown clock to known clock.
  77. typename _Clock::time_point __c_entry = _Clock::now();
  78. __clock_t::time_point __s_entry = __clock_t::now();
  79. chrono::nanoseconds __delta = __atime - __c_entry;
  80. __clock_t::time_point __s_atime = __s_entry + __delta;
  81. return __wait_until_impl(__lock, __s_atime);
  82. }
  83. template<typename _Clock, typename _Duration, typename _Predicate>
  84. bool
  85. wait_until(unique_lock<mutex>& __lock,
  86. const chrono::time_point<_Clock, _Duration>& __atime,
  87. _Predicate __p)
  88. {
  89. while (!__p())
  90. if (!wait_until(__lock, __atime))
  91. return __p();
  92. return true;
  93. }
  94. template<typename _Rep, typename _Period>
  95. bool
  96. wait_for(unique_lock<mutex>& __lock,
  97. const chrono::duration<_Rep, _Period>& __rtime)
  98. { return wait_until(__lock, __clock_t::now() + __rtime); }
  99. template<typename _Rep, typename _Period, typename _Predicate>
  100. bool
  101. wait_for(unique_lock<mutex>& __lock,
  102. const chrono::duration<_Rep, _Period>& __rtime,
  103. _Predicate __p)
  104. { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
  105. native_handle_type
  106. native_handle()
  107. { return &_M_cond; }
  108. private:
  109. template<typename _Clock, typename _Duration>
  110. bool
  111. __wait_until_impl(unique_lock<mutex>& __lock,
  112. const chrono::time_point<_Clock, _Duration>& __atime)
  113. {
  114. chrono::time_point<__clock_t, chrono::seconds> __s =
  115. chrono::time_point_cast<chrono::seconds>(__atime);
  116. chrono::nanoseconds __ns =
  117. chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  118. __gthread_time_t __ts =
  119. {
  120. static_cast<std::time_t>(__s.time_since_epoch().count()),
  121. static_cast<long>(__ns.count())
  122. };
  123. __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
  124. &__ts);
  125. return _Clock::now() < __atime;
  126. }
  127. };
  128. /// condition_variable_any
  129. // Like above, only mutex may not have try_lock.
  130. class condition_variable_any
  131. {
  132. typedef __gthread_cond_t __native_type;
  133. __native_type _M_cond;
  134. public:
  135. typedef __native_type* native_handle_type;
  136. condition_variable_any();
  137. ~condition_variable_any();
  138. condition_variable_any(const condition_variable_any&) = delete;
  139. condition_variable_any& operator=(const condition_variable_any&) = delete;
  140. void
  141. notify_one();
  142. void
  143. notify_all();
  144. template<typename _Lock>
  145. void
  146. wait(_Lock& __lock);
  147. template<typename _Lock, typename _Predicate>
  148. void
  149. wait(_Lock& __lock, _Predicate __p);
  150. template<typename _Lock, typename _Clock, typename _Duration>
  151. bool
  152. wait_until(_Lock& __lock,
  153. const chrono::time_point<_Clock, _Duration>& __atime);
  154. template<typename _Lock, typename _Clock,
  155. typename _Duration, typename _Predicate>
  156. bool
  157. wait_until(_Lock& __lock,
  158. const chrono::time_point<_Clock, _Duration>& __atime,
  159. _Predicate __p);
  160. template<typename _Lock, typename _Rep, typename _Period>
  161. bool
  162. wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
  163. template<typename _Lock, typename _Rep,
  164. typename _Period, typename _Predicate>
  165. bool
  166. wait_for(_Lock& __lock,
  167. const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
  168. native_handle_type
  169. native_handle()
  170. { return &_M_cond; }
  171. };
  172. // @} group condition_variables
  173. }
  174. #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
  175. #endif // __GXX_EXPERIMENTAL_CXX0X__
  176. #endif // _GLIBCXX_CONDITION_VARIABLE