3 #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_RCU_H
4 #define CDSLIB_CONTAINER_SPLIT_LIST_SET_RCU_H
6 #include <cds/intrusive/split_list_rcu.h>
7 #include <cds/container/details/make_split_list_set.h>
9 namespace cds { namespace container {
11 /// Split-ordered list set (template specialization for \ref cds_urcu_desc "RCU")
12 /** @ingroup cds_nonintrusive_set
13 \anchor cds_nonintrusive_SplitListSet_rcu
15 Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
16 - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
17 - [2008] Nir Shavit "The Art of Multiprocessor Programming"
19 See \p intrusive::SplitListSet for a brief description of the split-list algorithm.
22 - \p RCU - one of \ref cds_urcu_gc "RCU type"
23 - \p T - type of the value to be stored in the split-list.
24 - \p Traits - type traits, default is \p split_list::traits. Instead of declaring \p split_list::traits -based
25 struct you can apply option-based notation with \p split_list::make_traits metafunction.
29 The class supports a forward iterator (\ref iterator and \ref const_iterator).
30 The iteration is unordered.
32 You may iterate over split-list set items only under RCU lock.
33 Only in this case the iterator is thread-safe since
34 while RCU is locked any set's item cannot be reclaimed.
36 @warning The iterator object cannot be passed between threads
38 \warning Due to concurrent nature of skip-list set it is not guarantee that you can iterate
39 all elements in the set: any concurrent deletion can exclude the element
40 pointed by the iterator from the set, and your iteration can be terminated
41 before end of the set. Therefore, such iteration is more suitable for debugging purposes
43 The iterator class supports the following minimalistic interface:
50 iterator( iterator const& s);
52 value_type * operator ->() const;
53 value_type& operator *() const;
56 iterator& operator ++();
59 iterator& operator = (const iterator& src);
61 bool operator ==(iterator const& i ) const;
62 bool operator !=(iterator const& i ) const;
65 Note, the iterator object returned by \p end(), \p cend() member functions points to \p nullptr and should not be dereferenced.
69 You should decide what garbage collector you want, and what ordered list you want to use. Split-ordered list
70 is an original data structure based on an ordered list. Suppose, you want construct split-list set based on \p cds::urcu::general_buffered<> GC
71 and \p LazyList as ordered list implementation. So, you beginning your program with following include:
73 #include <cds/urcu/general_buffered.h>
74 #include <cds/container/lazy_list_rcu.h>
75 #include <cds/container/split_list_set_rcu.h>
77 namespace cc = cds::container;
79 // The data belonged to split-ordered list
81 int nKey; // key field
82 std::string strValue ; // value field
85 The inclusion order is important:
86 - first, include one of \ref cds_urcu_gc "RCU implementation" (<tt>cds/urcu/general_buffered.h</tt> in our case)
87 - second, include file for ordered-list implementation (for this example, <tt>cds/container/lazy_list_rcu.h</tt>),
88 - then, the header for RCU-based split-list set <tt>cds/container/split_list_set_rcu.h</tt>.
90 Now, you should declare traits for split-list set. The main parts of traits are a hash functor for the set and a comparing functor for ordered list.
91 Note that we define several function in \p foo_hash and \p foo_less functors for different argument types since we want call our \p %SplitListSet
92 object by the key of type \p int and by the value of type \p foo.
94 The second attention: instead of using \p %LazyList in \p %SplitListSet traits we use \p cds::contaner::lazy_list_tag tag for the lazy list.
95 The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
96 into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
101 size_t operator()( int key ) const { return std::hash( key ) ; }
102 size_t operator()( foo const& item ) const { return std::hash( item.nKey ) ; }
107 bool operator()(int i, foo const& f ) const { return i < f.nKey ; }
108 bool operator()(foo const& f, int i ) const { return f.nKey < i ; }
109 bool operator()(foo const& f1, foo const& f2) const { return f1.nKey < f2.nKey; }
112 // SplitListSet traits
113 struct foo_set_traits: public cc::split_list::traits
115 typedef cc::lazy_list_tag ordered_list ; // what type of ordered list we want to use
116 typedef foo_hash hash ; // hash functor for our data stored in split-list set
118 // Type traits for our LazyList class
119 struct ordered_list_traits: public cc::lazy_list::traits
121 typedef foo_less less ; // use our foo_less as comparator to order list nodes
126 Now you are ready to declare our set class based on \p %SplitListSet:
128 typedef cc::SplitListSet< cds::urcu::gc<cds::urcu::general_buffered<> >, foo, foo_set_traits > foo_set;
131 You may use the modern option-based declaration instead of classic type-traits-based one:
133 typedef cc:SplitListSet<
134 cds::urcu::gc<cds::urcu::general_buffered<> > // RCU type used
135 ,foo // type of data stored
136 ,cc::split_list::make_traits< // metafunction to build split-list traits
137 cc::split_list::ordered_list<cc::lazy_list_tag> // tag for underlying ordered list implementation
138 ,cc::opt::hash< foo_hash > // hash functor
139 ,cc::split_list::ordered_list_traits< // ordered list traits
140 cc::lazy_list::make_traits< // metafunction to build lazy list traits
141 cc::opt::less< foo_less > // less-based compare functor
147 In case of option-based declaration using \p split_list::make_traits metafunction
148 the struct \p foo_set_traits is not required.
150 Now, the set of type \p foo_set is ready to use in your program.
152 Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
153 from \p container::split_list::traits.
154 There are many other options for deep tuning of the split-list and ordered-list containers.
159 #ifdef CDS_DOXYGEN_INVOKED
160 class Traits = split_list::traits
165 class SplitListSet< cds::urcu::gc< RCU >, T, Traits >:
166 #ifdef CDS_DOXYGEN_INVOKED
167 protected intrusive::SplitListSet< cds::urcu::gc< RCU >, T, typename Traits::ordered_list, Traits >
169 protected details::make_split_list_set< cds::urcu::gc< RCU >, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> >::type
174 typedef details::make_split_list_set< cds::urcu::gc< RCU >, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> > maker;
175 typedef typename maker::type base_class;
179 typedef cds::urcu::gc< RCU > gc; ///< RCU-based garbage collector
180 typedef T value_type; ///< Type of value to be storedin the set
181 typedef Traits traits; ///< \p Traits template argument
183 typedef typename maker::ordered_list ordered_list; ///< Underlying ordered list class
184 typedef typename base_class::key_comparator key_comparator; ///< key compare functor
186 /// Hash functor for \ref value_type and all its derivatives that you use
187 typedef typename base_class::hash hash;
188 typedef typename base_class::item_counter item_counter; ///< Item counter type
189 typedef typename base_class::stat stat; ///< Internal statistics
191 typedef typename base_class::rcu_lock rcu_lock ; ///< RCU scoped lock
192 /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
193 static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal;
197 typedef typename maker::cxx_node_allocator cxx_node_allocator;
198 typedef typename maker::node_type node_type;
202 /// pointer to extracted node
203 using exempt_ptr = cds::urcu::exempt_ptr< gc, node_type, value_type, typename maker::ordered_list_traits::disposer >;
207 template <typename Q, typename Func>
208 bool find_( Q& val, Func f )
210 return base_class::find( val, [&f]( node_type& item, Q& val ) { f(item.m_Value, val) ; } );
213 template <typename Q, typename Less, typename Func>
214 bool find_with_( Q& val, Less pred, Func f )
217 return base_class::find_with( val, typename maker::template predicate_wrapper<Less>::type(),
218 [&f]( node_type& item, Q& val ) { f(item.m_Value, val) ; } );
221 template <typename Q>
222 static node_type * alloc_node( Q const& v )
224 return cxx_node_allocator().New( v );
227 template <typename... Args>
228 static node_type * alloc_node( Args&&... args )
230 return cxx_node_allocator().MoveNew( std::forward<Args>(args)...);
233 static void free_node( node_type * pNode )
235 cxx_node_allocator().Delete( pNode );
238 struct node_disposer {
239 void operator()( node_type * pNode )
244 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
246 bool insert_node( node_type * pNode )
248 assert( pNode != nullptr );
249 scoped_node_ptr p(pNode);
251 if ( base_class::insert( *pNode ) ) {
263 \p IsConst - constness boolean flag
265 The forward iterator for a split-list has the following features:
266 - it has no post-increment operator
267 - it depends on underlying ordered list iterator
268 - it is safe to iterate only inside RCU critical section
269 - deleting an item pointed by the iterator can cause to deadlock
271 Therefore, the use of iterators in concurrent environment is not good idea.
272 Use it for debug purpose only.
274 template <bool IsConst>
275 class iterator_type: protected base_class::template iterator_type<IsConst>
278 typedef typename base_class::template iterator_type<IsConst> iterator_base_class;
279 friend class SplitListSet;
282 /// Value pointer type (const for const iterator)
283 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
284 /// Value reference type (const for const iterator)
285 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
293 iterator_type( iterator_type const& src )
294 : iterator_base_class( src )
299 explicit iterator_type( iterator_base_class const& src )
300 : iterator_base_class( src )
305 /// Dereference operator
306 value_ptr operator ->() const
308 return &(iterator_base_class::operator->()->m_Value);
311 /// Dereference operator
312 value_ref operator *() const
314 return iterator_base_class::operator*().m_Value;
318 iterator_type& operator ++()
320 iterator_base_class::operator++();
324 /// Assignment operator
325 iterator_type& operator = (iterator_type const& src)
327 iterator_base_class::operator=(src);
331 /// Equality operator
333 bool operator ==(iterator_type<C> const& i ) const
335 return iterator_base_class::operator==(i);
338 /// Equality operator
340 bool operator !=(iterator_type<C> const& i ) const
342 return iterator_base_class::operator!=(i);
347 /// Initializes split-ordered list of default capacity
349 The default capacity is defined in bucket table constructor.
350 See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
351 which selects by \p container::split_list::dynamic_bucket_table option.
357 /// Initializes split-ordered list
359 size_t nItemCount ///< estimated average of item count
360 , size_t nLoadFactor = 1 ///< load factor - average item count per bucket. Small integer up to 8, default is 1.
362 : base_class( nItemCount, nLoadFactor )
366 typedef iterator_type<false> iterator ; ///< Forward iterator
367 typedef iterator_type<true> const_iterator ; ///< Forward const iterator
369 /// Returns a forward iterator addressing the first element in a set
371 For empty set \code begin() == end() \endcode
375 return iterator( base_class::begin() );
378 /// Returns an iterator that addresses the location succeeding the last element in a set
380 Do not use the value returned by <tt>end</tt> function to access any item.
381 The returned value can be used only to control reaching the end of the set.
382 For empty set \code begin() == end() \endcode
386 return iterator( base_class::end() );
389 /// Returns a forward const iterator addressing the first element in a set
390 const_iterator begin() const
394 /// Returns a forward const iterator addressing the first element in a set
395 const_iterator cbegin() const
397 return const_iterator( base_class::cbegin() );
400 /// Returns an const iterator that addresses the location succeeding the last element in a set
401 const_iterator end() const
405 /// Returns an const iterator that addresses the location succeeding the last element in a set
406 const_iterator cend() const
408 return const_iterator( base_class::cend() );
414 The function creates a node with copy of \p val value
415 and then inserts the node created into the set.
417 The type \p Q should contain as minimum the complete key for the node.
418 The object of \p value_type should be constructible from a value of type \p Q.
419 In trivial case, \p Q is equal to \p value_type.
421 The function applies RCU lock internally.
423 Returns \p true if \p val is inserted into the set, \p false otherwise.
425 template <typename Q>
426 bool insert( Q const& val )
428 return insert_node( alloc_node( val ) );
433 The function allows to split creating of new item into two part:
434 - create item with key only
435 - insert new item into the set
436 - if inserting is success, calls \p f functor to initialize value-field of \p val.
438 The functor signature is:
440 void func( value_type& val );
442 where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
443 \p val no any other changes could be made on this set's item by concurrent threads.
444 The user-defined functor is called only if the inserting is success.
446 The function applies RCU lock internally.
448 template <typename Q, typename Func>
449 bool insert( Q const& key, Func f )
451 scoped_node_ptr pNode( alloc_node( key ));
453 if ( base_class::insert( *pNode, [&f](node_type& node) { f( node.m_Value ) ; } )) {
460 /// Inserts data of type \p value_type created from \p args
462 Returns \p true if inserting successful, \p false otherwise.
464 The function applies RCU lock internally.
466 template <typename... Args>
467 bool emplace( Args&&... args )
469 return insert_node( alloc_node( std::forward<Args>(args)...));
472 /// Ensures that the \p val exists in the set
474 The operation performs inserting or changing data with lock-free manner.
476 If the \p val key not found in the set, then the new item created from \p val
477 is inserted into the set. Otherwise, the functor \p func is called with the item found.
478 The functor \p Func signature is:
481 void operator()( bool bNew, value_type& item, const Q& val );
486 - \p bNew - \p true if the item has been inserted, \p false otherwise
487 - \p item - item of the set
488 - \p val - argument \p val passed into the \p %ensure() function
490 The functor may change non-key fields of the \p item; however, \p func must guarantee
491 that during changing no any other modifications could be made on this item by concurrent threads.
493 The function applies RCU lock internally.
495 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
496 \p second is true if new item has been added or \p false if the item with \p key
497 already is in the set.
499 template <typename Q, typename Func>
500 std::pair<bool, bool> ensure( Q const& val, Func func )
502 scoped_node_ptr pNode( alloc_node( val ));
504 std::pair<bool, bool> bRet = base_class::ensure( *pNode,
505 [&func, &val]( bool bNew, node_type& item, node_type const& /*val*/ ) {
506 func( bNew, item.m_Value, val );
508 if ( bRet.first && bRet.second )
513 /// Deletes \p key from the set
514 /** \anchor cds_nonintrusive_SplitListSet_rcu_erase_val
516 Template parameter of type \p Q defines the key type searching in the list.
517 The set item comparator should be able to compare the values of type \p value_type
520 RCU \p synchronize method can be called. RCU should not be locked.
522 Return \p true if key is found and deleted, \p false otherwise
524 template <typename Q>
525 bool erase( Q const& key )
527 return base_class::erase( key );
530 /// Deletes the item from the set using \p pred predicate for searching
532 The function is an analog of \ref cds_nonintrusive_SplitListSet_rcu_erase_val "erase(Q const&)"
533 but \p pred is used for key comparing.
534 \p Less functor has the interface like \p std::less.
535 \p Less must imply the same element order as the comparator used for building the set.
537 template <typename Q, typename Less>
538 bool erase_with( Q const& key, Less pred )
541 return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>::type() );
544 /// Deletes \p key from the set
545 /** \anchor cds_nonintrusive_SplitListSet_rcu_erase_func
547 The function searches an item with key \p key, calls \p f functor
548 and deletes the item. If \p key is not found, the functor is not called.
550 The functor \p Func interface:
553 void operator()(value_type const& val);
557 Template parameter of type \p Q defines the key type searching in the list.
558 The list item comparator should be able to compare the values of the type \p value_type
561 RCU \p synchronize method can be called. RCU should not be locked.
563 Return \p true if key is found and deleted, \p false otherwise
565 template <typename Q, typename Func>
566 bool erase( Q const& key, Func f )
568 return base_class::erase( key, [&f](node_type& node) { f( node.m_Value ); } );
571 /// Deletes the item from the set using \p pred predicate for searching
573 The function is an analog of \ref cds_nonintrusive_SplitListSet_rcu_erase_func "erase(Q const&, Func)"
574 but \p pred is used for key comparing.
575 \p Less functor has the interface like \p std::less.
576 \p Less must imply the same element order as the comparator used for building the set.
578 template <typename Q, typename Less, typename Func>
579 bool erase_with( Q const& key, Less pred, Func f )
582 return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>::type(),
583 [&f](node_type& node) { f( node.m_Value ); } );
586 /// Extracts an item from the set
587 /** \anchor cds_nonintrusive_SplitListSet_rcu_extract
588 The function searches an item with key equal to \p key in the set,
589 unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
590 If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
592 @note The function does NOT call RCU read-side lock or synchronization,
593 and does NOT dispose the item found. It just excludes the item from the set
594 and returns a pointer to item found.
595 You should lock RCU before calling of the function, and you should synchronize RCU
596 outside the RCU lock to free extracted item
599 typedef cds::urcu::gc< general_buffered<> > rcu;
600 typedef cds::container::SplitListSet< rcu, Foo > splitlist_set;
602 splitlist_set theSet;
605 splitlist_set::exempt_ptr p;
607 // first, we should lock RCU
608 splitlist_set::rcu_lock lock;
610 // Now, you can apply extract function
611 // Note that you must not delete the item found inside the RCU lock
612 p = theSet.extract( 10 );
614 // do something with p
619 // We may safely release p here
620 // release() passes the pointer to RCU reclamation cycle
624 template <typename Q>
625 exempt_ptr extract( Q const& key )
627 return exempt_ptr( base_class::extract_( key, key_comparator() ));
630 /// Extracts an item from the set using \p pred predicate for searching
632 The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
633 \p Less functor has the interface like \p std::less.
634 \p pred must imply the same element order as the comparator used for building the set.
636 template <typename Q, typename Less>
637 exempt_ptr extract_with( Q const& key, Less pred )
640 return exempt_ptr( base_class::extract_with_( key, typename maker::template predicate_wrapper<Less>::type()));
643 /// Finds the key \p key
644 /** \anchor cds_nonintrusive_SplitListSet_rcu_find_func
646 The function searches the item with key equal to \p key and calls the functor \p f for item found.
647 The interface of \p Func functor is:
650 void operator()( value_type& item, Q& key );
653 where \p item is the item found, \p key is the <tt>find</tt> function argument.
655 The functor may change non-key fields of \p item. Note that the functor is only guarantee
656 that \p item cannot be disposed during functor is executing.
657 The functor does not serialize simultaneous access to the set's \p item. If such access is
658 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
660 Note the hash functor specified for class \p Traits template parameter
661 should accept a parameter of type \p Q that can be not the same as \p value_type.
663 The function makes RCU lock internally.
665 The function returns \p true if \p key is found, \p false otherwise.
667 template <typename Q, typename Func>
668 bool find( Q& key, Func f )
670 return find_( key, f );
673 template <typename Q, typename Func>
674 bool find( Q const& key, Func f )
676 return find_( key, f );
680 /// Finds the key \p key using \p pred predicate for searching
682 The function is an analog of \ref cds_nonintrusive_SplitListSet_rcu_find_func "find(Q&, Func)"
683 but \p pred is used for key comparing.
684 \p Less functor has the interface like \p std::less.
685 \p Less must imply the same element order as the comparator used for building the set.
687 template <typename Q, typename Less, typename Func>
688 bool find_with( Q& key, Less pred, Func f )
690 return find_with_( key, pred, f );
693 template <typename Q, typename Less, typename Func>
694 bool find_with( Q const& key, Less pred, Func f )
696 return find_with_( key, pred, f );
700 /// Finds the key \p key
701 /** \anchor cds_nonintrusive_SplitListSet_rcu_find_val
703 The function searches the item with key equal to \p key
704 and returns \p true if it is found, and \p false otherwise.
706 Note the hash functor specified for class \p Traits template parameter
707 should accept a parameter of type \p Q that can be not the same as \p value_type.
709 The function makes RCU lock internally.
711 template <typename Q>
712 bool find( Q const& key )
714 return base_class::find( key );
717 /// Finds the key \p key using \p pred predicate for searching
719 The function is an analog of \ref cds_nonintrusive_SplitListSet_rcu_find_val "find(Q const&)"
720 but \p pred is used for key comparing.
721 \p Less functor has the interface like \p std::less.
722 \p Less must imply the same element order as the comparator used for building the set.
724 template <typename Q, typename Less>
725 bool find_with( Q const& key, Less pred )
728 return base_class::find_with( key, typename maker::template predicate_wrapper<Less>::type() );
731 /// Finds the key \p key and return the item found
732 /** \anchor cds_nonintrusive_SplitListSet_rcu_get
733 The function searches the item with key equal to \p key and returns the pointer to item found.
734 If \p key is not found it returns \p nullptr.
736 Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
738 RCU should be locked before call of this function.
739 Returned item is valid only while RCU is locked:
741 typedef cds::urcu::gc< general_buffered<> > rcu;
742 typedef cds::container::SplitListSet< rcu, Foo > splitlist_set;
743 splitlist_set theSet;
747 splitlist_set::rcu_lock lock;
749 foo * pVal = theSet.get( 5 );
754 // Unlock RCU by rcu_lock destructor
755 // pVal can be retired by disposer at any time after RCU has been unlocked
759 template <typename Q>
760 value_type * get( Q const& key )
762 node_type * pNode = base_class::get( key );
763 return pNode ? &pNode->m_Value : nullptr;
766 /// Finds the key \p key and return the item found
768 The function is an analog of \ref cds_nonintrusive_SplitListSet_rcu_get "get(Q const&)"
769 but \p pred is used for comparing the keys.
771 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
773 \p pred must imply the same element order as the comparator used for building the set.
775 template <typename Q, typename Less>
776 value_type * get_with( Q const& key, Less pred )
779 node_type * pNode = base_class::get_with( key, typename maker::template predicate_wrapper<Less>::type());
780 return pNode ? &pNode->m_Value : nullptr;
783 /// Clears the set (not atomic)
789 /// Checks if the set is empty
791 Emptiness is checked by item counting: if item count is zero then assume that the set is empty.
792 Thus, the correct item counting feature is an important part of split-list set implementation.
796 return base_class::empty();
799 /// Returns item count in the set
802 return base_class::size();
805 /// Returns internal statistics
806 stat const& statistics() const
808 return base_class::statistics();
811 }} // namespace cds::container
813 #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_RCU_H