-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
-#ifndef __CDS_CONTAINER_SEGMENTED_QUEUE_H
-#define __CDS_CONTAINER_SEGMENTED_QUEUE_H
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+
+ Source code repo: http://github.com/khizmax/libcds/
+ Download: http://sourceforge.net/projects/libcds/files/
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef CDSLIB_CONTAINER_SEGMENTED_QUEUE_H
+#define CDSLIB_CONTAINER_SEGMENTED_QUEUE_H
#include <memory>
+#include <functional> // ref
#include <cds/intrusive/segmented_queue.h>
#include <cds/details/trivial_assign.h>
-#include <cds/ref.h>
namespace cds { namespace container {
typedef cds::intrusive::segmented_queue::empty_stat empty_stat;
/// SegmentedQueue default type traits
- struct type_traits {
+ struct traits {
/// Item allocator. Default is \ref CDS_DEFAULT_ALLOCATOR
typedef CDS_DEFAULT_ALLOCATOR node_allocator;
/// Alignment of critical data, default is cache line alignment. See cds::opt::alignment option specification
enum { alignment = opt::cache_line_alignment };
+ /// Padding of segment data, default is no special padding
+ /**
+ The segment is just an array of atomic data pointers,
+ so, the high load leads to false sharing and performance degradation.
+ A padding of segment data can eliminate false sharing issue.
+ On the other hand, the padding leads to increase segment size.
+ */
+ enum { padding = cds::intrusive::segmented_queue::traits::padding };
+
/// Segment allocator. Default is \ref CDS_DEFAULT_ALLOCATOR
typedef CDS_DEFAULT_ALLOCATOR allocator;
/// Lock type used to maintain an internal list of allocated segments
- typedef cds::lock::Spin lock_type;
+ typedef cds::sync::spin lock_type;
/// Random \ref cds::opt::permutation_generator "permutation generator" for sequence [0, quasi_factor)
typedef cds::opt::v::random2_permutation<int> permutation_generator;
/// Metafunction converting option list to traits for SegmentedQueue
/**
- The metafunction can be useful if a few fields in \ref type_traits should be changed.
+ The metafunction can be useful if a few fields in \p segmented_queue::traits should be changed.
For example:
\code
typedef cds::container::segmented_queue::make_traits<
>::type my_segmented_queue_traits;
\endcode
This code creates \p %SegmentedQueue type traits with item counting feature,
- all other \p type_traits members left unchanged.
+ all other \p segmented_queue::traits members left unchanged.
\p Options are:
- \p opt::node_allocator - node allocator.
- - \p opt::stat - internal statistics, possible type: \ref stat, \ref empty_stat (the default)
- - \p opt::item_counter - item counting feature. Note that atomicity::empty_item_counetr is not suitable
+ - \p opt::stat - internal statistics, possible type: \p segmented_queue::stat, \p segmented_queue::empty_stat (the default)
+ - \p opt::item_counter - item counting feature. Note that \p atomicity::empty_item_counetr is not suitable
for segmented queue.
- \p opt::memory_model - memory model, default is \p opt::v::relaxed_ordering.
See option description for the full list of possible models
- - \p opt::alignment - the alignmentfor critical data, see option description for explanation
+ - \p opt::alignment - the alignment of critical data, see option description for explanation
+ - \p opt::padding - the padding of segment data, default no special padding.
+ See \p traits::padding for explanation.
- \p opt::allocator - the allocator used to maintain segments.
- \p opt::lock_type - a mutual exclusion lock type used to maintain internal list of allocated
segments. Default is \p cds::opt::Spin, \p std::mutex is also suitable.
- \p opt::permutation_generator - a random permutation generator for sequence [0, quasi_factor),
- default is cds::opt::v::random2_permutation<int>
+ default is \p cds::opt::v::random2_permutation<int>
*/
- template <CDS_DECL_OPTIONS9>
+ template <typename... Options>
struct make_traits {
# ifdef CDS_DOXYGEN_INVOKED
typedef implementation_defined type ; ///< Metafunction result
# else
typedef typename cds::opt::make_options<
- typename cds::opt::find_type_traits< type_traits, CDS_OPTIONS9 >::type
- ,CDS_OPTIONS9
+ typename cds::opt::find_type_traits< traits, Options... >::type
+ ,Options...
>::type type;
# endif
};
}
};
- struct intrusive_type_traits: public original_type_traits {
+ struct intrusive_type_traits: public original_type_traits
+ {
typedef node_disposer disposer;
};
quasi factor. It means that the consumer dequeues any item from the current first segment.
Template parameters:
- - \p GC - a garbage collector, possible types are cds::gc::HP, cds::gc::PTB
+ - \p GC - a garbage collector, possible types are cds::gc::HP, cds::gc::DHP
- \p T - the type of values stored in the queue
- - \p Traits - queue type traits, default is segmented_queue::type_traits.
- segmented_queue::make_traits metafunction can be used to construct your
+ - \p Traits - queue type traits, default is \p segmented_queue::traits.
+ \p segmented_queue::make_traits metafunction can be used to construct your
type traits.
*/
- template <class GC, typename T, typename Traits = segmented_queue::type_traits >
+ template <class GC, typename T, typename Traits = segmented_queue::traits >
class SegmentedQueue:
#ifdef CDS_DOXYGEN_INVOKED
public cds::intrusive::SegmentedQueue< GC, T, Traits >
typedef typename maker::type base_class;
//@endcond
public:
- typedef GC gc ; ///< Garbage collector
- typedef T value_type ; ///< type of the value stored in the queue
- typedef Traits options ; ///< Queue's traits
+ typedef GC gc; ///< Garbage collector
+ typedef T value_type; ///< type of the value stored in the queue
+ typedef Traits traits; ///< Queue traits
- typedef typename options::node_allocator node_allocator; ///< Node allocator
+ typedef typename traits::node_allocator node_allocator; ///< Node allocator
typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
typedef typename base_class::item_counter item_counter; ///< Item counting policy, see cds::opt::item_counter option setter
typedef typename base_class::stat stat ; ///< Internal statistics policy
typedef typename base_class::lock_type lock_type ; ///< Type of mutex for maintaining an internal list of allocated segments.
typedef typename base_class::permutation_generator permutation_generator; ///< Random permutation generator for sequence [0, quasi-factor)
- static const size_t m_nHazardPtrCount = base_class::m_nHazardPtrCount ; ///< Count of hazard pointer required for the algorithm
+ static const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount ; ///< Count of hazard pointer required for the algorithm
protected:
//@cond
return cxx_node_allocator().New();
}
-# ifdef CDS_EMPLACE_SUPPORT
template <typename... Args>
static value_type * alloc_node_move( Args&&... args )
{
return cxx_node_allocator().MoveNew( std::forward<Args>( args )... );
}
-# endif
-
- struct dummy_disposer {
- void operator()( value_type * p )
- {}
- };
//@endcond
public:
*/
bool enqueue( value_type const& val )
{
- scoped_node_ptr p( alloc_node(val) );
- if ( base_class::enqueue( *p ) ) {
+ scoped_node_ptr p( alloc_node(val));
+ if ( base_class::enqueue( *p )) {
p.release();
return true;
}
return false;
}
- /// Synonym for <tt>enqueue(value_type const&)</tt> function
- bool push( value_type const& val )
+ /// Inserts a new element at last segment of the queue, move semantics
+ bool enqueue( value_type&& val )
{
- return enqueue( val );
+ scoped_node_ptr p( alloc_node_move( std::move( val )));
+ if ( base_class::enqueue( *p )) {
+ p.release();
+ return true;
+ }
+ return false;
}
- /// Inserts a new element at last segment of the queue using copy functor
+ /// Enqueues data to the queue using a functor
/**
- \p Func is a functor called to copy value \p data of type \p Q
- which may be differ from type \ref value_type stored in the queue.
- The functor's interface is:
+ \p Func is a functor called to create node.
+ The functor \p f takes one argument - a reference to a new node of type \ref value_type :
\code
- struct myFunctor {
- void operator()(value_type& dest, Q const& data)
- {
- // // Code to copy \p data to \p dest
- dest = data;
- }
- };
+ cds::container::SegmentedQueue< cds::gc::HP, Foo > myQueue;
+ Bar bar;
+ myQueue.enqueue_with( [&bar]( Foo& dest ) { dest = bar; } );
\endcode
- You may use \p boost:ref construction to pass functor \p f by reference.
*/
- template <typename Q, typename Func>
- bool enqueue( Q const& data, Func f )
+ template <typename Func>
+ bool enqueue_with( Func f )
{
- scoped_node_ptr p( alloc_node() );
- unref(f)( *p, data );
+ scoped_node_ptr p( alloc_node());
+ f( *p );
if ( base_class::enqueue( *p )) {
p.release();
return true;
return false;
}
- /// Synonym for <tt>enqueue(Q const&, Func)</tt> function
- template <typename Q, typename Func>
- bool push( Q const& data, Func f )
+
+ /// Synonym for \p enqueue( value_type const& ) member function
+ bool push( value_type const& val )
+ {
+ return enqueue( val );
+ }
+
+ /// Synonym for \p enqueue( value_type&& ) member function
+ bool push( value_type&& val )
{
- return enqueue( data, f );
+ return enqueue( std::move( val ));
+ }
+
+ /// Synonym for \p enqueue_with() member function
+ template <typename Func>
+ bool push_with( Func f )
+ {
+ return enqueue_with( f );
}
-# ifdef CDS_EMPLACE_SUPPORT
/// Enqueues data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
- /**
- This function is available only for compiler that supports
- variadic template and move semantics
- */
template <typename... Args>
bool emplace( Args&&... args )
{
- scoped_node_ptr p( alloc_node_move( std::forward<Args>(args)... ) );
+ scoped_node_ptr p( alloc_node_move( std::forward<Args>(args)... ));
if ( base_class::enqueue( *p )) {
p.release();
return true;
}
return false;
}
-# endif
- /// Removes an element from first segment of the queue
+ /// Dequeues a value from the queue
/**
- \p Func is a functor called to copy dequeued value to \p dest of type \p Q
- which may be differ from type \ref value_type stored in the queue.
- The functor's interface is:
+ If queue is not empty, the function returns \p true, \p dest contains copy of
+ dequeued value. The assignment operator for type \ref value_type is invoked.
+ If queue is empty, the function returns \p false, \p dest is unchanged.
+ */
+ bool dequeue( value_type& dest )
+ {
+ return dequeue_with( [&dest]( value_type& src ) { dest = std::move( src );});
+ }
+
+ /// Dequeues a value using a functor
+ /**
+ \p Func is a functor called to copy dequeued value.
+ The functor takes one argument - a reference to removed node:
\code
- struct myFunctor {
- void operator()(Q& dest, value_type const& data)
- {
- // Code to copy \p data to \p dest
- dest = data;
- }
- };
+ cds:container::MSQueue< cds::gc::HP, Foo > myQueue;
+ Bar bar;
+ myQueue.dequeue_with( [&bar]( Foo& src ) { bar = std::move( src );});
\endcode
- You may use \p boost:ref construction to pass functor \p f by reference.
+ The functor is called only if the queue is not empty.
*/
- template <typename Q, typename Func>
- bool dequeue( Q& dest, Func f )
+ template <typename Func>
+ bool dequeue_with( Func f )
{
value_type * p = base_class::dequeue();
if ( p ) {
- unref(f)( dest, *p );
+ f( *p );
gc::template retire< typename maker::node_disposer >( p );
return true;
}
return false;
}
- /// Synonym for <tt>dequeue( Q&, Func )</tt> function
- template <typename Q, typename Func>
- bool pop( Q& dest, Func f )
- {
- return dequeue( dest, f );
- }
-
- /// Dequeues a value from the queue
- /**
- If queue is not empty, the function returns \p true, \p dest contains copy of
- dequeued value. The assignment operator for type \ref value_type is invoked.
- If queue is empty, the function returns \p false, \p dest is unchanged.
- */
- bool dequeue( value_type& dest )
+ /// Synonym for \p dequeue_with() function
+ template <typename Func>
+ bool pop_with( Func f )
{
- typedef cds::details::trivial_assign<value_type, value_type> functor;
- return dequeue( dest, functor() );
+ return dequeue_with( f );
}
- /// Synonym for <tt>dequeue(value_type&)</tt> function
+ /// Synonym for \p dequeue() function
bool pop( value_type& dest )
{
return dequeue( dest );
/// Clear the queue
/**
- The function repeatedly calls \ref dequeue until it returns \p nullptr.
+ The function repeatedly calls \p dequeue() until it returns \p nullptr.
The disposer specified in \p Traits template argument is called for each removed item.
*/
void clear()
}} // namespace cds::container
-#endif // #ifndef __CDS_CONTAINER_SEGMENTED_QUEUE_H
+#endif // #ifndef CDSLIB_CONTAINER_SEGMENTED_QUEUE_H