sync_bounded_queue.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #ifndef BOOST_THREAD_SYNC_BOUNDED_QUEUE_HPP
  2. #define BOOST_THREAD_SYNC_BOUNDED_QUEUE_HPP
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // (C) Copyright Vicente J. Botet Escriba 2013. 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/smart_ptr/shared_ptr.hpp>
  18. #include <boost/smart_ptr/make_shared.hpp>
  19. #include <boost/config/abi_prefix.hpp>
  20. namespace boost
  21. {
  22. BOOST_SCOPED_ENUM_DECLARE_BEGIN(queue_op_status)
  23. { success = 0, empty, full, closed, busy }
  24. BOOST_SCOPED_ENUM_DECLARE_END(queue_op_status)
  25. struct no_block_tag{};
  26. BOOST_CONSTEXPR_OR_CONST no_block_tag no_block = {};
  27. struct sync_queue_is_closed : std::exception {};
  28. template <typename ValueType>
  29. class sync_bounded_queue
  30. {
  31. public:
  32. typedef ValueType value_type;
  33. typedef std::size_t size_type;
  34. // Constructors/Assignment/Destructors
  35. BOOST_THREAD_NO_COPYABLE(sync_bounded_queue)
  36. explicit sync_bounded_queue(size_type max_elems);
  37. template <typename Range>
  38. sync_bounded_queue(size_type max_elems, Range range);
  39. ~sync_bounded_queue();
  40. // Observers
  41. inline bool empty() const;
  42. inline bool full() const;
  43. inline size_type capacity() const;
  44. inline size_type size() const;
  45. inline bool closed() const;
  46. // Modifiers
  47. inline void close();
  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. // Observers/Modifiers
  55. inline void pull(value_type&);
  56. inline void pull(ValueType& elem, bool & closed);
  57. // enable_if is_nothrow_copy_movable<value_type>
  58. inline value_type pull();
  59. inline shared_ptr<ValueType> ptr_pull();
  60. inline bool try_pull(value_type&);
  61. inline bool try_pull(no_block_tag,value_type&);
  62. inline shared_ptr<ValueType> try_pull();
  63. private:
  64. mutable mutex mtx_;
  65. condition_variable not_empty_;
  66. condition_variable not_full_;
  67. size_type waiting_full_;
  68. size_type waiting_empty_;
  69. value_type* data_;
  70. size_type in_;
  71. size_type out_;
  72. size_type capacity_;
  73. bool closed_;
  74. inline size_type inc(size_type idx) const BOOST_NOEXCEPT
  75. {
  76. return (idx + 1) % capacity_;
  77. }
  78. inline bool empty(unique_lock<mutex>& ) const BOOST_NOEXCEPT
  79. {
  80. return in_ == out_;
  81. }
  82. inline bool empty(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  83. {
  84. return in_ == out_;
  85. }
  86. inline bool full(unique_lock<mutex>& ) const BOOST_NOEXCEPT
  87. {
  88. return (inc(in_) == out_);
  89. }
  90. inline bool full(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  91. {
  92. return (inc(in_) == out_);
  93. }
  94. inline size_type capacity(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  95. {
  96. return capacity_-1;
  97. }
  98. inline size_type size(lock_guard<mutex>& lk) const BOOST_NOEXCEPT
  99. {
  100. if (full(lk)) return capacity(lk);
  101. return ((out_+capacity(lk)-in_) % capacity(lk));
  102. }
  103. inline void throw_if_closed(unique_lock<mutex>&);
  104. inline bool try_pull(value_type& x, unique_lock<mutex>& lk);
  105. inline bool try_push(const value_type& x, unique_lock<mutex>& lk);
  106. inline bool try_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
  107. inline shared_ptr<value_type> try_pull(unique_lock<mutex>& lk);
  108. inline void wait_until_not_empty(unique_lock<mutex>& lk);
  109. inline void wait_until_not_empty(unique_lock<mutex>& lk, bool&);
  110. inline size_type wait_until_not_full(unique_lock<mutex>& lk);
  111. inline size_type wait_until_not_full(unique_lock<mutex>& lk, bool&);
  112. inline void notify_not_empty_if_needed(unique_lock<mutex>& lk)
  113. {
  114. if (waiting_empty_ > 0)
  115. {
  116. --waiting_empty_;
  117. lk.unlock();
  118. not_empty_.notify_one();
  119. }
  120. }
  121. inline void notify_not_full_if_needed(unique_lock<mutex>& lk)
  122. {
  123. if (waiting_full_ > 0)
  124. {
  125. --waiting_full_;
  126. lk.unlock();
  127. not_full_.notify_one();
  128. }
  129. }
  130. inline void pull(value_type& elem, unique_lock<mutex>& lk)
  131. {
  132. elem = boost::move(data_[out_]);
  133. out_ = inc(out_);
  134. notify_not_full_if_needed(lk);
  135. }
  136. inline boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& lk)
  137. {
  138. shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_[out_]));
  139. out_ = inc(out_);
  140. notify_not_full_if_needed(lk);
  141. return res;
  142. }
  143. inline void set_in(size_type in, unique_lock<mutex>& lk)
  144. {
  145. in_ = in;
  146. notify_not_empty_if_needed(lk);
  147. }
  148. inline void push_at(const value_type& elem, size_type in_p_1, unique_lock<mutex>& lk)
  149. {
  150. data_[in_] = elem;
  151. set_in(in_p_1, lk);
  152. }
  153. inline void push_at(BOOST_THREAD_RV_REF(value_type) elem, size_type in_p_1, unique_lock<mutex>& lk)
  154. {
  155. data_[in_] = boost::move(elem);
  156. set_in(in_p_1, lk);
  157. }
  158. };
  159. template <typename ValueType>
  160. sync_bounded_queue<ValueType>::sync_bounded_queue(typename sync_bounded_queue<ValueType>::size_type max_elems) :
  161. waiting_full_(0), waiting_empty_(0), data_(new value_type[max_elems + 1]), in_(0), out_(0), capacity_(max_elems + 1),
  162. closed_(false)
  163. {
  164. BOOST_ASSERT_MSG(max_elems >= 1, "number of elements must be > 1");
  165. }
  166. // template <typename ValueType>
  167. // template <typename Range>
  168. // sync_bounded_queue<ValueType>::sync_bounded_queue(size_type max_elems, Range range) :
  169. // waiting_full_(0), waiting_empty_(0), data_(new value_type[max_elems + 1]), in_(0), out_(0), capacity_(max_elems + 1),
  170. // closed_(false)
  171. // {
  172. // BOOST_ASSERT_MSG(max_elems >= 1, "number of elements must be > 1");
  173. // BOOST_ASSERT_MSG(max_elems == size(range), "number of elements must match range's size");
  174. // try
  175. // {
  176. // typedef typename Range::iterator iterator_t;
  177. // iterator_t first = boost::begin(range);
  178. // iterator_t end = boost::end(range);
  179. // size_type in = 0;
  180. // for (iterator_t cur = first; cur != end; ++cur, ++in)
  181. // {
  182. // data_[in] = *cur;
  183. // }
  184. // set_in(in);
  185. // }
  186. // catch (...)
  187. // {
  188. // delete[] data_;
  189. // }
  190. // }
  191. template <typename ValueType>
  192. sync_bounded_queue<ValueType>::~sync_bounded_queue()
  193. {
  194. delete[] data_;
  195. }
  196. template <typename ValueType>
  197. void sync_bounded_queue<ValueType>::close()
  198. {
  199. {
  200. lock_guard<mutex> lk(mtx_);
  201. closed_ = true;
  202. }
  203. not_empty_.notify_all();
  204. not_full_.notify_all();
  205. }
  206. template <typename ValueType>
  207. bool sync_bounded_queue<ValueType>::closed() const
  208. {
  209. lock_guard<mutex> lk(mtx_);
  210. return closed_;
  211. }
  212. template <typename ValueType>
  213. bool sync_bounded_queue<ValueType>::empty() const
  214. {
  215. lock_guard<mutex> lk(mtx_);
  216. return empty(lk);
  217. }
  218. template <typename ValueType>
  219. bool sync_bounded_queue<ValueType>::full() const
  220. {
  221. lock_guard<mutex> lk(mtx_);
  222. return full(lk);
  223. }
  224. template <typename ValueType>
  225. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::capacity() const
  226. {
  227. lock_guard<mutex> lk(mtx_);
  228. return capacity(lk);
  229. }
  230. template <typename ValueType>
  231. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::size() const
  232. {
  233. lock_guard<mutex> lk(mtx_);
  234. return size(lk);
  235. }
  236. template <typename ValueType>
  237. bool sync_bounded_queue<ValueType>::try_pull(ValueType& elem, unique_lock<mutex>& lk)
  238. {
  239. if (empty(lk))
  240. {
  241. throw_if_closed(lk);
  242. return false;
  243. }
  244. pull(elem, lk);
  245. return true;
  246. }
  247. template <typename ValueType>
  248. shared_ptr<ValueType> sync_bounded_queue<ValueType>::try_pull(unique_lock<mutex>& lk)
  249. {
  250. if (empty(lk))
  251. {
  252. throw_if_closed(lk);
  253. return shared_ptr<ValueType>();
  254. }
  255. return ptr_pull(lk);
  256. }
  257. template <typename ValueType>
  258. bool sync_bounded_queue<ValueType>::try_pull(ValueType& elem)
  259. {
  260. try
  261. {
  262. unique_lock<mutex> lk(mtx_);
  263. return try_pull(elem, lk);
  264. }
  265. catch (...)
  266. {
  267. close();
  268. throw;
  269. }
  270. }
  271. template <typename ValueType>
  272. bool sync_bounded_queue<ValueType>::try_pull(no_block_tag,ValueType& elem)
  273. {
  274. try
  275. {
  276. unique_lock<mutex> lk(mtx_, try_to_lock);
  277. if (!lk.owns_lock())
  278. {
  279. return false;
  280. }
  281. return try_pull(elem, lk);
  282. }
  283. catch (...)
  284. {
  285. close();
  286. throw;
  287. }
  288. }
  289. template <typename ValueType>
  290. boost::shared_ptr<ValueType> sync_bounded_queue<ValueType>::try_pull()
  291. {
  292. try
  293. {
  294. unique_lock<mutex> lk(mtx_);
  295. return try_pull(lk);
  296. }
  297. catch (...)
  298. {
  299. close();
  300. throw;
  301. }
  302. }
  303. template <typename ValueType>
  304. void sync_bounded_queue<ValueType>::throw_if_closed(unique_lock<mutex>&)
  305. {
  306. if (closed_)
  307. {
  308. BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
  309. }
  310. }
  311. template <typename ValueType>
  312. void sync_bounded_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk)
  313. {
  314. for (;;)
  315. {
  316. if (out_ != in_) break;
  317. throw_if_closed(lk);
  318. ++waiting_empty_;
  319. not_empty_.wait(lk);
  320. }
  321. }
  322. template <typename ValueType>
  323. void sync_bounded_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk, bool & closed)
  324. {
  325. for (;;)
  326. {
  327. if (out_ != in_) break;
  328. if (closed_) {closed=true; return;}
  329. ++waiting_empty_;
  330. not_empty_.wait(lk);
  331. }
  332. }
  333. template <typename ValueType>
  334. void sync_bounded_queue<ValueType>::pull(ValueType& elem)
  335. {
  336. try
  337. {
  338. unique_lock<mutex> lk(mtx_);
  339. wait_until_not_empty(lk);
  340. pull(elem, lk);
  341. }
  342. catch (...)
  343. {
  344. close();
  345. throw;
  346. }
  347. }
  348. template <typename ValueType>
  349. void sync_bounded_queue<ValueType>::pull(ValueType& elem, bool & closed)
  350. {
  351. try
  352. {
  353. unique_lock<mutex> lk(mtx_);
  354. wait_until_not_empty(lk, closed);
  355. if (closed) {return;}
  356. pull(elem, lk);
  357. }
  358. catch (...)
  359. {
  360. close();
  361. throw;
  362. }
  363. }
  364. // enable if ValueType is nothrow movable
  365. template <typename ValueType>
  366. ValueType sync_bounded_queue<ValueType>::pull()
  367. {
  368. try
  369. {
  370. value_type elem;
  371. pull(elem);
  372. return boost::move(elem);
  373. }
  374. catch (...)
  375. {
  376. close();
  377. throw;
  378. }
  379. }
  380. template <typename ValueType>
  381. boost::shared_ptr<ValueType> sync_bounded_queue<ValueType>::ptr_pull()
  382. {
  383. try
  384. {
  385. unique_lock<mutex> lk(mtx_);
  386. wait_until_not_empty(lk);
  387. return ptr_pull(lk);
  388. }
  389. catch (...)
  390. {
  391. close();
  392. throw;
  393. }
  394. }
  395. template <typename ValueType>
  396. bool sync_bounded_queue<ValueType>::try_push(const ValueType& elem, unique_lock<mutex>& lk)
  397. {
  398. throw_if_closed(lk);
  399. size_type in_p_1 = inc(in_);
  400. if (in_p_1 == out_) // full()
  401. {
  402. return false;
  403. }
  404. push_at(elem, in_p_1, lk);
  405. return true;
  406. }
  407. template <typename ValueType>
  408. bool sync_bounded_queue<ValueType>::try_push(const ValueType& elem)
  409. {
  410. try
  411. {
  412. unique_lock<mutex> lk(mtx_);
  413. return try_push(elem, lk);
  414. }
  415. catch (...)
  416. {
  417. close();
  418. throw;
  419. }
  420. }
  421. template <typename ValueType>
  422. bool sync_bounded_queue<ValueType>::try_push(no_block_tag, const ValueType& elem)
  423. {
  424. try
  425. {
  426. unique_lock<mutex> lk(mtx_, try_to_lock);
  427. if (!lk.owns_lock()) return false;
  428. return try_push(elem, lk);
  429. }
  430. catch (...)
  431. {
  432. close();
  433. throw;
  434. }
  435. }
  436. template <typename ValueType>
  437. typename sync_bounded_queue<ValueType>::size_type sync_bounded_queue<ValueType>::wait_until_not_full(unique_lock<mutex>& lk)
  438. {
  439. for (;;)
  440. {
  441. throw_if_closed(lk);
  442. size_type in_p_1 = inc(in_);
  443. if (in_p_1 != out_) // ! full()
  444. {
  445. return in_p_1;
  446. }
  447. ++waiting_full_;
  448. not_full_.wait(lk);
  449. }
  450. }
  451. template <typename ValueType>
  452. void sync_bounded_queue<ValueType>::push(const ValueType& elem)
  453. {
  454. try
  455. {
  456. unique_lock<mutex> lk(mtx_);
  457. push_at(elem, wait_until_not_full(lk), lk);
  458. }
  459. catch (...)
  460. {
  461. close();
  462. throw;
  463. }
  464. }
  465. template <typename ValueType>
  466. bool sync_bounded_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  467. {
  468. throw_if_closed(lk);
  469. size_type in_p_1 = inc(in_);
  470. if (in_p_1 == out_) // full()
  471. {
  472. return false;
  473. }
  474. push_at(boost::move(elem), in_p_1, lk);
  475. return true;
  476. }
  477. template <typename ValueType>
  478. bool sync_bounded_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem)
  479. {
  480. try
  481. {
  482. unique_lock<mutex> lk(mtx_);
  483. return try_push(elem, lk);
  484. }
  485. catch (...)
  486. {
  487. close();
  488. throw;
  489. }
  490. }
  491. template <typename ValueType>
  492. bool sync_bounded_queue<ValueType>::try_push(no_block_tag, BOOST_THREAD_RV_REF(ValueType) elem)
  493. {
  494. try
  495. {
  496. unique_lock<mutex> lk(mtx_, try_to_lock);
  497. if (!lk.owns_lock())
  498. {
  499. return false;
  500. }
  501. return try_push(elem, lk);
  502. }
  503. catch (...)
  504. {
  505. close();
  506. throw;
  507. }
  508. }
  509. template <typename ValueType>
  510. void sync_bounded_queue<ValueType>::push(BOOST_THREAD_RV_REF(ValueType) elem)
  511. {
  512. try
  513. {
  514. unique_lock<mutex> lk(mtx_);
  515. push_at(elem, wait_until_not_full(lk), lk);
  516. }
  517. catch (...)
  518. {
  519. close();
  520. throw;
  521. }
  522. }
  523. template <typename ValueType>
  524. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
  525. {
  526. sbq.push(boost::forward<ValueType>(elem));
  527. return sbq;
  528. }
  529. template <typename ValueType>
  530. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType const&elem)
  531. {
  532. sbq.push(elem);
  533. return sbq;
  534. }
  535. template <typename ValueType>
  536. sync_bounded_queue<ValueType>& operator>>(sync_bounded_queue<ValueType>& sbq, ValueType &elem)
  537. {
  538. sbq.pull(elem);
  539. return sbq;
  540. }
  541. }
  542. #include <boost/config/abi_suffix.hpp>
  543. #endif