Refactored Set_DelOdd MT-test
[libcds.git] / cds / intrusive / michael_set_rcu.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_RCU_H
4 #define CDSLIB_INTRUSIVE_MICHAEL_SET_RCU_H
5
6 #include <cds/intrusive/details/michael_set_base.h>
7 #include <cds/details/allocator.h>
8
9 namespace cds { namespace intrusive {
10
11     /// Michael's hash set, \ref cds_urcu_desc "RCU" specialization
12     /** @ingroup cds_intrusive_map
13         \anchor cds_intrusive_MichaelHashSet_rcu
14
15         Source:
16             - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
17
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.
22
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
28             the ordered list.
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.
31
32         \par Usage
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:
36             \code
37             #include <cds/urcu/general_buffered.h>
38             #include <cds/intrusive/michael_list_rcu.h>
39             #include <cds/intrusive/michael_set_rcu.h>
40
41             struct Foo { ... };
42             // Hash functor for struct Foo
43             struct foo_hash {
44                 size_t operator()( Foo const& foo ) const { return ... }
45             };
46
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;
49
50             // Declare Michael's set with MichaelList as bucket type
51             typedef cds::intrusive::MichaelSet<
52                 cds::urcu::gc< general_buffered<> >,
53                 rcu_michael_list,
54                 cds::intrusive::michael_set::make_traits<
55                     cds::opt::::hash< foo_hash >
56                 >::type
57             > rcu_michael_set;
58
59             // Declares hash set for 1000000 items with load factor 2
60             rcu_michael_set theSet( 1000000, 2 );
61
62             // Now you can use theSet object in many threads without any synchronization.
63             \endcode
64     */
65     template <
66         class RCU,
67         class OrderedList,
68 #ifdef CDS_DOXYGEN_INVOKED
69         class Traits = michael_set::traits
70 #else
71         class Traits
72 #endif
73     >
74     class MichaelHashSet< cds::urcu::gc< RCU >, OrderedList, Traits >
75     {
76     public:
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
80
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
84
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
88
89         /// Bucket table allocator
90         typedef cds::details::Allocator< bucket_type, typename traits::allocator >  bucket_table_allocator;
91
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;
97
98         //@cond
99         typedef cds::intrusive::michael_set::implementation_tag implementation_tag;
100         //@endcond
101
102     protected:
103         item_counter    m_ItemCounter;   ///< Item counter
104         hash            m_HashFunctor;   ///< Hash functor
105         bucket_type *   m_Buckets;       ///< bucket table
106
107     private:
108         //@cond
109         const size_t m_nHashBitmask;
110         //@endcond
111
112     protected:
113         //@cond
114         /// Calculates hash value of \p key
115         template <typename Q>
116         size_t hash_value( Q const& key ) const
117         {
118             return m_HashFunctor( key ) & m_nHashBitmask;
119         }
120
121         /// Returns the bucket (ordered list) for \p key
122         template <typename Q>
123         bucket_type& bucket( Q const& key )
124         {
125             return m_Buckets[ hash_value( key ) ];
126         }
127         template <typename Q>
128         bucket_type const& bucket( Q const& key ) const
129         {
130             return m_Buckets[ hash_value( key ) ];
131         }
132         //@endcond
133
134     public:
135         /// Forward iterator
136         /**
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
140         */
141         typedef michael_set::details::iterator< bucket_type, false >    iterator;
142
143         /// Const forward iterator
144         /**
145             For iterator's features and requirements see \ref iterator
146         */
147         typedef michael_set::details::iterator< bucket_type, true >     const_iterator;
148
149         /// Returns a forward iterator addressing the first element in a set
150         /**
151             For empty set \code begin() == end() \endcode
152         */
153         iterator begin()
154         {
155             return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
156         }
157
158         /// Returns an iterator that addresses the location succeeding the last element in a set
159         /**
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
163         */
164         iterator end()
165         {
166             return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
167         }
168
169         /// Returns a forward const iterator addressing the first element in a set
170         //@{
171         const_iterator begin() const
172         {
173             return cbegin();
174         }
175         const_iterator cbegin() const
176         {
177             return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
178         }
179         //@}
180
181         /// Returns an const iterator that addresses the location succeeding the last element in a set
182         //@{
183         const_iterator end() const
184         {
185             return cend();
186         }
187         const_iterator cend() const
188         {
189             return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
190         }
191         //@}
192
193     public:
194         /// Initialize hash set
195         /**
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.
201         */
202         MichaelHashSet(
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 ))
206         {
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");
209
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");
213
214             m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
215         }
216
217         /// Clear hash set and destroy it
218         ~MichaelHashSet()
219         {
220             clear();
221             bucket_table_allocator().Delete( m_Buckets, bucket_count() );
222         }
223
224         /// Inserts new node
225         /**
226             The function inserts \p val in the set if it does not contain
227             an item with key equal to \p val.
228
229             Returns \p true if \p val is placed into the set, \p false otherwise.
230         */
231         bool insert( value_type& val )
232         {
233             bool bRet = bucket( val ).insert( val );
234             if ( bRet )
235                 ++m_ItemCounter;
236             return bRet;
237         }
238
239         /// Inserts new node
240         /**
241             This function is intended for derived non-intrusive containers.
242
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.
247
248             The functor signature is:
249             \code
250                 void func( value_type& val );
251             \endcode
252             where \p val is the item inserted.
253             The user-defined functor is called only if the inserting is success.
254
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
257             synchronization.
258         */
259         template <typename Func>
260         bool insert( value_type& val, Func f )
261         {
262             bool bRet = bucket( val ).insert( val, f );
263             if ( bRet )
264                 ++m_ItemCounter;
265             return bRet;
266         }
267
268         /// Updates the element
269         /**
270             The operation performs inserting or changing data with lock-free manner.
271
272             If the item \p val not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
273             Otherwise, the functor \p func is called with item found.
274             The functor signature is:
275             \code
276                 struct functor {
277                     void operator()( bool bNew, value_type& item, value_type& val );
278                 };
279             \endcode
280             with arguments:
281             - \p bNew - \p true if the item has been inserted, \p false otherwise
282             - \p item - item of the set
283             - \p val - argument \p val passed into the \p %update() function
284             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
285             refers to the same thing.
286
287             The functor may change non-key fields of the \p item.
288
289             Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
290             \p second is \p true if new item has been added or \p false if the item with \p key
291             already is in the set.
292
293             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
294             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
295             synchronization.
296         */
297         template <typename Func>
298         std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
299         {
300             std::pair<bool, bool> bRet = bucket( val ).update( val, func, bAllowInsert );
301             if ( bRet.second )
302                 ++m_ItemCounter;
303             return bRet;
304         }
305         //@cond
306         template <typename Func>
307         CDS_DEPRECATED("ensure() is deprecated, use update()")
308         std::pair<bool, bool> ensure( value_type& val, Func func )
309         {
310             return update( val, func, true );
311         }
312         //@endcond
313
314         /// Unlinks the item \p val from the set
315         /**
316             The function searches the item \p val in the set and unlink it from the set
317             if it is found and is equal to \p val.
318
319             The function returns \p true if success and \p false otherwise.
320         */
321         bool unlink( value_type& val )
322         {
323             bool bRet = bucket( val ).unlink( val );
324             if ( bRet )
325                 --m_ItemCounter;
326             return bRet;
327         }
328
329         /// Deletes the item from the set
330         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase
331             The function searches an item with key equal to \p key in the set,
332             unlinks it from the set, and returns \p true.
333             If the item with key equal to \p key is not found the function return \p false.
334
335             Note the hash functor should accept a parameter of type \p Q that may be not the same as \p value_type.
336         */
337         template <typename Q>
338         bool erase( Q const& key )
339         {
340             if ( bucket( key ).erase( key )) {
341                 --m_ItemCounter;
342                 return true;
343             }
344             return false;
345         }
346
347         /// Deletes the item from the set using \p pred predicate for searching
348         /**
349             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase "erase(Q const&)"
350             but \p pred is used for key comparing.
351             \p Less functor has the interface like \p std::less.
352             \p pred must imply the same element order as the comparator used for building the set.
353         */
354         template <typename Q, typename Less>
355         bool erase_with( Q const& key, Less pred )
356         {
357             if ( bucket( key ).erase_with( key, pred )) {
358                 --m_ItemCounter;
359                 return true;
360             }
361             return false;
362         }
363
364         /// Deletes the item from the set
365         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase_func
366             The function searches an item with key equal to \p key in the set,
367             call \p f functor with item found, and unlinks it from the set.
368             The \ref disposer specified in \p OrderedList class template parameter is called
369             by garbage collector \p GC asynchronously.
370
371             The \p Func interface is
372             \code
373             struct functor {
374                 void operator()( value_type const& item );
375             };
376             \endcode
377
378             If the item with key equal to \p key is not found the function return \p false.
379
380             Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
381         */
382         template <typename Q, typename Func>
383         bool erase( const Q& key, Func f )
384         {
385             if ( bucket( key ).erase( key, f )) {
386                 --m_ItemCounter;
387                 return true;
388             }
389             return false;
390         }
391
392         /// Deletes the item from the set using \p pred predicate for searching
393         /**
394             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase_func "erase(Q const&)"
395             but \p pred is used for key comparing.
396             \p Less functor has the interface like \p std::less.
397             \p pred must imply the same element order as the comparator used for building the set.
398         */
399         template <typename Q, typename Less, typename Func>
400         bool erase_with( const Q& key, Less pred, Func f )
401         {
402             if ( bucket( key ).erase_with( key, pred, f )) {
403                 --m_ItemCounter;
404                 return true;
405             }
406             return false;
407         }
408
409         /// Extracts an item from the set
410         /** \anchor cds_intrusive_MichaelHashSet_rcu_extract
411             The function searches an item with key equal to \p key in the set,
412             unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
413             If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
414
415             Depends on \p bucket_type you should or should not lock RCU before calling of this function:
416             - for the set based on \ref cds_intrusive_MichaelList_rcu "MichaelList" RCU should not be locked
417             - for the set based on \ref cds_intrusive_LazyList_rcu "LazyList" RCU should be locked
418             See ordered list implementation for details.
419
420             \code
421             #include <cds/urcu/general_buffered.h>
422             #include <cds/intrusive/michael_list_rcu.h>
423             #include <cds/intrusive/michael_set_rcu.h>
424
425             typedef cds::urcu::gc< general_buffered<> > rcu;
426             typedef cds::intrusive::MichaelList< rcu, Foo > rcu_michael_list;
427             typedef cds::intrusive::MichaelHashSet< rcu, rcu_michael_list, foo_traits > rcu_michael_set;
428
429             rcu_michael_set theSet;
430             // ...
431
432             typename rcu_michael_set::exempt_ptr p;
433
434             // For MichaelList we should not lock RCU
435
436             // Now, you can apply extract function
437             // Note that you must not delete the item found inside the RCU lock
438             p = theSet.extract( 10 )
439             if ( p ) {
440                 // do something with p
441                 ...
442             }
443
444             // We may safely release p here
445             // release() passes the pointer to RCU reclamation cycle:
446             // it invokes RCU retire_ptr function with the disposer you provided for rcu_michael_list.
447             p.release();
448             \endcode
449         */
450         template <typename Q>
451         exempt_ptr extract( Q const& key )
452         {
453             exempt_ptr p( bucket( key ).extract( key ) );
454             if ( p )
455                 --m_ItemCounter;
456             return p;
457         }
458
459         /// Extracts an item from the set using \p pred predicate for searching
460         /**
461             The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
462             \p Less functor has the interface like \p std::less.
463             \p pred must imply the same element order as the comparator used for building the set.
464         */
465         template <typename Q, typename Less>
466         exempt_ptr extract_with( Q const& key, Less pred )
467         {
468             exempt_ptr p( bucket( key ).extract_with( key, pred ) );
469             if ( p )
470                 --m_ItemCounter;
471             return p;
472         }
473
474         /// Checks whether the set contains \p key
475         /** 
476             The function searches the item with key equal to \p key
477             and returns \p true if the key is found, and \p false otherwise.
478
479             Note the hash functor specified for class \p Traits template parameter
480             should accept a parameter of type \p Q that can be not the same as \p value_type.
481         */
482         template <typename Q>
483         bool contains( Q const& key )
484         {
485             return bucket( key ).contains( key );
486         }
487         //@cond
488         template <typename Q>
489         CDS_DEPRECATED("use contains()")
490         bool find( Q const& key )
491         {
492             return contains( key );
493         }
494         //@endcond
495
496         /// Checks whether the set contains \p key using \p pred predicate for searching
497         /**
498             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
499             \p Less functor has the interface like \p std::less.
500             \p Less must imply the same element order as the comparator used for building the set.
501         */
502         template <typename Q, typename Less>
503         bool contains( Q const& key, Less pred )
504         {
505             return bucket( key ).contains( key, pred );
506         }
507         //@cond
508         template <typename Q, typename Less>
509         CDS_DEPRECATED("use contains()")
510         bool find_with( Q const& key, Less pred )
511         {
512             return contains( key, pred );
513         }
514         //@endcond
515
516         /// Find the key \p key
517         /** \anchor cds_intrusive_MichaelHashSet_rcu_find_func
518             The function searches the item with key equal to \p key and calls the functor \p f for item found.
519             The interface of \p Func functor is:
520             \code
521             struct functor {
522                 void operator()( value_type& item, Q& key );
523             };
524             \endcode
525             where \p item is the item found, \p key is the <tt>find</tt> function argument.
526
527             The functor can change non-key fields of \p item.
528             The functor does not serialize simultaneous access to the set \p item. If such access is
529             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
530
531             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
532             can modify both arguments.
533
534             Note the hash functor specified for class \p Traits template parameter
535             should accept a parameter of type \p Q that can be not the same as \p value_type.
536
537             The function returns \p true if \p key is found, \p false otherwise.
538         */
539         template <typename Q, typename Func>
540         bool find( Q& key, Func f )
541         {
542             return bucket( key ).find( key, f );
543         }
544         //@cond
545         template <typename Q, typename Func>
546         bool find( Q const& key, Func f )
547         {
548             return bucket( key ).find( key, f );
549         }
550         //@endcond
551
552         /// Finds the key \p key using \p pred predicate for searching
553         /**
554             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_func "find(Q&, Func)"
555             but \p pred is used for key comparing.
556             \p Less functor has the interface like \p std::less.
557             \p pred must imply the same element order as the comparator used for building the set.
558         */
559         template <typename Q, typename Less, typename Func>
560         bool find_with( Q& key, Less pred, Func f )
561         {
562             return bucket( key ).find_with( key, pred, f );
563         }
564         //@cond
565         template <typename Q, typename Less, typename Func>
566         bool find_with( Q const& key, Less pred, Func f )
567         {
568             return bucket( key ).find_with( key, pred, f );
569         }
570         //@endcond
571
572         /// Finds the key \p key and return the item found
573         /** \anchor cds_intrusive_MichaelHashSet_rcu_get
574             The function searches the item with key equal to \p key and returns the pointer to item found.
575             If \p key is not found it returns \p nullptr.
576             Note the type of returned value depends on underlying \p bucket_type.
577             For details, see documentation of ordered list you use.
578
579             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
580
581             RCU should be locked before call of this function.
582             Returned item is valid only while RCU is locked:
583             \code
584             typedef cds::intrusive::MichaelHashSet< your_template_parameters > hash_set;
585             hash_set theSet;
586             // ...
587             // Result of get() call
588             typename hash_set::raw_ptr ptr;
589             {
590                 // Lock RCU
591                 hash_set::rcu_lock lock;
592
593                 ptr = theSet.get( 5 );
594                 if ( ptr ) {
595                     // Deal with ptr
596                     //...
597                 }
598                 // Unlock RCU by rcu_lock destructor
599                 // ptr can be reclaimed by disposer at any time after RCU has been unlocked
600             }
601             \endcode
602         */
603         template <typename Q>
604         raw_ptr get( Q const& key )
605         {
606             return bucket( key ).get( key );
607         }
608
609         /// Finds the key \p key and return the item found
610         /**
611             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_get "get(Q const&)"
612             but \p pred is used for comparing the keys.
613
614             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
615             in any order.
616             \p pred must imply the same element order as the comparator used for building the set.
617         */
618         template <typename Q, typename Less>
619         raw_ptr get_with( Q const& key, Less pred )
620         {
621             return bucket( key ).get_with( key, pred );
622         }
623
624         /// Clears the set (non-atomic)
625         /**
626             The function unlink all items from the set.
627             The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
628             If there are a thread that performs insertion while \p clear is working the result is undefined in general case:
629             <tt> empty() </tt> may return \p true but the set may contain item(s).
630             Therefore, \p clear may be used only for debugging purposes.
631
632             For each item the \p disposer is called after unlinking.
633         */
634         void clear()
635         {
636             for ( size_t i = 0; i < bucket_count(); ++i )
637                 m_Buckets[i].clear();
638             m_ItemCounter.reset();
639         }
640
641
642         /// Checks if the set is empty
643         /**
644             Emptiness is checked by item counting: if item count is zero then the set is empty.
645             Thus, the correct item counting feature is an important part of Michael's set implementation.
646         */
647         bool empty() const
648         {
649             return size() == 0;
650         }
651
652         /// Returns item count in the set
653         size_t size() const
654         {
655             return m_ItemCounter;
656         }
657
658         /// Returns the size of hash table
659         /**
660             Since %MichaelHashSet cannot dynamically extend the hash table size,
661             the value returned is an constant depending on object initialization parameters;
662             see \ref cds_intrusive_MichaelHashSet_hp "MichaelHashSet" for explanation.
663         */
664         size_t bucket_count() const
665         {
666             return m_nHashBitmask + 1;
667         }
668
669     };
670
671 }} // namespace cds::intrusive
672
673 #endif // #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_NOGC_H
674