Fixed doxygen 1.8.10 incompabilities
[libcds.git] / cds / container / segmented_queue.h
index ecd0b20d1fd51e8807f73643e9f002fb093acbfb..48c26d6607e83dcf0466a1d218e3864bc1f35556 100644 (file)
@@ -1,12 +1,12 @@
 //$$CDS-header$$
 
-#ifndef __CDS_CONTAINER_SEGMENTED_QUEUE_H
-#define __CDS_CONTAINER_SEGMENTED_QUEUE_H
+#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 {
 
@@ -24,7 +24,7 @@ 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;
@@ -46,11 +46,20 @@ namespace cds { namespace container {
             /// 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;
@@ -58,7 +67,7 @@ namespace cds { namespace container {
 
          /// 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<
@@ -66,30 +75,32 @@ namespace cds { namespace container {
             >::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
         };
@@ -114,7 +125,8 @@ namespace cds { namespace container {
                 }
             };
 
-            struct intrusive_type_traits: public original_type_traits {
+            struct intrusive_type_traits: public original_type_traits
+            {
                 typedef node_disposer   disposer;
             };
 
@@ -159,13 +171,13 @@ namespace cds { namespace container {
         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 >
@@ -178,18 +190,18 @@ namespace cds { namespace container {
         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
@@ -211,11 +223,6 @@ namespace cds { namespace container {
         {
             return cxx_node_allocator().MoveNew( std::forward<Args>( args )... );
         }
-
-        struct dummy_disposer {
-            void operator()( value_type * p )
-            {}
-        };
         //@endcond
 
     public:
@@ -246,45 +253,40 @@ namespace cds { namespace container {
             return false;
         }
 
-        /// Synonym for <tt>enqueue(value_type const&)</tt> function
-        bool push( value_type const& val )
-        {
-            return enqueue( val );
-        }
-
-        /// 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 );
-            if ( base_class::enqueue( *p )) {
+            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() member function
+        bool push( value_type const& val )
         {
-            return enqueue( data, f );
+            return enqueue( val );
+        }
+
+        /// Synonym for \p enqueue_with() member function
+        template <typename Func>
+        bool push_with( Func f )
+        {
+            return enqueue_with( f );
         }
 
         /// Enqueues data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
@@ -299,54 +301,48 @@ namespace cds { namespace container {
             return false;
         }
 
-        /// Removes an element from first segment of the queue
+        /// 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 )
+        {
+            return dequeue_with( [&dest]( value_type& src ) { dest = src; });
+        }
+
+        /// Dequeues a value using a functor
         /**
-            \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:
+            \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 );
@@ -398,4 +394,4 @@ namespace cds { namespace container {
 
 }} // namespace cds::container
 
-#endif // #ifndef __CDS_CONTAINER_SEGMENTED_QUEUE_H
+#endif // #ifndef CDSLIB_CONTAINER_SEGMENTED_QUEUE_H