Removed trailing whitespaces
[libcds.git] / cds / intrusive / impl / michael_list.h
index 12106f287ed1cba3c4e7aaf503c2a51525a0defd..f66b185ccf791ee17fc1f9fd81e2a709320d0c24 100644 (file)
@@ -1,10 +1,9 @@
 //$$CDS-header$$
 
-#ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
-#define __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#ifndef CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#define CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H
 
 #include <cds/intrusive/details/michael_list_base.h>
-#include <cds/gc/guarded_ptr.h>
 #include <cds/details/make_const_type.h>
 
 namespace cds { namespace intrusive {
@@ -182,7 +181,7 @@ namespace cds { namespace intrusive {
         typedef typename traits::item_counter item_counter;   ///< Item counting policy used
         typedef typename traits::memory_model  memory_model;   ///< Memory ordering. See cds::opt::memory_model option
 
-        typedef cds::gc::guarded_ptr< gc, value_type > guarded_ptr; ///< Guarded pointer
+        typedef typename gc::template guarded_ptr< value_type > guarded_ptr; ///< Guarded pointer
 
         //@cond
         // Rebind traits (split-list support)
@@ -244,7 +243,7 @@ namespace cds { namespace intrusive {
             link_checker::is_empty( pNode );
 
             marked_node_ptr cur(pos.pCur);
-            pNode->m_pNext.store( cur, memory_model::memory_order_relaxed );
+            pNode->m_pNext.store( cur, memory_model::memory_order_release );
             return pos.pPrev->compare_exchange_strong( cur, marked_node_ptr(pNode), memory_model::memory_order_release, atomics::memory_order_relaxed );
         }
 
@@ -259,7 +258,7 @@ namespace cds { namespace intrusive {
                 // physical deletion may be performed by search function if it detects that a node is logically deleted (marked)
                 // CAS may be successful here or in other thread that searching something
                 marked_node_ptr cur(pos.pCur);
-                if ( pos.pPrev->compare_exchange_strong( cur, marked_node_ptr( pos.pNext ), memory_model::memory_order_release, atomics::memory_order_relaxed ))
+                if ( pos.pPrev->compare_exchange_strong( cur, marked_node_ptr( pos.pNext ), memory_model::memory_order_acquire, atomics::memory_order_relaxed ))
                     retire_node( pos.pCur );
                 return true;
             }
@@ -526,11 +525,12 @@ namespace cds { namespace intrusive {
             return insert_at( m_pHead, val, f );
         }
 
-        /// Ensures that the \p val exists in the list
+        /// Updates the node
         /**
             The operation performs inserting or changing data with lock-free manner.
 
-            If the item \p val is not found in the list, then \p val is inserted.
+            If the item \p val is not found in the list, then \p val is inserted
+            iff \p bInsert is \p true.
             Otherwise, the functor \p func is called with item found.
             The functor signature is:
             \code
@@ -539,7 +539,7 @@ namespace cds { namespace intrusive {
             with arguments:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
             - \p item - item of the list
-            - \p val - argument \p val passed into the \p ensure function
+            - \p val - argument \p val passed into the \p update() function
             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
             refers to the same thing.
 
@@ -553,10 +553,19 @@ namespace cds { namespace intrusive {
             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
         */
         template <typename Func>
+        std::pair<bool, bool> update( value_type& val, Func func, bool bInsert = true )
+        {
+            return update_at( m_pHead, val, func, bInsert );
+        }
+
+        //@cond
+        // Deprecated, use update()
+        template <typename Func>
         std::pair<bool, bool> ensure( value_type& val, Func func )
         {
-            return ensure_at( m_pHead, val, func );
+            return update( val, func, true );
         }
+        //@endcond
 
         /// Unlinks the item \p val from the list
         /**
@@ -597,6 +606,7 @@ namespace cds { namespace intrusive {
         template <typename Q, typename Less>
         bool erase_with( Q const& key, Less pred )
         {
+            CDS_UNUSED( pred );
             return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
         }
 
@@ -628,14 +638,15 @@ namespace cds { namespace intrusive {
         template <typename Q, typename Less, typename Func>
         bool erase_with( Q const& key, Less pred, Func f )
         {
+            CDS_UNUSED( pred );
             return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
         }
 
         /// Extracts the item from the list with specified \p key
         /** \anchor cds_intrusive_MichaelList_hp_extract
             The function searches an item with key equal to \p key,
-            unlinks it from the list, and returns it in \p dest parameter.
-            If the item with key equal to \p key is not found the function returns \p false.
+            unlinks it from the list, and returns it as \p guarded_ptr.
+            If \p key is not found returns an empty guarded pointer.
 
             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
 
@@ -649,24 +660,26 @@ namespace cds { namespace intrusive {
             ord_list theList;
             // ...
             {
-                ord_list::guarded_ptr gp;
-                theList.extract( gp, 5 );
-                // Deal with gp
-                // ...
-
+                ord_list::guarded_ptr gp(theList.extract( 5 ));
+                if ( gp ) {
+                    // Deal with gp
+                    // ...
+                }
                 // Destructor of gp releases internal HP guard
             }
             \endcode
         */
         template <typename Q>
-        bool extract( guarded_ptr& dest, Q const& key )
+        guarded_ptr extract( Q const& key )
         {
-            return extract_at( m_pHead, dest.guard(), key, key_comparator() );
+            guarded_ptr gp;
+            extract_at( m_pHead, gp.guard(), key, key_comparator() );
+            return gp;
         }
 
         /// Extracts the item using compare functor \p pred
         /**
-            The function is an analog of \ref cds_intrusive_MichaelList_hp_extract "extract(guarded_ptr&, Q const&)"
+            The function is an analog of \ref cds_intrusive_MichaelList_hp_extract "extract(Q const&)"
             but \p pred predicate is used for key comparing.
 
             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
@@ -674,9 +687,12 @@ namespace cds { namespace intrusive {
             \p pred must imply the same element order as the comparator used for building the list.
         */
         template <typename Q, typename Less>
-        bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
+        guarded_ptr extract_with( Q const& key, Less pred )
         {
-            return extract_at( m_pHead, dest.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+            CDS_UNUSED( pred );
+            guarded_ptr gp;
+            extract_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+            return gp;
         }
 
         /// Finds \p key in the list
@@ -705,6 +721,13 @@ namespace cds { namespace intrusive {
         {
             return find_at( m_pHead, key, key_comparator(), f );
         }
+        //@cond
+        template <typename Q, typename Func>
+        bool find( Q const& key, Func f )
+        {
+            return find_at( m_pHead, key, key_comparator(), f );
+        }
+        //@endcond
 
         /// Finds the \p key using \p pred predicate for searching
         /**
@@ -716,8 +739,17 @@ namespace cds { namespace intrusive {
         template <typename Q, typename Less, typename Func>
         bool find_with( Q& key, Less pred, Func f )
         {
+            CDS_UNUSED( pred );
+            return find_at( m_pHead, key, cds::opt::details::make_comparator_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_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
         }
+        //@endcond
 
         /// Finds the \p key
         /** \anchor cds_intrusive_MichaelList_hp_find_val
@@ -740,15 +772,15 @@ namespace cds { namespace intrusive {
         template <typename Q, typename Less>
         bool find_with( Q const& key, Less pred )
         {
+            CDS_UNUSED( pred );
             return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>() );
         }
 
         /// Finds the \p key and return the item found
         /** \anchor cds_intrusive_MichaelList_hp_get
             The function searches the item with key equal to \p key
-            and assigns the item found to guarded pointer \p ptr.
-            The function returns \p true if \p key is found, and \p false otherwise.
-            If \p key is not found the \p ptr parameter is not changed.
+            and returns it as \p guarded_ptr.
+            If \p key is not found the function returns an empty guarded pointer.
 
             The \ref disposer specified in \p Traits class template parameter is called
             by garbage collector \p GC automatically when returned \ref guarded_ptr object
@@ -761,8 +793,8 @@ namespace cds { namespace intrusive {
             ord_list theList;
             // ...
             {
-                ord_list::guarded_ptr gp;
-                if ( theList.get( gp, 5 )) {
+                ord_list::guarded_ptr gp(theList.get( 5 ));
+                if ( gp ) {
                     // Deal with gp
                     //...
                 }
@@ -774,14 +806,16 @@ namespace cds { namespace intrusive {
             should accept a parameter of type \p Q that can be not the same as \p value_type.
         */
         template <typename Q>
-        bool get( guarded_ptr& ptr, Q const& key )
+        guarded_ptr get( Q const& key )
         {
-            return get_at( m_pHead, ptr.guard(), key, key_comparator() );
+            guarded_ptr gp;
+            get_at( m_pHead, gp.guard(), key, key_comparator() );
+            return gp;
         }
 
         /// Finds the \p key and return the item found
         /**
-            The function is an analog of \ref cds_intrusive_MichaelList_hp_get "get( guarded_ptr& ptr, Q const&)"
+            The function is an analog of \ref cds_intrusive_MichaelList_hp_get "get( Q const&)"
             but \p pred is used for comparing the keys.
 
             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
@@ -789,9 +823,12 @@ namespace cds { namespace intrusive {
             \p pred must imply the same element order as the comparator used for building the list.
         */
         template <typename Q, typename Less>
-        bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
+        guarded_ptr get_with( Q const& key, Less pred )
         {
-            return get_at( m_pHead, ptr.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+            CDS_UNUSED( pred );
+            guarded_ptr gp;
+            get_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+            return gp;
         }
 
         /// Clears the list
@@ -898,7 +935,7 @@ namespace cds { namespace intrusive {
         }
 
         template <typename Func>
-        std::pair<bool, bool> ensure_at( atomic_node_ptr& refHead, value_type& val, Func func )
+        std::pair<bool, bool> update_at( atomic_node_ptr& refHead, value_type& val, Func func, bool bInsert )
         {
             position pos;
 
@@ -915,6 +952,9 @@ namespace cds { namespace intrusive {
                     return std::make_pair( true, false );
                 }
                 else {
+                    if ( !bInsert )
+                        return std::make_pair( false, false );
+
                     typename gc::Guard guard;
                     guard.assign( &val );
                     if ( link_node( pNode, pos ) ) {
@@ -928,6 +968,12 @@ namespace cds { namespace intrusive {
             }
         }
 
+        template <typename Func>
+        std::pair<bool, bool> ensure_at( atomic_node_ptr& refHead, value_type& val, Func func )
+        {
+            return update_at( refHead, val, func, true );
+        }
+
         bool unlink_at( atomic_node_ptr& refHead, value_type& val )
         {
             position pos;
@@ -979,13 +1025,13 @@ namespace cds { namespace intrusive {
         }
 
         template <typename Q, typename Compare>
-        bool extract_at( atomic_node_ptr& refHead, typename gc::Guard& dest, Q const& val, Compare cmp )
+        bool extract_at( atomic_node_ptr& refHead, typename guarded_ptr::native_guard& dest, Q const& val, Compare cmp )
         {
             position pos;
             back_off bkoff;
             while ( search( refHead, val, pos, cmp )) {
                 if ( unlink_node( pos ) ) {
-                    dest.assign( pos.guards.template get<value_type>( position::guard_current_item ) );
+                    dest.set( pos.guards.template get<value_type>( position::guard_current_item ) );
                     --m_ItemCounter;
                     return true;
                 }
@@ -1014,11 +1060,11 @@ namespace cds { namespace intrusive {
         }
 
         template <typename Q, typename Compare>
-        bool get_at( atomic_node_ptr& refHead, typename gc::Guard& guard, Q const& val, Compare cmp )
+        bool get_at( atomic_node_ptr& refHead, typename guarded_ptr::native_guard& guard, Q const& val, Compare cmp )
         {
             position pos;
             if ( search( refHead, val, pos, cmp )) {
-                guard.assign( pos.guards.template get<value_type>( position::guard_current_item ));
+                guard.set( pos.guards.template get<value_type>( position::guard_current_item ));
                 return true;
             }
             return false;
@@ -1038,31 +1084,30 @@ namespace cds { namespace intrusive {
 
             back_off        bkoff;
 
-try_again:
+        try_again:
             pPrev = &refHead;
             pNext = nullptr;
 
-            pCur = pPrev->load(memory_model::memory_order_relaxed);
-            pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ) );
-            if ( pPrev->load(memory_model::memory_order_acquire) != pCur.ptr() )
-                goto try_again;
+            pCur = pos.guards.protect( position::guard_current_item, *pPrev,
+                   [](marked_node_ptr p) -> value_type *
+                    {
+                        return node_traits::to_value_ptr( p.ptr() );
+                    });
 
             while ( true ) {
                 if ( pCur.ptr() == nullptr ) {
                     pos.pPrev = pPrev;
-                    pos.pCur = pCur.ptr();
-                    pos.pNext = pNext.ptr();
+                    pos.pCur = nullptr;
+                    pos.pNext = nullptr;
                     return false;
                 }
 
-                pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed);
-                pos.guards.assign( position::guard_next_item, node_traits::to_value_ptr( pNext.ptr() ));
-                if ( pCur->m_pNext.load(memory_model::memory_order_relaxed).all() != pNext.all() ) {
-                    bkoff();
-                    goto try_again;
-                }
-
-                if ( pPrev->load(memory_model::memory_order_relaxed).all() != pCur.ptr() ) {
+                pNext = pos.guards.protect( position::guard_next_item, pCur->m_pNext,
+                        [](marked_node_ptr p ) -> value_type *
+                        {
+                            return node_traits::to_value_ptr( p.ptr() );
+                        });
+                if ( pPrev->load(memory_model::memory_order_acquire).all() != pCur.ptr() ) {
                     bkoff();
                     goto try_again;
                 }
@@ -1071,7 +1116,7 @@ try_again:
                 if ( pNext.bits() == 1 ) {
                     // pCur marked i.e. logically deleted. Help the erase/unlink function to unlink pCur node
                     marked_node_ptr cur( pCur.ptr());
-                    if ( pPrev->compare_exchange_strong( cur, marked_node_ptr( pNext.ptr() ), memory_model::memory_order_release, atomics::memory_order_relaxed )) {
+                    if ( pPrev->compare_exchange_strong( cur, marked_node_ptr( pNext.ptr() ), memory_model::memory_order_acquire, atomics::memory_order_relaxed )) {
                         retire_node( pCur.ptr() );
                     }
                     else {
@@ -1089,14 +1134,14 @@ try_again:
                         return nCmp == 0;
                     }
                     pPrev = &( pCur->m_pNext );
-                    pos.guards.assign( position::guard_prev_item, node_traits::to_value_ptr( pCur.ptr() ) );
+                    pos.guards.copy( position::guard_prev_item, position::guard_current_item );
                 }
                 pCur = pNext;
-                pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ));
+                pos.guards.copy( position::guard_current_item, position::guard_next_item );
             }
         }
         //@endcond
     };
 }} // namespace cds::intrusive
 
-#endif // #ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#endif // #ifndef CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H