Uses different pass count for different parallel queue test cases
[libcds.git] / cds / intrusive / michael_set.h
index 532ac4de1e40907844ed442985a8789efd4d5558..79b2b8cbf7264f41ea6be1c0df33698c5b95a39a 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/
@@ -271,10 +271,6 @@ namespace cds { namespace intrusive {
         // GC and OrderedList::gc must be the same
         static_assert(std::is_same<gc, typename ordered_list::gc>::value, "GC and OrderedList::gc must be the same");
 
-        // atomicity::empty_item_counter is not allowed as a item counter
-        static_assert(!std::is_same<item_counter, atomicity::empty_item_counter>::value,
-            "cds::atomicity::empty_item_counter is not allowed as a item counter");
-
     protected:
         //@cond
         typedef typename ordered_list::template select_stat_wrapper< typename ordered_list::stat > bucket_stat;
@@ -318,7 +314,6 @@ namespace cds { namespace intrusive {
               Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
               Use this iterator on the concurrent container for debugging purpose only.
             - for \p IterableList: iterator is thread-safe. You may use it freely in concurrent environment.
-              
         */
         typedef michael_set::details::iterator< internal_bucket_type, false > iterator;
 
@@ -334,7 +329,7 @@ namespace cds { namespace intrusive {
         */
         iterator begin()
         {
-            return iterator( m_Buckets[0].begin(), bucket_begin(), bucket_end() );
+            return iterator( m_Buckets[0].begin(), bucket_begin(), bucket_end());
         }
 
         /// Returns an iterator that addresses the location succeeding the last element in a set
@@ -345,7 +340,7 @@ namespace cds { namespace intrusive {
         */
         iterator end()
         {
-            return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end() );
+            return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end());
         }
 
         /// Returns a forward const iterator addressing the first element in a set
@@ -399,7 +394,7 @@ namespace cds { namespace intrusive {
 
             for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it )
                 it->~internal_bucket_type();
-            bucket_table_allocator().deallocate( m_Buckets, bucket_count() );
+            bucket_table_allocator().deallocate( m_Buckets, bucket_count());
         }
 
         /// Inserts new node
@@ -519,7 +514,7 @@ namespace cds { namespace intrusive {
         std::pair<bool, bool> upsert( value_type& val, bool bAllowInsert = true )
 #else
         template <typename Q>
-        typename std::enable_if< 
+        typename std::enable_if<
             std::is_same< Q, value_type>::value && is_iterable_list< ordered_list >::value,
             std::pair<bool, bool>
         >::type
@@ -627,6 +622,34 @@ namespace cds { namespace intrusive {
             return false;
         }
 
+        /// Deletes the item pointed by iterator \p iter (only for \p IterableList based set)
+        /**
+            Returns \p true if the operation is successful, \p false otherwise.
+            The function can return \p false if the node the iterator points to has already been deleted
+            by other thread.
+
+            The function does not invalidate the iterator, it remains valid and can be used for further traversing.
+
+            @note \p %erase_at() is supported only for \p %MichaelHashSet based on \p IterableList.
+        */
+#ifdef CDS_DOXYGEN_INVOKED
+        bool erase_at( iterator const& iter )
+#else
+        template <typename Iterator>
+        typename std::enable_if< std::is_same<Iterator, iterator>::value && is_iterable_list< ordered_list >::value, bool >::type
+        erase_at( Iterator const& iter )
+#endif
+        {
+            assert( iter != end());
+            assert( iter.bucket() != nullptr );
+
+            if ( iter.bucket()->erase_at( iter.underlying_iterator())) {
+                --m_ItemCounter;
+                return true;
+            }
+            return false;
+        }
+
         /// Extracts the item with specified \p key
         /** \anchor cds_intrusive_MichaelHashSet_hp_extract
             The function searches an item with key equal to \p key,
@@ -734,7 +757,7 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find( key );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
             return iterator( it, &b, bucket_end());
         }
@@ -745,9 +768,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find( key );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@endcond
 
@@ -792,9 +815,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find_with( key, pred );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@cond
         template <typename Q, typename Less>
@@ -803,9 +826,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find_with( key, pred );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@endcond
 
@@ -902,8 +925,8 @@ namespace cds { namespace intrusive {
 
         /// Checks if the set is empty
         /**
-            Emptiness is checked by item counting: if item count is zero then the set is empty.
-            Thus, the correct item counting feature is an important part of Michael's set implementation.
+            @warning If you use \p atomicity::empty_item_counter in \p traits::item_counter,
+            the function always returns \p true.
         */
         bool empty() const
         {
@@ -911,6 +934,10 @@ namespace cds { namespace intrusive {
         }
 
         /// Returns item count in the set
+        /**
+            If you use \p atomicity::empty_item_counter in \p traits::item_counter,
+            the function always returns 0.
+        */
         size_t size() const
         {
             return m_ItemCounter;
@@ -947,23 +974,23 @@ namespace cds { namespace intrusive {
 
         const_iterator get_const_begin() const
         {
-            return const_iterator( m_Buckets[0].cbegin(), bucket_begin(), bucket_end() );
+            return const_iterator( m_Buckets[0].cbegin(), bucket_begin(), bucket_end());
         }
         const_iterator get_const_end() const
         {
-            return const_iterator( bucket_end()[-1].cend(), bucket_end() - 1, bucket_end() );
+            return const_iterator( bucket_end()[-1].cend(), bucket_end() - 1, bucket_end());
         }
 
         template <typename Stat>
-        typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
+        typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type * b )
         {
-            new (bucket) internal_bucket_type;
+            new (b) internal_bucket_type;
         }
 
         template <typename Stat>
-        typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
+        typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type * b )
         {
-            new (bucket) internal_bucket_type( m_Stat );
+            new (b) internal_bucket_type( m_Stat );
         }
 
         /// Calculates hash value of \p key