Updated copyright
[libcds.git] / cds / container / fcdeque.h
index 452d7a5116da73ec041e07ed83da47b39df37087..3270be7bc7e58f0fa89f1a5b25181a16f267d0f0 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_CONTAINER_FCDEQUE_H
-#define __CDS_CONTAINER_FCDEQUE_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_FCDEQUE_H
+#define CDSLIB_CONTAINER_FCDEQUE_H
 
 #include <cds/algo/flat_combining.h>
 #include <cds/algo/elimination_opt.h>
@@ -57,35 +85,29 @@ namespace cds { namespace container {
         };
 
         /// FCDeque type traits
-        struct type_traits: public cds::algo::flat_combining::type_traits
+        struct traits: public cds::algo::flat_combining::traits
         {
             typedef empty_stat      stat;   ///< Internal statistics
-            static CDS_CONSTEXPR_CONST bool enable_elimination = false; ///< Enable \ref cds_elimination_description "elimination"
+            static CDS_CONSTEXPR const bool enable_elimination = false; ///< Enable \ref cds_elimination_description "elimination"
         };
 
         /// Metafunction converting option list to traits
         /**
-            This is a wrapper for <tt> cds::opt::make_options< type_traits, Options...> </tt>
             \p Options are:
-            - \p opt::lock_type - mutex type, default is \p cds::lock::Spin
-            - \p opt::back_off - back-off strategy, defalt is \p cds::backoff::Default
-            - \p opt::allocator - allocator type, default is \ref CDS_DEFAULT_ALLOCATOR
+            - any \p cds::algo::flat_combining::make_traits options
             - \p opt::stat - internal statistics, possible type: \ref stat, \ref empty_stat (the default)
-            - \p opt::memory_model - C++ memory ordering model.
-                List of all available memory ordering see opt::memory_model.
-                Default if cds::opt::v:relaxed_ordering
             - \p opt::enable_elimination - enable/disable operation \ref cds_elimination_description "elimination"
                 By default, the elimination is disabled. For queue, the elimination is possible if the queue
                 is empty.
         */
-        template <CDS_DECL_OPTIONS8>
+        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_OPTIONS8 >::type
-                ,CDS_OPTIONS8
+                typename cds::opt::find_type_traits< traits, Options... >::type
+                ,Options...
             >::type   type;
 #   endif
         };
