condition_variable_fwd.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #ifndef BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
  2. #define BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // (C) Copyright 2007-8 Anthony Williams
  7. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  8. #include <boost/assert.hpp>
  9. #include <boost/throw_exception.hpp>
  10. #include <pthread.h>
  11. #include <boost/thread/cv_status.hpp>
  12. #include <boost/thread/mutex.hpp>
  13. #include <boost/thread/lock_types.hpp>
  14. #include <boost/thread/thread_time.hpp>
  15. #include <boost/thread/detail/platform_time.hpp>
  16. #include <boost/thread/pthread/pthread_helpers.hpp>
  17. #if defined BOOST_THREAD_USES_DATETIME
  18. #include <boost/thread/xtime.hpp>
  19. #endif
  20. #ifdef BOOST_THREAD_USES_CHRONO
  21. #include <boost/chrono/system_clocks.hpp>
  22. #include <boost/chrono/ceil.hpp>
  23. #endif
  24. #include <boost/thread/detail/delete.hpp>
  25. #include <boost/date_time/posix_time/posix_time_duration.hpp>
  26. #include <algorithm>
  27. #include <boost/config/abi_prefix.hpp>
  28. namespace boost
  29. {
  30. class condition_variable
  31. {
  32. private:
  33. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  34. pthread_mutex_t internal_mutex;
  35. //#endif
  36. pthread_cond_t cond;
  37. public:
  38. //private: // used by boost::thread::try_join_until
  39. bool do_wait_until(
  40. unique_lock<mutex>& lock,
  41. detail::internal_platform_timepoint const &timeout);
  42. public:
  43. BOOST_THREAD_NO_COPYABLE(condition_variable)
  44. condition_variable()
  45. {
  46. int res;
  47. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  48. // Even if it is not used, the internal_mutex exists (see
  49. // above) and must be initialized (etc) in case some
  50. // compilation units provide interruptions and others
  51. // don't.
  52. res=posix::pthread_mutex_init(&internal_mutex);
  53. if(res)
  54. {
  55. boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in pthread_mutex_init"));
  56. }
  57. //#endif
  58. res = posix::pthread_cond_init(&cond);
  59. if (res)
  60. {
  61. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  62. // ditto
  63. BOOST_VERIFY(!posix::pthread_mutex_destroy(&internal_mutex));
  64. //#endif
  65. boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in pthread_cond_init"));
  66. }
  67. }
  68. ~condition_variable()
  69. {
  70. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  71. // ditto
  72. BOOST_VERIFY(!posix::pthread_mutex_destroy(&internal_mutex));
  73. //#endif
  74. BOOST_VERIFY(!posix::pthread_cond_destroy(&cond));
  75. }
  76. void wait(unique_lock<mutex>& m);
  77. template<typename predicate_type>
  78. void wait(unique_lock<mutex>& m,predicate_type pred)
  79. {
  80. while (!pred())
  81. {
  82. wait(m);
  83. }
  84. }
  85. #if defined BOOST_THREAD_USES_DATETIME
  86. bool timed_wait(
  87. unique_lock<mutex>& m,
  88. boost::system_time const& abs_time)
  89. {
  90. #if defined BOOST_THREAD_WAIT_BUG
  91. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  92. #else
  93. const detail::real_platform_timepoint ts(abs_time);
  94. #endif
  95. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  96. // The system time may jump while this function is waiting. To compensate for this and time
  97. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  98. // and recheck the time remaining each time through the loop. However, because we can't
  99. // check the predicate each time do_wait_until() completes, this introduces the possibility
  100. // of not exiting the function when a notification occurs, since do_wait_until() may report
  101. // that it timed out even though a notification was received. The best this function can do
  102. // is report correctly whether or not it reached the timeout time.
  103. const detail::platform_duration d(ts - detail::real_platform_clock::now());
  104. do_wait_until(m, detail::internal_platform_clock::now() + d);
  105. return ts > detail::real_platform_clock::now();
  106. #else
  107. return do_wait_until(m, ts);
  108. #endif
  109. }
  110. bool timed_wait(
  111. unique_lock<mutex>& m,
  112. ::boost::xtime const& abs_time)
  113. {
  114. return timed_wait(m,system_time(abs_time));
  115. }
  116. template<typename duration_type>
  117. bool timed_wait(
  118. unique_lock<mutex>& m,
  119. duration_type const& wait_duration)
  120. {
  121. if (wait_duration.is_pos_infinity())
  122. {
  123. wait(m);
  124. return true;
  125. }
  126. if (wait_duration.is_special())
  127. {
  128. return true;
  129. }
  130. detail::platform_duration d(wait_duration);
  131. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  132. // The system time may jump while this function is waiting. To compensate for this and time
  133. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  134. // and recheck the time remaining each time through the loop. However, because we can't
  135. // check the predicate each time do_wait_until() completes, this introduces the possibility
  136. // of not exiting the function when a notification occurs, since do_wait_until() may report
  137. // that it timed out even though a notification was received. The best this function can do
  138. // is report correctly whether or not it reached the timeout time.
  139. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  140. do_wait_until(m, detail::internal_platform_clock::now() + d);
  141. return ts > detail::mono_platform_clock::now();
  142. #else
  143. return do_wait_until(m, detail::internal_platform_clock::now() + d);
  144. #endif
  145. }
  146. template<typename predicate_type>
  147. bool timed_wait(
  148. unique_lock<mutex>& m,
  149. boost::system_time const& abs_time,predicate_type pred)
  150. {
  151. #if defined BOOST_THREAD_WAIT_BUG
  152. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  153. #else
  154. const detail::real_platform_timepoint ts(abs_time);
  155. #endif
  156. while (!pred())
  157. {
  158. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  159. // The system time may jump while this function is waiting. To compensate for this
  160. // and time out near the correct time, we call do_wait_until() in a loop with a
  161. // short timeout and recheck the time remaining each time through the loop.
  162. detail::platform_duration d(ts - detail::real_platform_clock::now());
  163. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  164. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  165. do_wait_until(m, detail::internal_platform_clock::now() + d);
  166. #else
  167. if (!do_wait_until(m, ts)) break; // timeout occurred
  168. #endif
  169. }
  170. return pred();
  171. }
  172. template<typename predicate_type>
  173. bool timed_wait(
  174. unique_lock<mutex>& m,
  175. ::boost::xtime const& abs_time,predicate_type pred)
  176. {
  177. return timed_wait(m,system_time(abs_time),pred);
  178. }
  179. template<typename duration_type,typename predicate_type>
  180. bool timed_wait(
  181. unique_lock<mutex>& m,
  182. duration_type const& wait_duration,predicate_type pred)
  183. {
  184. if (wait_duration.is_pos_infinity())
  185. {
  186. while (!pred())
  187. {
  188. wait(m);
  189. }
  190. return true;
  191. }
  192. if (wait_duration.is_special())
  193. {
  194. return pred();
  195. }
  196. detail::platform_duration d(wait_duration);
  197. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  198. // The system time may jump while this function is waiting. To compensate for this
  199. // and time out near the correct time, we call do_wait_until() in a loop with a
  200. // short timeout and recheck the time remaining each time through the loop.
  201. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  202. while (!pred())
  203. {
  204. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  205. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  206. do_wait_until(m, detail::internal_platform_clock::now() + d);
  207. d = ts - detail::mono_platform_clock::now();
  208. }
  209. #else
  210. const detail::internal_platform_timepoint ts(detail::internal_platform_clock::now() + d);
  211. while (!pred())
  212. {
  213. if (!do_wait_until(m, ts)) break; // timeout occurred
  214. }
  215. #endif
  216. return pred();
  217. }
  218. #endif
  219. #ifdef BOOST_THREAD_USES_CHRONO
  220. template <class Duration>
  221. cv_status
  222. wait_until(
  223. unique_lock<mutex>& lock,
  224. const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  225. {
  226. const detail::internal_platform_timepoint ts(t);
  227. if (do_wait_until(lock, ts)) return cv_status::no_timeout;
  228. else return cv_status::timeout;
  229. }
  230. template <class Clock, class Duration>
  231. cv_status
  232. wait_until(
  233. unique_lock<mutex>& lock,
  234. const chrono::time_point<Clock, Duration>& t)
  235. {
  236. // The system time may jump while this function is waiting. To compensate for this and time
  237. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  238. // and recheck the time remaining each time through the loop. However, because we can't
  239. // check the predicate each time do_wait_until() completes, this introduces the possibility
  240. // of not exiting the function when a notification occurs, since do_wait_until() may report
  241. // that it timed out even though a notification was received. The best this function can do
  242. // is report correctly whether or not it reached the timeout time.
  243. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  244. common_duration d(t - Clock::now());
  245. do_wait_until(lock, detail::internal_chrono_clock::now() + d);
  246. if (t > Clock::now()) return cv_status::no_timeout;
  247. else return cv_status::timeout;
  248. }
  249. template <class Rep, class Period>
  250. cv_status
  251. wait_for(
  252. unique_lock<mutex>& lock,
  253. const chrono::duration<Rep, Period>& d)
  254. {
  255. return wait_until(lock, chrono::steady_clock::now() + d);
  256. }
  257. template <class Duration, class Predicate>
  258. bool
  259. wait_until(
  260. unique_lock<mutex>& lock,
  261. const chrono::time_point<detail::internal_chrono_clock, Duration>& t,
  262. Predicate pred)
  263. {
  264. const detail::internal_platform_timepoint ts(t);
  265. while (!pred())
  266. {
  267. if (!do_wait_until(lock, ts)) break; // timeout occurred
  268. }
  269. return pred();
  270. }
  271. template <class Clock, class Duration, class Predicate>
  272. bool
  273. wait_until(
  274. unique_lock<mutex>& lock,
  275. const chrono::time_point<Clock, Duration>& t,
  276. Predicate pred)
  277. {
  278. // The system time may jump while this function is waiting. To compensate for this
  279. // and time out near the correct time, we call do_wait_until() in a loop with a
  280. // short timeout and recheck the time remaining each time through the loop.
  281. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  282. while (!pred())
  283. {
  284. common_duration d(t - Clock::now());
  285. if (d <= common_duration::zero()) break; // timeout occurred
  286. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  287. do_wait_until(lock, detail::internal_platform_clock::now() + detail::platform_duration(d));
  288. }
  289. return pred();
  290. }
  291. template <class Rep, class Period, class Predicate>
  292. bool
  293. wait_for(
  294. unique_lock<mutex>& lock,
  295. const chrono::duration<Rep, Period>& d,
  296. Predicate pred)
  297. {
  298. return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
  299. }
  300. #endif
  301. #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE
  302. typedef pthread_cond_t* native_handle_type;
  303. native_handle_type native_handle()
  304. {
  305. return &cond;
  306. }
  307. void notify_one() BOOST_NOEXCEPT;
  308. void notify_all() BOOST_NOEXCEPT;
  309. };
  310. BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  311. }
  312. #include <boost/config/abi_suffix.hpp>
  313. #endif