sync_queue.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #ifndef BOOST_THREAD_SYNC_QUEUE_HPP
  2. #define BOOST_THREAD_SYNC_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/thread/sync_bounded_queue.hpp>
  20. #include <boost/container/deque.hpp>
  21. #include <boost/config/abi_prefix.hpp>
  22. namespace boost
  23. {
  24. template <typename ValueType>
  25. class sync_queue
  26. {
  27. public:
  28. typedef ValueType value_type;
  29. typedef std::size_t size_type;
  30. // Constructors/Assignment/Destructors
  31. BOOST_THREAD_NO_COPYABLE(sync_queue)
  32. inline sync_queue();
  33. //template <typename Range>
  34. //inline explicit sync_queue(Range range);
  35. inline ~sync_queue();
  36. // Observers
  37. inline bool empty() const;
  38. inline bool full() const;
  39. inline size_type size() const;
  40. inline bool closed() const;
  41. // Modifiers
  42. inline void close();
  43. inline void push(const value_type& x);
  44. inline void push(BOOST_THREAD_RV_REF(value_type) x);
  45. inline bool try_push(const value_type& x);
  46. inline bool try_push(BOOST_THREAD_RV_REF(value_type) x);
  47. inline bool try_push(no_block_tag, const value_type& x);
  48. inline bool try_push(no_block_tag, BOOST_THREAD_RV_REF(value_type) x);
  49. // Observers/Modifiers
  50. inline void pull(value_type&);
  51. inline void pull(ValueType& elem, bool & closed);
  52. // enable_if is_nothrow_copy_movable<value_type>
  53. inline value_type pull();
  54. inline shared_ptr<ValueType> ptr_pull();
  55. inline bool try_pull(value_type&);
  56. inline bool try_pull(no_block_tag,value_type&);
  57. inline shared_ptr<ValueType> try_pull();
  58. private:
  59. mutable mutex mtx_;
  60. condition_variable not_empty_;
  61. size_type waiting_empty_;
  62. boost::container::deque<ValueType> data_;
  63. bool closed_;
  64. inline bool empty(unique_lock<mutex>& ) const BOOST_NOEXCEPT
  65. {
  66. return data_.empty();
  67. }
  68. inline bool empty(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  69. {
  70. return data_.empty();
  71. }
  72. inline size_type size(lock_guard<mutex>& ) const BOOST_NOEXCEPT
  73. {
  74. return data_.size();
  75. }
  76. inline void throw_if_closed(unique_lock<mutex>&);
  77. inline bool try_pull(value_type& x, unique_lock<mutex>& lk);
  78. inline bool try_push(const value_type& x, unique_lock<mutex>& lk);
  79. inline bool try_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
  80. inline shared_ptr<value_type> try_pull(unique_lock<mutex>& lk);
  81. inline void wait_until_not_empty(unique_lock<mutex>& lk);
  82. inline void wait_until_not_empty(unique_lock<mutex>& lk, bool&);
  83. inline void notify_not_empty_if_needed(unique_lock<mutex>& lk)
  84. {
  85. if (waiting_empty_ > 0)
  86. {
  87. --waiting_empty_;
  88. lk.unlock();
  89. not_empty_.notify_one();
  90. }
  91. }
  92. inline void pull(value_type& elem, unique_lock<mutex>& )
  93. {
  94. elem = boost::move(data_.front());
  95. data_.pop_front();
  96. }
  97. inline boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& )
  98. {
  99. shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_.front()));
  100. data_.pop_front();
  101. return res;
  102. }
  103. inline void push(const value_type& elem, unique_lock<mutex>& lk)
  104. {
  105. data_.push_back(elem);
  106. notify_not_empty_if_needed(lk);
  107. }
  108. inline void push(BOOST_THREAD_RV_REF(value_type) elem, unique_lock<mutex>& lk)
  109. {
  110. data_.push(boost::move(elem));
  111. notify_not_empty_if_needed(lk);
  112. }
  113. };
  114. template <typename ValueType>
  115. sync_queue<ValueType>::sync_queue() :
  116. waiting_empty_(0), data_(), closed_(false)
  117. {
  118. BOOST_ASSERT(data_.empty());
  119. }
  120. // template <typename ValueType>
  121. // template <typename Range>
  122. // explicit sync_queue<ValueType>::sync_queue(Range range) :
  123. // waiting_empty_(0), data_(), closed_(false)
  124. // {
  125. // try
  126. // {
  127. // typedef typename Range::iterator iterator_t;
  128. // iterator_t first = boost::begin(range);
  129. // iterator_t end = boost::end(range);
  130. // for (iterator_t cur = first; cur != end; ++cur)
  131. // {
  132. // data_.push(boost::move(*cur));;
  133. // }
  134. // notify_not_empty_if_needed(lk);
  135. // }
  136. // catch (...)
  137. // {
  138. // delete[] data_;
  139. // }
  140. // }
  141. template <typename ValueType>
  142. sync_queue<ValueType>::~sync_queue()
  143. {
  144. }
  145. template <typename ValueType>
  146. void sync_queue<ValueType>::close()
  147. {
  148. {
  149. lock_guard<mutex> lk(mtx_);
  150. closed_ = true;
  151. }
  152. not_empty_.notify_all();
  153. }
  154. template <typename ValueType>
  155. bool sync_queue<ValueType>::closed() const
  156. {
  157. lock_guard<mutex> lk(mtx_);
  158. return closed_;
  159. }
  160. template <typename ValueType>
  161. bool sync_queue<ValueType>::empty() const
  162. {
  163. lock_guard<mutex> lk(mtx_);
  164. return empty(lk);
  165. }
  166. template <typename ValueType>
  167. bool sync_queue<ValueType>::full() const
  168. {
  169. return false;
  170. }
  171. template <typename ValueType>
  172. typename sync_queue<ValueType>::size_type sync_queue<ValueType>::size() const
  173. {
  174. lock_guard<mutex> lk(mtx_);
  175. return size(lk);
  176. }
  177. template <typename ValueType>
  178. bool sync_queue<ValueType>::try_pull(ValueType& elem, unique_lock<mutex>& lk)
  179. {
  180. if (empty(lk))
  181. {
  182. throw_if_closed(lk);
  183. return false;
  184. }
  185. pull(elem, lk);
  186. return true;
  187. }
  188. template <typename ValueType>
  189. shared_ptr<ValueType> sync_queue<ValueType>::try_pull(unique_lock<mutex>& lk)
  190. {
  191. if (empty(lk))
  192. {
  193. throw_if_closed(lk);
  194. return shared_ptr<ValueType>();
  195. }
  196. return ptr_pull(lk);
  197. }
  198. template <typename ValueType>
  199. bool sync_queue<ValueType>::try_pull(ValueType& elem)
  200. {
  201. try
  202. {
  203. unique_lock<mutex> lk(mtx_);
  204. return try_pull(elem, lk);
  205. }
  206. catch (...)
  207. {
  208. close();
  209. throw;
  210. }
  211. }
  212. template <typename ValueType>
  213. bool sync_queue<ValueType>::try_pull(no_block_tag,ValueType& elem)
  214. {
  215. try
  216. {
  217. unique_lock<mutex> lk(mtx_, try_to_lock);
  218. if (!lk.owns_lock())
  219. {
  220. return false;
  221. }
  222. return try_pull(elem, lk);
  223. }
  224. catch (...)
  225. {
  226. close();
  227. throw;
  228. }
  229. }
  230. template <typename ValueType>
  231. boost::shared_ptr<ValueType> sync_queue<ValueType>::try_pull()
  232. {
  233. try
  234. {
  235. unique_lock<mutex> lk(mtx_);
  236. return try_pull(lk);
  237. }
  238. catch (...)
  239. {
  240. close();
  241. throw;
  242. }
  243. }
  244. template <typename ValueType>
  245. void sync_queue<ValueType>::throw_if_closed(unique_lock<mutex>&)
  246. {
  247. if (closed_)
  248. {
  249. BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
  250. }
  251. }
  252. template <typename ValueType>
  253. void sync_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk)
  254. {
  255. for (;;)
  256. {
  257. if (! empty(lk)) break;
  258. throw_if_closed(lk);
  259. ++waiting_empty_;
  260. not_empty_.wait(lk);
  261. }
  262. }
  263. template <typename ValueType>
  264. void sync_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk, bool & closed)
  265. {
  266. for (;;)
  267. {
  268. if (! empty(lk)) break;
  269. if (closed_) {closed=true; return;}
  270. ++waiting_empty_;
  271. not_empty_.wait(lk);
  272. }
  273. closed=false;
  274. }
  275. template <typename ValueType>
  276. void sync_queue<ValueType>::pull(ValueType& elem)
  277. {
  278. try
  279. {
  280. unique_lock<mutex> lk(mtx_);
  281. wait_until_not_empty(lk);
  282. pull(elem, lk);
  283. }
  284. catch (...)
  285. {
  286. close();
  287. throw;
  288. }
  289. }
  290. template <typename ValueType>
  291. void sync_queue<ValueType>::pull(ValueType& elem, bool & closed)
  292. {
  293. try
  294. {
  295. unique_lock<mutex> lk(mtx_);
  296. wait_until_not_empty(lk, closed);
  297. if (closed) {return;}
  298. pull(elem, lk);
  299. }
  300. catch (...)
  301. {
  302. close();
  303. throw;
  304. }
  305. }
  306. // enable if ValueType is nothrow movable
  307. template <typename ValueType>
  308. ValueType sync_queue<ValueType>::pull()
  309. {
  310. try
  311. {
  312. value_type elem;
  313. pull(elem);
  314. return boost::move(elem);
  315. }
  316. catch (...)
  317. {
  318. close();
  319. throw;
  320. }
  321. }
  322. template <typename ValueType>
  323. boost::shared_ptr<ValueType> sync_queue<ValueType>::ptr_pull()
  324. {
  325. try
  326. {
  327. unique_lock<mutex> lk(mtx_);
  328. wait_until_not_empty(lk);
  329. return ptr_pull(lk);
  330. }
  331. catch (...)
  332. {
  333. close();
  334. throw;
  335. }
  336. }
  337. template <typename ValueType>
  338. bool sync_queue<ValueType>::try_push(const ValueType& elem, unique_lock<mutex>& lk)
  339. {
  340. throw_if_closed(lk);
  341. push(elem, lk);
  342. return true;
  343. }
  344. template <typename ValueType>
  345. bool sync_queue<ValueType>::try_push(const ValueType& elem)
  346. {
  347. try
  348. {
  349. unique_lock<mutex> lk(mtx_);
  350. return try_push(elem, lk);
  351. }
  352. catch (...)
  353. {
  354. close();
  355. throw;
  356. }
  357. }
  358. template <typename ValueType>
  359. bool sync_queue<ValueType>::try_push(no_block_tag, const ValueType& elem)
  360. {
  361. try
  362. {
  363. unique_lock<mutex> lk(mtx_, try_to_lock);
  364. if (!lk.owns_lock()) return false;
  365. return try_push(elem, lk);
  366. }
  367. catch (...)
  368. {
  369. close();
  370. throw;
  371. }
  372. }
  373. template <typename ValueType>
  374. void sync_queue<ValueType>::push(const ValueType& elem)
  375. {
  376. try
  377. {
  378. unique_lock<mutex> lk(mtx_);
  379. throw_if_closed(lk);
  380. push(elem, lk);
  381. }
  382. catch (...)
  383. {
  384. close();
  385. throw;
  386. }
  387. }
  388. template <typename ValueType>
  389. bool sync_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  390. {
  391. throw_if_closed(lk);
  392. push(boost::forward<ValueType>(elem), lk);
  393. return true;
  394. }
  395. template <typename ValueType>
  396. bool sync_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem)
  397. {
  398. try
  399. {
  400. unique_lock<mutex> lk(mtx_);
  401. return try_push(elem, lk);
  402. }
  403. catch (...)
  404. {
  405. close();
  406. throw;
  407. }
  408. }
  409. template <typename ValueType>
  410. bool sync_queue<ValueType>::try_push(no_block_tag, BOOST_THREAD_RV_REF(ValueType) elem)
  411. {
  412. try
  413. {
  414. unique_lock<mutex> lk(mtx_, try_to_lock);
  415. if (!lk.owns_lock())
  416. {
  417. return false;
  418. }
  419. return try_push(elem, lk);
  420. }
  421. catch (...)
  422. {
  423. close();
  424. throw;
  425. }
  426. }
  427. template <typename ValueType>
  428. void sync_queue<ValueType>::push(BOOST_THREAD_RV_REF(ValueType) elem)
  429. {
  430. try
  431. {
  432. unique_lock<mutex> lk(mtx_);
  433. throw_if_closed(lk);
  434. push(elem, lk);
  435. }
  436. catch (...)
  437. {
  438. close();
  439. throw;
  440. }
  441. }
  442. template <typename ValueType>
  443. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
  444. {
  445. sbq.push(boost::forward<ValueType>(elem));
  446. return sbq;
  447. }
  448. template <typename ValueType>
  449. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType const&elem)
  450. {
  451. sbq.push(elem);
  452. return sbq;
  453. }
  454. template <typename ValueType>
  455. sync_queue<ValueType>& operator>>(sync_queue<ValueType>& sbq, ValueType &elem)
  456. {
  457. sbq.pull(elem);
  458. return sbq;
  459. }
  460. }
  461. #include <boost/config/abi_suffix.hpp>
  462. #endif