Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / cds / intrusive / split_list_rcu.h
index 73f05a26fd3a0d9058de5c727e5cccd82337c117..c8902d111c111df563004497fcef8ab46d82bb1c 100644 (file)
@@ -1,7 +1,7 @@
 /*
     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/
@@ -101,14 +101,14 @@ namespace cds { namespace intrusive {
 
     protected:
         //@cond
-        typedef split_list::details::rebind_list_traits<OrderedList, traits> wrapped_ordered_list;
+        typedef split_list::details::rebind_list_traits<OrderedList, traits> ordered_list_adapter;
         //@endcond
 
     public:
 #   ifdef CDS_DOXYGEN_INVOKED
         typedef OrderedList         ordered_list;   ///< type of ordered list used as base for split-list
 #   else
-        typedef typename wrapped_ordered_list::result    ordered_list;
+        typedef typename ordered_list_adapter::result    ordered_list;
 #   endif
         typedef typename ordered_list::value_type     value_type;     ///< type of value stored in the split-list
         typedef typename ordered_list::key_comparator key_comparator; ///< key compare functor
@@ -141,15 +141,16 @@ namespace cds { namespace intrusive {
             This traits is intended for converting between underlying ordered list node type \ref list_node_type
             and split-list node type \ref node_type
         */
-        typedef split_list::node_traits<typename ordered_list::node_traits>  node_traits;
+        typedef typename ordered_list_adapter::node_traits node_traits;
 
         /// Bucket table implementation
         typedef typename split_list::details::bucket_table_selector<
             traits::dynamic_bucket_table
             , gc
-            , node_type
+            , typename ordered_list_adapter::aux_node
             , opt::allocator< typename traits::allocator >
             , opt::memory_model< memory_model >
+            , opt::free_list< typename traits::free_list >
         >::type bucket_table;
 
         typedef typename bucket_table::aux_node_type aux_node_type; ///< auxiliary node type
