movable exempt_ptr: LazyList
[libcds.git] / cds / intrusive / lazy_list_rcu.h
index 21adc8bead21edd1258c33a1c6da3ddbe739050d..0d89f94085dc3c18c2a089f2a25c30c5094a76f3 100644 (file)
@@ -3,7 +3,8 @@
 #ifndef __CDS_INTRUSIVE_LAZY_LIST_RCU_H
 #define __CDS_INTRUSIVE_LAZY_LIST_RCU_H
 
-#include <cds/intrusive/lazy_list_base.h>
+#include <mutex>        // unique_lock
+#include <cds/intrusive/details/lazy_list_base.h>
 #include <cds/urcu/details/check_deadlock.h>
 #include <cds/details/binary_functor_wrapper.h>
 #include <cds/urcu/exempt_ptr.h>
@@ -58,7 +59,7 @@ namespace cds { namespace intrusive {
         Template arguments:
         - \p RCU - one of \ref cds_urcu_gc "RCU type"
         - \p T - type to be stored in the list
-        - \p Traits - type traits. See lazy_list::type_traits for explanation.
+        - \p Traits - type traits. See \p lazy_list::traits for explanation.
 
         It is possible to declare option-based list with \p %cds::intrusive::lazy_list::make_traits metafunction istead of \p Traits template
         argument. Template argument list \p Options of cds::intrusive::lazy_list::make_traits metafunction are:
@@ -91,7 +92,7 @@ namespace cds { namespace intrusive {
         typename RCU
         ,typename T
 #ifdef CDS_DOXYGEN_INVOKED
-        ,class Traits = lazy_list::type_traits
+        ,class Traits = lazy_list::traits
 #else
         ,class Traits
 #endif
@@ -99,58 +100,58 @@ namespace cds { namespace intrusive {
     class LazyList<cds::urcu::gc<RCU>, T, Traits>
     {
     public:
-        typedef T       value_type      ;   ///< type of value stored in the list
-        typedef Traits  options         ;   ///< Traits template parameter
+        typedef cds::urcu::gc<RCU> gc;      ///< RCU schema
+        typedef T                  value_type;   ///< type of value stored in the list
+        typedef Traits             traits;  ///< Traits template parameter
 
-        typedef typename options::hook      hook        ;   ///< hook type
-        typedef typename hook::node_type    node_type   ;   ///< node type
+        typedef typename traits::hook    hook;      ///< hook type
+        typedef typename hook::node_type node_type; ///< node type
 
 #   ifdef CDS_DOXYGEN_INVOKED
         typedef implementation_defined key_comparator  ;    ///< key compare functor based on opt::compare and opt::less option setter.
 #   else
-        typedef typename opt::details::make_comparator< value_type, options >::type key_comparator;
+        typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
 #   endif
 
-        typedef typename options::disposer  disposer    ;   ///< disposer used
-        typedef typename get_node_traits< value_type, node_type, hook>::type node_traits ;    ///< node traits
-        typedef typename lazy_list::get_link_checker< node_type, options::link_checker >::type link_checker   ;   ///< link checker
+        typedef typename traits::disposer  disposer;   ///< disposer used
+        typedef typename get_node_traits< value_type, node_type, hook>::type node_traits;    ///< node traits
+        typedef typename lazy_list::get_link_checker< node_type, traits::link_checker >::type link_checker;   ///< link checker
 
-        typedef cds::urcu::gc<RCU>                      gc              ;   ///< RCU schema
-        typedef typename options::back_off              back_off        ;   ///< back-off strategy (not used)
-        typedef typename options::item_counter          item_counter    ;   ///< Item counting policy used
-        typedef typename options::memory_model          memory_model    ;   ///< C++ memory ordering (see lazy_list::type_traits::memory_model)
-        typedef typename options::rcu_check_deadlock    rcu_check_deadlock ; ///< Deadlock checking policy
+        typedef typename traits::back_off              back_off;       ///< back-off strategy (not used)
+        typedef typename traits::item_counter          item_counter;   ///< Item counting policy used
+        typedef typename traits::memory_model          memory_model;   ///< C++ memory ordering (see \p lazy_list::traits::memory_model)
+        typedef typename traits::rcu_check_deadlock    rcu_check_deadlock; ///< Deadlock checking policy
 
         typedef typename gc::scoped_lock    rcu_lock ; ///< RCU scoped lock
-        static CDS_CONSTEXPR_CONST bool c_bExtractLockExternal = true; ///< Group of \p extract_xxx functions require external locking
+        static CDS_CONSTEXPR const bool c_bExtractLockExternal = true; ///< Group of \p extract_xxx functions require external locking
 
         //@cond
-        // Rebind options (split-list support)
+        // Rebind traits (split-list support)
         template <typename... Options>
-        struct rebind_options {
+        struct rebind_traits {
             typedef LazyList<
                 gc
                 , value_type
-                , typename cds::opt::make_options< options, Options...>::type
+                , typename cds::opt::make_options< traits, Options...>::type
             >   type;
         };
         //@endcond
 
     protected:
-        typedef typename node_type::marked_ptr  marked_node_ptr ;   ///< Node marked pointer
-        typedef node_type *     auxiliary_head   ;   ///< Auxiliary head type (for split-list support)
+        typedef typename node_type::marked_ptr  marked_node_ptr;   ///< Node marked pointer
+        typedef node_type *     auxiliary_head;   ///< Auxiliary head type (for split-list support)
 
     protected:
-        node_type       m_Head ;            ///< List head (dummy node)
-        node_type       m_Tail;             ///< List tail (dummy node)
-        item_counter    m_ItemCounter   ;   ///< Item counter
+        node_type       m_Head;        ///< List head (dummy node)
+        node_type       m_Tail;        ///< List tail (dummy node)
+        item_counter    m_ItemCounter; ///< Item counter
 
         //@cond
 
         /// Position pointer for item search
         struct position {
-            node_type *     pPred   ;    ///< Previous node
-            node_type *     pCur    ;    ///< Current node
+            node_type *     pPred; ///< Previous node
+            node_type *     pCur;  ///< Current node
 
             /// Locks nodes \p pPred and \p pCur
             void lock()
@@ -222,16 +223,15 @@ namespace cds { namespace intrusive {
             assert( pCur != &m_Tail );
 
             node_type * pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed).ptr();
-            //pCur->m_pNext.store( marked_node_ptr( pNext, 1), memory_model::memory_order_relaxed) ;   // logically deleting
-            pCur->m_pNext.store( marked_node_ptr( pHead, 1 ), memory_model::memory_order_relaxed )    ; // logical deletion + back-link for search
+            pCur->m_pNext.store( marked_node_ptr( pHead, 1 ), memory_model::memory_order_relaxed ); // logical deletion + back-link for search
             pPred->m_pNext.store( marked_node_ptr( pNext ), memory_model::memory_order_relaxed); // physically deleting
-            //pCur->m_pNext.store( marked_node_ptr( pHead, 1 ), memory_model::memory_order_relaxed )    ; // back-link for search
         }
 
         //@endcond
 
     public:
-        typedef cds::urcu::exempt_ptr< gc, value_type, value_type, clear_and_dispose, void > exempt_ptr ; ///< pointer to extracted node
+        /// pointer to extracted node
+        using exempt_ptr = cds::urcu::exempt_ptr< gc, value_type, value_type, clear_and_dispose, void >; 
 
     protected:
         //@cond
@@ -367,7 +367,7 @@ namespace cds { namespace intrusive {
         {
             return get_const_begin();
         }
-        const_iterator cbegin()
+        const_iterator cbegin() const
         {
             return get_const_begin();
         }
@@ -379,7 +379,7 @@ namespace cds { namespace intrusive {
         {
             return get_const_end();
         }
-        const_iterator cend()
+        const_iterator cend() const
         {
             return get_const_end();
         }
@@ -443,8 +443,7 @@ namespace cds { namespace intrusive {
             \endcode
             where \p val is the item inserted.
             While the functor \p f is working the item \p val is locked.
-            The user-defined functor is called only if the inserting is success and may be passed by reference
-            using <tt>boost::ref</tt>.
+            The user-defined functor is called only if the inserting is success.
         */
         template <typename Func>
         bool insert( value_type& val, Func f )
@@ -474,8 +473,6 @@ namespace cds { namespace intrusive {
             The functor may change non-key fields of the \p item.
             While the functor \p f is calling the item \p item is locked.
 
-            You may pass \p func argument by reference using <tt>boost::ref</tt> or cds::ref.
-
             Returns <tt> std::pair<bool, bool>  </tt> where \p first is true if operation is successfull,
             \p second is true if new item has been added or \p false if the item with \p key
             already is in the list.
@@ -512,9 +509,9 @@ namespace cds { namespace intrusive {
 
         /// Deletes the item from the list
         /** \anchor cds_intrusive_LazyList_rcu_find_erase
-            The function searches an item with key equal to \p val in the list,
+            The function searches an item with key equal to \p key in the list,
             unlinks it from the list, and returns \p true.
-            If the item with the key equal to \p val is not found the function return \p false.
+            If the item with the key equal to \p key is not found the function return \p false.
 
             RCU \p synchronize method can be called.
             Note that depending on RCU type used the \ref disposer call can be deferred.
@@ -523,9 +520,9 @@ namespace cds { namespace intrusive {
             deadlock checking policy is opt::v::rcu_throw_deadlock.
         */
         template <typename Q>
-        bool erase( Q const& val )
+        bool erase( Q const& key )
         {
-            return erase_at( &m_Head, val, key_comparator() );
+            return erase_at( &m_Head, key, key_comparator() );
         }
 
         /// Deletes the item from the list using \p pred predicate for searching
@@ -536,15 +533,15 @@ 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 erase_with( Q const& val, Less pred )
+        bool erase_with( Q const& key, Less pred )
         {
-            return erase_at( &m_Head, val, cds::opt::details::make_comparator_from_less<Less>());
+            return erase_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>());
         }
 
 
         /// Deletes the item from the list
         /** \anchor cds_intrusive_LazyList_rcu_find_erase_func
-            The function searches an item with key equal to \p val in the list,
+            The function searches an item with key equal to \p key in the list,
             call \p func functor with item found, unlinks it from the list, and returns \p true.
             The \p Func interface is
             \code
@@ -552,9 +549,8 @@ namespace cds { namespace intrusive {
                 void operator()( value_type const& item );
             };
             \endcode
-            The functor may be passed by reference using <tt>boost:ref</tt>
 
-            If the item with the key equal to \p val is not found the function return \p false.
+            If the item with the key equal to \p key is not found the function return \p false.
 
             RCU \p synchronize method can be called.
             Note that depending on RCU type used the \ref disposer call can be deferred.
@@ -563,9 +559,9 @@ namespace cds { namespace intrusive {
             deadlock checking policy is opt::v::rcu_throw_deadlock.
         */
         template <typename Q, typename Func>
-        bool erase( Q const& val, Func func )
+        bool erase( Q const& key, Func func )
         {
-            return erase_at( &m_Head, val, key_comparator(), func );
+            return erase_at( &m_Head, key, key_comparator(), func );
         }
 
         /// Deletes the item from the list using \p pred predicate for searching
@@ -576,23 +572,22 @@ 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, typename Func>
-        bool erase_with( Q const& val, Less pred, Func func )
+        bool erase_with( Q const& key, Less pred, Func func )
         {
-            return erase_at( &m_Head, val, cds::opt::details::make_comparator_from_less<Less>(), func );
+            return erase_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), func );
         }
 
         /// Extracts an item from the list
         /**
         \anchor cds_intrusive_LazyList_rcu_extract
-            The function searches an item with key equal to \p val in the list,
-            unlinks it from the list, and returns pointer to an item found in \p dest parameter.
-            If the item with the key equal to \p val is not found the function returns \p false,
-            \p dest is empty.
+            The function searches an item with key equal to \p key in the list,
+            unlinks it from the list, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to an item found.
+            If the item is not found the function returns empty \p exempt_ptr.
 
             @note The function does NOT call RCU read-side lock or synchronization,
             and does NOT dispose the item found. It just excludes the item from the list
-            and returns a pointer to item found.
-            You should lock RCU before calling this function, and you should manually synchronize RCU
+            and returns a pointer to it.
+            You should manually lock RCU before calling this function, and you should manually synchronize RCU
             outside the RCU lock region before reusing returned pointer.
 
             \code
@@ -612,7 +607,8 @@ namespace cds { namespace intrusive {
 
                 // Now, you can apply extract function
                 // Note that you must not delete the item found inside the RCU lock
-                if ( theList.extract( p1, 10 )) {
+                p1 = theList.extract(  10 )
+                if ( p1 ) {
                     // do something with p1
                     ...
                 }
@@ -625,10 +621,9 @@ namespace cds { namespace intrusive {
             \endcode
         */
         template <typename Q>
-        bool extract( exempt_ptr& dest, Q const& val )
+        exempt_ptr extract( Q const& key )
         {
-            dest = extract_at( &m_Head, val, key_comparator() );
-            return !dest.empty();
+            return exempt_ptr( extract_at( &m_Head, key, key_comparator() ));
         }
 
         /// Extracts an item from the list using \p pred predicate for searching
@@ -640,38 +635,42 @@ namespace cds { namespace intrusive {
             \p pred must imply the same element order as \ref key_comparator.
         */
         template <typename Q, typename Less>
-        bool extract_with( exempt_ptr& dest, Q const& val, Less pred )
+        exempt_ptr extract_with( Q const& key, Less pred )
         {
-            dest = extract_at( &m_Head, val, cds::opt::details::make_comparator_from_less<Less>() );
-            return !dest.empty();
+            return exempt_ptr( extract_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>() ));
         }
 
-        /// Finds the key \p val
+        /// Finds the key \p key
         /** \anchor cds_intrusive_LazyList_rcu_find_func
-            The function searches the item with key equal to \p val
+            The function searches the item with key equal to \p key
             and calls the functor \p f for item found.
             The interface of \p Func functor is:
             \code
             struct functor {
-                void operator()( value_type& item, Q& val );
+                void operator()( value_type& item, Q& key );
             };
             \endcode
-            where \p item is the item found, \p val is the <tt>find</tt> function argument.
-
-            You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
+            where \p item is the item found, \p key is the <tt>find</tt> function argument.
 
             The functor may change non-key fields of \p item.
             While the functor \p f is calling the item found \p item is locked.
 
-            The function returns \p true if \p val is found, \p false otherwise.
+            The function returns \p true if \p key is found, \p false otherwise.
         */
         template <typename Q, typename Func>
-        bool find( Q& val, Func f ) const
+        bool find( Q& key, Func f ) const
+        {
+            return find_at( const_cast<node_type *>( &m_Head ), key, key_comparator(), f );
+        }
+        //@cond
+        template <typename Q, typename Func>
+        bool find( Q const& key, Func f ) const
         {
-            return find_at( const_cast<node_type *>( &m_Head ), val, key_comparator(), f );
+            return find_at( const_cast<node_type *>(&m_Head), key, key_comparator(), f );
         }
+        //@endcond
 
-        /// Finds the key \p val using \p pred predicate for searching
+        /// Finds the key \p key using \p pred predicate for searching
         /**
             The function is an analog of \ref cds_intrusive_LazyList_rcu_find_func "find(Q&, Func)"
             but \p pred is used for key comparing.
@@ -679,61 +678,30 @@ 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, typename Func>
-        bool find_with( Q& val, Less pred, Func f ) const
+        bool find_with( Q& key, Less pred, Func f ) const
         {
-            return find_at( const_cast<node_type *>( &m_Head ), val, cds::opt::details::make_comparator_from_less<Less>(), f );
+            return find_at( const_cast<node_type *>( &m_Head ), key, cds::opt::details::make_comparator_from_less<Less>(), f );
         }
-
-        /// Finds the key \p val
-        /** \anchor cds_intrusive_LazyList_rcu_find_cfunc
-            The function searches the item with key equal to \p val
-            and calls the functor \p f for item found.
-            The interface of \p Func functor is:
-            \code
-            struct functor {
-                void operator()( value_type& item, Q const& val );
-            };
-            \endcode
-            where \p item is the item found, \p val is the <tt>find</tt> function argument.
-
-            You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
-
-            The functor may change non-key fields of \p item.
-            While the functor \p f is calling the item found \p item is locked.
-
-            The function returns \p true if \p val is found, \p false otherwise.
-        */
-        template <typename Q, typename Func>
-        bool find( Q const& val, Func f ) const
-        {
-            return find_at( const_cast<node_type *>( &m_Head ), val, key_comparator(), f );
-        }
-
-        /// Finds the key \p val using \p pred predicate for searching
-        /**
-            The function is an analog of \ref cds_intrusive_LazyList_rcu_find_cfunc "find(Q&, Func)"
-            but \p pred is used for key comparing.
-            \p Less functor has the interface like \p std::less.
-            \p pred must imply the same element order as the comparator used for building the list.
-        */
+        //@cond
         template <typename Q, typename Less, typename Func>
-        bool find_with( Q const& val, Less pred, Func f ) const
+        bool find_with( Q const& key, Less pred, Func f ) const
         {
-            return find_at( const_cast<node_type *>( &m_Head ), val, cds::opt::details::make_comparator_from_less<Less>(), f );
+            return find_at( const_cast<node_type *>(&m_Head), key, cds::opt::details::make_comparator_from_less<Less>(), f );
         }
+        //@endcond
 
-        /// Finds the key \p val
+        /// Finds the key \p key
         /** \anchor cds_intrusive_LazyList_rcu_find_val
-            The function searches the item with key equal to \p val
-            and returns \p true if \p val found or \p false otherwise.
+            The function searches the item with key equal to \p key
+            and returns \p true if \p key found or \p false otherwise.
         */
         template <typename Q>
-        bool find( Q const& val ) const
+        bool find( Q const& key ) const
         {
-            return find_at( const_cast<node_type *>( &m_Head ), val, key_comparator() );
+            return find_at( const_cast<node_type *>( &m_Head ), key, key_comparator() );
         }
 
-        /// Finds the key \p val using \p pred predicate for searching
+        /// Finds the key \p key using \p pred predicate for searching
         /**
             The function is an analog of \ref cds_intrusive_LazyList_rcu_find_val "find(Q const&)"
             but \p pred is used for key comparing.
@@ -741,15 +709,15 @@ 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 find_with( Q const& val, Less pred ) const
+        bool find_with( Q const& key, Less pred ) const
         {
-            return find_at( const_cast<node_type *>( &m_Head ), val, cds::opt::details::make_comparator_from_less<Less>() );
+            return find_at( const_cast<node_type *>( &m_Head ), key, cds::opt::details::make_comparator_from_less<Less>() );
         }
 
-        /// Finds the key \p val and return the item found
+        /// Finds the key \p key and return the item found
         /** \anchor cds_intrusive_LazyList_rcu_get
-            The function searches the item with key equal to \p val and returns the pointer to item found.
-            If \p val is not found it returns \p nullptr.
+            The function searches the item with key equal to \p key and returns the pointer to item found.
+            If \p key is not found it returns \p nullptr.
 
             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
 
@@ -774,12 +742,12 @@ namespace cds { namespace intrusive {
             \endcode
         */
         template <typename Q>
-        value_type * get( Q const& val ) const
+        value_type * get( Q const& key ) const
         {
-            return get_at( const_cast<node_type *>( &m_Head ), val, key_comparator());
+            return get_at( const_cast<node_type *>( &m_Head ), key, key_comparator());
         }
 
-        /// Finds the key \p val and return the item found
+        /// Finds the key \p key and return the item found
         /**
             The function is an analog of \ref cds_intrusive_LazyList_rcu_get "get(Q const&)"
             but \p pred is used for comparing the keys.
@@ -789,9 +757,9 @@ 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>
-        value_type * get_with( Q const& val, Less pred ) const
+        value_type * get_with( Q const& key, Less pred ) const
         {
-            return get_at( const_cast<node_type *>( &m_Head ), val, cds::opt::details::make_comparator_from_less<Less>());
+            return get_at( const_cast<node_type *>( &m_Head ), key, cds::opt::details::make_comparator_from_less<Less>());
         }
 
         /// Clears the list using default disposer
@@ -917,7 +885,7 @@ namespace cds { namespace intrusive {
                         }
                         else {
                             link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
-                            cds::unref(f)( val );
+                            f( val );
                             ++m_ItemCounter;
                             return true;
                         }
@@ -950,7 +918,7 @@ namespace cds { namespace intrusive {
                         if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
                             // key already in the list
 
-                            cds::unref(func)( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
+                            func( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
                             return std::make_pair( iterator( pos.pCur ), false );
                         }
                         else {
@@ -958,7 +926,7 @@ namespace cds { namespace intrusive {
                             link_checker::is_empty( node_traits::to_node_ptr( val ) );
 
                             link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
-                            cds::unref(func)( true, val, val );
+                            func( true, val, val );
                             ++m_ItemCounter;
                             return std::make_pair( iterator( node_traits::to_node_ptr( val )), true );
                         }
@@ -1030,7 +998,7 @@ namespace cds { namespace intrusive {
                             if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
                                 // key found
                                 unlink_node( pos.pPred, pos.pCur, pHead );
-                                cds::unref(f)( *node_traits::to_value_ptr( *pos.pCur ) );
+                                f( *node_traits::to_value_ptr( *pos.pCur ) );
                                 --m_ItemCounter;
                                 nResult = 1;
                             }
@@ -1105,10 +1073,10 @@ namespace cds { namespace intrusive {
             rcu_lock l( bLock );
             search( pHead, val, pos, cmp );
             if ( pos.pCur != &m_Tail ) {
-                cds::lock::scoped_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
+                std::unique_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
                 if ( cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 )
                 {
-                    cds::unref(f)( *node_traits::to_value_ptr( *pos.pCur ), val );
+                    f( *node_traits::to_value_ptr( *pos.pCur ), val );
                     return true;
                 }
             }
@@ -1131,7 +1099,7 @@ namespace cds { namespace intrusive {
 
             search( pHead, val, pos, cmp );
             if ( pos.pCur != &m_Tail ) {
-                cds::lock::scoped_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
+                std::unique_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
                 if ( cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 )
                 {
                     return const_iterator( pos.pCur );