3 #ifndef __CDS_CONTAINER_IMPL_LAZY_LIST_H
4 #define __CDS_CONTAINER_IMPL_LAZY_LIST_H
7 #include <cds/container/details/guarded_ptr_cast.h>
9 namespace cds { namespace container {
12 /** @ingroup cds_nonintrusive_list
13 \anchor cds_nonintrusive_LazyList_gc
15 Usually, ordered single-linked list is used as a building block for the hash table implementation.
16 The complexity of searching is <tt>O(N)</tt>.
19 - [2005] Steve Heller, Maurice Herlihy, Victor Luchangco, Mark Moir, William N. Scherer III, and Nir Shavit
20 "A Lazy Concurrent List-Based Set Algorithm"
22 The lazy list is based on an optimistic locking scheme for inserts and removes,
23 eliminating the need to use the equivalent of an atomically markable
24 reference. It also has a novel wait-free membership \p find() operation
25 that does not need to perform cleanup operations and is more efficient.
27 It is non-intrusive version of \p cds::intrusive::LazyList class.
30 - \p GC - garbage collector: \p gc::HP, \p gp::DHP
31 - \p T - type to be stored in the list.
32 - \p Traits - type traits, default is \p lazy_list::traits.
33 It is possible to declare option-based list with \p lazy_list::make_traits metafunction istead of \p Traits template
34 argument. For example, the following traits-based declaration of \p gc::HP lazy list
36 #include <cds/container/lazy_list_hp.h>
37 // Declare comparator for the item
39 int operator ()( int i1, int i2 )
46 struct my_traits: public cds::container::lazy_list::traits
48 typedef my_compare compare;
51 // Declare traits-based list
52 typedef cds::container::LazyList< cds::gc::HP, int, my_traits > traits_based_list;
54 is equal to the following option-based list:
56 #include <cds/container/lazy_list_hp.h>
58 // my_compare is the same
60 // Declare option-based list
61 typedef cds::container::LazyList< cds::gc::HP, int,
62 typename cds::container::lazy_list::make_traits<
63 cds::container::opt::compare< my_compare > // item comparator option
68 Unlike standard container, this implementation does not divide type \p T into key and value part and
69 may be used as main building block for hash set algorithms.
71 The key is a function (or a part) of type \p T, and the comparing function is specified by \p Traits::compare functor
72 or \p Traits::less predicate.
74 \p LazyKVList is a key-value version of lazy non-intrusive list that is closer to the C++ std library approach.
77 There are different specializations of this template for each garbage collecting schema used.
78 You should include appropriate .h-file depending on GC you are using:
79 - for gc::HP: <tt> <cds/container/lazy_list_hp.h> </tt>
80 - for gc::DHP: <tt> <cds/container/lazy_list_dhp.h> </tt>
81 - for \ref cds_urcu_desc "RCU": <tt> <cds/container/lazy_list_rcu.h> </tt>
82 - for gc::nogc: <tt> <cds/container/lazy_list_nogc.h> </tt>
87 #ifdef CDS_DOXYGEN_INVOKED
88 typename Traits = lazy_list::traits
94 #ifdef CDS_DOXYGEN_INVOKED
95 protected intrusive::LazyList< GC, T, Traits >
97 protected details::make_lazy_list< GC, T, Traits >::type
101 typedef details::make_lazy_list< GC, T, Traits > maker;
102 typedef typename maker::type base_class;
106 typedef GC gc; ///< Garbage collector used
107 typedef T value_type; ///< Type of value stored in the list
108 typedef Traits traits; ///< List traits
110 typedef typename base_class::back_off back_off; ///< Back-off strategy used
111 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
112 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
113 typedef typename maker::key_comparator key_comparator; ///< key comparison functor
114 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
118 typedef typename base_class::value_type node_type;
119 typedef typename maker::cxx_allocator cxx_allocator;
120 typedef typename maker::node_deallocator node_deallocator;
121 typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
123 typedef typename base_class::node_type head_type;
128 typedef cds::gc::guarded_ptr< gc, node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
132 static value_type& node_to_value( node_type& n )
136 static value_type const& node_to_value( node_type const& n )
144 template <typename Q>
145 static node_type * alloc_node( Q const& v )
147 return cxx_allocator().New( v );
150 template <typename... Args>
151 static node_type * alloc_node( Args&&... args )
153 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
156 static void free_node( node_type * pNode )
158 cxx_allocator().Delete( pNode );
161 struct node_disposer {
162 void operator()( node_type * pNode )
167 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
171 return base_class::m_Head;
174 head_type const& head() const
176 return base_class::m_Head;
181 return base_class::m_Tail;
184 head_type const& tail() const
186 return base_class::m_Tail;
192 template <bool IsConst>
193 class iterator_type: protected base_class::template iterator_type<IsConst>
195 typedef typename base_class::template iterator_type<IsConst> iterator_base;
197 iterator_type( head_type const& pNode )
198 : iterator_base( const_cast<head_type *>( &pNode ))
201 iterator_type( head_type const * pNode )
202 : iterator_base( const_cast<head_type *>( pNode ))
205 friend class LazyList;
208 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
209 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
214 iterator_type( const iterator_type& src )
215 : iterator_base( src )
218 value_ptr operator ->() const
220 typename iterator_base::value_ptr p = iterator_base::operator ->();
221 return p ? &(p->m_Value) : nullptr;
224 value_ref operator *() const
226 return (iterator_base::operator *()).m_Value;
230 iterator_type& operator ++()
232 iterator_base::operator ++();
237 bool operator ==(iterator_type<C> const& i ) const
239 return iterator_base::operator ==(i);
242 bool operator !=(iterator_type<C> const& i ) const
244 return iterator_base::operator !=(i);
252 The forward iterator for lazy list has some features:
253 - it has no post-increment operator
254 - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
255 For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
256 may be thrown if a limit of guard count per thread is exceeded.
257 - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
258 - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
259 deleting operations it is no guarantee that you iterate all item in the list.
261 Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator on the concurrent container
262 for debug purpose only.
264 typedef iterator_type<false> iterator;
266 /// Const forward iterator
268 For iterator's features and requirements see \ref iterator
270 typedef iterator_type<true> const_iterator;
272 /// Returns a forward iterator addressing the first element in a list
274 For empty list \code begin() == end() \endcode
278 iterator it( head() );
279 ++it ; // skip dummy head node
283 /// Returns an iterator that addresses the location succeeding the last element in a list
285 Do not use the value returned by <tt>end</tt> function to access any item.
287 The returned value can be used only to control reaching the end of the list.
288 For empty list \code begin() == end() \endcode
292 return iterator( tail() );
295 /// Returns a forward const iterator addressing the first element in a list
297 const_iterator begin() const
299 const_iterator it( head() );
300 ++it ; // skip dummy head node
303 const_iterator cbegin() const
305 const_iterator it( head() );
306 ++it ; // skip dummy head node
311 /// Returns an const iterator that addresses the location succeeding the last element in a list
313 const_iterator end() const
315 return const_iterator( tail() );
317 const_iterator cend() const
319 return const_iterator( tail() );
324 /// Default constructor
328 /// Destructor clears the list
336 The function creates a node with copy of \p val value
337 and then inserts the node created into the list.
339 The type \p Q should contain as minimum the complete key of the node.
340 The object of \ref value_type should be constructible from \p val of type \p Q.
341 In trivial case, \p Q is equal to \ref value_type.
343 Returns \p true if inserting successful, \p false otherwise.
345 template <typename Q>
346 bool insert( Q const& val )
348 return insert_at( head(), val );
353 This function inserts new node with default-constructed value and then it calls
354 \p func functor with signature
355 \code void func( value_type& item ) ;\endcode
357 The argument \p item of user-defined functor \p func is the reference
358 to the list's item inserted.
359 When \p func is called it has exclusive access to the item.
360 The user-defined functor is called only if the inserting is success.
362 The type \p Q should contain the complete key of the node.
363 The object of \p value_type should be constructible from \p key of type \p Q.
365 The function allows to split creating of new item into two part:
366 - create item from \p key with initializing key-fields only;
367 - insert new item into the list;
368 - if inserting is successful, initialize non-key fields of item by calling \p func functor
370 This can be useful if complete initialization of object of \p value_type is heavyweight and
371 it is preferable that the initialization should be completed only if inserting is successful.
373 template <typename Q, typename Func>
374 bool insert( Q const& key, Func func )
376 return insert_at( head(), key, func );
379 /// Inserts data of type \p value_type constructed from \p args
381 Returns \p true if inserting successful, \p false otherwise.
383 template <typename... Args>
384 bool emplace( Args&&... args )
386 return emplace_at( head(), std::forward<Args>(args)... );
389 /// Ensures that the \p key exists in the list
391 The operation performs inserting or changing data with lock-free manner.
393 If the \p key not found in the list, then the new item created from \p key
394 is inserted into the list. Otherwise, the functor \p f is called with the item found.
395 \p Func signature is:
398 void operator()( bool bNew, value_type& item, const Q& key );
403 - \p bNew - \p true if the item has been inserted, \p false otherwise
404 - \p item - an item of the list
405 - \p key - argument \p key passed into the \p %ensure() function
407 The functor may change non-key fields of the \p item.
408 When \p func is called it has exclusive access to the item.
410 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
411 \p second is true if new item has been added or \p false if the item with \p key
412 already is in the list.
414 template <typename Q, typename Func>
415 std::pair<bool, bool> ensure( Q const& key, Func f )
417 return ensure_at( head(), key, f );
420 /// Deletes \p key from the list
421 /** \anchor cds_nonintrusive_LazyList_hp_erase_val
422 Since the key of LazyList's item type \p T is not explicitly specified,
423 template parameter \p Q defines the key type searching in the list.
424 The list item comparator should be able to compare the type \p T of list item
427 Return \p true if key is found and deleted, \p false otherwise
429 template <typename Q>
430 bool erase( Q const& key )
432 return erase_at( head(), key, intrusive_key_comparator(), [](value_type const&){} );
435 /// Deletes the item from the list using \p pred predicate for searching
437 The function is an analog of \ref cds_nonintrusive_LazyList_hp_erase_val "erase(Q const&)"
438 but \p pred is used for key comparing.
439 \p Less functor has the interface like \p std::less.
440 \p pred must imply the same element order as the comparator used for building the list.
442 template <typename Q, typename Less>
443 bool erase_with( Q const& key, Less pred )
446 return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), [](value_type const&){} );
449 /// Deletes \p key from the list
450 /** \anchor cds_nonintrusive_LazyList_hp_erase_func
451 The function searches an item with key \p key, calls \p f functor with item found
452 and deletes the item. If \p key is not found, the functor is not called.
454 The functor \p Func interface:
457 void operator()(const value_type& val) { ... }
461 Since the key of LazyList's item type \p T is not explicitly specified,
462 template parameter \p Q defines the key type searching in the list.
463 The list item comparator should be able to compare the type \p T of list item
466 Return \p true if key is found and deleted, \p false otherwise
470 template <typename Q, typename Func>
471 bool erase( Q const& key, Func f )
473 return erase_at( head(), key, intrusive_key_comparator(), f );
476 /// Deletes the item from the list using \p pred predicate for searching
478 The function is an analog of \ref cds_nonintrusive_LazyList_hp_erase_func "erase(Q const&, Func)"
479 but \p pred is used for key comparing.
480 \p Less functor has the interface like \p std::less.
481 \p pred must imply the same element order as the comparator used for building the list.
483 template <typename Q, typename Less, typename Func>
484 bool erase_with( Q const& key, Less pred, Func f )
487 return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
490 /// Extracts the item from the list with specified \p key
491 /** \anchor cds_nonintrusive_LazyList_hp_extract
492 The function searches an item with key equal to \p key,
493 unlinks it from the list, and returns it in \p dest parameter.
494 If the item with key equal to \p key is not found the function returns \p false.
496 Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
498 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
502 typedef cds::container::LazyList< cds::gc::HP, foo, my_traits > ord_list;
506 ord_list::guarded_ptr gp;
507 theList.extract( gp, 5 );
511 // Destructor of gp releases internal HP guard and frees the item
515 template <typename Q>
516 bool extract( guarded_ptr& dest, Q const& key )
518 return extract_at( head(), dest.guard(), key, intrusive_key_comparator() );
521 /// Extracts the item from the list with comparing functor \p pred
523 The function is an analog of \ref cds_nonintrusive_LazyList_hp_extract "extract(guarded_ptr&, Q const&)"
524 but \p pred predicate is used for key comparing.
526 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
528 \p pred must imply the same element order as the comparator used for building the list.
530 template <typename Q, typename Less>
531 bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
534 return extract_at( head(), dest.guard(), key, typename maker::template less_wrapper<Less>::type() );
537 /// Finds the key \p key
538 /** \anchor cds_nonintrusive_LazyList_hp_find_val
539 The function searches the item with key equal to \p key
540 and returns \p true if it is found, and \p false otherwise
542 template <typename Q>
543 bool find( Q const& key )
545 return find_at( head(), key, intrusive_key_comparator() );
548 /// Finds the key \p key using \p pred predicate for searching
550 The function is an analog of \ref cds_nonintrusive_LazyList_hp_find_val "find(Q const&)"
551 but \p pred is used for key comparing.
552 \p Less functor has the interface like \p std::less.
553 \p pred must imply the same element order as the comparator used for building the list.
555 template <typename Q, typename Less>
556 bool find_with( Q const& key, Less pred )
559 return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
562 /// Finds the key \p key and performs an action with it
563 /** \anchor cds_nonintrusive_LazyList_hp_find_func
564 The function searches an item with key equal to \p key and calls the functor \p f for the item found.
565 The interface of \p Func functor is:
568 void operator()( value_type& item, Q& key );
571 where \p item is the item found, \p key is the <tt>find</tt> function argument.
573 The functor may change non-key fields of \p item. Note that the function is only guarantee
574 that \p item cannot be deleted during functor is executing.
575 The function does not serialize simultaneous access to the list \p item. If such access is
576 possible you must provide your own synchronization schema to exclude unsafe item modifications.
578 The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
579 may modify both arguments.
581 The function returns \p true if \p key is found, \p false otherwise.
583 template <typename Q, typename Func>
584 bool find( Q& key, Func f )
586 return find_at( head(), key, intrusive_key_comparator(), f );
589 template <typename Q, typename Func>
590 bool find( Q const& key, Func f )
592 return find_at( head(), key, intrusive_key_comparator(), f );
596 /// Finds the key \p key using \p pred predicate for searching
598 The function is an analog of \ref cds_nonintrusive_LazyList_hp_find_func "find(Q&, Func)"
599 but \p pred is used for key comparing.
600 \p Less functor has the interface like \p std::less.
601 \p pred must imply the same element order as the comparator used for building the list.
603 template <typename Q, typename Less, typename Func>
604 bool find_with( Q& key, Less pred, Func f )
607 return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
610 template <typename Q, typename Less, typename Func>
611 bool find_with( Q const& key, Less pred, Func f )
614 return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
618 /// Finds the key \p key and return the item found
619 /** \anchor cds_nonintrusive_LazyList_hp_get
620 The function searches the item with key equal to \p key
621 and assigns the item found to guarded pointer \p ptr.
622 The function returns \p true if \p key is found, and \p false otherwise.
623 If \p key is not found the \p ptr parameter is not changed.
625 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
629 typedef cds::container::LazyList< cds::gc::HP, foo, my_traits > ord_list;
633 ord_list::guarded_ptr gp;
634 if ( theList.get( gp, 5 )) {
638 // Destructor of guarded_ptr releases internal HP guard and frees the item
642 Note the compare functor specified for class \p Traits template parameter
643 should accept a parameter of type \p Q that can be not the same as \p value_type.
645 template <typename Q>
646 bool get( guarded_ptr& ptr, Q const& key )
648 return get_at( head(), ptr.guard(), key, intrusive_key_comparator() );
651 /// Finds the key \p key and return the item found
653 The function is an analog of \ref cds_nonintrusive_LazyList_hp_get "get( guarded_ptr& ptr, Q const&)"
654 but \p pred is used for comparing the keys.
656 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
658 \p pred must imply the same element order as the comparator used for building the list.
660 template <typename Q, typename Less>
661 bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
664 return get_at( head(), ptr.guard(), key, typename maker::template less_wrapper<Less>::type() );
667 /// Checks whether the list is empty
670 return base_class::empty();
673 /// Returns list's item count
675 The value returned depends on \p Traits::item_counter type. For \p atomicity::empty_item_counter,
676 this function always returns 0.
678 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
679 is empty. To check list emptyness use \ref empty() method.
683 return base_class::size();
694 bool insert_node_at( head_type& refHead, node_type * pNode )
696 assert( pNode != nullptr );
697 scoped_node_ptr p( pNode );
699 if ( base_class::insert_at( &refHead, *pNode )) {
707 template <typename Q>
708 bool insert_at( head_type& refHead, const Q& val )
710 return insert_node_at( refHead, alloc_node( val ));
713 template <typename... Args>
714 bool emplace_at( head_type& refHead, Args&&... args )
716 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
719 template <typename Q, typename Func>
720 bool insert_at( head_type& refHead, const Q& key, Func f )
722 scoped_node_ptr pNode( alloc_node( key ));
724 if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ f( node_to_value(node) ); } )) {
731 template <typename Q, typename Compare, typename Func>
732 bool erase_at( head_type& refHead, const Q& key, Compare cmp, Func f )
734 return base_class::erase_at( &refHead, key, cmp, [&f](node_type const& node){ f( node_to_value(node) ); } );
737 template <typename Q, typename Compare>
738 bool extract_at( head_type& refHead, typename gc::Guard& dest, Q const& key, Compare cmp )
740 return base_class::extract_at( &refHead, dest, key, cmp );
743 template <typename Q, typename Func>
744 std::pair<bool, bool> ensure_at( head_type& refHead, const Q& key, Func f )
746 scoped_node_ptr pNode( alloc_node( key ));
748 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
749 [&f, &key](bool bNew, node_type& node, node_type&){f( bNew, node_to_value(node), key ); });
750 if ( ret.first && ret.second )
756 template <typename Q, typename Compare>
757 bool find_at( head_type& refHead, Q const& key, Compare cmp )
759 return base_class::find_at( &refHead, key, cmp );
762 template <typename Q, typename Compare, typename Func>
763 bool find_at( head_type& refHead, Q& val, Compare cmp, Func f )
765 return base_class::find_at( &refHead, val, cmp, [&f](node_type& node, Q& val){ f( node_to_value(node), val ); });
768 template <typename Q, typename Compare>
769 bool get_at( head_type& refHead, typename gc::Guard& guard, Q const& key, Compare cmp )
771 return base_class::get_at( &refHead, guard, key, cmp );
777 }} // namespace cds::container
779 #endif // #ifndef __CDS_CONTAINER_IMPL_LAZY_LIST_H