3 #ifndef __CDS_INTRUSIVE_MICHAEL_SET_H
4 #define __CDS_INTRUSIVE_MICHAEL_SET_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
12 /** @ingroup cds_intrusive_map
13 \anchor cds_intrusive_MichaelHashSet_hp
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 GC - Garbage collector used. Note the \p GC must be the same as the GC used for \p OrderedList
25 - \p OrderedList - ordered list implementation used as bucket for hash set, for example, \p MichaelList, \p 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. See \p michael_set::traits for explanation.
30 Instead of defining \p Traits struct you can use option-based syntax with \p michael_set::make_traits metafunction.
32 There are several specializations of \p %MichaelHashSet for each GC. You should include:
33 - <tt><cds/intrusive/michael_set_rcu.h></tt> for \ref cds_intrusive_MichaelHashSet_rcu "RCU type"
34 - <tt><cds/intrusive/michael_set_nogc.h></tt> for \ref cds_intrusive_MichaelHashSet_nogc for append-only set
35 - <tt><cds/intrusive/michael_set.h></tt> for \p gc::HP, \p gc::DHP
39 Some member functions of Michael's hash set accept the key parameter of type \p Q which differs from \p value_type.
40 It is expected that type \p Q contains full key of \p value_type, and for equal keys of type \p Q and \p value_type
41 the hash values of these keys must be equal.
42 The hash functor \p Traits::hash should accept parameters of both type:
46 std::string key_; // key field
52 size_t operator()( const std::string& s ) const
54 return std::hash( s );
57 size_t operator()( const Foo& f ) const
59 return (*this)( f.key_ );
66 First, you should define ordered list type to use in your hash set:
68 // For gc::HP-based MichaelList implementation
69 #include <cds/intrusive/michael_list_hp.h>
71 // cds::intrusive::MichaelHashSet declaration
72 #include <cds/intrusive/michael_set.h>
74 // Type of hash-set items
75 struct Foo: public cds::intrusive::michael_list::node< cds::gc::HP >
77 std::string key_ ; // key field
78 unsigned val_ ; // value field
79 // ... other value fields
82 // Declare comparator for the item
85 int operator()( const Foo& f1, const Foo& f2 ) const
87 return f1.key_.compare( f2.key_ );
91 // Declare bucket type for Michael's hash set
92 // The bucket type is any ordered list type like MichaelList, LazyList
93 typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
94 typename cds::intrusive::michael_list::make_traits<
96 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > >
97 // item comparator option
98 ,cds::opt::compare< FooCmp >
103 Second, you should declare Michael's hash set container:
106 // Declare hash functor
107 // Note, the hash functor accepts parameter type Foo and std::string
109 size_t operator()( const Foo& f ) const
111 return cds::opt::v::hash<std::string>()( f.key_ );
113 size_t operator()( const std::string& f ) const
115 return cds::opt::v::hash<std::string>()( f );
119 // Michael's set typedef
120 typedef cds::intrusive::MichaelHashSet<
123 ,typename cds::intrusive::michael_set::make_traits<
124 cds::opt::hash< FooHash >
129 Now, you can use \p Foo_set in your application.
131 Like other intrusive containers, you may build several containers on single item structure:
133 #include <cds/intrusive/michael_list_hp.h>
134 #include <cds/intrusive/michael_list_dhp.h>
135 #include <cds/intrusive/michael_set.h>
141 // The first key is maintained by gc::HP, second key is maintained by gc::DHP garbage collectors
142 // (I don't know what is needed for, but it is correct)
144 : public cds::intrusive::michael_list::node< cds::gc::HP, tag_key1_idx >
145 , public cds::intrusive::michael_list::node< cds::gc::DHP, tag_key2_idx >
147 std::string key1_ ; // first key field
148 unsigned int key2_ ; // second key field
150 // ... value fields and fields for controlling item's lifetime
153 // Declare comparators for the item
156 int operator()( const Foo& f1, const Foo& f2 ) const { return f1.key1_.compare( f2.key1_ ) ; }
160 bool operator()( const Foo& f1, const Foo& f2 ) const { return f1.key2_ < f2.key1_ ; }
163 // Declare bucket type for Michael's hash set indexed by key1_ field and maintained by gc::HP
164 typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
165 typename cds::intrusive::michael_list::make_traits<
167 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP >, tag_key1_idx > >
168 // item comparator option
169 ,cds::opt::compare< Key1Cmp >
173 // Declare bucket type for Michael's hash set indexed by key2_ field and maintained by gc::DHP
174 typedef cds::intrusive::MichaelList< cds::gc::DHP, Foo,
175 typename cds::intrusive::michael_list::make_traits<
177 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP >, tag_key2_idx > >
178 // item comparator option
179 ,cds::opt::less< Key2Less >
183 // Declare hash functor
185 size_t operator()( const Foo& f ) const { return cds::opt::v::hash<std::string>()( f.key1_ ) ; }
186 size_t operator()( const std::string& s ) const { return cds::opt::v::hash<std::string>()( s ) ; }
188 inline size_t Key2Hash( const Foo& f ) { return (size_t) f.key2_ ; }
190 // Michael's set indexed by key1_ field
191 typedef cds::intrusive::MichaelHashSet<
194 ,typename cds::intrusive::michael_set::make_traits<
195 cds::opt::hash< Key1Hash >
199 // Michael's set indexed by key2_ field
200 typedef cds::intrusive::MichaelHashSet<
203 ,typename cds::intrusive::michael_set::make_traits<
204 cds::opt::hash< Key2Hash >
212 #ifdef CDS_DOXYGEN_INVOKED
213 class Traits = michael_set::traits
221 typedef GC gc; ///< Garbage collector
222 typedef OrderedList ordered_list; ///< type of ordered list used as a bucket implementation
223 typedef ordered_list bucket_type; ///< bucket type
224 typedef Traits traits; ///< Set traits
226 typedef typename ordered_list::value_type value_type ; ///< type of value to be stored in the set
227 typedef typename ordered_list::key_comparator key_comparator ; ///< key comparing functor
228 typedef typename ordered_list::disposer disposer ; ///< Node disposer functor
230 /// Hash functor for \p value_type and all its derivatives that you use
231 typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
232 typedef typename traits::item_counter item_counter; ///< Item counter type
234 typedef typename ordered_list::guarded_ptr guarded_ptr; ///< Guarded pointer
236 /// Bucket table allocator
237 typedef cds::details::Allocator< bucket_type, typename traits::allocator > bucket_table_allocator;
240 item_counter m_ItemCounter; ///< Item counter
241 hash m_HashFunctor; ///< Hash functor
242 bucket_type * m_Buckets; ///< bucket table
246 const size_t m_nHashBitmask;
251 /// Calculates hash value of \p key
252 template <typename Q>
253 size_t hash_value( const Q& key ) const
255 return m_HashFunctor( key ) & m_nHashBitmask;
258 /// Returns the bucket (ordered list) for \p key
259 template <typename Q>
260 bucket_type& bucket( const Q& key )
262 return m_Buckets[ hash_value( key ) ];
269 The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
270 - it has no post-increment operator
271 - it iterates items in unordered fashion
272 - The iterator cannot be moved across thread boundary since it may contain GC's guard that is thread-private GC data.
273 - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
274 deleting operations it is no guarantee that you iterate all item in the set.
276 Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator for the concurrent container
277 for debug purpose only.
279 typedef michael_set::details::iterator< bucket_type, false > iterator;
281 /// Const forward iterator
283 For iterator's features and requirements see \ref iterator
285 typedef michael_set::details::iterator< bucket_type, true > const_iterator;
287 /// Returns a forward iterator addressing the first element in a set
289 For empty set \code begin() == end() \endcode
293 return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
296 /// Returns an iterator that addresses the location succeeding the last element in a set
298 Do not use the value returned by <tt>end</tt> function to access any item.
299 The returned value can be used only to control reaching the end of the set.
300 For empty set \code begin() == end() \endcode
304 return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
307 /// Returns a forward const iterator addressing the first element in a set
309 const_iterator begin() const
311 return get_const_begin();
313 const_iterator cbegin() const
315 return get_const_begin();
319 /// Returns an const iterator that addresses the location succeeding the last element in a set
321 const_iterator end() const
323 return get_const_end();
325 const_iterator cend() const
327 return get_const_end();
333 const_iterator get_const_begin() const
335 return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
337 const_iterator get_const_end() const
339 return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
344 /// Initializes hash set
345 /** @anchor cds_intrusive_MichaelHashSet_hp_ctor
346 The Michael's hash set is an unbounded container, but its hash table is non-expandable.
347 At construction time you should pass estimated maximum item count and a load factor.
348 The load factor is average size of one bucket - a small number between 1 and 10.
349 The bucket is an ordered single-linked list, searching in the bucket has linear complexity <tt>O(nLoadFactor)</tt>.
350 The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
353 size_t nMaxItemCount, ///< estimation of max item count in the hash set
354 size_t nLoadFactor ///< load factor: estimation of max number of items in the bucket. Small integer up to 10.
355 ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
357 // GC and OrderedList::gc must be the same
358 static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
360 // atomicity::empty_item_counter is not allowed as a item counter
361 static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
362 "cds::atomicity::empty_item_counter is not allowed as a item counter");
364 m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
367 /// Clears hash set object and destroys it
371 bucket_table_allocator().Delete( m_Buckets, bucket_count() );
376 The function inserts \p val in the set if it does not contain
377 an item with key equal to \p val.
379 Returns \p true if \p val is placed into the set, \p false otherwise.
381 bool insert( value_type& val )
383 bool bRet = bucket( val ).insert( val );
391 This function is intended for derived non-intrusive containers.
393 The function allows to split creating of new item into two part:
394 - create item with key only
395 - insert new item into the set
396 - if inserting is success, calls \p f functor to initialize value-field of \p val.
398 The functor signature is:
400 void func( value_type& val );
402 where \p val is the item inserted.
404 The user-defined functor is called only if the inserting is success.
406 @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
407 \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
410 template <typename Func>
411 bool insert( value_type& val, Func f )
413 bool bRet = bucket( val ).insert( val, f );
419 /// Ensures that the \p val exists in the set
421 The operation performs inserting or changing data with lock-free manner.
423 If the item \p val not found in the set, then \p val is inserted into the set.
424 Otherwise, the functor \p func is called with item found.
425 The functor signature is:
427 void func( bool bNew, value_type& item, value_type& val );
430 - \p bNew - \p true if the item has been inserted, \p false otherwise
431 - \p item - item of the set
432 - \p val - argument \p val passed into the \p ensure function
433 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
434 refers to the same thing.
436 The functor may change non-key fields of the \p item.
438 Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
439 \p second is \p true if new item has been added or \p false if the item with \p key
440 already is in the set.
442 @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
443 \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
446 template <typename Func>
447 std::pair<bool, bool> ensure( value_type& val, Func func )
449 std::pair<bool, bool> bRet = bucket( val ).ensure( val, func );
450 if ( bRet.first && bRet.second )
455 /// Unlinks the item \p val from the set
457 The function searches the item \p val in the set and unlink it
458 if it is found and is equal to \p val.
460 The function returns \p true if success and \p false otherwise.
462 bool unlink( value_type& val )
464 bool bRet = bucket( val ).unlink( val );
470 /// Deletes the item from the set
471 /** \anchor cds_intrusive_MichaelHashSet_hp_erase
472 The function searches an item with key equal to \p key in the set,
473 unlinks it, and returns \p true.
474 If the item with key equal to \p key is not found the function return \p false.
476 Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
478 template <typename Q>
479 bool erase( Q const& key )
481 if ( bucket( key ).erase( key )) {
488 /// Deletes the item from the set using \p pred predicate for searching
490 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase "erase(Q const&)"
491 but \p pred is used for key comparing.
492 \p Less functor has the interface like \p std::less.
493 \p pred must imply the same element order as the comparator used for building the set.
495 template <typename Q, typename Less>
496 bool erase_with( Q const& key, Less pred )
498 if ( bucket( key ).erase_with( key, pred )) {
505 /// Deletes the item from the set
506 /** \anchor cds_intrusive_MichaelHashSet_hp_erase_func
507 The function searches an item with key equal to \p key in the set,
508 call \p f functor with item found, and unlinks it from the set.
509 The \ref disposer specified in \p OrderedList class template parameter is called
510 by garbage collector \p GC asynchronously.
512 The \p Func interface is
515 void operator()( value_type const& item );
519 If the item with key equal to \p key is not found the function return \p false.
521 Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
523 template <typename Q, typename Func>
524 bool erase( const Q& key, Func f )
526 if ( bucket( key ).erase( key, f )) {
533 /// Deletes the item from the set using \p pred predicate for searching
535 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase_func "erase(Q const&, Func)"
536 but \p pred is used for key comparing.
537 \p Less functor has the interface like \p std::less.
538 \p pred must imply the same element order as the comparator used for building the set.
540 template <typename Q, typename Less, typename Func>
541 bool erase_with( const Q& key, Less pred, Func f )
543 if ( bucket( key ).erase_with( key, pred, f )) {
550 /// Extracts the item with specified \p key
551 /** \anchor cds_intrusive_MichaelHashSet_hp_extract
552 The function searches an item with key equal to \p key,
553 unlinks it from the set, and returns it in \p dest parameter.
554 If the item with key equal to \p key is not found the function returns \p false.
556 Note the compare functor should accept a parameter of type \p Q that may be not the same as \p value_type.
558 The \ref disposer specified in \p OrderedList class' template parameter is called automatically
559 by garbage collector \p GC when returned \ref guarded_ptr object will be destroyed or released.
560 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
564 typedef cds::intrusive::MichaelHashSet< your_template_args > michael_set;
568 michael_set::guarded_ptr gp;
569 theSet.extract( gp, 5 );
573 // Destructor of gp releases internal HP guard
577 template <typename Q>
578 bool extract( guarded_ptr& dest, Q const& key )
580 if ( bucket( key ).extract( dest, key )) {
587 /// Extracts the item using compare functor \p pred
589 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_extract "extract(guarded_ptr&, Q const&)"
590 but \p pred predicate is used for key comparing.
592 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
594 \p pred must imply the same element order as the comparator used for building the list.
596 template <typename Q, typename Less>
597 bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
599 if ( bucket( key ).extract_with( dest, key, pred )) {
606 /// Finds the key \p key
607 /** \anchor cds_intrusive_MichaelHashSet_hp_find_func
608 The function searches the item with key equal to \p key and calls the functor \p f for item found.
609 The interface of \p Func functor is:
612 void operator()( value_type& item, Q& key );
615 where \p item is the item found, \p key is the <tt>find</tt> function argument.
617 The functor may change non-key fields of \p item. Note that the functor is only guarantee
618 that \p item cannot be disposed during functor is executing.
619 The functor does not serialize simultaneous access to the set \p item. If such access is
620 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
622 The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
623 may modify both arguments.
625 Note the hash functor specified for class \p Traits template parameter
626 should accept a parameter of type \p Q that can be not the same as \p value_type.
628 The function returns \p true if \p key is found, \p false otherwise.
630 template <typename Q, typename Func>
631 bool find( Q& key, Func f )
633 return bucket( key ).find( key, f );
636 template <typename Q, typename Func>
637 bool find( Q const& key, Func f )
639 return bucket( key ).find( key, f );
643 /// Finds the key \p key using \p pred predicate for searching
645 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_find_func "find(Q&, Func)"
646 but \p pred is used for key comparing.
647 \p Less functor has the interface like \p std::less.
648 \p pred must imply the same element order as the comparator used for building the set.
650 template <typename Q, typename Less, typename Func>
651 bool find_with( Q& key, Less pred, Func f )
653 return bucket( key ).find_with( key, pred, f );
656 template <typename Q, typename Less, typename Func>
657 bool find_with( Q const& key, Less pred, Func f )
659 return bucket( key ).find_with( key, pred, f );
663 /// Finds the key \p key
664 /** \anchor cds_intrusive_MichaelHashSet_hp_find_val
665 The function searches the item with key equal to \p key
666 and returns \p true if it is found, and \p false otherwise.
668 Note the hash functor specified for class \p Traits template parameter
669 should accept a parameter of type \p Q that can be not the same as \p value_type.
671 template <typename Q>
672 bool find( Q const& key )
674 return bucket( key ).find( key );
677 /// Finds the key \p key using \p pred predicate for searching
679 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_find_val "find(Q const&)"
680 but \p pred is used for key comparing.
681 \p Less functor has the interface like \p std::less.
682 \p pred must imply the same element order as the comparator used for building the set.
684 template <typename Q, typename Less>
685 bool find_with( Q const& key, Less pred )
687 return bucket( key ).find_with( key, pred );
690 /// Finds the key \p key and return the item found
691 /** \anchor cds_intrusive_MichaelHashSet_hp_get
692 The function searches the item with key equal to \p key
693 and assigns the item found to guarded pointer \p ptr.
694 The function returns \p true if \p key is found, and \p false otherwise.
695 If \p key is not found the \p ptr parameter is not changed.
697 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
701 typedef cds::intrusive::MichaeHashSet< your_template_params > michael_set;
705 michael_set::guarded_ptr gp;
706 if ( theSet.get( gp, 5 )) {
710 // Destructor of guarded_ptr releases internal HP guard
714 Note the compare functor specified for \p OrderedList template parameter
715 should accept a parameter of type \p Q that can be not the same as \p value_type.
717 template <typename Q>
718 bool get( guarded_ptr& ptr, Q const& key )
720 return bucket( key ).get( ptr, key );
723 /// Finds the key \p key and return the item found
725 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_get "get( guarded_ptr& ptr, Q const&)"
726 but \p pred is used for comparing the keys.
728 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
730 \p pred must imply the same element order as the comparator used for building the set.
732 template <typename Q, typename Less>
733 bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
735 return bucket( key ).get_with( ptr, key, pred );
738 /// Clears the set (non-atomic)
740 The function unlink all items from the set.
741 The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
742 If there are a thread that performs insertion while \p clear is working the result is undefined in general case:
743 <tt> empty() </tt> may return \p true but the set may contain item(s).
744 Therefore, \p clear may be used only for debugging purposes.
746 For each item the \p disposer is called after unlinking.
750 for ( size_t i = 0; i < bucket_count(); ++i )
751 m_Buckets[i].clear();
752 m_ItemCounter.reset();
756 /// Checks if the set is empty
758 Emptiness is checked by item counting: if item count is zero then the set is empty.
759 Thus, the correct item counting feature is an important part of Michael's set implementation.
766 /// Returns item count in the set
769 return m_ItemCounter;
772 /// Returns the size of hash table
774 Since \p %MichaelHashSet cannot dynamically extend the hash table size,
775 the value returned is an constant depending on object initialization parameters,
776 see \p MichaelHashSet::MichaelHashSet.
778 size_t bucket_count() const
780 return m_nHashBitmask + 1;
784 }} // namespace cds::intrusive
786 #endif // ifndef __CDS_INTRUSIVE_MICHAEL_SET_H