Updated copyright
[libcds.git] / cds / container / fcdeque.h
index 7dccc157cdedf821e3aa36bfb77e26bc17f9b586..3270be7bc7e58f0fa89f1a5b25181a16f267d0f0 100644 (file)
@@ -1,11 +1,11 @@
 /*
     This file is a part of libcds - Concurrent Data Structures library
 
-    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+    (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:
 
@@ -25,7 +25,7 @@
     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.     
+    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 #ifndef CDSLIB_CONTAINER_FCDEQUE_H
@@ -94,13 +94,8 @@ namespace cds { namespace container {
         /// Metafunction converting option list to traits
         /**
             \p Options are:
-            - \p opt::lock_type - mutex type, default is \p cds::sync::spin
-            - \p opt::back_off - back-off strategy, defalt is \p cds::backoff::delay_of<2>
-            - \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.
@@ -161,8 +156,7 @@ namespace cds { namespace container {
             op_push_back_move,      ///< Push back (move semantics)
             op_pop_front,           ///< Pop front
             op_pop_back,            ///< Pop back
-            op_clear,               ///< Clear
-            op_empty                ///< Empty
+            op_clear                ///< Clear
         };
 
         /// Flat combining publication list record
@@ -181,8 +175,8 @@ namespace cds { namespace container {
 
     protected:
         //@cond
-        fc_kernel   m_FlatCombining;
-        deque_type  m_Deque;
+        mutable fc_kernel m_FlatCombining;
+        deque_type        m_Deque;
         //@endcond
 
     public:
@@ -206,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 )
@@ -214,7 +208,7 @@ 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;
@@ -228,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 )
@@ -236,7 +230,7 @@ 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;
@@ -250,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 )
@@ -258,7 +252,7 @@ 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;
@@ -272,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 )
@@ -280,7 +274,7 @@ 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;
@@ -295,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 )
@@ -303,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;
@@ -318,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 )
@@ -326,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;
@@ -335,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 );
         }
 
@@ -361,18 +355,12 @@ namespace cds { namespace container {
         /**
             If the combining is in process the function waits while combining done.
         */
-        bool empty()
+        bool empty() const
         {
-            fc_record * pRec = m_FlatCombining.acquire_record();
-
-            if ( c_bEliminationEnabled )
-                m_FlatCombining.batch_combine( op_empty, pRec, *this );
-            else
-                m_FlatCombining.combine( op_empty, pRec, *this );
-
-            assert( pRec->is_done() );
-            m_FlatCombining.release_record( pRec );
-            return pRec->bEmpty;
+            bool bRet = false;
+            auto const& deq = m_Deque;
+            m_FlatCombining.invoke_exclusive( [&deq, &bRet]() { bRet = deq.empty(); } );
+            return bRet;
         }
 
         /// Internal statistics
@@ -396,22 +384,22 @@ namespace cds { namespace container {
             // this function is called under FC mutex, so switch TSan off
             CDS_TSAN_ANNOTATE_IGNORE_RW_BEGIN;
 
-            switch ( pRec->op() ) {
+            switch ( pRec->op()) {
             case op_push_front:
                 assert( pRec->pValPush );
-                m_Deque.push_front( *(pRec->pValPush) );
+                m_Deque.push_front( *(pRec->pValPush));
                 break;
             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;
             case op_push_back:
                 assert( pRec->pValPush );
-                m_Deque.push_back( *(pRec->pValPush) );
+                m_Deque.push_back( *(pRec->pValPush));
                 break;
             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;
             case op_pop_front:
                 assert( pRec->pValPop );
@@ -430,12 +418,9 @@ namespace cds { namespace container {
                 }
                 break;
             case op_clear:
-                while ( !m_Deque.empty() )
+                while ( !m_Deque.empty())
                     m_Deque.pop_front();
                 break;
-            case op_empty:
-                pRec->bEmpty = m_Deque.empty();
-                break;
             default:
                 assert(false);
                 break;
@@ -452,10 +437,10 @@ namespace cds { namespace container {
             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)) )
+                        && (itPrev->op() == op_pop_front || (m_Deque.empty() && itPrev->op() == op_pop_back)))
                     {
                         collide( *it, *itPrev );
                         itPrev = itEnd;
@@ -475,7 +460,7 @@ namespace cds { namespace container {
                     break;
                 case op_push_back:
                     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;
@@ -495,8 +480,8 @@ namespace cds { namespace container {
                     break;
                 case op_pop_front:
                     if ( itPrev != itEnd ) {
-                        if ( m_Deque.empty() ) {
-                            switch ( itPrev->op() ) {
+                        if ( m_Deque.empty()) {
+                            switch ( itPrev->op()) {
                             case op_push_back:
                                 collide( *itPrev, *it );
                                 itPrev = itEnd;
@@ -511,7 +496,7 @@ namespace cds { namespace container {
                             }
                         }
                         else {
-                            switch ( itPrev->op() ) {
+                            switch ( itPrev->op()) {
                             case op_push_front:
                                 collide( *itPrev, *it );
                                 itPrev = itEnd;
@@ -531,8 +516,8 @@ namespace cds { namespace container {
                     break;
                 case op_pop_back:
                     if ( itPrev != itEnd ) {
-                        if ( m_Deque.empty() ) {
-                            switch ( itPrev->op() ) {
+                        if ( m_Deque.empty()) {
+                            switch ( itPrev->op()) {
                             case op_push_front:
                                 collide( *itPrev, *it );
                                 itPrev = itEnd;
@@ -547,7 +532,7 @@ namespace cds { namespace container {
                             }
                         }
                         else {
-                            switch ( itPrev->op() ) {
+                            switch ( itPrev->op()) {
                             case op_push_back:
                                 collide( *itPrev, *it );
                                 itPrev = itEnd;