[draft] implemented container::MultiLevelHashSet
[libcds.git] / cds / container / skip_list_map_rcu.h
index c5abe34aa234f448381206b3b2faa389d57bf93d..70185d7fe8a296ee1d78f598b5481a972b9b6e83 100644 (file)
@@ -144,17 +144,37 @@ namespace cds { namespace container {
         /// pointer to extracted node
         using exempt_ptr = cds::urcu::exempt_ptr< gc, node_type, value_type, typename maker::intrusive_type_traits::disposer >;
 
+    private:
+        //@cond
+        struct raw_ptr_converter
+        {
+            value_type * operator()( node_type * p ) const
+            {
+               return p ? &p->m_Value : nullptr;
+            }
+
+            value_type& operator()( node_type& n ) const
+            {
+                return n.m_Value;
+            }
+
+            value_type const& operator()( node_type const& n ) const
+            {
+                return n.m_Value;
+            }
+        };
+        //@endcond
+
+    public:
+        /// Result of \p get(), \p get_with() functions - pointer to the node found
+        typedef cds::urcu::raw_ptr_adaptor< value_type, typename base_class::raw_ptr, raw_ptr_converter > raw_ptr;
+
     protected:
         //@cond
         unsigned int random_level()
         {
             return base_class::random_level();
         }
-
-        value_type * to_value_ptr( node_type * pNode ) const CDS_NOEXCEPT
-        {
-            return pNode ? &pNode->m_Value : nullptr;
-        }
         //@endcond
 
     public:
@@ -306,46 +326,55 @@ namespace cds { namespace container {
             return false;
         }
 
-        /// Ensures that the \p key exists in the map
+        /// Updates data by \p key
         /**
             The operation performs inserting or changing data with lock-free manner.
 
             If the \p key not found in the map, then the new item created from \p key
-            is inserted into the map (note that in this case the \ref key_type should be
-            constructible from type \p K).
-            Otherwise, the functor \p func is called with item found.
+            is inserted into the map iff \p bInsert is \p true.
+            Otherwise, if \p key found, the functor \p func is called with item found.
             The functor \p Func interface is:
             \code
                 struct my_functor {
                     void operator()( bool bNew, value_type& item );
                 };
             \endcode
-            with arguments:
+            where:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
-            - \p item - item of the list
+            - \p item - item of the map
 
             The functor may change any fields of \p item.second.
 
             RCU \p synchronize() method can be called. RCU should not be locked.
 
-            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.
+            Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
+            \p second is \p true if new item has been added or \p false if the item with \p key
+            already exists.
 
             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
         */
         template <typename K, typename Func>
-        std::pair<bool, bool> ensure( K const& key, Func func )
+        std::pair<bool, bool> update( K const& key, Func func, bool bInsert = true )
         {
             scoped_node_ptr pNode( node_allocator().New( random_level(), key ));
-            std::pair<bool, bool> res = base_class::ensure( *pNode,
-                [&func](bool bNew, node_type& item, node_type const& ){ func( bNew, item.m_Value ); }
+            std::pair<bool, bool> res = base_class::update( *pNode,
+                [&func](bool bNew, node_type& item, node_type const& ){ func( bNew, item.m_Value );},
+                bInsert
             );
             if ( res.first && res.second )
                 pNode.release();
             return res;
         }
 
+        //@cond
+        // Deprecated, use update()
+        template <typename K, typename Func>
+        std::pair<bool, bool> ensure( K const& key, Func func )
+        {
+            return update( key, func, true );
+        }
+        //@endcond
+
         /// Delete \p key from the map
         /**\anchor cds_nonintrusive_SkipListMap_rcu_erase_val
 
@@ -548,8 +577,8 @@ namespace cds { namespace container {
 
         /// Finds the key \p key and return the item found
         /** \anchor cds_nonintrusive_SkipListMap_rcu_get
-            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.
+            The function searches the item with key equal to \p key and returns a \p raw_ptr object pointing to an item found.
+            If \p key is not found it returns empty \p raw_ptr.
 
             Note the compare functor in \p Traits class' template argument
             should accept a parameter of type \p K that can be not the same as \p key_type.
@@ -560,26 +589,25 @@ namespace cds { namespace container {
             typedef cds::container::SkipListMap< cds::urcu::gc< cds::urcu::general_buffered<> >, int, foo, my_traits > skip_list;
             skip_list theList;
             // ...
+            typename skip_list::raw_ptr pVal;
             {
                 // Lock RCU
                 skip_list::rcu_lock lock;
 
-                skip_list::value_type * pVal = theList.get( 5 );
-                // Deal with pVal
-                //...
-
-                // Unlock RCU by rcu_lock destructor
-                // pVal can be freed at any time after RCU unlocking
+                pVal = theList.get( 5 );
+                if ( pVal ) {
+                    // Deal with pVal
+                    //...
+                }
             }
+            // You can manually release pVal after RCU-locked section
+            pVal.release();
             \endcode
-
-            After RCU unlocking the \p %force_dispose member function can be called manually,
-            see \ref force_dispose for explanation.
         */
         template <typename K>
-        value_type * get( K const& key )
+        raw_ptr get( K const& key )
         {
-            return to_value_ptr( base_class::get( key ));
+            return raw_ptr( base_class::get( key ));
         }
 
         /// Finds the key \p key and return the item found
@@ -592,10 +620,10 @@ namespace cds { namespace container {
             \p pred must imply the same element order as the comparator used for building the map.
         */
         template <typename K, typename Less>
-        value_type * get_with( K const& key, Less pred )
+        raw_ptr get_with( K const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return to_value_ptr( base_class::get_with( key, cds::details::predicate_wrapper< node_type, Less, typename maker::key_accessor >() ));
+            return raw_ptr( base_class::get_with( key, cds::details::predicate_wrapper< node_type, Less, typename maker::key_accessor >() ));
         }
 
         /// Clears the map (not atomic)
@@ -624,14 +652,6 @@ namespace cds { namespace container {
         {
             return base_class::statistics();
         }
-
-        /// Clears internal list of ready-to-delete items passing them to RCU reclamation cycle
-        /** @copydetails cds_intrusive_SkipListSet_rcu_force_dispose
-        */
-        void force_dispose()
-        {
-            return base_class::force_dispose();
-        }
     };
 }} // namespace cds::container