Merge branch 'flat_combinig_add_stress_and_unint_tests' of https://github.com/mgalimu...
[libcds.git] / cds / intrusive / feldman_hashset_rcu.h
index 4495b8ab7cc0a5daaa15b546eba10af0ec9f7c16..446582fccaa07de7549fbd9876275eb935788444 100644 (file)
@@ -1,4 +1,32 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (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_INTRUSIVE_FELDMAN_HASHSET_RCU_H
 #define CDSLIB_INTRUSIVE_FELDMAN_HASHSET_RCU_H
@@ -85,6 +113,13 @@ namespace cds { namespace intrusive {
 
         using exempt_ptr = cds::urcu::exempt_ptr< gc, value_type, value_type, disposer, void >; ///< pointer to extracted node
 
+        /// The size of hash_type in bytes, see \p feldman_hashset::traits::hash_size for explanation
+        static CDS_CONSTEXPR size_t const c_hash_size = base_class::c_hash_size;
+
+        //@cond
+        typedef feldman_hashset::level_statistics level_statistics;
+        //@endcond
+
     protected:
         //@cond
         typedef typename base_class::node_ptr node_ptr;
@@ -170,10 +205,11 @@ namespace cds { namespace intrusive {
             hash_comparator cmp;
 
             while (true) {
+                rcu_lock rcuLock;
+
                 node_ptr slot = base_class::traverse( pos );
                 assert(slot.bits() == 0);
 
-                rcu_lock rcuLock;
                 if ( pos.pArr->nodes[pos.nSlot].load(memory_model::memory_order_acquire) == slot) {
                     if (slot.ptr()) {
                         if ( cmp( hash, hash_accessor()(*slot.ptr())) == 0 ) {
@@ -219,7 +255,7 @@ namespace cds { namespace intrusive {
             - If hash value is not found and \p bInsert is \p false then the set is unchanged,
               the function returns <tt> std::pair<false, false> </tt>
 
-            Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull
+            Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful
             (i.e. the item has been inserted or updated),
             \p second is \p true if new item has been added or \p false if the set contains that hash.
 
@@ -467,7 +503,13 @@ namespace cds { namespace intrusive {
         using base_class::array_node_size;
 
         /// Collects tree level statistics into \p stat
-        /** @copydetails cds::intrusive::FeldmanHashSet::get_level_statistics
+        /**
+            The function traverses the set and collects statistics for each level of the tree
+            into \p feldman_hashset::level_statistics struct. The element of \p stat[i]
+            represents statistics for level \p i, level 0 is head array.
+            The function is thread-safe and may be called in multi-threaded environment.
+
+            Result can be useful for estimating efficiency of hash functor you use.
         */
         void get_level_statistics(std::vector<feldman_hashset::level_statistics>& stat) const
         {
@@ -551,13 +593,11 @@ namespace cds { namespace intrusive {
 
             value_type * pointer() const CDS_NOEXCEPT
             {
-                assert(gc::is_locked());
                 return m_pValue;
             }
 
             void forward()
             {
-                assert( gc::is_locked());
                 assert(m_set != nullptr);
                 assert(m_pNode != nullptr);
 
@@ -614,7 +654,6 @@ namespace cds { namespace intrusive {
 
             void backward()
             {
-                assert(gc::is_locked());
                 assert(m_set != nullptr);
                 assert(m_pNode != nullptr);
 
@@ -860,9 +899,10 @@ namespace cds { namespace intrusive {
         /** @anchor cds_intrusive_FeldmanHashSet_rcu_iterators
             The set supports thread-safe iterators: you may iterate over the set in multi-threaded environment
             under explicit RCU lock.
-            RCU lock requirement means that inserting or searching is allowed but you must not erase the items from the set
-            since erasing under RCU lock can lead to a deadlock. However, another thread can call \p erase() safely
-            while your thread is iterating.
+
+            RCU lock requirement means that inserting or searching is allowed for iterating thread
+            but you must not erase the items from the set because erasing under RCU lock can lead
+            to a deadlock. However, another thread can call \p erase() safely while your thread is iterating.
 
             A typical example is:
             \code
@@ -1014,71 +1054,69 @@ namespace cds { namespace intrusive {
             value_type * pOld;
 
             while ( true ) {
+                rcu_lock rcuLock;
+
                 node_ptr slot = base_class::traverse( pos );
                 assert(slot.bits() == 0);
 
                 pOld = nullptr;
-                {
-                    rcu_lock rcuLock;
-
-                    if ( pos.pArr->nodes[pos.nSlot].load(memory_model::memory_order_acquire) == slot) {
-                        if ( slot.ptr()) {
-                            if ( cmp( hash, hash_accessor()(*slot.ptr())) == 0 ) {
-                                // the item with that hash value already exists
-                                // Replace it with val
-                                if ( slot.ptr() == &val ) {
-                                    stats().onUpdateExisting();
-                                    return std::make_pair(true, false);
-                                }
-
-                                if ( pos.pArr->nodes[pos.nSlot].compare_exchange_strong(slot, node_ptr(&val), memory_model::memory_order_release, atomics::memory_order_relaxed)) {
-                                    // slot can be disposed
-                                    f( val, slot.ptr());
-                                    pOld = slot.ptr();
-                                    stats().onUpdateExisting();
-                                    goto update_existing_done;
-                                }
-
-                                stats().onUpdateRetry();
+                if ( pos.pArr->nodes[pos.nSlot].load(memory_model::memory_order_acquire) == slot) {
+                    if ( slot.ptr()) {
+                        if ( cmp( hash, hash_accessor()(*slot.ptr())) == 0 ) {
+                            // the item with that hash value already exists
+                            // Replace it with val
+                            if ( slot.ptr() == &val ) {
+                                stats().onUpdateExisting();
+                                return std::make_pair(true, false);
                             }
-                            else {
-                                if ( bInsert ) {
-                                    // the slot must be expanded
-                                    base_class::expand_slot( pos, slot );
-                                }
-                                else {
-                                    stats().onUpdateFailed();
-                                    return std::make_pair(false, false);
-                                }
+
+                            if ( pos.pArr->nodes[pos.nSlot].compare_exchange_strong(slot, node_ptr(&val), memory_model::memory_order_release, atomics::memory_order_relaxed)) {
+                                // slot can be disposed
+                                f( val, slot.ptr());
+                                pOld = slot.ptr();
+                                stats().onUpdateExisting();
+                                goto update_existing_done;
                             }
+
+                            stats().onUpdateRetry();
                         }
                         else {
-                            // the slot is empty, try to insert data node
-                            if (bInsert) {
-                                node_ptr pNull;
-                                if ( pos.pArr->nodes[pos.nSlot].compare_exchange_strong(pNull, node_ptr(&val), memory_model::memory_order_release, atomics::memory_order_relaxed))
-                                {
-                                    // the new data node has been inserted
-                                    f(val, nullptr);
-                                    ++m_ItemCounter;
-                                    stats().onUpdateNew();
-                                    stats().height( pos.nHeight );
-                                    return std::make_pair(true, true);
-                                }
+                            if ( bInsert ) {
+                                // the slot must be expanded
+                                base_class::expand_slot( pos, slot );
                             }
                             else {
                                 stats().onUpdateFailed();
                                 return std::make_pair(false, false);
                             }
-
-                            // insert failed - slot has been changed by another thread
-                            // retry updating
-                            stats().onUpdateRetry();
                         }
                     }
-                    else
-                        stats().onSlotChanged();
-                } // rcu_lock
+                    else {
+                        // the slot is empty, try to insert data node
+                        if (bInsert) {
+                            node_ptr pNull;
+                            if ( pos.pArr->nodes[pos.nSlot].compare_exchange_strong(pNull, node_ptr(&val), memory_model::memory_order_release, atomics::memory_order_relaxed))
+                            {
+                                // the new data node has been inserted
+                                f(val, nullptr);
+                                ++m_ItemCounter;
+                                stats().onUpdateNew();
+                                stats().height( pos.nHeight );
+                                return std::make_pair(true, true);
+                            }
+                        }
+                        else {
+                            stats().onUpdateFailed();
+                            return std::make_pair(false, false);
+                        }
+
+                        // insert failed - slot has been changed by another thread
+                        // retry updating
+                        stats().onUpdateRetry();
+                    }
+                }
+                else
+                    stats().onSlotChanged();
             } // while
 
             // update success