Docfix
[libcds.git] / cds / container / michael_list_rcu.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_CONTAINER_MICHAEL_LIST_RCU_H
32 #define CDSLIB_CONTAINER_MICHAEL_LIST_RCU_H
33
34 #include <memory>
35 #include <cds/container/details/michael_list_base.h>
36 #include <cds/intrusive/michael_list_rcu.h>
37 #include <cds/container/details/make_michael_list.h>
38 #include <cds/details/binary_functor_wrapper.h>
39
40 namespace cds { namespace container {
41
42     /// Michael's ordered list (template specialization for \ref cds_urcu_desc "RCU")
43     /** @ingroup cds_nonintrusive_list
44         \anchor cds_nonintrusive_MichaelList_rcu
45
46         Usually, ordered single-linked list is used as a building block for the hash table implementation.
47         The complexity of searching is <tt>O(N)</tt>.
48
49         Source:
50         - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
51
52         This class is non-intrusive version of \ref cds_intrusive_MichaelList_rcu "cds::intrusive::MichaelList" RCU specialization.
53
54         Template arguments:
55         - \p RCU - one of \ref cds_urcu_gc "RCU type"
56         - \p T - type stored in the list. The type must be default- and copy-constructible.
57         - \p Traits - type traits, default is michael_list::traits
58
59         The implementation does not divide type \p T into key and value part and
60         may be used as a main building block for hash set containers.
61         The key is a function (or a part) of type \p T, and this function is specified by <tt>Traits::compare</tt> functor
62         or <tt>Traits::less</tt> predicate.
63
64         \ref cds_nonintrusive_MichaelKVList_rcu "MichaelKVList" is a key-value version of Michael's
65         non-intrusive list that is closer to the C++ std library approach.
66
67         @note Before including <tt><cds/container/michael_list_rcu.h></tt> you should include appropriate RCU header file,
68         see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
69
70         It is possible to declare option-based list with cds::container::michael_list::make_traits metafunction istead of \p Traits template
71         argument. For example, the following traits-based declaration of Michael's list
72
73         \code
74         #include <cds/urcu/general_buffered.h>
75         #include <cds/container/michael_list_rcu.h>
76         // Declare comparator for the item
77         struct my_compare {
78             int operator ()( int i1, int i2 )
79             {
80                 return i1 - i2;
81             }
82         };
83
84         // Declare traits
85         struct my_traits: public cds::container::michael_list::traits
86         {
87             typedef my_compare compare;
88         };
89
90         // Declare traits-based list
91         typedef cds::container::MichaelList< cds::urcu::gc< cds::urcu::general_buffered<> >, int, my_traits >     traits_based_list;
92         \endcode
93
94         is equivalent for the following option-based list
95         \code
96         #include <cds/urcu/general_buffered.h>
97         #include <cds/container/michael_list_rcu.h>
98
99         // my_compare is the same
100
101         // Declare option-based list
102         typedef cds::container::MichaelList< cds::urcu::gc< cds::urcu::general_buffered<> >, int,
103             typename cds::container::michael_list::make_traits<
104                 cds::container::opt::compare< my_compare >     // item comparator option
105             >::type
106         >     option_based_list;
107         \endcode
108
109         Template argument list \p Options of cds::container::michael_list::make_traits metafunction are:
110         - opt::compare - key comparison functor. No default functor is provided.
111             If the option is not specified, the opt::less is used.
112         - opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
113         - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::empty is used.
114         - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
115         - opt::allocator - the allocator used for creating and freeing list's item. Default is \ref CDS_DEFAULT_ALLOCATOR macro.
116         - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
117             or opt::v::sequential_consistent (sequentially consisnent memory model).
118         - opt::rcu_check_deadlock - a deadlock checking policy. Default is opt::v::rcu_throw_deadlock
119     */
120     template <
121         typename RCU,
122         typename T,
123 #ifdef CDS_DOXYGEN_INVOKED
124         typename Traits = michael_list::traits
125 #else
126         typename Traits
127 #endif
128     >
129     class MichaelList< cds::urcu::gc<RCU>, T, Traits > :
130 #ifdef CDS_DOXYGEN_INVOKED
131         protected intrusive::MichaelList< cds::urcu::gc<RCU>, T, Traits >
132 #else
133         protected details::make_michael_list< cds::urcu::gc<RCU>, T, Traits >::type
134 #endif
135     {
136         //@cond
137         typedef details::make_michael_list< cds::urcu::gc<RCU>, T, Traits > maker;
138         typedef typename maker::type  base_class;
139         //@endcond
140
141     public:
142         typedef cds::urcu::gc<RCU> gc;          ///< RCU
143         typedef T                  value_type;  ///< Type of value stored in the list
144         typedef Traits             traits;      ///< List traits
145
146         typedef typename base_class::back_off     back_off;       ///< Back-off strategy used
147         typedef typename maker::allocator_type    allocator_type; ///< Allocator type used for allocate/deallocate the nodes
148         typedef typename base_class::item_counter item_counter;   ///< Item counting policy used
149         typedef typename maker::key_comparator    key_comparator; ///< key comparison functor
150         typedef typename base_class::memory_model memory_model;   ///< Memory ordering. See cds::opt::memory_model option
151         typedef typename base_class::rcu_check_deadlock rcu_check_deadlock ; ///< RCU deadlock checking policy
152
153         typedef typename gc::scoped_lock    rcu_lock ;  ///< RCU scoped lock
154         static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal; ///< Group of \p extract_xxx functions do not require external locking
155
156     protected:
157         //@cond
158         typedef typename base_class::value_type   node_type;
159         typedef typename maker::cxx_allocator     cxx_allocator;
160         typedef typename maker::node_deallocator  node_deallocator;
161         typedef typename maker::intrusive_traits::compare  intrusive_key_comparator;
162
163         typedef typename base_class::atomic_node_ptr      head_type;
164         //@endcond
165
166     public:
167         using exempt_ptr = cds::urcu::exempt_ptr< gc, node_type, value_type, typename maker::intrusive_traits::disposer >; ///< pointer to extracted node
168
169     private:
170         //@cond
171         struct raw_ptr_converter
172         {
173             value_type * operator()( node_type * p ) const
174             {
175                return p ? &p->m_Value : nullptr;
176             }
177
178             value_type& operator()( node_type& n ) const
179             {
180                 return n.m_Value;
181             }
182
183             value_type const& operator()( node_type const& n ) const
184             {
185                 return n.m_Value;
186             }
187         };
188         //@endcond
189
190     public:
191         /// Result of \p get(), \p get_with() functions - pointer to the node found
192         typedef cds::urcu::raw_ptr_adaptor< value_type, typename base_class::raw_ptr, raw_ptr_converter > raw_ptr;
193
194     protected:
195         //@cond
196         static value_type& node_to_value( node_type& n )
197         {
198             return n.m_Value;
199         }
200         static value_type const& node_to_value( node_type const& n )
201         {
202             return n.m_Value;
203         }
204
205         template <typename Q>
206         static node_type * alloc_node( Q const& v )
207         {
208             return cxx_allocator().New( v );
209         }
210
211         template <typename... Args>
212         static node_type * alloc_node( Args&&... args )
213         {
214             return cxx_allocator().MoveNew( std::forward<Args>(args)... );
215         }
216
217         static void free_node( node_type * pNode )
218         {
219             cxx_allocator().Delete( pNode );
220         }
221
222         struct node_disposer {
223             void operator()( node_type * pNode )
224             {
225                 free_node( pNode );
226             }
227         };
228         typedef std::unique_ptr< node_type, node_disposer >     scoped_node_ptr;
229
230         head_type& head()
231         {
232             return base_class::m_pHead;
233         }
234
235         head_type& head() const
236         {
237             return const_cast<head_type&>( base_class::m_pHead );
238         }
239         //@endcond
240
241     protected:
242                 //@cond
243         template <bool IsConst>
244         class iterator_type: protected base_class::template iterator_type<IsConst>
245         {
246             typedef typename base_class::template iterator_type<IsConst>    iterator_base;
247
248             iterator_type( head_type const& pNode )
249                 : iterator_base( pNode )
250             {}
251
252             friend class MichaelList;
253
254         public:
255             typedef typename cds::details::make_const_type<value_type, IsConst>::pointer   value_ptr;
256             typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
257
258             iterator_type()
259             {}
260
261             iterator_type( iterator_type const& src )
262                 : iterator_base( src )
263             {}
264
265             value_ptr operator ->() const
266             {
267                 typename iterator_base::value_ptr p = iterator_base::operator ->();
268                 return p ? &(p->m_Value) : nullptr;
269             }
270
271             value_ref operator *() const
272             {
273                 return (iterator_base::operator *()).m_Value;
274             }
275
276             /// Pre-increment
277             iterator_type& operator ++()
278             {
279                 iterator_base::operator ++();
280                 return *this;
281             }
282
283             template <bool C>
284             bool operator ==(iterator_type<C> const& i ) const
285             {
286                 return iterator_base::operator ==(i);
287             }
288             template <bool C>
289             bool operator !=(iterator_type<C> const& i ) const
290             {
291                 return iterator_base::operator !=(i);
292             }
293         };
294         //@endcond
295
296     public:
297     ///@name Forward iterators (only for debugging purpose)
298     //@{
299         /// Forward iterator
300         /**
301             You may safely use iterators in multi-threaded environment only under RCU lock.
302             Otherwise, a crash is possible if another thread deletes the item the iterator points to.
303         */
304         typedef iterator_type<false>    iterator;
305
306         /// Const forward iterator
307         typedef iterator_type<true>     const_iterator;
308
309         /// Returns a forward iterator addressing the first element in a list
310         /**
311             For empty list \code begin() == end() \endcode
312         */
313         iterator begin()
314         {
315             return iterator( head() );
316         }
317
318         /// Returns an iterator that addresses the location succeeding the last element in a list
319         /**
320             Do not use the value returned by <tt>end</tt> function to access any item.
321             Internally, <tt>end</tt> returning value equals to \p nullptr.
322
323             The returned value can be used only to control reaching the end of the list.
324             For empty list \code begin() == end() \endcode
325         */
326         iterator end()
327         {
328             return iterator();
329         }
330
331         /// Returns a forward const iterator addressing the first element in a list
332         const_iterator begin() const
333         {
334             return const_iterator( head() );
335         }
336
337         /// Returns a forward const iterator addressing the first element in a list
338         const_iterator cbegin() const
339         {
340             return const_iterator( head() );
341         }
342
343         /// Returns an const iterator that addresses the location succeeding the last element in a list
344         const_iterator end() const
345         {
346             return const_iterator();
347         }
348
349         /// Returns an const iterator that addresses the location succeeding the last element in a list
350         const_iterator cend() const
351         {
352             return const_iterator();
353         }
354     //@}
355
356     public:
357         /// Default constructor
358         /**
359             Initialize empty list
360         */
361         MichaelList()
362         {}
363
364         /// List destructor
365         /**
366             Clears the list
367         */
368         ~MichaelList()
369         {
370             clear();
371         }
372
373         /// Inserts new node
374         /**
375             The function creates a node with copy of \p val value
376             and then inserts the node created into the list.
377
378             The type \p Q should contain as minimum the complete key of the node.
379             The object of \ref value_type should be constructible from \p val of type \p Q.
380             In trivial case, \p Q is equal to \ref value_type.
381
382             The function makes RCU lock internally.
383
384             Returns \p true if inserting successful, \p false otherwise.
385         */
386         template <typename Q>
387         bool insert( Q const& val )
388         {
389             return insert_at( head(), val );
390         }
391
392         /// Inserts new node
393         /**
394             This function inserts new node with default-constructed value and then it calls
395             \p func functor with signature
396             \code void func( value_type& itemValue ) ;\endcode
397
398             The argument \p itemValue of user-defined functor \p func is the reference
399             to the list's item inserted. User-defined functor \p func should guarantee that during changing
400             item's value no any other changes could be made on this list's item by concurrent threads.
401
402             The type \p Q should contain the complete key of the node.
403             The object of \ref value_type should be constructible from \p key of type \p Q.
404
405             The function allows to split creating of new item into two part:
406             - create item from \p key with initializing key-fields only;
407             - insert new item into the list;
408             - if inserting is successful, initialize non-key fields of item by calling \p f functor
409
410             This can be useful if complete initialization of object of \p value_type is heavyweight and
411             it is preferable that the initialization should be completed only if inserting is successful.
412
413             The function makes RCU lock internally.
414
415             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
416         */
417         template <typename Q, typename Func>
418         bool insert( Q const& key, Func func )
419         {
420             return insert_at( head(), key, func );
421         }
422
423         /// Updates data by \p key
424         /**
425             The operation performs inserting or replacing the element with lock-free manner.
426
427             If the \p key not found in the list, then the new item created from \p key
428             will be inserted iff \p bAllowInsert is \p true.
429             Otherwise, if \p key is found, the functor \p func is called with item found.
430
431             The functor \p Func signature is:
432             \code
433                 struct my_functor {
434                     void operator()( bool bNew, value_type& item, Q const& val );
435                 };
436             \endcode
437
438             with arguments:
439             - \p bNew - \p true if the item has been inserted, \p false otherwise
440             - \p item - item of the list
441             - \p val - argument \p key passed into the \p %update() function
442
443             The functor may change non-key fields of the \p item; however, \p func must guarantee
444             that during changing no any other modifications could be made on this item by concurrent threads.
445
446             The function applies RCU lock internally.
447
448             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
449             \p second is true if new item has been added or \p false if the item with \p key
450             already exists.
451
452             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
453         */
454         template <typename Q, typename Func>
455         std::pair<bool, bool> update( Q const& key, Func func, bool bAllowInsert = true )
456         {
457             return update_at( head(), key, func, bAllowInsert );
458         }
459         //@cond
460         template <typename Q, typename Func>
461         CDS_DEPRECATED("ensure() is deprecated, use update()")
462         std::pair<bool, bool> ensure( Q const& key, Func f )
463         {
464             return update( key, f, true );
465         }
466         //@endcond
467
468         /// Inserts data of type \ref value_type constructed from \p args
469         /**
470             Returns \p true if inserting successful, \p false otherwise.
471
472             The function makes RCU lock internally.
473         */
474         template <typename... Args>
475         bool emplace( Args&&... args )
476         {
477             return emplace_at( head(), std::forward<Args>(args)... );
478         }
479
480         /// Deletes \p key from the list
481         /** \anchor cds_nonintrusive_MichealList_rcu_erase_val
482             Since the key of MichaelList's item type \p value_type is not explicitly specified,
483             template parameter \p Q defines the key type searching in the list.
484             The list item comparator should be able to compare values of the type \p value_type
485             and \p Q in any order.
486
487             RCU \p synchronize method can be called. RCU should not be locked.
488
489             Return \p true if key is found and deleted, \p false otherwise
490         */
491         template <typename Q>
492         bool erase( Q const& key )
493         {
494             return erase_at( head(), key, intrusive_key_comparator(),  [](value_type const&){} );
495         }
496
497         /// Deletes the item from the list using \p pred predicate for searching
498         /**
499             The function is an analog of \ref cds_nonintrusive_MichealList_rcu_erase_val "erase(Q const&)"
500             but \p pred is used for key comparing.
501             \p Less functor has the interface like \p std::less.
502             \p pred must imply the same element order as the comparator used for building the list.
503         */
504         template <typename Q, typename Less>
505         bool erase_with( Q const& key, Less pred )
506         {
507             CDS_UNUSED( pred );
508             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), [](value_type const&){} );
509         }
510
511         /// Deletes \p key from the list
512         /** \anchor cds_nonintrusive_MichaelList_rcu_erase_func
513             The function searches an item with key \p key, calls \p f functor with item found
514             and deletes it. If \p key is not found, the functor is not called.
515
516             The functor \p Func interface:
517             \code
518             struct functor {
519                 void operator()(const value_type& val) { ... }
520             };
521             \endcode
522
523             Since the key of MichaelList's item type \p value_type is not explicitly specified,
524             template parameter \p Q defines the key type searching in the list.
525             The list item comparator should be able to compare the values of type \p value_type
526             and \p Q in any order.
527
528             RCU \p synchronize method can be called. RCU should not be locked.
529
530             Return \p true if key is found and deleted, \p false otherwise
531         */
532         template <typename Q, typename Func>
533         bool erase( Q const& key, Func f )
534         {
535             return erase_at( head(), key, intrusive_key_comparator(), f );
536         }
537
538         /// Deletes the item from the list using \p pred predicate for searching
539         /**
540             The function is an analog of \ref cds_nonintrusive_MichaelList_rcu_erase_func "erase(Q const&, Func)"
541             but \p pred is used for key comparing.
542             \p Less functor has the interface like \p std::less.
543             \p pred must imply the same element order as the comparator used for building the list.
544         */
545         template <typename Q, typename Less, typename Func>
546         bool erase_with( Q const& key, Less pred, Func f )
547         {
548             CDS_UNUSED( pred );
549             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
550         }
551
552         /// Extracts an item from the list
553         /**
554         @anchor cds_nonintrusive_MichaelList_rcu_extract
555             The function searches an item with key equal to \p key in the list,
556             unlinks it from the list, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
557             If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
558
559             @note The function does NOT dispose the item found. It just excludes the item from the list
560             and returns a pointer to the item.
561             You shouldn't lock RCU for current thread before calling this function.
562
563             \code
564             #include <cds/urcu/general_buffered.h>
565             #include <cds/container/michael_list_rcu.h>
566
567             typedef cds::urcu::gc< general_buffered<> > rcu;
568             typedef cds::container::MichaelList< rcu, Foo > rcu_michael_list;
569
570             rcu_michael_list theList;
571             // ...
572
573             rcu_michael_list::exempt_ptr p;
574
575             // The RCU should NOT be locked when extract() is called!
576             assert( !rcu::is_locked() );
577
578             // extract() call
579             p = theList.extract( 10 )
580             if ( p ) {
581                 // do something with p
582                 ...
583             }
584
585             // we may safely release extracted pointer here.
586             // release() passes the pointer to RCU reclamation cycle.
587             p.release();
588             \endcode
589         */
590         template <typename Q>
591         exempt_ptr extract( Q const& key )
592         {
593             return exempt_ptr( extract_at( head(), key, intrusive_key_comparator() ));
594         }
595
596         /// Extracts an item from the list using \p pred predicate for searching
597         /**
598             This function is the analog for \p extract(Q const&).
599
600             The \p pred is a predicate used for key comparing.
601             \p Less has the interface like \p std::less.
602             \p pred must imply the same element order as \ref key_comparator.
603         */
604         template <typename Q, typename Less>
605         exempt_ptr extract_with( Q const& key, Less pred )
606         {
607             CDS_UNUSED( pred );
608             return exempt_ptr( extract_at( head(), key, typename maker::template less_wrapper<Less>::type() ));
609         }
610
611         /// Checks whether the list contains \p key
612         /**
613             The function searches the item with key equal to \p key
614             and returns \p true if it is found, and \p false otherwise.
615
616             The function applies RCU lock internally.
617         */
618         template <typename Q>
619         bool contains( Q const& key )
620         {
621             return find_at( head(), key, intrusive_key_comparator() );
622         }
623         //@cond
624         template <typename Q>
625         CDS_DEPRECATED("deprecated, use contains()")
626         bool find( Q const& key )
627         {
628             return contains( key );
629         }
630         //@endcond
631
632         /// Checks whether the list contains \p key using \p pred predicate for searching
633         /**
634             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
635             \p Less functor has the interface like \p std::less.
636             \p pred must imply the same element order as the comparator used for building the list.
637         */
638         template <typename Q, typename Less>
639         bool contains( Q const& key, Less pred )
640         {
641             CDS_UNUSED( pred );
642             return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
643         }
644         //@cond
645         // Deprecatd, use contains()
646         template <typename Q, typename Less>
647         bool find_with( Q const& key, Less pred )
648         {
649             CDS_UNUSED( pred );
650             return contains( key, pred );
651         }
652         //@endcond
653
654         /// Finds the key \p key and performs an action with it
655         /** \anchor cds_nonintrusive_MichaelList_rcu_find_func
656             The function searches an item with key equal to \p key and calls the functor \p f for the item found.
657             The interface of \p Func functor is:
658             \code
659             struct functor {
660                 void operator()( value_type& item, Q& key );
661             };
662             \endcode
663             where \p item is the item found, \p key is the \p %find() function argument.
664
665             The functor may change non-key fields of \p item. Note that the function is only guarantee
666             that \p item cannot be deleted during functor is executing.
667             The function does not serialize simultaneous access to the list \p item. If such access is
668             possible you must provide your own synchronization schema to exclude unsafe item modifications.
669
670             The function makes RCU lock internally.
671
672             The function returns \p true if \p val is found, \p false otherwise.
673         */
674         template <typename Q, typename Func>
675         bool find( Q& key, Func f )
676         {
677             return find_at( head(), key, intrusive_key_comparator(), f );
678         }
679         //@cond
680         template <typename Q, typename Func>
681         bool find( Q const& key, Func f )
682         {
683             return find_at( head(), key, intrusive_key_comparator(), f );
684         }
685         //@endcond
686
687         /// Finds the key \p key using \p pred predicate for searching
688         /**
689             The function is an analog of \ref cds_nonintrusive_MichaelList_rcu_find_func "find(Q&, Func)"
690             but \p pred is used for key comparing.
691             \p Less functor has the interface like \p std::less.
692             \p pred must imply the same element order as the comparator used for building the list.
693         */
694         template <typename Q, typename Less, typename Func>
695         bool find_with( Q& key, Less pred, Func f )
696         {
697             CDS_UNUSED( pred );
698             return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
699         }
700         //@cond
701         template <typename Q, typename Less, typename Func>
702         bool find_with( Q const& key, Less pred, Func f )
703         {
704             CDS_UNUSED( pred );
705             return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
706         }
707         //@endcond
708
709         /// Finds the key \p key and return the item found
710         /** \anchor cds_nonintrusive_MichaelList_rcu_get
711             The function searches the item with key equal to \p key and returns the pointer to item found.
712             If \p key is not found it returns an empty \p raw_ptr.
713
714             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
715
716             RCU should be locked before call of this function.
717             Returned item is valid only while RCU is locked:
718             \code
719             typedef cds::container::MichaelList< cds::urcu::gc< cds::urcu::general_buffered<> >, foo, my_traits > ord_list;
720             ord_list theList;
721             // ...
722             typename ord_list::raw_ptr rp;
723             {
724                 // Lock RCU
725                 ord_list::rcu_lock lock;
726
727                 rp = theList.get( 5 );
728                 if ( rp ) {
729                     // Deal with rp
730                     //...
731                 }
732                 // Unlock RCU by rcu_lock destructor
733                 // A value owned by rp can be freed at any time after RCU has been unlocked
734             }
735             // You can manually release rp after RCU-locked section
736             rp.release();
737             \endcode
738         */
739         template <typename Q>
740         raw_ptr get( Q const& key )
741         {
742             return get_at( head(), key, intrusive_key_comparator());
743         }
744
745         /// Finds \p key and return the item found
746         /**
747             The function is an analog of \ref cds_nonintrusive_MichaelList_rcu_get "get(Q const&)"
748             but \p pred is used for comparing the keys.
749
750             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
751             in any order.
752             \p pred must imply the same element order as the comparator used for building the list.
753         */
754         template <typename Q, typename Less>
755         raw_ptr get_with( Q const& key, Less pred )
756         {
757             CDS_UNUSED( pred );
758             return get_at( head(), key, typename maker::template less_wrapper<Less>::type());
759         }
760
761         /// Checks if the list is empty
762         bool empty() const
763         {
764             return base_class::empty();
765         }
766
767         /// Returns list's item count
768         /**
769             The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
770             this function always returns 0.
771
772             @note Even if you use real item counter and it returns 0, this fact does not mean that the list
773             is empty. To check list emptyness use \p empty() method.
774         */
775         size_t size() const
776         {
777             return base_class::size();
778         }
779
780         /// Clears the list
781         void clear()
782         {
783             base_class::clear();
784         }
785
786     protected:
787         //@cond
788         bool insert_node( node_type * pNode )
789         {
790             return insert_node_at( head(), pNode );
791         }
792
793         bool insert_node_at( head_type& refHead, node_type * pNode )
794         {
795             assert( pNode );
796             scoped_node_ptr p(pNode);
797             if ( base_class::insert_at( refHead, *pNode )) {
798                 p.release();
799                 return true;
800             }
801
802             return false;
803         }
804
805         template <typename Q>
806         bool insert_at( head_type& refHead, Q const& val )
807         {
808             return insert_node_at( refHead, alloc_node( val ));
809         }
810
811         template <typename Q, typename Func>
812         bool insert_at( head_type& refHead, Q const& key, Func f )
813         {
814             scoped_node_ptr pNode( alloc_node( key ));
815
816             if ( base_class::insert_at( refHead, *pNode, [&f]( node_type& node ) { f( node_to_value(node) ); } )) {
817                 pNode.release();
818                 return true;
819             }
820             return false;
821         }
822
823         template <typename... Args>
824         bool emplace_at( head_type& refHead, Args&&... args )
825         {
826             return insert_node_at( refHead, alloc_node( std::forward<Args>(args) ... ));
827         }
828
829         template <typename Q, typename Compare, typename Func>
830         bool erase_at( head_type& refHead, Q const& key, Compare cmp, Func f )
831         {
832             return base_class::erase_at( refHead, key, cmp, [&f](node_type const& node){ f( node_to_value(node) ); } );
833         }
834
835         template <typename Q, typename Func>
836         std::pair<bool, bool> update_at( head_type& refHead, Q const& key, Func f, bool bAllowInsert )
837         {
838             scoped_node_ptr pNode( alloc_node( key ));
839
840             std::pair<bool, bool> ret = base_class::update_at( refHead, *pNode,
841                 [&f, &key](bool bNew, node_type& node, node_type&){ f( bNew, node_to_value(node), key );},
842                 bAllowInsert );
843             if ( ret.first && ret.second )
844                 pNode.release();
845
846             return ret;
847         }
848
849         template <typename Q, typename Compare>
850         node_type * extract_at( head_type& refHead, Q const& key, Compare cmp )
851         {
852             return base_class::extract_at( refHead, key, cmp );
853         }
854
855         template <typename Q, typename Compare>
856         bool find_at( head_type& refHead, Q const& key, Compare cmp )
857         {
858             return base_class::find_at( refHead, key, cmp, [](node_type&, Q const &) {} );
859         }
860
861         template <typename Q, typename Compare, typename Func>
862         bool find_at( head_type& refHead, Q& val, Compare cmp, Func f )
863         {
864             return base_class::find_at( refHead, val, cmp, [&f](node_type& node, Q& v){ f( node_to_value(node), v ); });
865         }
866
867         template <typename Q, typename Compare>
868         raw_ptr get_at( head_type& refHead, Q const& val, Compare cmp )
869         {
870             return raw_ptr( base_class::get_at( refHead, val, cmp ));
871         }
872
873         //@endcond
874     };
875
876 }}  // namespace cds::container
877
878 #endif  // #ifndef CDSLIB_CONTAINER_MICHAEL_LIST_RCU_H