Merge branch 'integration' into dev
[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         /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
95         static CDS_CONSTEXPR const bool c_bExtractLockExternal = bucket_type::c_bExtractLockExternal;
96
97         //@cond
98         typedef cds::intrusive::michael_set::implementation_tag implementation_tag;
99         //@endcond
100
101     protected:
102         item_counter    m_ItemCounter;   ///< Item counter
103         hash            m_HashFunctor;   ///< Hash functor
104         bucket_type *   m_Buckets;       ///< bucket table
105
106     private:
107         //@cond
108         const size_t m_nHashBitmask;
109         //@endcond
110
111     protected:
112         //@cond
113         /// Calculates hash value of \p key
114         template <typename Q>
115         size_t hash_value( Q const& key ) const
116         {
117             return m_HashFunctor( key ) & m_nHashBitmask;
118         }
119
120         /// Returns the bucket (ordered list) for \p key
121         template <typename Q>
122         bucket_type& bucket( Q const& key )
123         {
124             return m_Buckets[ hash_value( key ) ];
125         }
126         template <typename Q>
127         bucket_type const& bucket( Q const& key ) const
128         {
129             return m_Buckets[ hash_value( key ) ];
130         }
131         //@endcond
132
133     public:
134         /// Forward iterator
135         /**
136             The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
137             - it has no post-increment operator
138             - it iterates items in unordered fashion
139         */
140         typedef michael_set::details::iterator< bucket_type, false >    iterator;
141
142         /// Const forward iterator
143         /**
144             For iterator's features and requirements see \ref iterator
145         */
146         typedef michael_set::details::iterator< bucket_type, true >     const_iterator;
147
148         /// Returns a forward iterator addressing the first element in a set
149         /**
150             For empty set \code begin() == end() \endcode
151         */
152         iterator begin()
153         {
154             return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
155         }
156
157         /// Returns an iterator that addresses the location succeeding the last element in a set
158         /**
159             Do not use the value returned by <tt>end</tt> function to access any item.
160             The returned value can be used only to control reaching the end of the set.
161             For empty set \code begin() == end() \endcode
162         */
163         iterator end()
164         {
165             return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
166         }
167
168         /// Returns a forward const iterator addressing the first element in a set
169         //@{
170         const_iterator begin() const
171         {
172             return cbegin();
173         }
174         const_iterator cbegin() const
175         {
176             return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
177         }
178         //@}
179
180         /// Returns an const iterator that addresses the location succeeding the last element in a set
181         //@{
182         const_iterator end() const
183         {
184             return cend();
185         }
186         const_iterator cend() const
187         {
188             return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
189         }
190         //@}
191
192     public:
193         /// Initialize hash set
194         /**
195             The Michael's hash set is an unbounded container, but its hash table is non-expandable.
196             At construction time you should pass estimated maximum item count and a load factor.
197             The load factor is average size of one bucket - a small number between 1 and 10.
198             The bucket is an ordered single-linked list, the complexity of searching in the bucket is linear <tt>O(nLoadFactor)</tt>.
199             The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
200         */
201         MichaelHashSet(
202             size_t nMaxItemCount,   ///< estimation of max item count in the hash set
203             size_t nLoadFactor      ///< load factor: average size of the bucket
204         ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
205         {
206             // GC and OrderedList::gc must be the same
207             static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
208
209             // atomicity::empty_item_counter is not allowed as a item counter
210             static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
211                 "atomicity::empty_item_counter is not allowed as a item counter");
212
213             m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
214         }
215
216         /// Clear hash set and destroy it
217         ~MichaelHashSet()
218         {
219             clear();
220             bucket_table_allocator().Delete( m_Buckets, bucket_count() );
221         }
222
223         /// Inserts new node
224         /**
225             The function inserts \p val in the set if it does not contain
226             an item with key equal to \p val.
227
228             Returns \p true if \p val is placed into the set, \p false otherwise.
229         */
230         bool insert( value_type& val )
231         {
232             bool bRet = bucket( val ).insert( val );
233             if ( bRet )
234                 ++m_ItemCounter;
235             return bRet;
236         }
237
238         /// Inserts new node
239         /**
240             This function is intended for derived non-intrusive containers.
241
242             The function allows to split creating of new item into two part:
243             - create item with key only
244             - insert new item into the set
245             - if inserting is success, calls  \p f functor to initialize value-field of \p val.
246
247             The functor signature is:
248             \code
249                 void func( value_type& val );
250             \endcode
251             where \p val is the item inserted.
252             The user-defined functor is called only if the inserting is success.
253
254             @warning For \ref cds_intrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
255             \ref cds_intrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
256             synchronization.
257         */
258         template <typename Func>
259         bool insert( value_type& val, Func f )
260         {
261             bool bRet = bucket( val ).insert( val, f );
262             if ( bRet )
263                 ++m_ItemCounter;
264             return bRet;
265         }
266
267         /// Ensures that the \p item exists in the set
268         /**
269             The operation performs inserting or changing data with lock-free manner.
270
271             If the item \p val not found in the set, then \p val is inserted into the set.
272             Otherwise, the functor \p func is called with item found.
273             The functor signature is:
274             \code
275                 void func( bool bNew, value_type& item, value_type& val );
276             \endcode
277             with arguments:
278             - \p bNew - \p true if the item has been inserted, \p false otherwise
279             - \p item - item of the set
280             - \p val - argument \p val passed into the \p ensure function
281             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
282             refers to the same thing.
283
284             The functor can change non-key fields of the \p item.
285
286             Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
287             \p second is \p true if new item has been added or \p false if the item with \p key
288             already is in the set.
289
290             @warning For \ref cds_intrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
291             \ref cds_intrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
292             synchronization.
293             */
294         template <typename Func>
295         std::pair<bool, bool> ensure( value_type& val, Func func )
296         {
297             std::pair<bool, bool> bRet = bucket( val ).ensure( val, func );
298             if ( bRet.first && bRet.second )
299                 ++m_ItemCounter;
300             return bRet;
301         }
302
303         /// Unlinks the item \p val from the set
304         /**
305             The function searches the item \p val in the set and unlink it from the set
306             if it is found and is equal to \p val.
307
308             The function returns \p true if success and \p false otherwise.
309         */
310         bool unlink( value_type& val )
311         {
312             bool bRet = bucket( val ).unlink( val );
313             if ( bRet )
314                 --m_ItemCounter;
315             return bRet;
316         }
317
318         /// Deletes the item from the set
319         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase
320             The function searches an item with key equal to \p key in the set,
321             unlinks it from the set, and returns \p true.
322             If the item with key equal to \p key is not found the function return \p false.
323
324             Note the hash functor should accept a parameter of type \p Q that may be not the same as \p value_type.
325         */
326         template <typename Q>
327         bool erase( Q const& key )
328         {
329             if ( bucket( key ).erase( key )) {
330                 --m_ItemCounter;
331                 return true;
332             }
333             return false;
334         }
335
336         /// Deletes the item from the set using \p pred predicate for searching
337         /**
338             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase "erase(Q const&)"
339             but \p pred is used for key comparing.
340             \p Less functor has the interface like \p std::less.
341             \p pred must imply the same element order as the comparator used for building the set.
342         */
343         template <typename Q, typename Less>
344         bool erase_with( Q const& key, Less pred )
345         {
346             if ( bucket( key ).erase_with( key, pred )) {
347                 --m_ItemCounter;
348                 return true;
349             }
350             return false;
351         }
352
353         /// Deletes the item from the set
354         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase_func
355             The function searches an item with key equal to \p key in the set,
356             call \p f functor with item found, and unlinks it from the set.
357             The \ref disposer specified in \p OrderedList class template parameter is called
358             by garbage collector \p GC asynchronously.
359
360             The \p Func interface is
361             \code
362             struct functor {
363                 void operator()( value_type const& item );
364             };
365             \endcode
366
367             If the item with key equal to \p key is not found the function return \p false.
368
369             Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
370         */
371         template <typename Q, typename Func>
372         bool erase( const Q& key, Func f )
373         {
374             if ( bucket( key ).erase( key, f )) {
375                 --m_ItemCounter;
376                 return true;
377             }
378             return false;
379         }
380
381         /// Deletes the item from the set using \p pred predicate for searching
382         /**
383             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase_func "erase(Q const&)"
384             but \p pred is used for key comparing.
385             \p Less functor has the interface like \p std::less.
386             \p pred must imply the same element order as the comparator used for building the set.
387         */
388         template <typename Q, typename Less, typename Func>
389         bool erase_with( const Q& key, Less pred, Func f )
390         {
391             if ( bucket( key ).erase_with( key, pred, f )) {
392                 --m_ItemCounter;
393                 return true;
394             }
395             return false;
396         }
397
398         /// Extracts an item from the set
399         /** \anchor cds_intrusive_MichaelHashSet_rcu_extract
400             The function searches an item with key equal to \p key in the set,
401             unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
402             If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
403
404             @note The function does NOT call RCU read-side lock or synchronization,
405             and does NOT dispose the item found. It just excludes the item from the set
406             and returns a pointer to item found.
407             You should lock RCU before calling of the function, and you should synchronize RCU
408             outside the RCU lock before reusing returned pointer.
409
410             \code
411             #include <cds/urcu/general_buffered.h>
412             #include <cds/intrusive/michael_list_rcu.h>
413             #include <cds/intrusive/michael_set_rcu.h>
414
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;
418
419             rcu_michael_set theSet;
420             // ...
421
422             typename rcu_michael_set::exempt_ptr p;
423             {
424                 // first, we should lock RCU
425                 typename rcu_michael_set::rcu_lock lock;
426
427                 // Now, you can apply extract function
428                 // Note that you must not delete the item found inside the RCU lock
429                 p = theSet.extract( 10 )
430                 if ( p ) {
431                     // do something with p
432                     ...
433                 }
434             }
435
436             // We may safely release p here
437             // release() passes the pointer to RCU reclamation cycle:
438             // it invokes RCU retire_ptr function with the disposer you provided for rcu_michael_list.
439             p.release();
440             \endcode
441         */
442         template <typename Q>
443         exempt_ptr extract( Q const& key )
444         {
445             exempt_ptr p( bucket( key ).extract( key ) );
446             if ( p )
447                 --m_ItemCounter;
448             return p;
449         }
450
451         /// Extracts an item from the set using \p pred predicate for searching
452         /**
453             The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
454             \p Less functor has the interface like \p std::less.
455             \p pred must imply the same element order as the comparator used for building the set.
456         */
457         template <typename Q, typename Less>
458         exempt_ptr extract_with( Q const& key, Less pred )
459         {
460             exempt_ptr p( bucket( key ).extract_with( key, pred ) );
461             if ( p )
462                 --m_ItemCounter;
463             return p;
464         }
465
466         /// Finds the key \p key
467         /** \anchor cds_intrusive_MichaelHashSet_rcu_find_val
468             The function searches the item with key equal to \p key
469             and returns \p true if \p key found or \p false otherwise.
470         */
471         template <typename Q>
472         bool find( Q const& key ) const
473         {
474             return bucket( key ).find( key );
475         }
476
477         /// Finds the key \p key using \p pred predicate for searching
478         /**
479             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_val "find(Q const&)"
480             but \p pred is used for key comparing.
481             \p Less functor has the interface like \p std::less.
482             \p pred must imply the same element order as the comparator used for building the set.
483         */
484         template <typename Q, typename Less>
485         bool find_with( Q const& key, Less pred ) const
486         {
487             return bucket( key ).find_with( key, pred );
488         }
489
490         /// Find the key \p key
491         /** \anchor cds_intrusive_MichaelHashSet_rcu_find_func
492             The function searches the item with key equal to \p key and calls the functor \p f for item found.
493             The interface of \p Func functor is:
494             \code
495             struct functor {
496                 void operator()( value_type& item, Q& key );
497             };
498             \endcode
499             where \p item is the item found, \p key is the <tt>find</tt> function argument.
500
501             The functor can change non-key fields of \p item.
502             The functor does not serialize simultaneous access to the set \p item. If such access is
503             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
504
505             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
506             can modify both arguments.
507
508             Note the hash functor specified for class \p Traits template parameter
509             should accept a parameter of type \p Q that can be not the same as \p value_type.
510
511             The function returns \p true if \p key is found, \p false otherwise.
512         */
513         template <typename Q, typename Func>
514         bool find( Q& key, Func f ) const
515         {
516             return bucket( key ).find( key, f );
517         }
518         //@cond
519         template <typename Q, typename Func>
520         bool find( Q const& key, Func f ) const
521         {
522             return bucket( key ).find( key, f );
523         }
524         //@endcond
525
526         /// Finds the key \p key using \p pred predicate for searching
527         /**
528             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_func "find(Q&, Func)"
529             but \p pred is used for key comparing.
530             \p Less functor has the interface like \p std::less.
531             \p pred must imply the same element order as the comparator used for building the set.
532         */
533         template <typename Q, typename Less, typename Func>
534         bool find_with( Q& key, Less pred, Func f ) const
535         {
536             return bucket( key ).find_with( key, pred, f );
537         }
538         //@cond
539         template <typename Q, typename Less, typename Func>
540         bool find_with( Q const& key, Less pred, Func f ) const
541         {
542             return bucket( key ).find_with( key, pred, f );
543         }
544         //@endcond
545
546         /// Finds the key \p key and return the item found
547         /** \anchor cds_intrusive_MichaelHashSet_rcu_get
548             The function searches the item with key equal to \p key and returns the pointer to item found.
549             If \p key is not found it returns \p nullptr.
550
551             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
552
553             RCU should be locked before call of this function.
554             Returned item is valid only while RCU is locked:
555             \code
556             typedef cds::intrusive::MichaelHashSet< your_template_parameters > hash_set;
557             hash_set theSet;
558             // ...
559             {
560                 // Lock RCU
561                 hash_set::rcu_lock lock;
562
563                 foo * pVal = theSet.get( 5 );
564                 if ( pVal ) {
565                     // Deal with pVal
566                     //...
567                 }
568                 // Unlock RCU by rcu_lock destructor
569                 // pVal can be retired by disposer at any time after RCU has been unlocked
570             }
571             \endcode
572         */
573         template <typename Q>
574         value_type * get( Q const& key ) const
575         {
576             return bucket( key ).get( key );
577         }
578
579         /// Finds the key \p key and return the item found
580         /**
581             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_get "get(Q const&)"
582             but \p pred is used for comparing the keys.
583
584             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
585             in any order.
586             \p pred must imply the same element order as the comparator used for building the set.
587         */
588         template <typename Q, typename Less>
589         value_type * get_with( Q const& key, Less pred ) const
590         {
591             return bucket( key ).get_with( key, pred );
592         }
593
594         /// Clears the set (non-atomic)
595         /**
596             The function unlink all items from the set.
597             The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
598             If there are a thread that performs insertion while \p clear is working the result is undefined in general case:
599             <tt> empty() </tt> may return \p true but the set may contain item(s).
600             Therefore, \p clear may be used only for debugging purposes.
601
602             For each item the \p disposer is called after unlinking.
603         */
604         void clear()
605         {
606             for ( size_t i = 0; i < bucket_count(); ++i )
607                 m_Buckets[i].clear();
608             m_ItemCounter.reset();
609         }
610
611
612         /// Checks if the set is empty
613         /**
614             Emptiness is checked by item counting: if item count is zero then the set is empty.
615             Thus, the correct item counting feature is an important part of Michael's set implementation.
616         */
617         bool empty() const
618         {
619             return size() == 0;
620         }
621
622         /// Returns item count in the set
623         size_t size() const
624         {
625             return m_ItemCounter;
626         }
627
628         /// Returns the size of hash table
629         /**
630             Since %MichaelHashSet cannot dynamically extend the hash table size,
631             the value returned is an constant depending on object initialization parameters;
632             see \ref cds_intrusive_MichaelHashSet_hp "MichaelHashSet" for explanation.
633         */
634         size_t bucket_count() const
635         {
636             return m_nHashBitmask + 1;
637         }
638
639     };
640
641 }} // namespace cds::intrusive
642
643 #endif // #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_NOGC_H
644