Changed default back-off strategy
[libcds.git] / cds / container / msqueue.h
index 7fbdd782736fd53e31e4b86062e89aaee9d335c0..e38585130741ed7d9be1ffee03eb3c6cb182d55b 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_CONTAINER_MSQUEUE_H
-#define __CDS_CONTAINER_MSQUEUE_H
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+    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_MSQUEUE_H
+#define CDSLIB_CONTAINER_MSQUEUE_H
 
 #include <memory>
 #include <cds/intrusive/msqueue.h>
@@ -40,37 +68,36 @@ namespace cds { namespace container {
             typedef msqueue::empty_stat         stat;
 
             /// C++ memory ordering model
-            /** 
+            /**
                 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
                 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
             */
             typedef opt::v::relaxed_ordering    memory_model;
 
-            /// Alignment of internal queue data. Default is \p opt::cache_line_alignment
-            enum { alignment = opt::cache_line_alignment };
+            /// Padding for internal critical atomic data. Default is \p opt::cache_line_padding
+            enum { padding = opt::cache_line_padding };
         };
 
         /// Metafunction converting option list to \p msqueue::traits
         /**
-            This is a wrapper for <tt> cds::opt::make_options< type_traits, Options...> </tt>
             Supported \p Options are:
-            - opt::allocator - allocator (like \p std::allocator) used for allocating queue nodes. Default is \ref CDS_DEFAULT_ALLOCATOR
-            - opt::back_off - back-off strategy used, default is \p cds::backoff::empty.
-            - opt::item_counter - the type of item counting feature. Default is \p cds::atomicity::empty_item_counter (item counting disabled)
+            - \p opt::allocator - allocator (like \p std::allocator) used for allocating queue nodes. Default is \ref CDS_DEFAULT_ALLOCATOR
+            - \p opt::back_off - back-off strategy used, default is \p cds::backoff::empty.
+            - \p opt::item_counter - the type of item counting feature. Default is \p cds::atomicity::empty_item_counter (item counting disabled)
                 To enable item counting use \p cds::atomicity::item_counter
-            - opt::stat - the type to gather internal statistics.
+            - \p opt::stat - the type to gather internal statistics.
                 Possible statistics types are: \p msqueue::stat, \p msqueue::empty_stat, user-provided class that supports \p %msqueue::stat interface.
                 Default is \p %msqueue::empty_stat.
-            - opt::alignment - the alignment for internal queue data. Default is \p opt::cache_line_alignment
-            - opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
+            - \p opt::padding - padding for internal critical atomic data. Default is \p opt::cache_line_padding
+            - \p opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
                 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
 
             Example: declare \p %MSQueue with item counting and internal statistics
             \code
-            typedef cds::container::MSQueue< cds::gc::HP, Foo, 
+            typedef cds::container::MSQueue< cds::gc::HP, Foo,
                 typename cds::container::msqueue::make_traits<
-                    cds::opt::item_counte< cds::atomicity::item_counter >,
-                    cds::opt::stat< cds::intrusive::msqueue::stat<> >
+                    cds::opt::item_counter< cds::atomicity::item_counter >,
+                    cds::opt::stat< cds::container::msqueue::stat<> >
                 >::type
             > myQueue;
             \endcode
@@ -124,8 +151,9 @@ namespace cds { namespace container {
 
             struct intrusive_traits : public traits
             {
-                typedef cds::intrusive::base_hook< cds::opt::gc<gc> > hook;
+                typedef cds::intrusive::msqueue::base_hook< cds::opt::gc<gc> > hook;
                 typedef node_deallocator disposer;
+                static CDS_CONSTEXPR const cds::intrusive::opt::link_check_type link_checker = cds::intrusive::msqueue::traits::link_checker;
             };
 
             typedef intrusive::MSQueue< gc, node_type, intrusive_traits > type;
@@ -151,20 +179,20 @@ namespace cds { namespace container {
             typedef cds::container::MSQueue< cds::gc::HP, Foo, myTraits > myQueue;
 
             // Equivalent make_traits example:
-            typedef cds::container::MSQueue< cds::gc::HP, Foo, 
-                typename cds::container::msqueue::make_traits< 
+            typedef cds::container::MSQueue< cds::gc::HP, Foo,
+                typename cds::container::msqueue::make_traits<
                     cds::opt::stat< cds::container::msqueue::stat<> >,
                     cds::opt::item_counter< cds::atomicity::item_counter >
                 >::type
             > myQueue;
             \endcode
     */
-    template <typename GC, typename T, typename Traits = msqueue::traits>
+    template <typename GC, typename T, typename Traits = cds::container::msqueue::traits>
     class MSQueue:
 #ifdef CDS_DOXYGEN_INVOKED
-        intrusive::MSQueue< GC, cds::intrusive::msqueue::node< T >, Traits >
+        private intrusive::MSQueue< GC, cds::intrusive::msqueue::node< T >, Traits >
 #else
-        details::make_msqueue< GC, T, Traits >::type
+        private details::make_msqueue< GC, T, Traits >::type
 #endif
     {
         //@cond
@@ -180,7 +208,9 @@ namespace cds { namespace container {
         };
 
     public:
-        typedef T value_type; ///< Value type stored in the queue
+        typedef T value_type;   ///< Value type stored in the queue
+        typedef Traits traits;  ///< Queue traits
+
         typedef typename base_class::gc             gc;             ///< Garbage collector used
         typedef typename base_class::back_off       back_off;       ///< Back-off strategy used
         typedef typename maker::allocator_type      allocator_type; ///< Allocator type used for allocate/deallocate the nodes
@@ -188,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)
@@ -238,25 +270,36 @@ 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 )
         {
-            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;
+        }
+
+        /// 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 \p data to queue using a functor
+        /// Enqueues data to the queue using a functor
         /**
             \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:
+            The functor \p f takes one argument - a reference to a new node of type \ref value_type :
             \code
-            cds:container::MSQueue< cds::gc::HP, Foo > myQueue;
+            cds::container::MSQueue< cds::gc::HP, Foo > myQueue;
             Bar bar;
             myQueue.enqueue_with( [&bar]( Foo& dest ) { dest = bar; } );
             \endcode
@@ -264,7 +307,7 @@ namespace cds { namespace container {
         template <typename Func>
         bool enqueue_with( Func f )
         {
-            scoped_node_ptr p( alloc_node() );
+            scoped_node_ptr p( alloc_node());
             f( p->m_value );
             if ( base_class::enqueue( *p )) {
                 p.release();
@@ -277,8 +320,8 @@ namespace cds { namespace container {
         template <typename... Args>
         bool emplace( Args&&... args )
         {
-            scoped_node_ptr p( alloc_node_move( std::forward<Args>( args )... ) );
-            if ( base_class::enqueue( *p ) ) {
+            scoped_node_ptr p( alloc_node_move( std::forward<Args>( args )... ));
+            if ( base_class::enqueue( *p )) {
                 p.release();
                 return true;
             }
@@ -291,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 <typename Func>
         bool push_with( Func f )
@@ -306,7 +355,13 @@ namespace cds { namespace container {
         */
         bool dequeue( value_type& dest )
         {
-            return dequeue_with( [&dest]( value_type& src ) { dest = src;  } );
+            return dequeue_with( [&dest]( value_type& src ) {
+                // TSan finds a race between this read of \p src and node_type constructor
+                // I think, it is wrong
+                CDS_TSAN_ANNOTATE_IGNORE_READS_BEGIN;
+                dest = std::move( src );
+                CDS_TSAN_ANNOTATE_IGNORE_READS_END;
+            });
         }
 
         /// Dequeues a value using a functor
@@ -347,7 +402,7 @@ namespace cds { namespace container {
 
         /// Clear the queue
         /**
-        The function repeatedly calls \ref dequeue until it returns \p nullptr.
+            The function repeatedly calls \ref dequeue until it returns \p nullptr.
         */
         void clear()
         {
@@ -361,6 +416,8 @@ namespace cds { namespace container {
         }
 
         /// Returns queue's item count (see \ref intrusive::MSQueue::size for explanation)
+        /** \copydetails cds::intrusive::MSQueue::size()
+        */
         size_t size() const
         {
             return base_class::size();
@@ -375,4 +432,4 @@ namespace cds { namespace container {
 
 }}  // namespace cds::container
 
-#endif  // #ifndef __CDS_CONTAINER_MSQUEUE_H
+#endif  // #ifndef CDSLIB_CONTAINER_MSQUEUE_H