123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #ifndef BOOST_THREAD_CONCURRENT_QUEUES_SYNC_DEQUE_HPP
- #define BOOST_THREAD_CONCURRENT_QUEUES_SYNC_DEQUE_HPP
- //////////////////////////////////////////////////////////////////////////////
- //
- // (C) Copyright Vicente J. Botet Escriba 2013-2014. Distributed under the Boost
- // Software License, Version 1.0. (See accompanying file
- // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // See http://www.boost.org/libs/thread for documentation.
- //
- //////////////////////////////////////////////////////////////////////////////
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/concurrent_queues/detail/sync_queue_base.hpp>
- #include <boost/thread/concurrent_queues/queue_op_status.hpp>
- #include <boost/thread/condition_variable.hpp>
- #include <boost/thread/csbl/devector.hpp>
- #include <boost/thread/detail/move.hpp>
- #include <boost/thread/mutex.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/smart_ptr/shared_ptr.hpp>
- #include <boost/smart_ptr/make_shared.hpp>
- #include <boost/config/abi_prefix.hpp>
- namespace boost
- {
- namespace concurrent
- {
- template <class ValueType, class Container = csbl::devector<ValueType> >
- class sync_deque
- : public detail::sync_queue_base<ValueType, Container >
- {
- typedef detail::sync_queue_base<ValueType, Container > super;
- public:
- typedef ValueType value_type;
- //typedef typename super::value_type value_type; // fixme
- typedef typename super::underlying_queue_type underlying_queue_type;
- typedef typename super::size_type size_type;
- typedef typename super::op_status op_status;
- // Constructors/Assignment/Destructors
- BOOST_THREAD_NO_COPYABLE(sync_deque)
- inline sync_deque();
- //template <typename Range>
- //inline explicit sync_deque(Range range);
- inline ~sync_deque();
- // Modifiers
- inline void push_back(const value_type& x);
- inline queue_op_status try_push_back(const value_type& x);
- inline queue_op_status nonblocking_push_back(const value_type& x);
- inline queue_op_status wait_push_back(const value_type& x);
- inline void push_back(BOOST_THREAD_RV_REF(value_type) x);
- inline queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x);
- inline queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x);
- inline queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x);
- // Observers/Modifiers
- inline void pull_front(value_type&);
- // enable_if is_nothrow_copy_movable<value_type>
- inline value_type pull_front();
- inline queue_op_status try_pull_front(value_type&);
- inline queue_op_status nonblocking_pull_front(value_type&);
- inline queue_op_status wait_pull_front(ValueType& elem);
- private:
- inline queue_op_status try_pull_front(value_type& x, unique_lock<mutex>& lk);
- inline queue_op_status wait_pull_front(value_type& x, unique_lock<mutex>& lk);
- inline queue_op_status try_push_back(const value_type& x, unique_lock<mutex>& lk);
- inline queue_op_status wait_push_back(const value_type& x, unique_lock<mutex>& lk);
- inline queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
- inline queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
- inline void pull_front(value_type& elem, unique_lock<mutex>& )
- {
- elem = boost::move(super::data_.front());
- super::data_.pop_front();
- }
- inline value_type pull_front(unique_lock<mutex>& )
- {
- value_type e = boost::move(super::data_.front());
- super::data_.pop_front();
- return boost::move(e);
- }
- inline void push_back(const value_type& elem, unique_lock<mutex>& lk)
- {
- super::data_.push_back(elem);
- super::notify_elem_added(lk);
- }
- inline void push_back(BOOST_THREAD_RV_REF(value_type) elem, unique_lock<mutex>& lk)
- {
- super::data_.push_back(boost::move(elem));
- super::notify_elem_added(lk);
- }
- };
- template <class ValueType, class Container>
- sync_deque<ValueType, Container>::sync_deque() :
- super()
- {
- }
- // template <class ValueType, class Container>
- // template <class Range>
- // explicit sync_deque<ValueType, Container>::sync_deque(Range range) :
- // data_(), closed_(false)
- // {
- // try
- // {
- // typedef typename Range::iterator iterator_t;
- // iterator_t first = boost::begin(range);
- // iterator_t end = boost::end(range);
- // for (iterator_t cur = first; cur != end; ++cur)
- // {
- // data_.push(boost::move(*cur));;
- // }
- // notify_elem_added(lk);
- // }
- // catch (...)
- // {
- // delete[] data_;
- // }
- // }
- template <class ValueType, class Container>
- sync_deque<ValueType, Container>::~sync_deque()
- {
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_pull_front(ValueType& elem, unique_lock<mutex>& lk)
- {
- if (super::empty(lk))
- {
- if (super::closed(lk)) return queue_op_status::closed;
- return queue_op_status::empty;
- }
- pull_front(elem, lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_pull_front(ValueType& elem, unique_lock<mutex>& lk)
- {
- const bool has_been_closed = super::wait_until_not_empty_or_closed(lk);
- if (has_been_closed) return queue_op_status::closed;
- pull_front(elem, lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_pull_front(ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return try_pull_front(elem, lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_pull_front(ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return wait_pull_front(elem, lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::nonblocking_pull_front(ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_, try_to_lock);
- if (!lk.owns_lock())
- {
- return queue_op_status::busy;
- }
- return try_pull_front(elem, lk);
- }
- template <class ValueType, class Container>
- void sync_deque<ValueType, Container>::pull_front(ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- const bool has_been_closed = super::wait_until_not_empty_or_closed(lk);
- if (has_been_closed) super::throw_if_closed(lk);
- pull_front(elem, lk);
- }
- // enable if ValueType is nothrow movable
- template <class ValueType, class Container>
- ValueType sync_deque<ValueType, Container>::pull_front()
- {
- unique_lock<mutex> lk(super::mtx_);
- const bool has_been_closed = super::wait_until_not_empty_or_closed(lk);
- if (has_been_closed) super::throw_if_closed(lk);
- return pull_front(lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_push_back(const ValueType& elem, unique_lock<mutex>& lk)
- {
- if (super::closed(lk)) return queue_op_status::closed;
- push_back(elem, lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_push_back(const ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return try_push_back(elem, lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_push_back(const ValueType& elem, unique_lock<mutex>& lk)
- {
- if (super::closed(lk)) return queue_op_status::closed;
- push_back(elem, lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_push_back(const ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return wait_push_back(elem, lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::nonblocking_push_back(const ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_, try_to_lock);
- if (!lk.owns_lock()) return queue_op_status::busy;
- return try_push_back(elem, lk);
- }
- template <class ValueType, class Container>
- void sync_deque<ValueType, Container>::push_back(const ValueType& elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- super::throw_if_closed(lk);
- push_back(elem, lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_push_back(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
- {
- if (super::closed(lk)) return queue_op_status::closed;
- push_back(boost::move(elem), lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::try_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return try_push_back(boost::move(elem), lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_push_back(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
- {
- if (super::closed(lk)) return queue_op_status::closed;
- push_back(boost::move(elem), lk);
- return queue_op_status::success;
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::wait_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- return wait_push_back(boost::move(elem), lk);
- }
- template <class ValueType, class Container>
- queue_op_status sync_deque<ValueType, Container>::nonblocking_push_back(BOOST_THREAD_RV_REF(ValueType) elem)
- {
- unique_lock<mutex> lk(super::mtx_, try_to_lock);
- if (!lk.owns_lock())
- {
- return queue_op_status::busy;
- }
- return try_push_back(boost::move(elem), lk);
- }
- template <class ValueType, class Container>
- void sync_deque<ValueType, Container>::push_back(BOOST_THREAD_RV_REF(ValueType) elem)
- {
- unique_lock<mutex> lk(super::mtx_);
- super::throw_if_closed(lk);
- push_back(boost::move(elem), lk);
- }
- template <class ValueType, class Container>
- sync_deque<ValueType, Container>& operator<<(sync_deque<ValueType, Container>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
- {
- sbq.push_back(boost::move(elem));
- return sbq;
- }
- template <class ValueType, class Container>
- sync_deque<ValueType, Container>& operator<<(sync_deque<ValueType, Container>& sbq, ValueType const&elem)
- {
- sbq.push_back(elem);
- return sbq;
- }
- template <class ValueType, class Container>
- sync_deque<ValueType, Container>& operator>>(sync_deque<ValueType, Container>& sbq, ValueType &elem)
- {
- sbq.pull_front(elem);
- return sbq;
- }
- }
- using concurrent::sync_deque;
- }
- #include <boost/config/abi_suffix.hpp>
- #endif
|