@@ -104,12 +126,12 @@ namespace cds { namespace container {
         - \p T - a value type stored in the deque
         - \p Deque - sequential deque implementation, for example, \p std::deque<T> (the default)
             or \p boost::container::deque
-        - \p Trats - type traits of flat combining, default is \p fcdeque::type_traits.
-            \p fcdeque::make_traits metafunction can be used to construct specialized \p %type_traits
+        - \p Trats - type traits of flat combining, default is \p fcdeque::traits.
+            \p fcdeque::make_traits metafunction can be used to construct specialized \p %fcdeque::traits
     */
     template <typename T,
         class Deque = std::deque<T>,
-        typename Traits = fcdeque::type_traits
+        typename Traits = fcdeque::traits
     >
     class FCDeque
 #ifndef CDS_DOXYGEN_INVOKED
@@ -119,10 +141,10 @@ namespace cds { namespace container {
     public:
         typedef T           value_type;     ///< Value type
         typedef Deque       deque_type;     ///< Sequential deque class
-        typedef Traits      type_traits;    ///< Deque type traits
+        typedef Traits      traits;         ///< Deque type traits
 
-        typedef typename type_traits::stat  stat;   ///< Internal statistics type
-        static CDS_CONSTEXPR_CONST bool c_bEliminationEnabled = type_traits::enable_elimination; ///< \p true if elimination is enabled
+        typedef typename traits::stat  stat;   ///< Internal statistics type
+        static CDS_CONSTEXPR const bool c_bEliminationEnabled = traits::enable_elimination; ///< \p true if elimination is enabled
 
     protected:
         //@cond
@@ -149,12 +171,12 @@ namespace cds { namespace container {
         //@endcond
 
         /// Flat combining kernel
-        typedef cds::algo::flat_combining::kernel< fc_record, type_traits > fc_kernel;
+        typedef cds::algo::flat_combining::kernel< fc_record, traits > fc_kernel;
 
     protected:
         //@cond
-        fc_kernel   m_FlatCombining;
-        deque_type  m_Deque;
+        mutable fc_kernel m_FlatCombining;
+        deque_type        m_Deque;
         //@endcond
 
     public:
@@ -178,7 +200,7 @@ namespace cds { namespace container {
             value_type const& val ///< Value to be copied to inserted element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPush = &val;
 
             if ( c_bEliminationEnabled )
@@ -186,13 +208,12 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_push_front, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPushFront();
             return true;
         }
 
-#   ifdef CDS_MOVE_SEMANTICS_SUPPORT
         /// Inserts a new element at the beginning of the deque container (move semantics)
         /**
             The function always returns \p true
@@ -201,7 +222,7 @@ namespace cds { namespace container {
             value_type&& val ///< Value to be moved to inserted element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPush = &val;
 
             if ( c_bEliminationEnabled )
@@ -209,12 +230,11 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_push_front_move, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPushFrontMove();
             return true;
         }
-#   endif
 
         /// Inserts a new element at the end of the deque container
         /**
@@ -224,7 +244,7 @@ namespace cds { namespace container {
             value_type const& val ///< Value to be copied to inserted element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPush = &val;
 
             if ( c_bEliminationEnabled )
@@ -232,13 +252,12 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_push_back, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPushBack();
             return true;
         }
 
-#   ifdef CDS_MOVE_SEMANTICS_SUPPORT
         /// Inserts a new element at the end of the deque container (move semantics)
         /**
             The function always returns \p true
@@ -247,7 +266,7 @@ namespace cds { namespace container {
             value_type&& val ///< Value to be moved to inserted element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPush = &val;
 
             if ( c_bEliminationEnabled )
@@ -255,12 +274,11 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_push_back_move, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPushBackMove();
             return true;
         }
-#   endif
 
         /// Removes the first element in the deque container
         /**
@@ -271,7 +289,7 @@ namespace cds { namespace container {
             value_type& val ///< Target to be received the copy of removed element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPop = &val;
 
             if ( c_bEliminationEnabled )
@@ -279,7 +297,7 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_pop_front, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPopFront( pRec->bEmpty );
             return !pRec->bEmpty;
@@ -294,7 +312,7 @@ namespace cds { namespace container {
             value_type& val ///< Target to be received the copy of removed element
         )
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
             pRec->pValPop = &val;
 
             if ( c_bEliminationEnabled )
@@ -302,7 +320,7 @@ namespace cds { namespace container {
             else
                 m_FlatCombining.combine( op_pop_back, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
             m_FlatCombining.internal_statistics().onPopBack( pRec->bEmpty );
             return !pRec->bEmpty;
@@ -311,14 +329,14 @@ namespace cds { namespace container {
         /// Clears the deque
         void clear()
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
+            auto pRec = m_FlatCombining.acquire_record();
 
             if ( c_bEliminationEnabled )
                 m_FlatCombining.batch_combine( op_clear, pRec, *this );
             else
                 m_FlatCombining.combine( op_clear, pRec, *this );
 
-            assert( pRec->is_done() );
+            assert( pRec->is_done());
             m_FlatCombining.release_record( pRec );
         }
 
@@ -339,8 +357,10 @@ namespace cds { namespace container {
         */
         bool empty() const
         {
-            m_FlatCombining.wait_while_combining();
-            return m_Deque.empty();
+            bool bRet = false;
+            auto const& deq = m_Deque;
+            m_FlatCombining.invoke_exclusive( [&deq, &bRet]() { bRet = deq.empty(); } );
+            return bRet;
         }
 
         /// Internal statistics
@@ -361,32 +381,31 @@ namespace cds { namespace container {
         {
             assert( pRec );
 
-            switch ( pRec->op() ) {
+            // this function is called under FC mutex, so switch TSan off
+            CDS_TSAN_ANNOTATE_IGNORE_RW_BEGIN;
+
+            switch ( pRec->op()) {
             case op_push_front:
                 assert( pRec->pValPush );
-                m_Deque.push_front( *(pRec->pValPush) );
+                m_Deque.push_front( *(pRec->pValPush));
                 break;
-#       ifdef CDS_MOVE_SEMANTICS_SUPPORT
             case op_push_front_move:
                 assert( pRec->pValPush );
-                m_Deque.push_front( std::move( *(pRec->pValPush )) );
+                m_Deque.push_front( std::move( *(pRec->pValPush )));
                 break;
-#       endif
             case op_push_back:
                 assert( pRec->pValPush );
-                m_Deque.push_back( *(pRec->pValPush) );
+                m_Deque.push_back( *(pRec->pValPush));
                 break;
-#       ifdef CDS_MOVE_SEMANTICS_SUPPORT
             case op_push_back_move:
                 assert( pRec->pValPush );
-                m_Deque.push_back( std::move( *(pRec->pValPush )) );
+                m_Deque.push_back( std::move( *(pRec->pValPush )));
                 break;
-#       endif
             case op_pop_front:
                 assert( pRec->pValPop );
                 pRec->bEmpty = m_Deque.empty();
                 if ( !pRec->bEmpty ) {
-                    *(pRec->pValPop) = m_Deque.front();
+                    *(pRec->pValPop) = std::move( m_Deque.front());
                     m_Deque.pop_front();
                 }
                 break;
@@ -394,41 +413,54 @@ namespace cds { namespace container {
                 assert( pRec->pValPop );
                 pRec->bEmpty = m_Deque.empty();
                 if ( !pRec->bEmpty ) {
-                    *(pRec->pValPop) = m_Deque.back();
+                    *(pRec->pValPop) = std::move( m_Deque.back());
                     m_Deque.pop_back();
                 }
                 break;
             case op_clear:
-                while ( !m_Deque.empty() )
+                while ( !m_Deque.empty())
                     m_Deque.pop_front();
                 break;
             default:
                 assert(false);
                 break;
             }
+            CDS_TSAN_ANNOTATE_IGNORE_RW_END;
         }
 
         /// Batch-processing flat combining
         void fc_process( typename fc_kernel::iterator itBegin, typename fc_kernel::iterator itEnd )
         {
             typedef typename fc_kernel::iterator fc_iterator;
+
+            // this function is called under FC mutex, so switch TSan off
+            CDS_TSAN_ANNOTATE_IGNORE_RW_BEGIN;
+
             for ( fc_iterator it = itBegin, itPrev = itEnd; it != itEnd; ++it ) {
-                switch ( it->op() ) {
+                switch ( it->op()) {
                 case op_push_front:
+                    if ( itPrev != itEnd
+                        && (itPrev->op() == op_pop_front || (m_Deque.empty() && itPrev->op() == op_pop_back)))
+                    {
+                        collide( *it, *itPrev );
+                        itPrev = itEnd;
+                    }
+                    else
+                        itPrev = it;
+                    break;
                 case op_push_front_move:
                     if ( itPrev != itEnd
                       && (itPrev->op() == op_pop_front || ( m_Deque.empty() && itPrev->op() == op_pop_back )))
                     {
-                        collide( *it, *itPrev );
+                        collide_move( *it, *itPrev );
                         itPrev = itEnd;
                     }
                     else
                         itPrev = it;
                     break;
                 case op_push_back:
-                case op_push_back_move:
                     if ( itPrev != itEnd
-                        && (itPrev->op() == op_pop_back || ( m_Deque.empty() && itPrev->op() == op_pop_front )))
+                        && (itPrev->op() == op_pop_back || (m_Deque.empty() && itPrev->op() == op_pop_front)))
                     {
                         collide( *it, *itPrev );
                         itPrev = itEnd;
@@ -436,30 +468,91 @@ namespace cds { namespace container {
                     else
                         itPrev = it;
                     break;
-                case op_pop_front:
+                case op_push_back_move:
                     if ( itPrev != itEnd
-                        && ( itPrev->op() == op_push_front || itPrev->op() == op_push_front_move
-                          || ( m_Deque.empty() && ( itPrev->op() == op_push_back || itPrev->op() == op_push_back_move ))))
+                        && (itPrev->op() == op_pop_back || ( m_Deque.empty() && itPrev->op() == op_pop_front )))
                     {
-                        collide( *itPrev, *it );
+                        collide_move( *it, *itPrev );
                         itPrev = itEnd;
                     }
                     else
                         itPrev = it;
                     break;
+                case op_pop_front:
+                    if ( itPrev != itEnd ) {
+                        if ( m_Deque.empty()) {
+                            switch ( itPrev->op()) {
+                            case op_push_back:
+                                collide( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            case op_push_back_move:
+                                collide_move( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            default:
+                                itPrev = it;
+                                break;
+                            }
+                        }
+                        else {
+                            switch ( itPrev->op()) {
+                            case op_push_front:
+                                collide( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            case op_push_front_move:
+                                collide_move( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            default:
+                                itPrev = it;
+                                break;
+                            }
+                        }
+                    }
+                    else
+                        itPrev = it;
+                    break;
                 case op_pop_back:
-                    if ( itPrev != itEnd
-                        && ( itPrev->op() == op_push_back || itPrev->op() == op_push_back_move
-                        || ( m_Deque.empty() && ( itPrev->op() == op_push_front || itPrev->op() == op_push_front_move ))))
-                    {
-                        collide( *itPrev, *it );
-                        itPrev = itEnd;
+                    if ( itPrev != itEnd ) {
+                        if ( m_Deque.empty()) {
+                            switch ( itPrev->op()) {
+                            case op_push_front:
+                                collide( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            case op_push_front_move:
+                                collide_move( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            default:
+                                itPrev = it;
+                                break;
+                            }
+                        }
+                        else {
+                            switch ( itPrev->op()) {
+                            case op_push_back:
+                                collide( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            case op_push_back_move:
+                                collide_move( *itPrev, *it );
+                                itPrev = itEnd;
+                                break;
+                            default:
+                                itPrev = it;
+                                break;
+                            }
+                        }
                     }
                     else
                         itPrev = it;
                     break;
                 }
             }
+            CDS_TSAN_ANNOTATE_IGNORE_RW_END;
         }
         //@endcond
 
@@ -473,9 +566,18 @@ namespace cds { namespace container {
             m_FlatCombining.operation_done( recPop );
             m_FlatCombining.internal_statistics().onCollide();
         }
+
+        void collide_move( fc_record& recPush, fc_record& recPop )
+        {
+            *(recPop.pValPop) = std::move( *(recPush.pValPush));
+            recPop.bEmpty = false;
+            m_FlatCombining.operation_done( recPush );
+            m_FlatCombining.operation_done( recPop );
+            m_FlatCombining.internal_statistics().onCollide();
+        }
         //@endcond
     };
 
 }} // namespace cds::container
 
-#endif // #ifndef __CDS_CONTAINER_FCDEQUE_H
+#endif // #ifndef CDSLIB_CONTAINER_FCDEQUE_H