issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / cds / container / lazy_kvlist_rcu.h
index 10d47ae441680b977a833eab52d53294c99062df..8b9514ac2cbbb7b165f31ac454248b51baa4954c 100644 (file)
@@ -1,14 +1,12 @@
 //$$CDS-header$$
 
-#ifndef __CDS_CONTAINER_LAZY_KVLIST_RCU_H
-#define __CDS_CONTAINER_LAZY_KVLIST_RCU_H
+#ifndef CDSLIB_CONTAINER_LAZY_KVLIST_RCU_H
+#define CDSLIB_CONTAINER_LAZY_KVLIST_RCU_H
 
 #include <memory>
 #include <cds/container/details/lazy_list_base.h>
 #include <cds/intrusive/lazy_list_rcu.h>
 #include <cds/container/details/make_lazy_kvlist.h>
-#include <cds/ref.h>
-#include <cds/details/functor_wrapper.h>
 
 namespace cds { namespace container {
 
@@ -16,7 +14,7 @@ namespace cds { namespace container {
     /** @ingroup cds_nonintrusive_list
         \anchor cds_nonintrusive_LazyKVList_rcu
 
-        This is key-value variation of non-intrusive LazyList.
+        This is key-value variation of non-intrusive \p %LazyList.
         Like standard container, this implementation split a value stored into two part -
         constant key and alterable value.
 
@@ -25,67 +23,55 @@ namespace cds { namespace container {
 
         Template arguments:
         - \p RCU - one of \ref cds_urcu_gc "RCU type"
-        - \p Key - key type of an item stored in the list. It should be copy-constructible
-        - \p Value - value type stored in the list
-        - \p Traits - type traits, default is lazy_list::type_traits
+        - \p Key - key type of an item to be stored in the list. It should be copy-constructible
+        - \p Value - value type to be stored in the list
+        - \p Traits - type traits, default is \p lazy_list::traits
+            It is possible to declare option-based list with \p lazy_list::make_traits metafunction istead of \p Traits template
+            argument. For example, the following traits-based declaration of \p gc::HP lazy list
+            \code
+            #include <cds/urcu/general_threaded.h>
+            #include <cds/container/lazy_kvlist_rcu.h>
+            // Declare comparator for the item
+            struct my_compare {
+                int operator ()( int i1, int i2 )
+                {
+                    return i1 - i2;
+                }
+            };
 
-        It is possible to declare option-based list with cds::container::lazy_list::make_traits metafunction istead of \p Traits template
-        argument. For example, the following traits-based declaration of gc::HP lazy list
-        @note Before including <tt><cds/container/lazy_kvlist_rcu.h></tt> you should include appropriate RCU header file,
-        see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
-        \code
-        #include <cds/urcu/general_threaded.h>
-        #include <cds/container/lazy_kvlist_rcu.h>
-        // Declare comparator for the item
-        struct my_compare {
-            int operator ()( int i1, int i2 )
+            // Declare traits
+            struct my_traits: public cds::container::lazy_list::traits
             {
-                return i1 - i2;
-            }
-        };
+                typedef my_compare compare;
+            };
 
-        // Declare type_traits
-        struct my_traits: public cds::container::lazy_list::type_traits
-        {
-            typedef my_compare compare;
-        };
+            // Declare traits-based list
+            typedef cds::container::LazyKVList< cds::urcu::gc< cds::urcu::general_threaded<> >, int, int, my_traits >     traits_based_list;
+            \endcode
+            is equal to the following option-based list
+            \code
+            #include <cds/urcu/general_threaded.h>
+            #include <cds/container/lazy_kvlist_rcu.h>
+
+            // my_compare is the same
 
-        // Declare traits-based list
-        typedef cds::container::LazyKVList< cds::urcu::gc< cds::urcu::general_threaded<> >, int, int, my_traits >     traits_based_list;
-        \endcode
-
-        is equivalent for the following option-based list
-        \code
-        #include <cds/urcu/general_threaded.h>
-        #include <cds/container/lazy_kvlist_rcu.h>
-
-        // my_compare is the same
-
-        // Declare option-based list
-        typedef cds::container::LazyKVList< cds::urcu::gc< cds::urcu::general_threaded<> >, int, int,
-            typename cds::container::lazy_list::make_traits<
-                cds::container::opt::compare< my_compare >     // item comparator option
-            >::type
-        >     option_based_list;
-        \endcode
-
-        Template argument list \p Options of cds::container::lazy_list::make_traits metafunction are:
-        - opt::compare - key comparison functor. No default functor is provided.
-            If the option is not specified, the opt::less is used.
-        - opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
-        - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::empty is used.
-        - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
-        - opt::allocator - the allocator used for creating and freeing list's item. Default is \ref CDS_DEFAULT_ALLOCATOR macro.
-        - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
-            or opt::v::sequential_consistent (sequentially consisnent memory model).
-        - opt::rcu_check_deadlock - a deadlock checking policy. Default is opt::v::rcu_throw_deadlock
+            // Declare option-based list
+            typedef cds::container::LazyKVList< cds::urcu::gc< cds::urcu::general_threaded<> >, int, int,
+                typename cds::container::lazy_list::make_traits<
+                    cds::container::opt::compare< my_compare >     // item comparator option
+                >::type
+            >     option_based_list;
+            \endcode
+
+        @note Before including <tt><cds/container/lazy_kvlist_rcu.h></tt> you should include appropriate RCU header file,
+        see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
     */
     template <
         typename RCU,
         typename Key,
         typename Value,
 #ifdef CDS_DOXYGEN_INVOKED
-        typename Traits = lazy_list::type_traits
+        typename Traits = lazy_list::traits
 #else
         typename Traits
 #endif
@@ -98,47 +84,46 @@ namespace cds { namespace container {
 #endif
     {
         //@cond
-        typedef details::make_lazy_kvlist< cds::urcu::gc<RCU>, Key, Value, Traits > options;
-        typedef typename options::type  base_class;
+        typedef details::make_lazy_kvlist< cds::urcu::gc<RCU>, Key, Value, Traits > maker;
+        typedef typename maker::type base_class;
         //@endcond
 
     public:
+        typedef cds::urcu::gc<RCU> gc; ///< Garbage collector
 #ifdef CDS_DOXYGEN_INVOKED
         typedef Key                                 key_type        ;   ///< Key type
         typedef Value                               mapped_type     ;   ///< Type of value stored in the list
         typedef std::pair<key_type const, mapped_type> value_type   ;   ///< key/value pair stored in the list
 #else
-        typedef typename options::key_type          key_type;
-        typedef typename options::value_type        mapped_type;
-        typedef typename options::pair_type         value_type;
+        typedef typename maker::key_type    key_type;
+        typedef typename maker::mapped_type mapped_type;
+        typedef typename maker::value_type  value_type;
 #endif
-
-        typedef typename base_class::gc             gc              ;   ///< Garbage collector used
-        typedef typename base_class::back_off       back_off        ;   ///< Back-off strategy used
-        typedef typename options::allocator_type    allocator_type  ;   ///< Allocator type used for allocate/deallocate the nodes
-        typedef typename base_class::item_counter   item_counter    ;   ///< Item counting policy used
-        typedef typename options::key_comparator    key_comparator  ;   ///< key comparison functor
-        typedef typename base_class::memory_model   memory_model    ;   ///< Memory ordering. See cds::opt::memory_model option
+        typedef typename base_class::back_off     back_off;       ///< Back-off strategy used
+        typedef typename maker::allocator_type    allocator_type; ///< Allocator type used for allocate/deallocate the nodes
+        typedef typename base_class::item_counter item_counter;   ///< Item counting policy used
+        typedef typename maker::key_comparator    key_comparator; ///< key comparison functor
+        typedef typename base_class::memory_model memory_model;   ///< Memory ordering. See cds::opt::memory_model option
         typedef typename base_class::rcu_check_deadlock rcu_check_deadlock ; ///< RCU deadlock checking policy
 
         typedef typename gc::scoped_lock    rcu_lock ;  ///< RCU scoped lock
-        static CDS_CONSTEXPR_CONST bool c_bExtractLockExternal = base_class::c_bExtractLockExternal; ///< Group of \p extract_xxx functions require external locking
+        static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal; ///< Group of \p extract_xxx functions require external locking
 
     protected:
         //@cond
-        typedef typename base_class::value_type     node_type;
-        typedef typename options::cxx_allocator     cxx_allocator;
-        typedef typename options::node_deallocator  node_deallocator;
-        typedef typename options::type_traits::compare  intrusive_key_comparator;
+        typedef typename base_class::value_type   node_type;
+        typedef typename maker::cxx_allocator     cxx_allocator;
+        typedef typename maker::node_deallocator  node_deallocator;
+        typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
 
-        typedef typename base_class::node_type      head_type;
+        typedef typename base_class::node_type head_type;
         //@endcond
 
     public:
         /// pointer to extracted node
-        typedef cds::urcu::exempt_ptr< gc, node_type, value_type, typename options::type_traits::disposer,
+        using exempt_ptr = cds::urcu::exempt_ptr< gc, node_type, value_type, typename maker::intrusive_traits::disposer,
             cds::urcu::details::conventional_exempt_pair_cast<node_type, value_type>
-        > exempt_ptr;
+        >;
 
     protected:
         //@cond
@@ -310,7 +295,7 @@ namespace cds { namespace container {
             ++it;   // skip dummy head
             return it;
         }
-        const_iterator cbegin()
+        const_iterator cbegin() const
         {
             const_iterator it( head() );
             ++it;   // skip dummy head
@@ -324,7 +309,7 @@ namespace cds { namespace container {
         {
             return const_iterator( tail());
         }
-        const_iterator cend()
+        const_iterator cend() const
         {
             return const_iterator( tail());
         }
@@ -332,16 +317,10 @@ namespace cds { namespace container {
 
     public:
         /// Default constructor
-        /**
-            Initializes empty list
-        */
         LazyKVList()
         {}
 
-        /// List destructor
-        /**
-            Clears the list
-        */
+        /// Destructor clears the list
         ~LazyKVList()
         {
             clear();
@@ -353,7 +332,7 @@ namespace cds { namespace container {
 
             Preconditions:
             - The \ref key_type should be constructible from value of type \p K.
-                In trivial case, \p K is equal to \ref key_type.
+                In trivial case, \p K is equal to \p key_type.
             - The \ref mapped_type should be default-constructible.
 
             The function makes RCU lock internally.
@@ -371,8 +350,8 @@ namespace cds { namespace container {
             The function creates a node with \p key and value \p val, and then inserts the node created into the list.
 
             Preconditions:
-            - The \ref key_type should be constructible from \p key of type \p K.
-            - The \ref mapped_type should be constructible from \p val of type \p V.
+            - The \p key_type should be constructible from \p key of type \p K.
+            - The \p mapped_type should be constructible from \p val of type \p V.
 
             The function makes RCU lock internally.
 
@@ -396,10 +375,7 @@ namespace cds { namespace container {
 
             The argument \p item of user-defined functor \p func is the reference
             to the list's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
-            User-defined functor \p func should guarantee that during changing item's value no any other changes
-            could be made on this list's item by concurrent threads.
-            The user-defined functor can be passed by reference using <tt>boost::ref</tt>
-            and it is called only if inserting is successful.
+            The user-defined functor is called only if inserting is successful.
 
             The key_type should be constructible from value of type \p K.
 
@@ -412,14 +388,16 @@ namespace cds { namespace container {
             it is preferable that the initialization should be completed only if inserting is successful.
 
             The function makes RCU lock internally.
+
+            @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
         */
         template <typename K, typename Func>
-        bool insert_key( const K& key, Func func )
+        bool insert_with( const K& key, Func func )
         {
-            return insert_key_at( head(), key, func );
+            return insert_with_at( head(), key, func );
         }
 
-        /// Inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
+        /// Inserts data of type \p mapped_type constructed from \p args
         /**
             Returns \p true if inserting successful, \p false otherwise.
 
@@ -454,17 +432,15 @@ namespace cds { namespace container {
             - \p bNew - \p true if the item has been inserted, \p false otherwise
             - \p item - item of the list
 
-            The functor may change any fields of the \p item.second that is \ref mapped_type;
-            however, \p func must guarantee that during changing no any other modifications
-            could be made on this item by concurrent threads.
-
-            You may pass \p func argument by reference using <tt>boost::ref</tt>.
+            The functor may change any fields of the \p item.second of type \p mapped_type.
 
             The function makes RCU lock internally.
 
             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.
+
+            @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
         */
         template <typename K, typename Func>
         std::pair<bool, bool> ensure( const K& key, Func f )
@@ -495,7 +471,8 @@ namespace cds { namespace container {
         template <typename K, typename Less>
         bool erase_with( K const& key, Less pred )
         {
-            return erase_at( head(), key, typename options::template less_wrapper<Less>::type() );
+            CDS_UNUSED( pred );
+            return erase_at( head(), key, typename maker::template less_wrapper<Less>::type() );
         }
 
         /// Deletes \p key from the list
@@ -509,7 +486,6 @@ namespace cds { namespace container {
                 void operator()(value_type& val) { ... }
             };
             \endcode
-            The functor may be passed by reference with <tt>boost:ref</tt>
 
             RCU \p synchronize method can be called. RCU should not be locked.
 
@@ -531,20 +507,21 @@ namespace cds { namespace container {
         template <typename K, typename Less, typename Func>
         bool erase_with( K const& key, Less pred, Func f )
         {
-            return erase_at( head(), key, typename options::template less_wrapper<Less>::type(), f );
+            CDS_UNUSED( pred );
+            return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
         }
 
         /// Extracts an item from the list
         /**
         @anchor cds_nonintrusive_LazyKVList_rcu_extract
             The function searches an item with key equal to \p key in the list,
-            unlinks it from the list, and returns pointer to an item found in \p dest argument.
-            If \p key is not found the function returns \p false.
+            unlinks it from the list, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
+            If \p key is not found the function returns an 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.
+            You should manually lock RCU before calling this function.
 
             \code
             #include <cds/urcu/general_buffered.h>
@@ -563,7 +540,8 @@ namespace cds { namespace container {
 
                 // Now, you can apply extract function
                 // Note that you must not delete the item found inside the RCU lock
-                if ( theList.extract( p, 10 )) {
+                p = theList.extract( 10 );
+                if ( !p ) {
                     // do something with p
                     ...
                 }
@@ -574,10 +552,9 @@ namespace cds { namespace container {
             \endcode
         */
         template <typename K>
-        bool extract( exempt_ptr& dest, K const& key )
+        exempt_ptr extract( K const& key )
         {
-            dest = extract_at( head(), key, intrusive_key_comparator() );
-            return !dest.empty();
+            return exempt_ptr( extract_at( head(), key, intrusive_key_comparator()));
         }
 
         /// Extracts an item from the list using \p pred predicate for searching
@@ -588,10 +565,10 @@ namespace cds { namespace container {
             \p pred must imply the same element order as \ref key_comparator.
         */
         template <typename K, typename Less>
-        bool extract_with( exempt_ptr& dest, K const& key, Less pred )
+        exempt_ptr extract_with( K const& key, Less pred )
         {
-            dest = extract_at( head(), key, typename options::template less_wrapper<Less>::type() );
-            return !dest.empty();
+            CDS_UNUSED( pred );
+            return exempt_ptr( extract_at( head(), key, typename maker::template less_wrapper<Less>::type() ));
         }
 
         /// Finds the key \p key
@@ -617,7 +594,8 @@ namespace cds { namespace container {
         template <typename Q, typename Less>
         bool find_with( Q const& key, Less pred ) const
         {
-            return find_at( head(), key, typename options::template less_wrapper<Less>::type() );
+            CDS_UNUSED( pred );
+            return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
         }
 
         /// Finds the key \p key and performs an action with it
@@ -631,8 +609,6 @@ namespace cds { namespace container {
             \endcode
             where \p item is the item found.
 
-            You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
-
             The functor may change <tt>item.second</tt> that is reference to value of node.
             Note that the function is only guarantee that \p item cannot be deleted during functor is executing.
             The function does not serialize simultaneous access to the list \p item. If such access is
@@ -658,7 +634,8 @@ namespace cds { namespace container {
         template <typename Q, typename Less, typename Func>
         bool find_with( Q const& key, Less pred, Func f ) const
         {
-            return find_at( head(), key, typename options::template less_wrapper<Less>::type(), f );
+            CDS_UNUSED( pred );
+            return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
         }
 
         /// Finds \p key and return the item found
@@ -706,7 +683,8 @@ namespace cds { namespace container {
         template <typename K, typename Less>
         value_type * get_with( K const& key, Less pred ) const
         {
-            return get_at( head(), key, typename options::template less_wrapper<Less>::type());
+            CDS_UNUSED( pred );
+            return get_at( head(), key, typename maker::template less_wrapper<Less>::type());
         }
 
         /// Checks if the list is empty
@@ -720,7 +698,7 @@ namespace cds { namespace container {
             The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
             this function always returns 0.
 
-            <b>Warning</b>: even if you use real item counter and it returns 0, this fact is not mean that the list
+            @note Even if you use real item counter and it returns 0, this fact is not mean that the list
             is empty. To check list emptyness use \ref empty() method.
         */
         size_t size() const
@@ -729,9 +707,6 @@ namespace cds { namespace container {
         }
 
         /// Clears the list
-        /**
-            Post-condition: the list is empty
-        */
         void clear()
         {
             base_class::clear();
@@ -765,11 +740,11 @@ namespace cds { namespace container {
         }
 
         template <typename K, typename Func>
-        bool insert_key_at( head_type& refHead, const K& key, Func f )
+        bool insert_with_at( head_type& refHead, const K& key, Func f )
         {
             scoped_node_ptr pNode( alloc_node( key ));
 
-            if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ cds::unref(f)( node.m_Data ); } )) {
+            if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ f( node.m_Data ); } )) {
                 pNode.release();
                 return true;
             }
@@ -791,7 +766,7 @@ namespace cds { namespace container {
         template <typename K, typename Compare, typename Func>
         bool erase_at( head_type& refHead, K const & key, Compare cmp, Func f )
         {
-            return base_class::erase_at( &refHead, key, cmp, [&f](node_type const & node){cds::unref(f)( const_cast<value_type&>(node.m_Data)); });
+            return base_class::erase_at( &refHead, key, cmp, [&f](node_type const & node){f( const_cast<value_type&>(node.m_Data)); });
         }
 
         template <typename K, typename Func>
@@ -800,7 +775,7 @@ namespace cds { namespace container {
             scoped_node_ptr pNode( alloc_node( key ));
 
             std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
-                [&f]( bool bNew, node_type& node, node_type& ){ cds::unref(f)( bNew, node.m_Data ); });
+                [&f]( bool bNew, node_type& node, node_type& ){ f( bNew, node.m_Data ); });
             if ( ret.first && ret.second )
                 pNode.release();
 
@@ -822,7 +797,7 @@ namespace cds { namespace container {
         template <typename K, typename Compare, typename Func>
         bool find_at( head_type& refHead, K& key, Compare cmp, Func f ) const
         {
-            return base_class::find_at( &refHead, key, cmp, [&f]( node_type& node, K& ){ cds::unref(f)( node.m_Data ); });
+            return base_class::find_at( &refHead, key, cmp, [&f]( node_type& node, K& ){ f( node.m_Data ); });
         }
 
         template <typename K, typename Compare>
@@ -837,4 +812,4 @@ namespace cds { namespace container {
 
 }}  // namespace cds::container
 
-#endif  // #ifndef __CDS_CONTAINER_LAZY_KVLIST_RCU_H
+#endif  // #ifndef CDSLIB_CONTAINER_LAZY_KVLIST_RCU_H