From caec31bccbe51c6dd64f3e8fed0c157ea4b47258 Mon Sep 17 00:00:00 2001 From: khizmax Date: Thu, 21 Jan 2016 09:35:43 +0300 Subject: [PATCH] Added enqueue() with move semantics --- cds/container/basket_queue.h | 21 ++++++++++++++++++++- cds/container/moir_queue.h | 21 ++++++++++++++++++++- cds/container/msqueue.h | 21 ++++++++++++++++++++- cds/container/optimistic_queue.h | 19 ++++++++++++++++++- cds/container/rwqueue.h | 22 ++++++++++++++++++++-- 5 files changed, 98 insertions(+), 6 deletions(-) diff --git a/cds/container/basket_queue.h b/cds/container/basket_queue.h index 1e443c12..574b6e16 100644 --- a/cds/container/basket_queue.h +++ b/cds/container/basket_queue.h @@ -260,6 +260,8 @@ namespace cds { namespace container { typedef typename base_class::stat stat; ///< Internal statistics policy used typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option + static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm + protected: typedef typename maker::node_type node_type; ///< queue node type (derived from intrusive::basket_queue::node) @@ -323,6 +325,17 @@ namespace cds { namespace container { return false; } + /// Enqueues \p val value into the queue, move semantics + bool enqueue( value_type&& val ) + { + scoped_node_ptr p( alloc_node_move( std::move( val ))); + if ( base_class::enqueue( *p ) ) { + p.release(); + return true; + } + return false; + } + /// Enqueues \p data to queue using a functor /** \p Func is a functor called to create node. @@ -346,11 +359,17 @@ namespace cds { namespace container { } /// Synonym for \p enqueue() function - bool push( const value_type& val ) + bool push( value_type const& val ) { return enqueue( val ); } + /// Synonym for \p enqueue() function, move semantics + bool push( value_type&& val ) + { + return enqueue( std::move( val )); + } + /// Synonym for \p enqueue_with() function template bool push_with( Func f ) diff --git a/cds/container/moir_queue.h b/cds/container/moir_queue.h index 40acf9c6..42e86df9 100644 --- a/cds/container/moir_queue.h +++ b/cds/container/moir_queue.h @@ -93,6 +93,8 @@ namespace cds { namespace container { typedef MoirQueue< GC2, T2, Traits2 > other ; ///< Rebinding result }; + static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm + public: typedef T value_type ; ///< Value type stored in the queue typedef typename base_class::gc gc; ///< Garbage collector @@ -152,7 +154,7 @@ namespace cds { namespace container { /// Enqueues \p val value into the queue. /** The function makes queue node in dynamic memory calling copy constructor for \p val - and then it calls intrusive::MoirQueue::enqueue. + and then it calls \p intrusive::MoirQueue::enqueue. Returns \p true if success, \p false otherwise. */ bool enqueue( value_type const& val ) @@ -165,6 +167,17 @@ namespace cds { namespace container { return false; } + /// Enqueues \p val value into the queue, move semantics + bool enqueue( value_type&& val ) + { + scoped_node_ptr p( alloc_node_move( std::move( val ))); + if ( base_class::enqueue( *p ) ) { + p.release(); + return true; + } + return false; + } + /// Enqueues \p data to queue using a functor /** \p Func is a functor calling to create a new node. @@ -206,6 +219,12 @@ namespace cds { namespace container { return enqueue( val ); } + /// Synonym for \p enqueue() function, move semantics + bool push( value_type&& val ) + { + return enqueue( std::move( val )); + } + /// Synonym for \p enqueue_with() function template bool push_with( Func f ) diff --git a/cds/container/msqueue.h b/cds/container/msqueue.h index a26dff20..db42d3d4 100644 --- a/cds/container/msqueue.h +++ b/cds/container/msqueue.h @@ -218,6 +218,8 @@ namespace cds { namespace container { typedef typename base_class::stat stat; ///< Internal statistics policy used typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option + static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm + protected: //@cond typedef typename maker::node_type node_type; ///< queue node type (derived from \p intrusive::msqueue::node) @@ -268,7 +270,7 @@ namespace cds { namespace container { /// Enqueues \p val value into the queue. /** The function makes queue node in dynamic memory calling copy constructor for \p val - and then it calls intrusive::MSQueue::enqueue. + and then it calls \p intrusive::MSQueue::enqueue. Returns \p true if success, \p false otherwise. */ bool enqueue( value_type const& val ) @@ -281,6 +283,17 @@ namespace cds { namespace container { return false; } + /// Enqueues \p val in the queue, move semantics + bool enqueue( value_type&& val ) + { + scoped_node_ptr p( alloc_node_move( std::move( val ))); + if ( base_class::enqueue( *p ) ) { + p.release(); + return true; + } + return false; + } + /// Enqueues data to the queue using a functor /** \p Func is a functor called to create node. @@ -321,6 +334,12 @@ namespace cds { namespace container { return enqueue( val ); } + /// Synonym for \p enqueue() function + bool push( value_type&& val ) + { + return enqueue( std::move( val )); + } + /// Synonym for \p enqueue_with() function template bool push_with( Func f ) diff --git a/cds/container/optimistic_queue.h b/cds/container/optimistic_queue.h index 0bb01cb4..d567dfcf 100644 --- a/cds/container/optimistic_queue.h +++ b/cds/container/optimistic_queue.h @@ -283,6 +283,17 @@ namespace cds { namespace container { return false; } + /// Enqueues \p val value into the queue, move semntics + bool enqueue( value_type&& val ) + { + scoped_node_ptr p( alloc_node_move( std::move( val ))); + if ( base_class::enqueue( *p ) ) { + p.release(); + return true; + } + return false; + } + /// Enqueues \p data to queue using a functor /** \p Func is a functor called to create node. @@ -317,12 +328,18 @@ namespace cds { namespace container { return false; } - /// Synonym for \p enqueue() function + /// Synonym for \p enqueue( const value_type& ) function bool push( const value_type& val ) { return enqueue( val ); } + /// Synonym for \p enqueue( value_type&& ) function + bool push( value_type&& val ) + { + return enqueue( std::move( val )); + } + /// Synonym for \p enqueue_with() function template bool push_with( Func f ) diff --git a/cds/container/rwqueue.h b/cds/container/rwqueue.h index de0c8e0c..85206857 100644 --- a/cds/container/rwqueue.h +++ b/cds/container/rwqueue.h @@ -132,7 +132,7 @@ namespace cds { namespace container { typedef T value_type; ///< Type of value to be stored in the queue typedef Traits traits; ///< Queue traits - typedef typename traits::lock_type lock_type; ///< Locking primitive + typedef typename traits::lock_type lock_type; ///< Locking primitive typedef typename traits::item_counter item_counter; ///< Item counting policy used protected: @@ -252,6 +252,17 @@ namespace cds { namespace container { return false; } + /// Enqueues \p data, move semantics + bool enqueue( value_type&& data ) + { + scoped_node_ptr p( alloc_node_move( std::move( data ))); + if ( enqueue_node( p.get() ) ) { + p.release(); + return true; + } + return false; + } + /// Enqueues \p data to the queue using a functor /** \p Func is a functor called to create node. @@ -286,12 +297,18 @@ namespace cds { namespace container { return false; } - /// Synonym for \p enqueue() function + /// Synonym for \p enqueue( value_type const& ) function bool push( value_type const& val ) { return enqueue( val ); } + /// Synonym for \p enqueue( value_type&& ) function + bool push( value_type&& val ) + { + return enqueue( std::move( val )); + } + /// Synonym for \p enqueue_with() function template bool push_with( Func f ) @@ -368,6 +385,7 @@ namespace cds { namespace container { m_Head.ptr = m_Head.ptr->m_pNext.load( atomics::memory_order_relaxed ); free_node( pHead ); } + m_ItemCounter.reset(); } /// Returns queue's item count -- 2.34.1