@@ -282,7 +283,7 @@ namespace cds { namespace intrusive {
         */
         SplitListSet()
             : m_nBucketCountLog2(1)
-            , m_nMaxItemCount( max_item_count(2, m_Buckets.load_factor()) )
+            , m_nMaxItemCount( max_item_count(2, m_Buckets.load_factor()))
         {
             init();
         }
@@ -294,7 +295,7 @@ namespace cds { namespace intrusive {
             )
             : m_Buckets( nItemCount, nLoadFactor )
             , m_nBucketCountLog2(1)
-            , m_nMaxItemCount( max_item_count(2, m_Buckets.load_factor()) )
+            , m_nMaxItemCount( max_item_count(2, m_Buckets.load_factor()))
         {
             init();
         }
@@ -449,7 +450,7 @@ namespace cds { namespace intrusive {
             aux_node_type * pHead = get_bucket( nHash );
             assert( pHead != nullptr );
 
-            if ( m_List.unlink_at( pHead, val ) ) {
+            if ( m_List.unlink_at( pHead, val )) {
                 --m_ItemCounter;
                 m_Stat.onEraseSuccess();
                 return true;
@@ -476,7 +477,7 @@ namespace cds { namespace intrusive {
         template <typename Q>
         bool erase( Q const& key )
         {
-            return erase_( key, key_comparator() );
+            return erase_( key, key_comparator());
         }
 
         /// Deletes the item from the set using \p pred for searching
@@ -490,7 +491,7 @@ namespace cds { namespace intrusive {
         bool erase_with( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return erase_( key, typename wrapped_ordered_list::template make_compare_from_less<Less>() );
+            return erase_( key, typename ordered_list_adapter::template make_compare_from_less<Less>());
         }
 
         /// Deletes the item from the set
@@ -530,7 +531,7 @@ namespace cds { namespace intrusive {
         bool erase_with( Q const& key, Less pred, Func f )
         {
             CDS_UNUSED( pred );
-            return erase_( key, typename wrapped_ordered_list::template make_compare_from_less<Less>(), f );
+            return erase_( key, typename ordered_list_adapter::template make_compare_from_less<Less>(), f );
         }
 
         /// Extracts an item from the set
@@ -573,7 +574,7 @@ namespace cds { namespace intrusive {
         template <typename Q>
         exempt_ptr extract( Q const& key )
         {
-            return exempt_ptr(extract_( key, key_comparator() ));
+            return exempt_ptr(extract_( key, key_comparator()));
         }
 
         /// Extracts an item from the set using \p pred for searching
@@ -638,14 +639,14 @@ namespace cds { namespace intrusive {
         bool find_with( Q& key, Less pred, Func f )
         {
             CDS_UNUSED( pred );
-            return find_( key, typename wrapped_ordered_list::template make_compare_from_less<Less>(), f );
+            return find_( key, typename ordered_list_adapter::template make_compare_from_less<Less>(), f );
         }
         //@cond
         template <typename Q, typename Less, typename Func>
         bool find_with( Q const& key, Less pred, Func f )
         {
             CDS_UNUSED( pred );
-            return find_( key, typename wrapped_ordered_list::template make_compare_from_less<Less>(), f );
+            return find_( key, typename ordered_list_adapter::template make_compare_from_less<Less>(), f );
         }
         //@endcond
 
@@ -661,7 +662,7 @@ namespace cds { namespace intrusive {
         template <typename Q>
         bool contains( Q const& key )
         {
-            return find_value( key, key_comparator() );
+            return find_value( key, key_comparator());
         }
         //@cond
         template <typename Q>
@@ -682,7 +683,7 @@ namespace cds { namespace intrusive {
         bool contains( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return find_value( key, typename wrapped_ordered_list::template make_compare_from_less<Less>() );
+            return find_value( key, typename ordered_list_adapter::template make_compare_from_less<Less>());
         }
         //@cond
         template <typename Q, typename Less>
@@ -724,7 +725,7 @@ namespace cds { namespace intrusive {
         template <typename Q>
         raw_ptr get( Q const& key )
         {
-            return get_( key, key_comparator() );
+            return get_( key, key_comparator());
         }
 
         /// Finds the key \p key and return the item found
@@ -740,7 +741,7 @@ namespace cds { namespace intrusive {
         raw_ptr get_with( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return get_( key, typename wrapped_ordered_list::template make_compare_from_less<Less>());
+            return get_( key, typename ordered_list_adapter::template make_compare_from_less<Less>());
         }
 
 
@@ -764,7 +765,7 @@ namespace cds { namespace intrusive {
         void clear()
         {
             iterator it = begin();
-            while ( it != end() ) {
+            while ( it != end()) {
                 iterator i(it);
                 ++i;
                 unlink( *it );
@@ -778,6 +779,12 @@ namespace cds { namespace intrusive {
             return m_Stat;
         }
 
+        /// Returns internal statistics for \p OrderedList
+        typename OrderedList::stat const& list_statistics() const
+        {
+            return m_List.statistics();
+        }
+
     protected:
         //@cond
         template <bool IsConst>
@@ -828,7 +835,7 @@ namespace cds { namespace intrusive {
         */
         iterator begin()
         {
-            return iterator( m_List.begin(), m_List.end() );
+            return iterator( m_List.begin(), m_List.end());
         }
 
         /// Returns an iterator that addresses the location succeeding the last element in a split-list
@@ -840,7 +847,7 @@ namespace cds { namespace intrusive {
         */
         iterator end()
         {
-            return iterator( m_List.end(), m_List.end() );
+            return iterator( m_List.end(), m_List.end());
         }
 
         /// Returns a forward const iterator addressing the first element in a split-list
@@ -851,7 +858,7 @@ namespace cds { namespace intrusive {
         /// Returns a forward const iterator addressing the first element in a split-list
         const_iterator cbegin() const
         {
-            return const_iterator( m_List.cbegin(), m_List.cend() );
+            return const_iterator( m_List.cbegin(), m_List.cend());
         }
 
         /// Returns an const iterator that addresses the location succeeding the last element in a split-list
@@ -862,7 +869,7 @@ namespace cds { namespace intrusive {
         /// Returns an const iterator that addresses the location succeeding the last element in a split-list
         const_iterator cend() const
         {
-            return const_iterator( m_List.cend(), m_List.cend() );
+            return const_iterator( m_List.cend(), m_List.cend());
         }
     //@}
 
@@ -898,10 +905,10 @@ namespace cds { namespace intrusive {
         static size_t parent_bucket( size_t nBucket )
         {
             assert( nBucket > 0 );
-            return nBucket & ~( 1 << bitop::MSBnz( nBucket ) );
+            return nBucket & ~( 1 << bitop::MSBnz( nBucket ));
         }
 
-        aux_node_type * init_bucket( size_t nBucket )
+        aux_node_type * init_bucket( size_t const nBucket )
         {
             assert( nBucket > 0 );
             size_t nParent = parent_bucket( nBucket );
@@ -914,32 +921,41 @@ namespace cds { namespace intrusive {
 
             assert( pParentBucket != nullptr );
 
-            // Allocate a dummy node for new bucket
-            {
-                aux_node_type * pBucket = alloc_aux_node( split_list::dummy_hash( nBucket ) );
-                if ( m_List.insert_aux_node( pParentBucket, pBucket ) ) {
-                    m_Buckets.bucket( nBucket, pBucket );
-                    m_Stat.onNewBucket();
+            // Allocate an aux node for new bucket
+            aux_node_type * pBucket = m_Buckets.bucket( nBucket );
+
+            back_off bkoff;
+            for ( ;; pBucket = m_Buckets.bucket( nBucket )) {
+                if ( pBucket )
                     return pBucket;
+
+                pBucket = alloc_aux_node( split_list::dummy_hash( nBucket ));
+                if ( pBucket ) {
+                    if ( m_List.insert_aux_node( pParentBucket, pBucket )) {
+                        m_Buckets.bucket( nBucket, pBucket );
+                        m_Stat.onNewBucket();
+                        return pBucket;
+                    }
+
+                    // Another thread set the bucket. Wait while it done
+                    free_aux_node( pBucket );
+                    m_Stat.onBucketInitContenton();
+                    break;
                 }
-                free_aux_node( pBucket );
+
+                // There are no free buckets. It means that the bucket table is full
+                // Wait while another thread set the bucket or a free bucket will be available
+                m_Stat.onBucketsExhausted();
+                bkoff();
             }
 
             // Another thread set the bucket. Wait while it done
-
-            // In this point, we must wait while nBucket is empty.
-            // The compiler can decide that waiting loop can be "optimized" (stripped)
-            // To prevent this situation, we use waiting on volatile bucket_head_ptr pointer.
-            //
-            m_Stat.onBucketInitContenton();
-            back_off bkoff;
-            while ( true ) {
-                aux_node_type volatile * p = m_Buckets.bucket( nBucket );
-                if ( p != nullptr )
-                    return const_cast<aux_node_type *>( p );
+            for ( pBucket = m_Buckets.bucket( nBucket ); pBucket == nullptr; pBucket = m_Buckets.bucket( nBucket )) {
                 bkoff();
                 m_Stat.onBusyWaitBucketInit();
             }
+
+            return pBucket;
         }
 
         aux_node_type * get_bucket( size_t nHash )
@@ -950,7 +966,7 @@ namespace cds { namespace intrusive {
             if ( pHead == nullptr )
                 pHead = init_bucket( nBucket );
 
-            assert( pHead->is_dummy() );
+            assert( pHead->is_dummy());
 
             return pHead;
         }
@@ -979,7 +995,7 @@ namespace cds { namespace intrusive {
 
             size_t sz = m_nBucketCountLog2.load(memory_model::memory_order_relaxed);
             const size_t nBucketCount = static_cast<size_t>(1) << sz;
-            if ( nBucketCount < m_Buckets.capacity() ) {
+            if ( nBucketCount < m_Buckets.capacity()) {
                 // we may grow the bucket table
                 const size_t nLoadFactor = m_Buckets.load_factor();
                 if ( nMaxCount < max_item_count( nBucketCount, nLoadFactor ))
@@ -1051,7 +1067,7 @@ namespace cds { namespace intrusive {
         value_type * extract_with_( Q const& val, Less pred )
         {
             CDS_UNUSED( pred );
-            return extract_( val, typename wrapped_ordered_list::template make_compare_from_less<Less>());
+            return extract_( val, typename ordered_list_adapter::template make_compare_from_less<Less>());
         }
 
         template <typename Q, typename Compare>
@@ -1062,7 +1078,7 @@ namespace cds { namespace intrusive {
             aux_node_type * pHead = get_bucket( nHash );
             assert( pHead != nullptr );
 
-            if ( m_List.erase_at( pHead, sv, cmp ) ) {
+            if ( m_List.erase_at( pHead, sv, cmp )) {
                 --m_ItemCounter;
                 m_Stat.onEraseSuccess();
                 return true;