condition_variable_fwd.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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=pthread_mutex_init(&internal_mutex,NULL);
  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 = pthread::cond_init(cond);
  59. if (res)
  60. {
  61. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  62. // ditto
  63. BOOST_VERIFY(!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. int ret;
  71. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  72. // ditto
  73. do {
  74. ret = pthread_mutex_destroy(&internal_mutex);
  75. } while (ret == EINTR);
  76. BOOST_ASSERT(!ret);
  77. //#endif
  78. do {
  79. ret = pthread_cond_destroy(&cond);
  80. } while (ret == EINTR);
  81. BOOST_ASSERT(!ret);
  82. }
  83. void wait(unique_lock<mutex>& m);
  84. template<typename predicate_type>
  85. void wait(unique_lock<mutex>& m,predicate_type pred)
  86. {
  87. while (!pred())
  88. {
  89. wait(m);
  90. }
  91. }
  92. #if defined BOOST_THREAD_USES_DATETIME
  93. bool timed_wait(
  94. unique_lock<mutex>& m,
  95. boost::system_time const& abs_time)
  96. {
  97. #if defined BOOST_THREAD_WAIT_BUG
  98. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  99. #else
  100. const detail::real_platform_timepoint ts(abs_time);
  101. #endif
  102. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  103. // The system time may jump while this function is waiting. To compensate for this and time
  104. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  105. // and recheck the time remaining each time through the loop. However, because we can't
  106. // check the predicate each time do_wait_until() completes, this introduces the possibility
  107. // of not exiting the function when a notification occurs, since do_wait_until() may report
  108. // that it timed out even though a notification was received. The best this function can do
  109. // is report correctly whether or not it reached the timeout time.
  110. const detail::platform_duration d(ts - detail::real_platform_clock::now());
  111. do_wait_until(m, detail::internal_platform_clock::now() + d);
  112. return ts > detail::real_platform_clock::now();
  113. #else
  114. return do_wait_until(m, ts);
  115. #endif
  116. }
  117. bool timed_wait(
  118. unique_lock<mutex>& m,
  119. ::boost::xtime const& abs_time)
  120. {
  121. return timed_wait(m,system_time(abs_time));
  122. }
  123. template<typename duration_type>
  124. bool timed_wait(
  125. unique_lock<mutex>& m,
  126. duration_type const& wait_duration)
  127. {
  128. if (wait_duration.is_pos_infinity())
  129. {
  130. wait(m);
  131. return true;
  132. }
  133. if (wait_duration.is_special())
  134. {
  135. return true;
  136. }
  137. detail::platform_duration d(wait_duration);
  138. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  139. // The system time may jump while this function is waiting. To compensate for this and time
  140. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  141. // and recheck the time remaining each time through the loop. However, because we can't
  142. // check the predicate each time do_wait_until() completes, this introduces the possibility
  143. // of not exiting the function when a notification occurs, since do_wait_until() may report
  144. // that it timed out even though a notification was received. The best this function can do
  145. // is report correctly whether or not it reached the timeout time.
  146. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  147. do_wait_until(m, detail::internal_platform_clock::now() + d);
  148. return ts > detail::mono_platform_clock::now();
  149. #else
  150. return do_wait_until(m, detail::internal_platform_clock::now() + d);
  151. #endif
  152. }
  153. template<typename predicate_type>
  154. bool timed_wait(
  155. unique_lock<mutex>& m,
  156. boost::system_time const& abs_time,predicate_type pred)
  157. {
  158. #if defined BOOST_THREAD_WAIT_BUG
  159. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  160. #else
  161. const detail::real_platform_timepoint ts(abs_time);
  162. #endif
  163. while (!pred())
  164. {
  165. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  166. // The system time may jump while this function is waiting. To compensate for this
  167. // and time out near the correct time, we call do_wait_until() in a loop with a
  168. // short timeout and recheck the time remaining each time through the loop.
  169. detail::platform_duration d(ts - detail::real_platform_clock::now());
  170. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  171. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  172. do_wait_until(m, detail::internal_platform_clock::now() + d);
  173. #else
  174. if (!do_wait_until(m, ts)) break; // timeout occurred
  175. #endif
  176. }
  177. return pred();
  178. }
  179. template<typename predicate_type>
  180. bool timed_wait(
  181. unique_lock<mutex>& m,
  182. ::boost::xtime const& abs_time,predicate_type pred)
  183. {
  184. return timed_wait(m,system_time(abs_time),pred);
  185. }
  186. template<typename duration_type,typename predicate_type>
  187. bool timed_wait(
  188. unique_lock<mutex>& m,
  189. duration_type const& wait_duration,predicate_type pred)
  190. {
  191. if (wait_duration.is_pos_infinity())
  192. {
  193. while (!pred())
  194. {
  195. wait(m);
  196. }
  197. return true;
  198. }
  199. if (wait_duration.is_special())
  200. {
  201. return pred();
  202. }
  203. detail::platform_duration d(wait_duration);
  204. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  205. // The system time may jump while this function is waiting. To compensate for this
  206. // and time out near the correct time, we call do_wait_until() in a loop with a
  207. // short timeout and recheck the time remaining each time through the loop.
  208. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  209. while (!pred())
  210. {
  211. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  212. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  213. do_wait_until(m, detail::internal_platform_clock::now() + d);
  214. d = ts - detail::mono_platform_clock::now();
  215. }
  216. #else
  217. const detail::internal_platform_timepoint ts(detail::internal_platform_clock::now() + d);
  218. while (!pred())
  219. {
  220. if (!do_wait_until(m, ts)) break; // timeout occurred
  221. }
  222. #endif
  223. return pred();
  224. }
  225. #endif
  226. #ifdef BOOST_THREAD_USES_CHRONO
  227. template <class Duration>
  228. cv_status
  229. wait_until(
  230. unique_lock<mutex>& lock,
  231. const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  232. {
  233. const detail::internal_platform_timepoint ts(t);
  234. if (do_wait_until(lock, ts)) return cv_status::no_timeout;
  235. else return cv_status::timeout;
  236. }
  237. template <class Clock, class Duration>
  238. cv_status
  239. wait_until(
  240. unique_lock<mutex>& lock,
  241. const chrono::time_point<Clock, Duration>& t)
  242. {
  243. // The system time may jump while this function is waiting. To compensate for this and time
  244. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  245. // and recheck the time remaining each time through the loop. However, because we can't
  246. // check the predicate each time do_wait_until() completes, this introduces the possibility
  247. // of not exiting the function when a notification occurs, since do_wait_until() may report
  248. // that it timed out even though a notification was received. The best this function can do
  249. // is report correctly whether or not it reached the timeout time.
  250. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  251. common_duration d(t - Clock::now());
  252. do_wait_until(lock, detail::internal_chrono_clock::now() + d);
  253. if (t > Clock::now()) return cv_status::no_timeout;
  254. else return cv_status::timeout;
  255. }
  256. template <class Rep, class Period>
  257. cv_status
  258. wait_for(
  259. unique_lock<mutex>& lock,
  260. const chrono::duration<Rep, Period>& d)
  261. {
  262. return wait_until(lock, chrono::steady_clock::now() + d);
  263. }
  264. template <class Duration, class Predicate>
  265. bool
  266. wait_until(
  267. unique_lock<mutex>& lock,
  268. const chrono::time_point<detail::internal_chrono_clock, Duration>& t,
  269. Predicate pred)
  270. {
  271. const detail::internal_platform_timepoint ts(t);
  272. while (!pred())
  273. {
  274. if (!do_wait_until(lock, ts)) break; // timeout occurred
  275. }
  276. return pred();
  277. }
  278. template <class Clock, class Duration, class Predicate>
  279. bool
  280. wait_until(
  281. unique_lock<mutex>& lock,
  282. const chrono::time_point<Clock, Duration>& t,
  283. Predicate pred)
  284. {
  285. // The system time may jump while this function is waiting. To compensate for this
  286. // and time out near the correct time, we call do_wait_until() in a loop with a
  287. // short timeout and recheck the time remaining each time through the loop.
  288. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  289. while (!pred())
  290. {
  291. common_duration d(t - Clock::now());
  292. if (d <= common_duration::zero()) break; // timeout occurred
  293. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  294. do_wait_until(lock, detail::internal_platform_clock::now() + detail::platform_duration(d));
  295. }
  296. return pred();
  297. }
  298. template <class Rep, class Period, class Predicate>
  299. bool
  300. wait_for(
  301. unique_lock<mutex>& lock,
  302. const chrono::duration<Rep, Period>& d,
  303. Predicate pred)
  304. {
  305. return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
  306. }
  307. #endif
  308. #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE
  309. typedef pthread_cond_t* native_handle_type;
  310. native_handle_type native_handle()
  311. {
  312. return &cond;
  313. }
  314. void notify_one() BOOST_NOEXCEPT;
  315. void notify_all() BOOST_NOEXCEPT;
  316. };
  317. BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  318. }
  319. #include <boost/config/abi_suffix.hpp>
  320. #endif