condition_variable.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #ifndef BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_HPP
  2. #define BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_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-10 Anthony Williams
  7. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  8. #include <boost/thread/detail/platform_time.hpp>
  9. #include <boost/thread/pthread/pthread_mutex_scoped_lock.hpp>
  10. #include <boost/thread/pthread/pthread_helpers.hpp>
  11. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  12. #include <boost/thread/interruption.hpp>
  13. #include <boost/thread/pthread/thread_data.hpp>
  14. #endif
  15. #include <boost/thread/pthread/condition_variable_fwd.hpp>
  16. #ifdef BOOST_THREAD_USES_CHRONO
  17. #include <boost/chrono/system_clocks.hpp>
  18. #include <boost/chrono/ceil.hpp>
  19. #endif
  20. #include <boost/thread/detail/delete.hpp>
  21. #include <algorithm>
  22. #include <boost/config/abi_prefix.hpp>
  23. namespace boost
  24. {
  25. namespace thread_cv_detail
  26. {
  27. template<typename MutexType>
  28. struct lock_on_exit
  29. {
  30. MutexType* m;
  31. lock_on_exit():
  32. m(0)
  33. {}
  34. void activate(MutexType& m_)
  35. {
  36. m_.unlock();
  37. m=&m_;
  38. }
  39. void deactivate()
  40. {
  41. if (m)
  42. {
  43. m->lock();
  44. }
  45. m = 0;
  46. }
  47. ~lock_on_exit() BOOST_NOEXCEPT_IF(false)
  48. {
  49. if (m)
  50. {
  51. m->lock();
  52. }
  53. }
  54. };
  55. }
  56. inline void condition_variable::wait(unique_lock<mutex>& m)
  57. {
  58. #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
  59. if(! m.owns_lock())
  60. {
  61. boost::throw_exception(condition_error(-1, "boost::condition_variable::wait() failed precondition mutex not owned"));
  62. }
  63. #endif
  64. int res=0;
  65. {
  66. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  67. thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
  68. detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
  69. pthread_mutex_t* the_mutex = &internal_mutex;
  70. guard.activate(m);
  71. res = posix::pthread_cond_wait(&cond,the_mutex);
  72. check_for_interruption.unlock_if_locked();
  73. guard.deactivate();
  74. #else
  75. pthread_mutex_t* the_mutex = m.mutex()->native_handle();
  76. res = posix::pthread_cond_wait(&cond,the_mutex);
  77. #endif
  78. }
  79. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  80. this_thread::interruption_point();
  81. #endif
  82. if(res)
  83. {
  84. boost::throw_exception(condition_error(res, "boost::condition_variable::wait failed in pthread_cond_wait"));
  85. }
  86. }
  87. // When this function returns true:
  88. // * A notification (or sometimes a spurious OS signal) has been received
  89. // * Do not assume that the timeout has not been reached
  90. // * Do not assume that the predicate has been changed
  91. //
  92. // When this function returns false:
  93. // * The timeout has been reached
  94. // * Do not assume that a notification has not been received
  95. // * Do not assume that the predicate has not been changed
  96. inline bool condition_variable::do_wait_until(
  97. unique_lock<mutex>& m,
  98. detail::internal_platform_timepoint const &timeout)
  99. {
  100. #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
  101. if (!m.owns_lock())
  102. {
  103. boost::throw_exception(condition_error(EPERM, "boost::condition_variable::do_wait_until() failed precondition mutex not owned"));
  104. }
  105. #endif
  106. int cond_res;
  107. {
  108. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  109. thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
  110. detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
  111. pthread_mutex_t* the_mutex = &internal_mutex;
  112. guard.activate(m);
  113. cond_res=posix::pthread_cond_timedwait(&cond,the_mutex,&timeout.getTs());
  114. check_for_interruption.unlock_if_locked();
  115. guard.deactivate();
  116. #else
  117. pthread_mutex_t* the_mutex = m.mutex()->native_handle();
  118. cond_res=posix::pthread_cond_timedwait(&cond,the_mutex,&timeout.getTs());
  119. #endif
  120. }
  121. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  122. this_thread::interruption_point();
  123. #endif
  124. if(cond_res==ETIMEDOUT)
  125. {
  126. return false;
  127. }
  128. if(cond_res)
  129. {
  130. boost::throw_exception(condition_error(cond_res, "boost::condition_variable::do_wait_until failed in pthread_cond_timedwait"));
  131. }
  132. return true;
  133. }
  134. inline void condition_variable::notify_one() BOOST_NOEXCEPT
  135. {
  136. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  137. boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
  138. #endif
  139. BOOST_VERIFY(!posix::pthread_cond_signal(&cond));
  140. }
  141. inline void condition_variable::notify_all() BOOST_NOEXCEPT
  142. {
  143. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  144. boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
  145. #endif
  146. BOOST_VERIFY(!posix::pthread_cond_broadcast(&cond));
  147. }
  148. class condition_variable_any
  149. {
  150. pthread_mutex_t internal_mutex;
  151. pthread_cond_t cond;
  152. public:
  153. BOOST_THREAD_NO_COPYABLE(condition_variable_any)
  154. condition_variable_any()
  155. {
  156. int const res=posix::pthread_mutex_init(&internal_mutex);
  157. if(res)
  158. {
  159. boost::throw_exception(thread_resource_error(res, "boost::condition_variable_any::condition_variable_any() failed in pthread_mutex_init"));
  160. }
  161. int const res2 = posix::pthread_cond_init(&cond);
  162. if(res2)
  163. {
  164. BOOST_VERIFY(!posix::pthread_mutex_destroy(&internal_mutex));
  165. boost::throw_exception(thread_resource_error(res2, "boost::condition_variable_any::condition_variable_any() failed in pthread_cond_init"));
  166. }
  167. }
  168. ~condition_variable_any()
  169. {
  170. BOOST_VERIFY(!posix::pthread_mutex_destroy(&internal_mutex));
  171. BOOST_VERIFY(!posix::pthread_cond_destroy(&cond));
  172. }
  173. template<typename lock_type>
  174. void wait(lock_type& m)
  175. {
  176. int res=0;
  177. {
  178. thread_cv_detail::lock_on_exit<lock_type> guard;
  179. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  180. detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
  181. #else
  182. boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
  183. #endif
  184. guard.activate(m);
  185. res=posix::pthread_cond_wait(&cond,&internal_mutex);
  186. check_for_interruption.unlock_if_locked();
  187. guard.deactivate();
  188. }
  189. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  190. this_thread::interruption_point();
  191. #endif
  192. if(res)
  193. {
  194. boost::throw_exception(condition_error(res, "boost::condition_variable_any::wait() failed in pthread_cond_wait"));
  195. }
  196. }
  197. template<typename lock_type,typename predicate_type>
  198. void wait(lock_type& m,predicate_type pred)
  199. {
  200. while (!pred())
  201. {
  202. wait(m);
  203. }
  204. }
  205. #if defined BOOST_THREAD_USES_DATETIME
  206. template<typename lock_type>
  207. bool timed_wait(lock_type& m,boost::system_time const& abs_time)
  208. {
  209. #if defined BOOST_THREAD_WAIT_BUG
  210. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  211. #else
  212. const detail::real_platform_timepoint ts(abs_time);
  213. #endif
  214. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  215. // The system time may jump while this function is waiting. To compensate for this and time
  216. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  217. // and recheck the time remaining each time through the loop. However, because we can't
  218. // check the predicate each time do_wait_until() completes, this introduces the possibility
  219. // of not exiting the function when a notification occurs, since do_wait_until() may report
  220. // that it timed out even though a notification was received. The best this function can do
  221. // is report correctly whether or not it reached the timeout time.
  222. const detail::platform_duration d(ts - detail::real_platform_clock::now());
  223. do_wait_until(m, detail::internal_platform_clock::now() + d);
  224. return ts > detail::real_platform_clock::now();
  225. #else
  226. return do_wait_until(m, ts);
  227. #endif
  228. }
  229. template<typename lock_type>
  230. bool timed_wait(lock_type& m,::boost::xtime const& abs_time)
  231. {
  232. return timed_wait(m,system_time(abs_time));
  233. }
  234. template<typename lock_type,typename duration_type>
  235. bool timed_wait(lock_type& m,duration_type const& wait_duration)
  236. {
  237. if (wait_duration.is_pos_infinity())
  238. {
  239. wait(m);
  240. return true;
  241. }
  242. if (wait_duration.is_special())
  243. {
  244. return true;
  245. }
  246. detail::platform_duration d(wait_duration);
  247. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  248. // The system time may jump while this function is waiting. To compensate for this and time
  249. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  250. // and recheck the time remaining each time through the loop. However, because we can't
  251. // check the predicate each time do_wait_until() completes, this introduces the possibility
  252. // of not exiting the function when a notification occurs, since do_wait_until() may report
  253. // that it timed out even though a notification was received. The best this function can do
  254. // is report correctly whether or not it reached the timeout time.
  255. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  256. do_wait_until(m, detail::internal_platform_clock::now() + d);
  257. return ts > detail::mono_platform_clock::now();
  258. #else
  259. return do_wait_until(m, detail::internal_platform_clock::now() + d);
  260. #endif
  261. }
  262. template<typename lock_type,typename predicate_type>
  263. bool timed_wait(lock_type& m,boost::system_time const& abs_time, predicate_type pred)
  264. {
  265. #if defined BOOST_THREAD_WAIT_BUG
  266. const detail::real_platform_timepoint ts(abs_time + BOOST_THREAD_WAIT_BUG);
  267. #else
  268. const detail::real_platform_timepoint ts(abs_time);
  269. #endif
  270. while (!pred())
  271. {
  272. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  273. // The system time may jump while this function is waiting. To compensate for this
  274. // and time out near the correct time, we call do_wait_until() in a loop with a
  275. // short timeout and recheck the time remaining each time through the loop.
  276. detail::platform_duration d(ts - detail::real_platform_clock::now());
  277. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  278. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  279. do_wait_until(m, detail::internal_platform_clock::now() + d);
  280. #else
  281. if (!do_wait_until(m, ts)) break; // timeout occurred
  282. #endif
  283. }
  284. return pred();
  285. }
  286. template<typename lock_type,typename predicate_type>
  287. bool timed_wait(lock_type& m,::boost::xtime const& abs_time, predicate_type pred)
  288. {
  289. return timed_wait(m,system_time(abs_time),pred);
  290. }
  291. template<typename lock_type,typename duration_type,typename predicate_type>
  292. bool timed_wait(lock_type& m,duration_type const& wait_duration,predicate_type pred)
  293. {
  294. if (wait_duration.is_pos_infinity())
  295. {
  296. while (!pred())
  297. {
  298. wait(m);
  299. }
  300. return true;
  301. }
  302. if (wait_duration.is_special())
  303. {
  304. return pred();
  305. }
  306. detail::platform_duration d(wait_duration);
  307. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  308. // The system time may jump while this function is waiting. To compensate for this
  309. // and time out near the correct time, we call do_wait_until() in a loop with a
  310. // short timeout and recheck the time remaining each time through the loop.
  311. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  312. while (!pred())
  313. {
  314. if (d <= detail::platform_duration::zero()) break; // timeout occurred
  315. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  316. do_wait_until(m, detail::internal_platform_clock::now() + d);
  317. d = ts - detail::mono_platform_clock::now();
  318. }
  319. #else
  320. const detail::internal_platform_timepoint ts(detail::internal_platform_clock::now() + d);
  321. while (!pred())
  322. {
  323. if (!do_wait_until(m, ts)) break; // timeout occurred
  324. }
  325. #endif
  326. return pred();
  327. }
  328. #endif
  329. #ifdef BOOST_THREAD_USES_CHRONO
  330. template <class lock_type,class Duration>
  331. cv_status
  332. wait_until(
  333. lock_type& lock,
  334. const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  335. {
  336. const boost::detail::internal_platform_timepoint ts(t);
  337. if (do_wait_until(lock, ts)) return cv_status::no_timeout;
  338. else return cv_status::timeout;
  339. }
  340. template <class lock_type, class Clock, class Duration>
  341. cv_status
  342. wait_until(
  343. lock_type& lock,
  344. const chrono::time_point<Clock, Duration>& t)
  345. {
  346. // The system time may jump while this function is waiting. To compensate for this and time
  347. // out near the correct time, we could call do_wait_until() in a loop with a short timeout
  348. // and recheck the time remaining each time through the loop. However, because we can't
  349. // check the predicate each time do_wait_until() completes, this introduces the possibility
  350. // of not exiting the function when a notification occurs, since do_wait_until() may report
  351. // that it timed out even though a notification was received. The best this function can do
  352. // is report correctly whether or not it reached the timeout time.
  353. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  354. common_duration d(t - Clock::now());
  355. do_wait_until(lock, detail::internal_chrono_clock::now() + d);
  356. if (t > Clock::now()) return cv_status::no_timeout;
  357. else return cv_status::timeout;
  358. }
  359. template <class lock_type, class Rep, class Period>
  360. cv_status
  361. wait_for(
  362. lock_type& lock,
  363. const chrono::duration<Rep, Period>& d)
  364. {
  365. return wait_until(lock, chrono::steady_clock::now() + d);
  366. }
  367. template <class lock_type, class Duration, class Predicate>
  368. bool
  369. wait_until(
  370. lock_type& lock,
  371. const chrono::time_point<detail::internal_chrono_clock, Duration>& t,
  372. Predicate pred)
  373. {
  374. const detail::internal_platform_timepoint ts(t);
  375. while (!pred())
  376. {
  377. if (!do_wait_until(lock, ts)) break; // timeout occurred
  378. }
  379. return pred();
  380. }
  381. template <class lock_type, class Clock, class Duration, class Predicate>
  382. bool
  383. wait_until(
  384. lock_type& lock,
  385. const chrono::time_point<Clock, Duration>& t,
  386. Predicate pred)
  387. {
  388. // The system time may jump while this function is waiting. To compensate for this
  389. // and time out near the correct time, we call do_wait_until() in a loop with a
  390. // short timeout and recheck the time remaining each time through the loop.
  391. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  392. while (!pred())
  393. {
  394. common_duration d(t - Clock::now());
  395. if (d <= common_duration::zero()) break; // timeout occurred
  396. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  397. do_wait_until(lock, detail::internal_platform_clock::now() + detail::platform_duration(d));
  398. }
  399. return pred();
  400. }
  401. template <class lock_type, class Rep, class Period, class Predicate>
  402. bool
  403. wait_for(
  404. lock_type& lock,
  405. const chrono::duration<Rep, Period>& d,
  406. Predicate pred)
  407. {
  408. return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
  409. }
  410. #endif
  411. void notify_one() BOOST_NOEXCEPT
  412. {
  413. boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
  414. BOOST_VERIFY(!posix::pthread_cond_signal(&cond));
  415. }
  416. void notify_all() BOOST_NOEXCEPT
  417. {
  418. boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
  419. BOOST_VERIFY(!posix::pthread_cond_broadcast(&cond));
  420. }
  421. private:
  422. // When this function returns true:
  423. // * A notification (or sometimes a spurious OS signal) has been received
  424. // * Do not assume that the timeout has not been reached
  425. // * Do not assume that the predicate has been changed
  426. //
  427. // When this function returns false:
  428. // * The timeout has been reached
  429. // * Do not assume that a notification has not been received
  430. // * Do not assume that the predicate has not been changed
  431. template <class lock_type>
  432. bool do_wait_until(
  433. lock_type& m,
  434. detail::internal_platform_timepoint const &timeout)
  435. {
  436. int res=0;
  437. {
  438. thread_cv_detail::lock_on_exit<lock_type> guard;
  439. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  440. detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
  441. #else
  442. boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
  443. #endif
  444. guard.activate(m);
  445. res=posix::pthread_cond_timedwait(&cond,&internal_mutex,&timeout.getTs());
  446. check_for_interruption.unlock_if_locked();
  447. guard.deactivate();
  448. }
  449. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  450. this_thread::interruption_point();
  451. #endif
  452. if(res==ETIMEDOUT)
  453. {
  454. return false;
  455. }
  456. if(res)
  457. {
  458. boost::throw_exception(condition_error(res, "boost::condition_variable_any::do_wait_until() failed in pthread_cond_timedwait"));
  459. }
  460. return true;
  461. }
  462. };
  463. }
  464. #include <boost/config/abi_suffix.hpp>
  465. #endif