sync_timed_queue.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Copyright (C) 2014 Ian Forbed
  2. // Copyright (C) 2014-2017 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_THREAD_SYNC_TIMED_QUEUE_HPP
  8. #define BOOST_THREAD_SYNC_TIMED_QUEUE_HPP
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/concurrent_queues/sync_priority_queue.hpp>
  11. #include <boost/chrono/duration.hpp>
  12. #include <boost/chrono/time_point.hpp>
  13. #include <boost/chrono/system_clocks.hpp>
  14. #include <boost/chrono/chrono_io.hpp>
  15. #include <algorithm> // std::min
  16. #include <boost/config/abi_prefix.hpp>
  17. namespace boost
  18. {
  19. namespace concurrent
  20. {
  21. namespace detail
  22. {
  23. // fixme: shouldn't the timepoint be configurable
  24. template <class T, class Clock = chrono::steady_clock, class TimePoint=typename Clock::time_point>
  25. struct scheduled_type
  26. {
  27. typedef T value_type;
  28. typedef Clock clock;
  29. typedef TimePoint time_point;
  30. T data;
  31. time_point time;
  32. BOOST_THREAD_COPYABLE_AND_MOVABLE(scheduled_type)
  33. scheduled_type(T const& pdata, time_point tp) : data(pdata), time(tp) {}
  34. scheduled_type(BOOST_THREAD_RV_REF(T) pdata, time_point tp) : data(boost::move(pdata)), time(tp) {}
  35. scheduled_type(scheduled_type const& other) : data(other.data), time(other.time) {}
  36. scheduled_type& operator=(BOOST_THREAD_COPY_ASSIGN_REF(scheduled_type) other) {
  37. data = other.data;
  38. time = other.time;
  39. return *this;
  40. }
  41. scheduled_type(BOOST_THREAD_RV_REF(scheduled_type) other) : data(boost::move(other.data)), time(other.time) {}
  42. scheduled_type& operator=(BOOST_THREAD_RV_REF(scheduled_type) other) {
  43. data = boost::move(other.data);
  44. time = other.time;
  45. return *this;
  46. }
  47. bool operator <(const scheduled_type & other) const
  48. {
  49. return this->time > other.time;
  50. }
  51. }; //end struct
  52. template <class Duration>
  53. chrono::time_point<chrono::steady_clock,Duration>
  54. limit_timepoint(chrono::time_point<chrono::steady_clock,Duration> const& tp)
  55. {
  56. // Clock == chrono::steady_clock
  57. return tp;
  58. }
  59. template <class Clock, class Duration>
  60. chrono::time_point<Clock,Duration>
  61. limit_timepoint(chrono::time_point<Clock,Duration> const& tp)
  62. {
  63. // Clock != chrono::steady_clock
  64. // The system time may jump while wait_until() is waiting. To compensate for this and time out near
  65. // the correct time, we limit how long wait_until() can wait before going around the loop again.
  66. const chrono::time_point<Clock,Duration> tpmax(chrono::time_point_cast<Duration>(Clock::now() + chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  67. return (std::min)(tp, tpmax);
  68. }
  69. template <class Duration>
  70. chrono::steady_clock::time_point
  71. convert_to_steady_clock_timepoint(chrono::time_point<chrono::steady_clock,Duration> const& tp)
  72. {
  73. // Clock == chrono::steady_clock
  74. return chrono::time_point_cast<chrono::steady_clock::duration>(tp);
  75. }
  76. template <class Clock, class Duration>
  77. chrono::steady_clock::time_point
  78. convert_to_steady_clock_timepoint(chrono::time_point<Clock,Duration> const& tp)
  79. {
  80. // Clock != chrono::steady_clock
  81. // The system time may jump while wait_until() is waiting. To compensate for this and time out near
  82. // the correct time, we limit how long wait_until() can wait before going around the loop again.
  83. const chrono::steady_clock::duration dura(chrono::duration_cast<chrono::steady_clock::duration>(tp - Clock::now()));
  84. const chrono::steady_clock::duration duramax(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  85. return chrono::steady_clock::now() + (std::min)(dura, duramax);
  86. }
  87. } //end detail namespace
  88. template <class T, class Clock = chrono::steady_clock, class TimePoint=typename Clock::time_point>
  89. class sync_timed_queue
  90. : private sync_priority_queue<detail::scheduled_type<T, Clock, TimePoint> >
  91. {
  92. typedef detail::scheduled_type<T, Clock, TimePoint> stype;
  93. typedef sync_priority_queue<stype> super;
  94. public:
  95. typedef T value_type;
  96. typedef Clock clock;
  97. typedef typename clock::duration duration;
  98. typedef typename clock::time_point time_point;
  99. typedef typename super::underlying_queue_type underlying_queue_type;
  100. typedef typename super::size_type size_type;
  101. typedef typename super::op_status op_status;
  102. sync_timed_queue() : super() {};
  103. ~sync_timed_queue() {}
  104. using super::size;
  105. using super::empty;
  106. using super::full;
  107. using super::close;
  108. using super::closed;
  109. T pull();
  110. void pull(T& elem);
  111. template <class Duration>
  112. queue_op_status pull_until(chrono::time_point<clock,Duration> const& tp, T& elem);
  113. template <class Rep, class Period>
  114. queue_op_status pull_for(chrono::duration<Rep,Period> const& dura, T& elem);
  115. queue_op_status try_pull(T& elem);
  116. queue_op_status wait_pull(T& elem);
  117. queue_op_status nonblocking_pull(T& elem);
  118. template <class Duration>
  119. void push(const T& elem, chrono::time_point<clock,Duration> const& tp);
  120. template <class Rep, class Period>
  121. void push(const T& elem, chrono::duration<Rep,Period> const& dura);
  122. template <class Duration>
  123. void push(BOOST_THREAD_RV_REF(T) elem, chrono::time_point<clock,Duration> const& tp);
  124. template <class Rep, class Period>
  125. void push(BOOST_THREAD_RV_REF(T) elem, chrono::duration<Rep,Period> const& dura);
  126. template <class Duration>
  127. queue_op_status try_push(const T& elem, chrono::time_point<clock,Duration> const& tp);
  128. template <class Rep, class Period>
  129. queue_op_status try_push(const T& elem, chrono::duration<Rep,Period> const& dura);
  130. template <class Duration>
  131. queue_op_status try_push(BOOST_THREAD_RV_REF(T) elem, chrono::time_point<clock,Duration> const& tp);
  132. template <class Rep, class Period>
  133. queue_op_status try_push(BOOST_THREAD_RV_REF(T) elem, chrono::duration<Rep,Period> const& dura);
  134. private:
  135. inline bool not_empty_and_time_reached(unique_lock<mutex>& lk) const;
  136. inline bool not_empty_and_time_reached(lock_guard<mutex>& lk) const;
  137. bool wait_to_pull(unique_lock<mutex>&);
  138. queue_op_status wait_to_pull_until(unique_lock<mutex>&, TimePoint const& tp);
  139. template <class Rep, class Period>
  140. queue_op_status wait_to_pull_for(unique_lock<mutex>& lk, chrono::duration<Rep,Period> const& dura);
  141. T pull(unique_lock<mutex>&);
  142. T pull(lock_guard<mutex>&);
  143. void pull(unique_lock<mutex>&, T& elem);
  144. void pull(lock_guard<mutex>&, T& elem);
  145. queue_op_status try_pull(unique_lock<mutex>&, T& elem);
  146. queue_op_status try_pull(lock_guard<mutex>&, T& elem);
  147. queue_op_status wait_pull(unique_lock<mutex>& lk, T& elem);
  148. sync_timed_queue(const sync_timed_queue&);
  149. sync_timed_queue& operator=(const sync_timed_queue&);
  150. sync_timed_queue(BOOST_THREAD_RV_REF(sync_timed_queue));
  151. sync_timed_queue& operator=(BOOST_THREAD_RV_REF(sync_timed_queue));
  152. }; //end class
  153. template <class T, class Clock, class TimePoint>
  154. template <class Duration>
  155. void sync_timed_queue<T, Clock, TimePoint>::push(const T& elem, chrono::time_point<clock,Duration> const& tp)
  156. {
  157. super::push(stype(elem,tp));
  158. }
  159. template <class T, class Clock, class TimePoint>
  160. template <class Rep, class Period>
  161. void sync_timed_queue<T, Clock, TimePoint>::push(const T& elem, chrono::duration<Rep,Period> const& dura)
  162. {
  163. push(elem, clock::now() + dura);
  164. }
  165. template <class T, class Clock, class TimePoint>
  166. template <class Duration>
  167. void sync_timed_queue<T, Clock, TimePoint>::push(BOOST_THREAD_RV_REF(T) elem, chrono::time_point<clock,Duration> const& tp)
  168. {
  169. super::push(stype(boost::move(elem),tp));
  170. }
  171. template <class T, class Clock, class TimePoint>
  172. template <class Rep, class Period>
  173. void sync_timed_queue<T, Clock, TimePoint>::push(BOOST_THREAD_RV_REF(T) elem, chrono::duration<Rep,Period> const& dura)
  174. {
  175. push(boost::move(elem), clock::now() + dura);
  176. }
  177. template <class T, class Clock, class TimePoint>
  178. template <class Duration>
  179. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_push(const T& elem, chrono::time_point<clock,Duration> const& tp)
  180. {
  181. return super::try_push(stype(elem,tp));
  182. }
  183. template <class T, class Clock, class TimePoint>
  184. template <class Rep, class Period>
  185. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_push(const T& elem, chrono::duration<Rep,Period> const& dura)
  186. {
  187. return try_push(elem,clock::now() + dura);
  188. }
  189. template <class T, class Clock, class TimePoint>
  190. template <class Duration>
  191. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_push(BOOST_THREAD_RV_REF(T) elem, chrono::time_point<clock,Duration> const& tp)
  192. {
  193. return super::try_push(stype(boost::move(elem), tp));
  194. }
  195. template <class T, class Clock, class TimePoint>
  196. template <class Rep, class Period>
  197. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_push(BOOST_THREAD_RV_REF(T) elem, chrono::duration<Rep,Period> const& dura)
  198. {
  199. return try_push(boost::move(elem), clock::now() + dura);
  200. }
  201. ///////////////////////////
  202. template <class T, class Clock, class TimePoint>
  203. bool sync_timed_queue<T, Clock, TimePoint>::not_empty_and_time_reached(unique_lock<mutex>& lk) const
  204. {
  205. return ! super::empty(lk) && clock::now() >= super::data_.top().time;
  206. }
  207. template <class T, class Clock, class TimePoint>
  208. bool sync_timed_queue<T, Clock, TimePoint>::not_empty_and_time_reached(lock_guard<mutex>& lk) const
  209. {
  210. return ! super::empty(lk) && clock::now() >= super::data_.top().time;
  211. }
  212. ///////////////////////////
  213. template <class T, class Clock, class TimePoint>
  214. bool sync_timed_queue<T, Clock, TimePoint>::wait_to_pull(unique_lock<mutex>& lk)
  215. {
  216. for (;;)
  217. {
  218. if (not_empty_and_time_reached(lk)) return false; // success
  219. if (super::closed(lk)) return true; // closed
  220. super::wait_until_not_empty_or_closed(lk);
  221. if (not_empty_and_time_reached(lk)) return false; // success
  222. if (super::closed(lk)) return true; // closed
  223. const time_point tpmin(detail::limit_timepoint(super::data_.top().time));
  224. super::cond_.wait_until(lk, tpmin);
  225. }
  226. }
  227. template <class T, class Clock, class TimePoint>
  228. queue_op_status sync_timed_queue<T, Clock, TimePoint>::wait_to_pull_until(unique_lock<mutex>& lk, TimePoint const& tp)
  229. {
  230. for (;;)
  231. {
  232. if (not_empty_and_time_reached(lk)) return queue_op_status::success;
  233. if (super::closed(lk)) return queue_op_status::closed;
  234. if (clock::now() >= tp) return super::empty(lk) ? queue_op_status::timeout : queue_op_status::not_ready;
  235. super::wait_until_not_empty_or_closed_until(lk, tp);
  236. if (not_empty_and_time_reached(lk)) return queue_op_status::success;
  237. if (super::closed(lk)) return queue_op_status::closed;
  238. if (clock::now() >= tp) return super::empty(lk) ? queue_op_status::timeout : queue_op_status::not_ready;
  239. const time_point tpmin((std::min)(tp, detail::limit_timepoint(super::data_.top().time)));
  240. super::cond_.wait_until(lk, tpmin);
  241. }
  242. }
  243. template <class T, class Clock, class TimePoint>
  244. template <class Rep, class Period>
  245. queue_op_status sync_timed_queue<T, Clock, TimePoint>::wait_to_pull_for(unique_lock<mutex>& lk, chrono::duration<Rep,Period> const& dura)
  246. {
  247. const chrono::steady_clock::time_point tp(chrono::steady_clock::now() + chrono::duration_cast<chrono::steady_clock::duration>(dura));
  248. for (;;)
  249. {
  250. if (not_empty_and_time_reached(lk)) return queue_op_status::success;
  251. if (super::closed(lk)) return queue_op_status::closed;
  252. if (chrono::steady_clock::now() >= tp) return super::empty(lk) ? queue_op_status::timeout : queue_op_status::not_ready;
  253. super::wait_until_not_empty_or_closed_until(lk, tp);
  254. if (not_empty_and_time_reached(lk)) return queue_op_status::success;
  255. if (super::closed(lk)) return queue_op_status::closed;
  256. if (chrono::steady_clock::now() >= tp) return super::empty(lk) ? queue_op_status::timeout : queue_op_status::not_ready;
  257. const chrono::steady_clock::time_point tpmin((std::min)(tp, detail::convert_to_steady_clock_timepoint(super::data_.top().time)));
  258. super::cond_.wait_until(lk, tpmin);
  259. }
  260. }
  261. ///////////////////////////
  262. template <class T, class Clock, class TimePoint>
  263. T sync_timed_queue<T, Clock, TimePoint>::pull(unique_lock<mutex>&)
  264. {
  265. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  266. return boost::move(super::data_.pull().data);
  267. #else
  268. return super::data_.pull().data;
  269. #endif
  270. }
  271. template <class T, class Clock, class TimePoint>
  272. T sync_timed_queue<T, Clock, TimePoint>::pull(lock_guard<mutex>&)
  273. {
  274. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  275. return boost::move(super::data_.pull().data);
  276. #else
  277. return super::data_.pull().data;
  278. #endif
  279. }
  280. template <class T, class Clock, class TimePoint>
  281. T sync_timed_queue<T, Clock, TimePoint>::pull()
  282. {
  283. unique_lock<mutex> lk(super::mtx_);
  284. const bool has_been_closed = wait_to_pull(lk);
  285. if (has_been_closed) super::throw_if_closed(lk);
  286. return pull(lk);
  287. }
  288. ///////////////////////////
  289. template <class T, class Clock, class TimePoint>
  290. void sync_timed_queue<T, Clock, TimePoint>::pull(unique_lock<mutex>&, T& elem)
  291. {
  292. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  293. elem = boost::move(super::data_.pull().data);
  294. #else
  295. elem = super::data_.pull().data;
  296. #endif
  297. }
  298. template <class T, class Clock, class TimePoint>
  299. void sync_timed_queue<T, Clock, TimePoint>::pull(lock_guard<mutex>&, T& elem)
  300. {
  301. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  302. elem = boost::move(super::data_.pull().data);
  303. #else
  304. elem = super::data_.pull().data;
  305. #endif
  306. }
  307. template <class T, class Clock, class TimePoint>
  308. void sync_timed_queue<T, Clock, TimePoint>::pull(T& elem)
  309. {
  310. unique_lock<mutex> lk(super::mtx_);
  311. const bool has_been_closed = wait_to_pull(lk);
  312. if (has_been_closed) super::throw_if_closed(lk);
  313. pull(lk, elem);
  314. }
  315. //////////////////////
  316. template <class T, class Clock, class TimePoint>
  317. template <class Duration>
  318. queue_op_status
  319. sync_timed_queue<T, Clock, TimePoint>::pull_until(chrono::time_point<clock,Duration> const& tp, T& elem)
  320. {
  321. unique_lock<mutex> lk(super::mtx_);
  322. const queue_op_status rc = wait_to_pull_until(lk, chrono::time_point_cast<typename time_point::duration>(tp));
  323. if (rc == queue_op_status::success) pull(lk, elem);
  324. return rc;
  325. }
  326. //////////////////////
  327. template <class T, class Clock, class TimePoint>
  328. template <class Rep, class Period>
  329. queue_op_status
  330. sync_timed_queue<T, Clock, TimePoint>::pull_for(chrono::duration<Rep,Period> const& dura, T& elem)
  331. {
  332. unique_lock<mutex> lk(super::mtx_);
  333. const queue_op_status rc = wait_to_pull_for(lk, dura);
  334. if (rc == queue_op_status::success) pull(lk, elem);
  335. return rc;
  336. }
  337. ///////////////////////////
  338. template <class T, class Clock, class TimePoint>
  339. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_pull(unique_lock<mutex>& lk, T& elem)
  340. {
  341. if (not_empty_and_time_reached(lk))
  342. {
  343. pull(lk, elem);
  344. return queue_op_status::success;
  345. }
  346. if (super::closed(lk)) return queue_op_status::closed;
  347. if (super::empty(lk)) return queue_op_status::empty;
  348. return queue_op_status::not_ready;
  349. }
  350. template <class T, class Clock, class TimePoint>
  351. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_pull(lock_guard<mutex>& lk, T& elem)
  352. {
  353. if (not_empty_and_time_reached(lk))
  354. {
  355. pull(lk, elem);
  356. return queue_op_status::success;
  357. }
  358. if (super::closed(lk)) return queue_op_status::closed;
  359. if (super::empty(lk)) return queue_op_status::empty;
  360. return queue_op_status::not_ready;
  361. }
  362. template <class T, class Clock, class TimePoint>
  363. queue_op_status sync_timed_queue<T, Clock, TimePoint>::try_pull(T& elem)
  364. {
  365. lock_guard<mutex> lk(super::mtx_);
  366. return try_pull(lk, elem);
  367. }
  368. ///////////////////////////
  369. template <class T, class Clock, class TimePoint>
  370. queue_op_status sync_timed_queue<T, Clock, TimePoint>::wait_pull(unique_lock<mutex>& lk, T& elem)
  371. {
  372. const bool has_been_closed = wait_to_pull(lk);
  373. if (has_been_closed) return queue_op_status::closed;
  374. pull(lk, elem);
  375. return queue_op_status::success;
  376. }
  377. template <class T, class Clock, class TimePoint>
  378. queue_op_status sync_timed_queue<T, Clock, TimePoint>::wait_pull(T& elem)
  379. {
  380. unique_lock<mutex> lk(super::mtx_);
  381. return wait_pull(lk, elem);
  382. }
  383. ///////////////////////////
  384. template <class T, class Clock, class TimePoint>
  385. queue_op_status sync_timed_queue<T, Clock, TimePoint>::nonblocking_pull(T& elem)
  386. {
  387. unique_lock<mutex> lk(super::mtx_, try_to_lock);
  388. if (! lk.owns_lock()) return queue_op_status::busy;
  389. return try_pull(lk, elem);
  390. }
  391. } //end concurrent namespace
  392. using concurrent::sync_timed_queue;
  393. } //end boost namespace
  394. #include <boost/config/abi_suffix.hpp>
  395. #endif