3 #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_RCU_H
4 #define CDSLIB_INTRUSIVE_MICHAEL_SET_RCU_H
6 #include <cds/intrusive/details/michael_set_base.h>
7 #include <cds/details/allocator.h>
9 namespace cds { namespace intrusive {
11 /// Michael's hash set, \ref cds_urcu_desc "RCU" specialization
12 /** @ingroup cds_intrusive_map
13 \anchor cds_intrusive_MichaelHashSet_rcu
16 - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
18 Michael's hash table algorithm is based on lock-free ordered list and it is very simple.
19 The main structure is an array \p T of size \p M. Each element in \p T is basically a pointer
20 to a hash bucket, implemented as a singly linked list. The array of buckets cannot be dynamically expanded.
21 However, each bucket may contain unbounded number of items.
23 Template parameters are:
24 - \p RCU - one of \ref cds_urcu_gc "RCU type"
25 - \p OrderedList - ordered list implementation used as bucket for hash set, for example, MichaelList, LazyList.
26 The intrusive ordered list implementation specifies the type \p T stored in the hash-set, the reclamation
27 schema \p GC used by hash-set, the comparison functor for the type \p T and other features specific for
29 - \p Traits - type traits, default is \p michael_set::traits.
30 Instead of defining \p Traits struct you can use option-based syntax with \p michael_set::make_traits metafunction.
33 Before including <tt><cds/intrusive/michael_set_rcu.h></tt> you should include appropriate RCU header file,
34 see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
35 For example, for \ref cds_urcu_general_buffered_gc "general-purpose buffered RCU" you should include:
37 #include <cds/urcu/general_buffered.h>
38 #include <cds/intrusive/michael_list_rcu.h>
39 #include <cds/intrusive/michael_set_rcu.h>
42 // Hash functor for struct Foo
44 size_t operator()( Foo const& foo ) const { return ... }
47 // Now, you can declare Michael's list for type Foo and default traits:
48 typedef cds::intrusive::MichaelList<cds::urcu::gc< general_buffered<> >, Foo > rcu_michael_list;
50 // Declare Michael's set with MichaelList as bucket type
51 typedef cds::intrusive::MichaelSet<
52 cds::urcu::gc< general_buffered<> >,
54 cds::intrusive::michael_set::make_traits<
55 cds::opt::::hash< foo_hash >
59 // Declares hash set for 1000000 items with load factor 2
60 rcu_michael_set theSet( 1000000, 2 );
62 // Now you can use theSet object in many threads without any synchronization.
68 #ifdef CDS_DOXYGEN_INVOKED
69 class Traits = michael_set::traits
74 class MichaelHashSet< cds::urcu::gc< RCU >, OrderedList, Traits >
77 typedef cds::urcu::gc< RCU > gc; ///< RCU schema
78 typedef OrderedList bucket_type; ///< type of ordered list used as a bucket implementation
79 typedef Traits traits; ///< Set traits
81 typedef typename bucket_type::value_type value_type ; ///< type of value stored in the list
82 typedef typename bucket_type::key_comparator key_comparator ; ///< key comparing functor
83 typedef typename bucket_type::disposer disposer ; ///< Node disposer functor
85 /// Hash functor for \ref value_type and all its derivatives that you use
86 typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
87 typedef typename traits::item_counter item_counter; ///< Item counter type
89 /// Bucket table allocator
90 typedef cds::details::Allocator< bucket_type, typename traits::allocator > bucket_table_allocator;
92 typedef typename bucket_type::rcu_lock rcu_lock; ///< RCU scoped lock
93 typedef typename bucket_type::exempt_ptr exempt_ptr; ///< pointer to extracted node
94 typedef typename bucket_type::raw_ptr raw_ptr; ///< Return type of \p get() member function and its derivatives
95 /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
96 static CDS_CONSTEXPR const bool c_bExtractLockExternal = bucket_type::c_bExtractLockExternal;
99 typedef cds::intrusive::michael_set::implementation_tag implementation_tag;
103 item_counter m_ItemCounter; ///< Item counter
104 hash m_HashFunctor; ///< Hash functor
105 bucket_type * m_Buckets; ///< bucket table
109 const size_t m_nHashBitmask;
114 /// Calculates hash value of \p key
115 template <typename Q>
116 size_t hash_value( Q const& key ) const
118 return m_HashFunctor( key ) & m_nHashBitmask;
121 /// Returns the bucket (ordered list) for \p key
122 template <typename Q>
123 bucket_type& bucket( Q const& key )
125 return m_Buckets[ hash_value( key ) ];
127 template <typename Q>
128 bucket_type const& bucket( Q const& key ) const
130 return m_Buckets[ hash_value( key ) ];
137 The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
138 - it has no post-increment operator
139 - it iterates items in unordered fashion
141 typedef michael_set::details::iterator< bucket_type, false > iterator;
143 /// Const forward iterator
145 For iterator's features and requirements see \ref iterator
147 typedef michael_set::details::iterator< bucket_type, true > const_iterator;
149 /// Returns a forward iterator addressing the first element in a set
151 For empty set \code begin() == end() \endcode
155 return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
158 /// Returns an iterator that addresses the location succeeding the last element in a set
160 Do not use the value returned by <tt>end</tt> function to access any item.
161 The returned value can be used only to control reaching the end of the set.
162 For empty set \code begin() == end() \endcode
166 return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
169 /// Returns a forward const iterator addressing the first element in a set
171 const_iterator begin() const
175 const_iterator cbegin() const
177 return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
181 /// Returns an const iterator that addresses the location succeeding the last element in a set
183 const_iterator end() const
187 const_iterator cend() const
189 return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
194 /// Initialize hash set
196 The Michael's hash set is an unbounded container, but its hash table is non-expandable.
197 At construction time you should pass estimated maximum item count and a load factor.
198 The load factor is average size of one bucket - a small number between 1 and 10.
199 The bucket is an ordered single-linked list, the complexity of searching in the bucket is linear <tt>O(nLoadFactor)</tt>.
200 The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
203 size_t nMaxItemCount, ///< estimation of max item count in the hash set
204 size_t nLoadFactor ///< load factor: average size of the bucket
205 ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
207 // GC and OrderedList::gc must be the same
208 static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
210 // atomicity::empty_item_counter is not allowed as a item counter
211 static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
212 "atomicity::empty_item_counter is not allowed as a item counter");
214 m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
217 /// Clear hash set and destroy it
221 bucket_table_allocator().Delete( m_Buckets, bucket_count() );
226 The function inserts \p val in the set if it does not contain
227 an item with key equal to \p val.
229 Returns \p true if \p val is placed into the set, \p false otherwise.
231 bool insert( value_type& val )
233 bool bRet = bucket( val ).insert( val );
241 This function is intended for derived non-intrusive containers.
243 The function allows to split creating of new item into two part:
244 - create item with key only
245 - insert new item into the set
246 - if inserting is success, calls \p f functor to initialize value-field of \p val.
248 The functor signature is:
250 void func( value_type& val );
252 where \p val is the item inserted.
253 The user-defined functor is called only if the inserting is success.
255 @warning For \ref cds_intrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
256 \ref cds_intrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
259 template <typename Func>
260 bool insert( value_type& val, Func f )
262 bool bRet = bucket( val ).insert( val, f );
268 /// Ensures that the \p item exists in the set
270 The operation performs inserting or changing data with lock-free manner.
272 If the item \p val not found in the set, then \p val is inserted into the set.
273 Otherwise, the functor \p func is called with item found.
274 The functor signature is:
276 void func( bool bNew, value_type& item, value_type& val );
279 - \p bNew - \p true if the item has been inserted, \p false otherwise
280 - \p item - item of the set
281 - \p val - argument \p val passed into the \p ensure function
282 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
283 refers to the same thing.
285 The functor can change non-key fields of the \p item.
287 Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
288 \p second is \p true if new item has been added or \p false if the item with \p key
289 already is in the set.
291 @warning For \ref cds_intrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
292 \ref cds_intrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
295 template <typename Func>
296 std::pair<bool, bool> ensure( value_type& val, Func func )
298 std::pair<bool, bool> bRet = bucket( val ).ensure( val, func );
299 if ( bRet.first && bRet.second )
304 /// Unlinks the item \p val from the set
306 The function searches the item \p val in the set and unlink it from the set
307 if it is found and is equal to \p val.
309 The function returns \p true if success and \p false otherwise.
311 bool unlink( value_type& val )
313 bool bRet = bucket( val ).unlink( val );
319 /// Deletes the item from the set
320 /** \anchor cds_intrusive_MichaelHashSet_rcu_erase
321 The function searches an item with key equal to \p key in the set,
322 unlinks it from the set, and returns \p true.
323 If the item with key equal to \p key is not found the function return \p false.
325 Note the hash functor should accept a parameter of type \p Q that may be not the same as \p value_type.
327 template <typename Q>
328 bool erase( Q const& key )
330 if ( bucket( key ).erase( key )) {
337 /// Deletes the item from the set using \p pred predicate for searching
339 The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase "erase(Q const&)"
340 but \p pred is used for key comparing.
341 \p Less functor has the interface like \p std::less.
342 \p pred must imply the same element order as the comparator used for building the set.
344 template <typename Q, typename Less>
345 bool erase_with( Q const& key, Less pred )
347 if ( bucket( key ).erase_with( key, pred )) {
354 /// Deletes the item from the set
355 /** \anchor cds_intrusive_MichaelHashSet_rcu_erase_func
356 The function searches an item with key equal to \p key in the set,
357 call \p f functor with item found, and unlinks it from the set.
358 The \ref disposer specified in \p OrderedList class template parameter is called
359 by garbage collector \p GC asynchronously.
361 The \p Func interface is
364 void operator()( value_type const& item );
368 If the item with key equal to \p key is not found the function return \p false.
370 Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
372 template <typename Q, typename Func>
373 bool erase( const Q& key, Func f )
375 if ( bucket( key ).erase( key, f )) {
382 /// Deletes the item from the set using \p pred predicate for searching
384 The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase_func "erase(Q const&)"
385 but \p pred is used for key comparing.
386 \p Less functor has the interface like \p std::less.
387 \p pred must imply the same element order as the comparator used for building the set.
389 template <typename Q, typename Less, typename Func>
390 bool erase_with( const Q& key, Less pred, Func f )
392 if ( bucket( key ).erase_with( key, pred, f )) {
399 /// Extracts an item from the set
400 /** \anchor cds_intrusive_MichaelHashSet_rcu_extract
401 The function searches an item with key equal to \p key in the set,
402 unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
403 If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
405 Depends on \p bucket_type you should or should not lock RCU before calling of this function:
406 - for the set based on \ref cds_intrusive_MichaelList_rcu "MichaelList" RCU should not be locked
407 - for the set based on \ref cds_intrusive_LazyList_rcu "LazyList" RCU should be locked
408 See ordered list implementation for details.
411 #include <cds/urcu/general_buffered.h>
412 #include <cds/intrusive/michael_list_rcu.h>
413 #include <cds/intrusive/michael_set_rcu.h>
415 typedef cds::urcu::gc< general_buffered<> > rcu;
416 typedef cds::intrusive::MichaelList< rcu, Foo > rcu_michael_list;
417 typedef cds::intrusive::MichaelHashSet< rcu, rcu_michael_list, foo_traits > rcu_michael_set;
419 rcu_michael_set theSet;
422 typename rcu_michael_set::exempt_ptr p;
424 // For MichaelList we should not lock RCU
426 // Now, you can apply extract function
427 // Note that you must not delete the item found inside the RCU lock
428 p = theSet.extract( 10 )
430 // do something with p
434 // We may safely release p here
435 // release() passes the pointer to RCU reclamation cycle:
436 // it invokes RCU retire_ptr function with the disposer you provided for rcu_michael_list.
440 template <typename Q>
441 exempt_ptr extract( Q const& key )
443 exempt_ptr p( bucket( key ).extract( key ) );
449 /// Extracts an item from the set using \p pred predicate for searching
451 The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
452 \p Less functor has the interface like \p std::less.
453 \p pred must imply the same element order as the comparator used for building the set.
455 template <typename Q, typename Less>
456 exempt_ptr extract_with( Q const& key, Less pred )
458 exempt_ptr p( bucket( key ).extract_with( key, pred ) );
464 /// Finds the key \p key
465 /** \anchor cds_intrusive_MichaelHashSet_rcu_find_val
466 The function searches the item with key equal to \p key
467 and returns \p true if \p key found or \p false otherwise.
469 template <typename Q>
470 bool find( Q const& key )
472 return bucket( key ).find( key );
475 /// Finds the key \p key using \p pred predicate for searching
477 The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_val "find(Q const&)"
478 but \p pred is used for key comparing.
479 \p Less functor has the interface like \p std::less.
480 \p pred must imply the same element order as the comparator used for building the set.
482 template <typename Q, typename Less>
483 bool find_with( Q const& key, Less pred )
485 return bucket( key ).find_with( key, pred );
488 /// Find the key \p key
489 /** \anchor cds_intrusive_MichaelHashSet_rcu_find_func
490 The function searches the item with key equal to \p key and calls the functor \p f for item found.
491 The interface of \p Func functor is:
494 void operator()( value_type& item, Q& key );
497 where \p item is the item found, \p key is the <tt>find</tt> function argument.
499 The functor can change non-key fields of \p item.
500 The functor does not serialize simultaneous access to the set \p item. If such access is
501 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
503 The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
504 can modify both arguments.
506 Note the hash functor specified for class \p Traits template parameter
507 should accept a parameter of type \p Q that can be not the same as \p value_type.
509 The function returns \p true if \p key is found, \p false otherwise.
511 template <typename Q, typename Func>
512 bool find( Q& key, Func f )
514 return bucket( key ).find( key, f );
517 template <typename Q, typename Func>
518 bool find( Q const& key, Func f )
520 return bucket( key ).find( key, f );
524 /// Finds the key \p key using \p pred predicate for searching
526 The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_func "find(Q&, Func)"
527 but \p pred is used for key comparing.
528 \p Less functor has the interface like \p std::less.
529 \p pred must imply the same element order as the comparator used for building the set.
531 template <typename Q, typename Less, typename Func>
532 bool find_with( Q& key, Less pred, Func f )
534 return bucket( key ).find_with( key, pred, f );
537 template <typename Q, typename Less, typename Func>
538 bool find_with( Q const& key, Less pred, Func f )
540 return bucket( key ).find_with( key, pred, f );
544 /// Finds the key \p key and return the item found
545 /** \anchor cds_intrusive_MichaelHashSet_rcu_get
546 The function searches the item with key equal to \p key and returns the pointer to item found.
547 If \p key is not found it returns \p nullptr.
548 Note the type of returned value depends on underlying \p bucket_type.
549 For details, see documentation of ordered list you use.
551 Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
553 RCU should be locked before call of this function.
554 Returned item is valid only while RCU is locked:
556 typedef cds::intrusive::MichaelHashSet< your_template_parameters > hash_set;
559 // Result of get() call
560 typename hash_set::raw_ptr ptr;
563 hash_set::rcu_lock lock;
565 ptr = theSet.get( 5 );
570 // Unlock RCU by rcu_lock destructor
571 // ptr can be reclaimed by disposer at any time after RCU has been unlocked
575 template <typename Q>
576 raw_ptr get( Q const& key )
578 return bucket( key ).get( key );
581 /// Finds the key \p key and return the item found
583 The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_get "get(Q const&)"
584 but \p pred is used for comparing the keys.
586 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
588 \p pred must imply the same element order as the comparator used for building the set.
590 template <typename Q, typename Less>
591 raw_ptr get_with( Q const& key, Less pred )
593 return bucket( key ).get_with( key, pred );
596 /// Clears the set (non-atomic)
598 The function unlink all items from the set.
599 The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
600 If there are a thread that performs insertion while \p clear is working the result is undefined in general case:
601 <tt> empty() </tt> may return \p true but the set may contain item(s).
602 Therefore, \p clear may be used only for debugging purposes.
604 For each item the \p disposer is called after unlinking.
608 for ( size_t i = 0; i < bucket_count(); ++i )
609 m_Buckets[i].clear();
610 m_ItemCounter.reset();
614 /// Checks if the set is empty
616 Emptiness is checked by item counting: if item count is zero then the set is empty.
617 Thus, the correct item counting feature is an important part of Michael's set implementation.
624 /// Returns item count in the set
627 return m_ItemCounter;
630 /// Returns the size of hash table
632 Since %MichaelHashSet cannot dynamically extend the hash table size,
633 the value returned is an constant depending on object initialization parameters;
634 see \ref cds_intrusive_MichaelHashSet_hp "MichaelHashSet" for explanation.
636 size_t bucket_count() const
638 return m_nHashBitmask + 1;
643 }} // namespace cds::intrusive
645 #endif // #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_NOGC_H