sync_bounded_queue.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #ifndef BOOST_THREAD_CONCURRENT_QUEUES_SYNC_BOUNDED_QUEUE_HPP
  2. #define BOOST_THREAD_CONCURRENT_QUEUES_SYNC_BOUNDED_QUEUE_HPP
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // (C) Copyright Vicente J. Botet Escriba 2013-2014. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/thread for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include <boost/thread/detail/config.hpp>
  13. #include <boost/thread/condition_variable.hpp>
  14. #include <boost/thread/mutex.hpp>
  15. #include <boost/thread/detail/move.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/thread/concurrent_queues/queue_op_status.hpp>
  18. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  19. #include <boost/smart_ptr/shared_ptr.hpp>
  20. #include <boost/smart_ptr/make_shared.hpp>
  21. #endif
  22. #include <boost/config/abi_prefix.hpp>
  23. namespace boost
  24. {
  25. namespace concurrent
  26. {
  27. template <typename ValueType>
  28. class sync_bounded_queue
  29. {
  30. public:
  31. typedef ValueType value_type;
  32. typedef std::size_t size_type;
  33. // Constructors/Assignment/Destructors
  34. BOOST_THREAD_NO_COPYABLE(sync_bounded_queue)
  35. explicit sync_bounded_queue(size_type max_elems);
  36. template <typename Range>
  37. sync_bounded_queue(size_type max_elems, Range range);
  38. ~sync_bounded_queue();
  39. // Observers
  40. inline bool empty() const;
  41. inline bool full() const;
  42. inline size_type capacity() const;
  43. inline size_type size() const;
  44. inline bool closed() const;
  45. // Modifiers
  46. inline void close();
  47. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  48. inline void push(const value_type& x);
  49. inline void push(BOOST_THREAD_RV_REF(value_type) x);
  50. inline bool try_push(const value_type& x);
  51. inline bool try_push(BOOST_THREAD_RV_REF(value_type) x);
  52. inline bool try_push(no_block_tag, const value_type& x);
  53. inline bool try_push(no_block_tag, BOOST_THREAD_RV_REF(value_type) x);
  54. #endif
  55. inline void push_back(const value_type& x);
  56. inline void push_back(BOOST_THREAD_RV_REF(value_type) x);
  57. inline queue_op_status try_push_back(const value_type& x);
  58. inline queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x);
  59. inline queue_op_status nonblocking_push_back(const value_type& x);
  60. inline queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x);
  61. inline queue_op_status wait_push_back(const value_type& x);
  62. inline queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x);
  63. // Observers/Modifiers
  64. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  65. inline void pull(value_type&);
  66. // enable_if is_nothrow_copy_movable<value_type>
  67. inline value_type pull();
  68. inline shared_ptr<ValueType> ptr_pull();
  69. inline bool try_pull(value_type&);
  70. inline bool try_pull(no_block_tag,value_type&);
  71. inline shared_ptr<ValueType> try_pull();
  72. #endif
  73. inline void pull_front(value_type&);
  74. // enable_if is_nothrow_copy_movable<value_type>
  75. inline value_type pull_front();
  76. inline queue_op_status try_pull_front(value_type&);
  77. inline queue_op_status nonblocking_pull_front(value_type&);
  78. inline queue_op_status wait_pull_front(ValueType& elem);
  79. private:
  80. mutable mutex mtx_;
  81. condition_variable not_empty_;
  82. condition_variable not_full_;
  83. size_type waiting_full_;
  84. size_type waiting_empty_;
  85. value_type* data_;
  86. size_type in_;
  87. size_type out_;
  88. size_type capacity_;
  89. bool closed_;
  90. inline size_type inc(size_type idx) const BOOST_NOEXCEPT
  91. {
  92. return (idx + 1) % capacity_;
  93. }
  94. inline bool empty(unique_lock<mutex>& ) const BOOST_NOEXCEPT
  95. {
  96. return in_ == out_;
  97. }
  98. inline bool empty(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  99. {
  100. return in_ == out_;
  101. }
  102. inline bool full(unique_lock<mutex>& ) const BOOST_NOEXCEPT
  103. {
  104. return (inc(in_) == out_);
  105. }
  106. inline bool full(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  107. {
  108. return (inc(in_) == out_);
  109. }
  110. inline size_type capacity(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  111. {
  112. return capacity_-1;
  113. }
  114. inline size_type size(lock_guard<mutex>& lk) const BOOST_NOEXCEPT
  115. {
  116. if (full(lk)) return capacity(lk);
  117. return ((in_+capacity(lk)-out_) % capacity(lk));
  118. }
  119. inline void throw_if_closed(unique_lock<mutex>&);
  120. inline bool closed(unique_lock<mutex>&) const;
  121. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  122. inline bool try_pull(value_type& x, unique_lock<mutex>& lk);
  123. inline shared_ptr<value_type> try_pull(unique_lock<mutex>& lk);
  124. inline bool try_push(const value_type& x, unique_lock<mutex>& lk);
  125. inline bool try_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
  126. #endif
  127. inline queue_op_status try_pull_front(value_type& x, unique_lock<mutex>& lk);
  128. inline queue_op_status try_push_back(const value_type& x, unique_lock<mutex>& lk);
  129. inline queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
  130. inline queue_op_status wait_pull_front(value_type& x, unique_lock<mutex>& lk);
  131. inline queue_op_status wait_push_back(const value_type& x, unique_lock<mutex>& lk);
  132. inline queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
  133. inline void wait_until_not_empty(unique_lock<mutex>& lk);
  134. inline void wait_until_not_empty(unique_lock<mutex>& lk, bool&);
  135. inline size_type wait_until_not_full(unique_lock<mutex>& lk);
  136. inline size_type wait_until_not_full(unique_lock<mutex>& lk, bool&);
  137. inline void notify_not_empty_if_needed(unique_lock<mutex>& lk)
  138. {
  139. if (waiting_empty_ > 0)
  140. {
  141. --waiting_empty_;
  142. lk.unlock();
  143. not_empty_.notify_one();
  144. }
  145. }
  146. inline void notify_not_full_if_needed(unique_lock<mutex>& lk)
  147. {
  148. if (waiting_full_ > 0)
  149. {
  150. --waiting_full_;
  151. lk.unlock();
  152. not_full_.notify_one();
  153. }
  154. }
  155. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  156. inline void pull(value_type& elem, unique_lock<mutex>& lk)
  157. {
  158. elem = boost::move(data_[out_]);
  159. out_ = inc(out_);
  160. notify_not_full_if_needed(lk);
  161. }
  162. inline value_type pull(unique_lock<mutex>& lk)
  163. {
  164. value_type elem = boost::move(data_[out_]);
  165. out_ = inc(out_);
  166. notify_not_full_if_needed(lk);
  167. return boost::move(elem);
  168. }
  169. inline boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& lk)
  170. {
  171. shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_[out_]));
  172. out_ = inc(out_);
  173. notify_not_full_if_needed(lk);
  174. return res;
  175. }
  176. #endif
  177. inline void pull_front(value_type& elem, unique_lock<mutex>& lk)
  178. {
  179. elem = boost::move(data_[out_]);
  180. out_ = inc(out_);
  181. notify_not_full_if_needed(lk);
  182. }
  183. inline value_type pull_front(unique_lock<mutex>& lk)
  184. {
  185. value_type elem = boost::move(data_[out_]);
  186. out_ = inc(out_);
  187. notify_not_full_if_needed(lk);
  188. return boost::move(elem);
  189. }
  190. inline void set_in(size_type in, unique_lock<mutex>& lk)
  191. {
  192. in_ = in;
  193. notify_not_empty_if_needed(lk);
  194. }
  195. inline void push_at(const value_type& elem, size_type in_p_1, unique_lock<mutex>& lk)
  196. {
  197. data_[in_] = elem;
  198. set_in(in_p_1, lk);
  199. }
  200. inline void push_at(BOOST_THREAD_RV_REF(value_type) elem, size_type in_p_1, unique_lock<mutex>& lk)
  201. {
  202. data_[in_] = boost::move(elem);
  203. set_in(in_p_1, lk);
  204. }
  205. };
  206. template <typename ValueType>
  207. sync_bounded_queue<ValueType>::sync_bounded_queue(typename sync_bounded_queue<ValueType>::size_type max_elems) :
  208. waiting_full_(0), waiting_empty_(0), data_(new value_type[max_elems + 1]), in_(0), out_(0), capacity_(max_elems + 1),
  209. closed_(false)
  210. {
  211. BOOST_ASSERT_MSG(max_elems >= 1, "number of elements must be > 1");
  212. }
  213. // template <typename ValueType>
  214. // template <typename Range>
  215. // sync_bounded_queue<ValueType>::sync_bounded_queue(size_type max_elems, Range range) :
  216. // waiting_full_(0), waiting_empty_(0), data_(new value_type[max_elems + 1]), in_(0), out_(0), capacity_(max_elems + 1),
  217. // closed_(false)
  218. // {
  219. // BOOST_ASSERT_MSG(max_elems >= 1, "number of elements must be > 1");
  220. // BOOST_ASSERT_MSG(max_elems == size(range), "number of elements must match range's size");
  221. // try
  222. // {
  223. // typedef typename Range::iterator iterator_t;
  224. // iterator_t first = boost::begin(range);
  225. // iterator_t end = boost::end(range);
  226. // size_type in = 0;
  227. // for (iterator_t cur = first; cur != end; ++cur, ++in)
  228. // {
  229. // data_[in] = *cur;
  230. // }
  231. // set_in(in);
  232. // }
  233. // catch (...)
  234. // {
  235. // delete[] data_;
  236. // }
  237. // }
  238. template <typename ValueType>
  239. sync_bounded_queue<ValueType>::~sync_bounded_queue()
  240. {
  241. delete[] data_;
  242. }
  243. template <typename ValueType>
  244. void sync_bounded_queue<ValueType>::close()
  245. {
  246. {
  247. lock_guard<mutex> lk(mtx_);
  248. closed_ = true;
  249. }
  250. not_empty_.notify_all();
  251. not_full_.notify_all();
  252. }
  253. template <typename ValueType>
  254. bool sync_bounded_queue<ValueType>::closed() const
  255. {
  256. lock_guard<mutex> lk(mtx_);
  257. return closed_;
  258. }
  259. template <typename ValueType>
  260. bool sync_bounded_queue<ValueType>::closed(unique_lock<mutex>& ) const
  261. {
  262. return closed_;
  263. }
  264. template <typename ValueType>
  265. bool sync_bounded_queue<ValueType>::empty() const
  266. {
  267. lock_guard<mutex> lk(mtx_);
  268. return empty(lk);
  269. }
  270. template <typename ValueType>
  271. bool sync_bounded_queue<ValueType>::full() const
  272. {
  273. lock_guard<mutex> lk(mtx_);
  274. return full(lk);
  275. }
  276. template <typename ValueType>
  277. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::capacity() const
  278. {
  279. lock_guard<mutex> lk(mtx_);
  280. return capacity(lk);
  281. }
  282. template <typename ValueType>
  283. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::size() const
  284. {
  285. lock_guard<mutex> lk(mtx_);
  286. return size(lk);
  287. }
  288. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  289. template <typename ValueType>
  290. bool sync_bounded_queue<ValueType>::try_pull(ValueType& elem, unique_lock<mutex>& lk)
  291. {
  292. if (empty(lk))
  293. {
  294. throw_if_closed(lk);
  295. return false;
  296. }
  297. pull(elem, lk);
  298. return true;
  299. }
  300. template <typename ValueType>
  301. shared_ptr<ValueType> sync_bounded_queue<ValueType>::try_pull(unique_lock<mutex>& lk)
  302. {
  303. if (empty(lk))
  304. {
  305. throw_if_closed(lk);
  306. return shared_ptr<ValueType>();
  307. }
  308. return ptr_pull(lk);
  309. }
  310. template <typename ValueType>
  311. bool sync_bounded_queue<ValueType>::try_pull(ValueType& elem)
  312. {
  313. unique_lock<mutex> lk(mtx_);
  314. return try_pull(elem, lk);
  315. }
  316. #endif
  317. template <typename ValueType>
  318. queue_op_status sync_bounded_queue<ValueType>::try_pull_front(ValueType& elem, unique_lock<mutex>& lk)
  319. {
  320. if (empty(lk))
  321. {
  322. if (closed(lk)) return queue_op_status::closed;
  323. return queue_op_status::empty;
  324. }
  325. pull_front(elem, lk);
  326. return queue_op_status::success;
  327. }
  328. template <typename ValueType>
  329. queue_op_status sync_bounded_queue<ValueType>::try_pull_front(ValueType& elem)
  330. {
  331. unique_lock<mutex> lk(mtx_);
  332. return try_pull_front(elem, lk);
  333. }
  334. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  335. template <typename ValueType>
  336. bool sync_bounded_queue<ValueType>::try_pull(no_block_tag,ValueType& elem)
  337. {
  338. unique_lock<mutex> lk(mtx_, try_to_lock);
  339. if (!lk.owns_lock())
  340. {
  341. return false;
  342. }
  343. return try_pull(elem, lk);
  344. }
  345. template <typename ValueType>
  346. boost::shared_ptr<ValueType> sync_bounded_queue<ValueType>::try_pull()
  347. {
  348. unique_lock<mutex> lk(mtx_);
  349. return try_pull(lk);
  350. }
  351. #endif
  352. template <typename ValueType>
  353. queue_op_status sync_bounded_queue<ValueType>::nonblocking_pull_front(ValueType& elem)
  354. {
  355. unique_lock<mutex> lk(mtx_, try_to_lock);
  356. if (!lk.owns_lock())
  357. {
  358. return queue_op_status::busy;
  359. }
  360. return try_pull_front(elem, lk);
  361. }
  362. template <typename ValueType>
  363. void sync_bounded_queue<ValueType>::throw_if_closed(unique_lock<mutex>&)
  364. {
  365. if (closed_)
  366. {
  367. BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
  368. }
  369. }
  370. template <typename ValueType>
  371. void sync_bounded_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk)
  372. {
  373. for (;;)
  374. {
  375. if (out_ != in_) break;
  376. throw_if_closed(lk);
  377. ++waiting_empty_;
  378. not_empty_.wait(lk);
  379. }
  380. }
  381. template <typename ValueType>
  382. void sync_bounded_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk, bool & closed)
  383. {
  384. for (;;)
  385. {
  386. if (out_ != in_) break;
  387. if (closed_) {closed=true; return;}
  388. ++waiting_empty_;
  389. not_empty_.wait(lk);
  390. }
  391. }
  392. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  393. template <typename ValueType>
  394. void sync_bounded_queue<ValueType>::pull(ValueType& elem)
  395. {
  396. unique_lock<mutex> lk(mtx_);
  397. wait_until_not_empty(lk);
  398. pull(elem, lk);
  399. }
  400. // template <typename ValueType>
  401. // void sync_bounded_queue<ValueType>::pull(ValueType& elem, bool & closed)
  402. // {
  403. // unique_lock<mutex> lk(mtx_);
  404. // wait_until_not_empty(lk, closed);
  405. // if (closed) {return;}
  406. // pull(elem, lk);
  407. // }
  408. // enable if ValueType is nothrow movable
  409. template <typename ValueType>
  410. ValueType sync_bounded_queue<ValueType>::pull()
  411. {
  412. unique_lock<mutex> lk(mtx_);
  413. wait_until_not_empty(lk);
  414. return pull(lk);
  415. }
  416. template <typename ValueType>
  417. boost::shared_ptr<ValueType> sync_bounded_queue<ValueType>::ptr_pull()
  418. {
  419. unique_lock<mutex> lk(mtx_);
  420. wait_until_not_empty(lk);
  421. return ptr_pull(lk);
  422. }
  423. #endif
  424. template <typename ValueType>
  425. void sync_bounded_queue<ValueType>::pull_front(ValueType& elem)
  426. {
  427. unique_lock<mutex> lk(mtx_);
  428. wait_until_not_empty(lk);
  429. pull_front(elem, lk);
  430. }
  431. // enable if ValueType is nothrow movable
  432. template <typename ValueType>
  433. ValueType sync_bounded_queue<ValueType>::pull_front()
  434. {
  435. unique_lock<mutex> lk(mtx_);
  436. wait_until_not_empty(lk);
  437. return pull_front(lk);
  438. }
  439. template <typename ValueType>
  440. queue_op_status sync_bounded_queue<ValueType>::wait_pull_front(ValueType& elem, unique_lock<mutex>& lk)
  441. {
  442. if (empty(lk) && closed(lk)) {return queue_op_status::closed;}
  443. bool is_closed = false;
  444. wait_until_not_empty(lk, is_closed);
  445. if (is_closed) {return queue_op_status::closed;}
  446. pull_front(elem, lk);
  447. return queue_op_status::success;
  448. }
  449. template <typename ValueType>
  450. queue_op_status sync_bounded_queue<ValueType>::wait_pull_front(ValueType& elem)
  451. {
  452. unique_lock<mutex> lk(mtx_);
  453. return wait_pull_front(elem, lk);
  454. }
  455. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  456. template <typename ValueType>
  457. bool sync_bounded_queue<ValueType>::try_push(const ValueType& elem, unique_lock<mutex>& lk)
  458. {
  459. throw_if_closed(lk);
  460. size_type in_p_1 = inc(in_);
  461. if (in_p_1 == out_) // full()
  462. {
  463. return false;
  464. }
  465. push_at(elem, in_p_1, lk);
  466. return true;
  467. }
  468. template <typename ValueType>
  469. bool sync_bounded_queue<ValueType>::try_push(const ValueType& elem)
  470. {
  471. unique_lock<mutex> lk(mtx_);
  472. return try_push(elem, lk);
  473. }
  474. #endif
  475. template <typename ValueType>
  476. queue_op_status sync_bounded_queue<ValueType>::try_push_back(const ValueType& elem, unique_lock<mutex>& lk)
  477. {
  478. if (closed(lk)) return queue_op_status::closed;
  479. size_type in_p_1 = inc(in_);
  480. if (in_p_1 == out_) // full()
  481. {
  482. return queue_op_status::full;
  483. }
  484. push_at(elem, in_p_1, lk);
  485. return queue_op_status::success;
  486. }
  487. template <typename ValueType>
  488. queue_op_status sync_bounded_queue<ValueType>::try_push_back(const ValueType& elem)
  489. {
  490. unique_lock<mutex> lk(mtx_);
  491. return try_push_back(elem, lk);
  492. }
  493. template <typename ValueType>
  494. queue_op_status sync_bounded_queue<ValueType>::wait_push_back(const ValueType& elem, unique_lock<mutex>& lk)
  495. {
  496. if (closed(lk)) return queue_op_status::closed;
  497. push_at(elem, wait_until_not_full(lk), lk);
  498. return queue_op_status::success;
  499. }
  500. template <typename ValueType>
  501. queue_op_status sync_bounded_queue<ValueType>::wait_push_back(const ValueType& elem)
  502. {
  503. unique_lock<mutex> lk(mtx_);
  504. return wait_push_back(elem, lk);
  505. }
  506. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  507. template <typename ValueType>
  508. bool sync_bounded_queue<ValueType>::try_push(no_block_tag, const ValueType& elem)
  509. {
  510. unique_lock<mutex> lk(mtx_, try_to_lock);
  511. if (!lk.owns_lock()) return false;
  512. return try_push(elem, lk);
  513. }
  514. #endif
  515. template <typename ValueType>
  516. queue_op_status sync_bounded_queue<ValueType>::nonblocking_push_back(const ValueType& elem)
  517. {
  518. unique_lock<mutex> lk(mtx_, try_to_lock);
  519. if (!lk.owns_lock()) return queue_op_status::busy;
  520. return try_push_back(elem, lk);
  521. }
  522. template <typename ValueType>
  523. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::wait_until_not_full(unique_lock<mutex>& lk)
  524. {
  525. for (;;)
  526. {
  527. throw_if_closed(lk);
  528. size_type in_p_1 = inc(in_);
  529. if (in_p_1 != out_) // ! full()
  530. {
  531. return in_p_1;
  532. }
  533. ++waiting_full_;
  534. not_full_.wait(lk);
  535. }
  536. }
  537. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  538. template <typename ValueType>
  539. void sync_bounded_queue<ValueType>::push(const ValueType& elem)
  540. {
  541. unique_lock<mutex> lk(mtx_);
  542. push_at(elem, wait_until_not_full(lk), lk);
  543. }
  544. #endif
  545. template <typename ValueType>
  546. void sync_bounded_queue<ValueType>::push_back(const ValueType& elem)
  547. {
  548. unique_lock<mutex> lk(mtx_);
  549. push_at(elem, wait_until_not_full(lk), lk);
  550. }
  551. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  552. template <typename ValueType>
  553. bool sync_bounded_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  554. {
  555. throw_if_closed(lk);
  556. size_type in_p_1 = inc(in_);
  557. if (in_p_1 == out_) // full()
  558. {
  559. return false;
  560. }
  561. push_at(boost::move(elem), in_p_1, lk);
  562. return true;
  563. }
  564. template <typename ValueType>
  565. bool sync_bounded_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem)
  566. {
  567. unique_lock<mutex> lk(mtx_);
  568. return try_push(boost::move(elem), lk);
  569. }
  570. #endif
  571. template <typename ValueType>
  572. queue_op_status sync_bounded_queue<ValueType>::try_push_back(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  573. {
  574. if (closed(lk)) return queue_op_status::closed;
  575. size_type in_p_1 = inc(in_);
  576. if (in_p_1 == out_) // full()
  577. {
  578. return queue_op_status::full;
  579. }
  580. push_at(boost::move(elem), in_p_1, lk);
  581. return queue_op_status::success;
  582. }
  583. template <typename ValueType>
  584. queue_op_status sync_bounded_queue<ValueType>::try_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
  585. {
  586. unique_lock<mutex> lk(mtx_);
  587. return try_push_back(boost::move(elem), lk);
  588. }
  589. template <typename ValueType>
  590. queue_op_status sync_bounded_queue<ValueType>::wait_push_back(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  591. {
  592. if (closed(lk)) return queue_op_status::closed;
  593. push_at(boost::move(elem), wait_until_not_full(lk), lk);
  594. return queue_op_status::success;
  595. }
  596. template <typename ValueType>
  597. queue_op_status sync_bounded_queue<ValueType>::wait_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
  598. {
  599. unique_lock<mutex> lk(mtx_);
  600. return try_push_back(boost::move(elem), lk);
  601. }
  602. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  603. template <typename ValueType>
  604. bool sync_bounded_queue<ValueType>::try_push(no_block_tag, BOOST_THREAD_RV_REF(ValueType) elem)
  605. {
  606. unique_lock<mutex> lk(mtx_, try_to_lock);
  607. if (!lk.owns_lock())
  608. {
  609. return false;
  610. }
  611. return try_push(boost::move(elem), lk);
  612. }
  613. #endif
  614. template <typename ValueType>
  615. queue_op_status sync_bounded_queue<ValueType>::nonblocking_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
  616. {
  617. unique_lock<mutex> lk(mtx_, try_to_lock);
  618. if (!lk.owns_lock())
  619. {
  620. return queue_op_status::busy;
  621. }
  622. return try_push_back(boost::move(elem), lk);
  623. }
  624. #ifndef BOOST_THREAD_QUEUE_DEPRECATE_OLD
  625. template <typename ValueType>
  626. void sync_bounded_queue<ValueType>::push(BOOST_THREAD_RV_REF(ValueType) elem)
  627. {
  628. unique_lock<mutex> lk(mtx_);
  629. push_at(boost::move(elem), wait_until_not_full(lk), lk);
  630. }
  631. #endif
  632. template <typename ValueType>
  633. void sync_bounded_queue<ValueType>::push_back(BOOST_THREAD_RV_REF(ValueType) elem)
  634. {
  635. unique_lock<mutex> lk(mtx_);
  636. push_at(boost::move(elem), wait_until_not_full(lk), lk);
  637. }
  638. template <typename ValueType>
  639. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
  640. {
  641. sbq.push_back(boost::move(elem));
  642. return sbq;
  643. }
  644. template <typename ValueType>
  645. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType const&elem)
  646. {
  647. sbq.push_back(elem);
  648. return sbq;
  649. }
  650. template <typename ValueType>
  651. sync_bounded_queue<ValueType>& operator>>(sync_bounded_queue<ValueType>& sbq, ValueType &elem)
  652. {
  653. sbq.pull_front(elem);
  654. return sbq;
  655. }
  656. }
  657. using concurrent::sync_bounded_queue;
  658. }
  659. #include <boost/config/abi_suffix.hpp>
  660. #endif