1
#ifndef BOOST_THREAD_SYNC_QUEUE_HPP
2
#define BOOST_THREAD_SYNC_QUEUE_HPP
4
//////////////////////////////////////////////////////////////////////////////
6
// (C) Copyright Vicente J. Botet Escriba 2013. Distributed under the Boost
7
// Software License, Version 1.0. (See accompanying file
8
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10
// See http://www.boost.org/libs/thread for documentation.
12
//////////////////////////////////////////////////////////////////////////////
14
#include <boost/thread/detail/config.hpp>
15
#include <boost/thread/condition_variable.hpp>
16
#include <boost/thread/mutex.hpp>
17
#include <boost/thread/detail/move.hpp>
18
#include <boost/throw_exception.hpp>
19
#include <boost/smart_ptr/shared_ptr.hpp>
20
#include <boost/smart_ptr/make_shared.hpp>
22
#include <boost/thread/sync_bounded_queue.hpp>
23
#include <boost/container/deque.hpp>
25
#include <boost/config/abi_prefix.hpp>
31
template <typename ValueType>
35
typedef ValueType value_type;
36
typedef std::size_t size_type;
38
// Constructors/Assignment/Destructors
39
BOOST_THREAD_NO_COPYABLE(sync_queue)
41
template <typename Range>
42
explicit sync_queue(Range range);
48
size_type size() const;
54
void push(const value_type& x);
55
void push(BOOST_THREAD_RV_REF(value_type) x);
56
bool try_push(const value_type& x);
57
bool try_push(BOOST_THREAD_RV_REF(value_type) x);
58
bool try_push(no_block_tag, const value_type& x);
59
bool try_push(no_block_tag, BOOST_THREAD_RV_REF(value_type) x);
61
// Observers/Modifiers
62
void pull(value_type&);
63
void pull(ValueType& elem, bool & closed);
64
// enable_if is_nothrow_copy_movable<value_type>
66
shared_ptr<ValueType> ptr_pull();
67
bool try_pull(value_type&);
68
bool try_pull(no_block_tag,value_type&);
69
shared_ptr<ValueType> try_pull();
74
condition_variable not_empty_;
75
size_type waiting_empty_;
76
boost::container::deque<ValueType> data_;
79
bool empty(unique_lock<mutex>& ) const BOOST_NOEXCEPT
83
bool empty(lock_guard<mutex>& ) const BOOST_NOEXCEPT
88
size_type size(lock_guard<mutex>& ) const BOOST_NOEXCEPT
93
void throw_if_closed(unique_lock<mutex>&);
95
bool try_pull(value_type& x, unique_lock<mutex>& lk);
96
bool try_push(const value_type& x, unique_lock<mutex>& lk);
97
bool try_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
98
shared_ptr<value_type> try_pull(unique_lock<mutex>& lk);
100
void wait_until_not_empty(unique_lock<mutex>& lk);
101
void wait_until_not_empty(unique_lock<mutex>& lk, bool&);
104
void notify_not_empty_if_needed(unique_lock<mutex>& lk)
106
if (waiting_empty_ > 0)
110
not_empty_.notify_one();
114
void pull(value_type& elem, unique_lock<mutex>& )
116
elem = boost::move(data_.front());
119
boost::shared_ptr<value_type> ptr_pull(unique_lock<mutex>& )
121
shared_ptr<value_type> res = make_shared<value_type>(boost::move(data_.front()));
126
void push(const value_type& elem, unique_lock<mutex>& lk)
128
data_.push_back(elem);
129
notify_not_empty_if_needed(lk);
132
void push(BOOST_THREAD_RV_REF(value_type) elem, unique_lock<mutex>& lk)
134
data_.push(boost::move(elem));
135
notify_not_empty_if_needed(lk);
141
template <typename ValueType>
142
sync_queue<ValueType>::sync_queue() :
143
waiting_empty_(0), data_(), closed_(false)
145
BOOST_ASSERT(data_.empty());
148
// template <typename ValueType>
149
// template <typename Range>
150
// explicit sync_queue<ValueType>::sync_queue(Range range) :
151
// waiting_empty_(0), data_(), closed_(false)
155
// typedef typename Range::iterator iterator_t;
156
// iterator_t first = boost::begin(range);
157
// iterator_t end = boost::end(range);
158
// for (iterator_t cur = first; cur != end; ++cur)
160
// data_.push(boost::move(*cur));;
162
// notify_not_empty_if_needed(lk);
170
template <typename ValueType>
171
sync_queue<ValueType>::~sync_queue()
175
template <typename ValueType>
176
void sync_queue<ValueType>::close()
179
lock_guard<mutex> lk(mtx_);
182
not_empty_.notify_all();
185
template <typename ValueType>
186
bool sync_queue<ValueType>::closed() const
188
lock_guard<mutex> lk(mtx_);
192
template <typename ValueType>
193
bool sync_queue<ValueType>::empty() const
195
lock_guard<mutex> lk(mtx_);
198
template <typename ValueType>
199
bool sync_queue<ValueType>::full() const
204
template <typename ValueType>
205
typename sync_queue<ValueType>::size_type sync_queue<ValueType>::size() const
207
lock_guard<mutex> lk(mtx_);
212
template <typename ValueType>
213
bool sync_queue<ValueType>::try_pull(ValueType& elem, unique_lock<mutex>& lk)
223
template <typename ValueType>
224
shared_ptr<ValueType> sync_queue<ValueType>::try_pull(unique_lock<mutex>& lk)
229
return shared_ptr<ValueType>();
234
template <typename ValueType>
235
bool sync_queue<ValueType>::try_pull(ValueType& elem)
239
unique_lock<mutex> lk(mtx_);
240
return try_pull(elem, lk);
249
template <typename ValueType>
250
bool sync_queue<ValueType>::try_pull(no_block_tag,ValueType& elem)
254
unique_lock<mutex> lk(mtx_, try_to_lock);
259
return try_pull(elem, lk);
267
template <typename ValueType>
268
boost::shared_ptr<ValueType> sync_queue<ValueType>::try_pull()
272
unique_lock<mutex> lk(mtx_);
282
template <typename ValueType>
283
void sync_queue<ValueType>::throw_if_closed(unique_lock<mutex>&)
287
BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
291
template <typename ValueType>
292
void sync_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk)
296
if (! empty(lk)) break;
302
template <typename ValueType>
303
void sync_queue<ValueType>::wait_until_not_empty(unique_lock<mutex>& lk, bool & closed)
307
if (! empty(lk)) break;
308
if (closed_) {closed=true; return;}
314
template <typename ValueType>
315
void sync_queue<ValueType>::pull(ValueType& elem)
319
unique_lock<mutex> lk(mtx_);
320
wait_until_not_empty(lk);
329
template <typename ValueType>
330
void sync_queue<ValueType>::pull(ValueType& elem, bool & closed)
334
unique_lock<mutex> lk(mtx_);
335
wait_until_not_empty(lk, closed);
336
if (closed) {return;}
346
// enable if ValueType is nothrow movable
347
template <typename ValueType>
348
ValueType sync_queue<ValueType>::pull()
354
return boost::move(elem);
362
template <typename ValueType>
363
boost::shared_ptr<ValueType> sync_queue<ValueType>::ptr_pull()
367
unique_lock<mutex> lk(mtx_);
368
wait_until_not_empty(lk);
378
template <typename ValueType>
379
bool sync_queue<ValueType>::try_push(const ValueType& elem, unique_lock<mutex>& lk)
386
template <typename ValueType>
387
bool sync_queue<ValueType>::try_push(const ValueType& elem)
391
unique_lock<mutex> lk(mtx_);
392
return try_push(elem, lk);
401
template <typename ValueType>
402
bool sync_queue<ValueType>::try_push(no_block_tag, const ValueType& elem)
406
unique_lock<mutex> lk(mtx_, try_to_lock);
407
if (!lk.owns_lock()) return false;
408
return try_push(elem, lk);
417
template <typename ValueType>
418
void sync_queue<ValueType>::push(const ValueType& elem)
422
unique_lock<mutex> lk(mtx_);
433
template <typename ValueType>
434
bool sync_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
437
push(boost::forward<ValueType>(elem), lk);
441
template <typename ValueType>
442
bool sync_queue<ValueType>::try_push(BOOST_THREAD_RV_REF(ValueType) elem)
446
unique_lock<mutex> lk(mtx_);
447
return try_push(elem, lk);
456
template <typename ValueType>
457
bool sync_queue<ValueType>::try_push(no_block_tag, BOOST_THREAD_RV_REF(ValueType) elem)
461
unique_lock<mutex> lk(mtx_, try_to_lock);
466
return try_push(elem, lk);
475
template <typename ValueType>
476
void sync_queue<ValueType>::push(BOOST_THREAD_RV_REF(ValueType) elem)
480
unique_lock<mutex> lk(mtx_);
491
template <typename ValueType>
492
sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
494
sbq.push(boost::forward<ValueType>(elem));
498
template <typename ValueType>
499
sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType const&elem)
505
template <typename ValueType>
506
sync_queue<ValueType>& operator>>(sync_queue<ValueType>& sbq, ValueType &elem)
514
#include <boost/config/abi_suffix.hpp>