3 #ifndef __CDS_INTRUSIVE_IMPL_LAZY_LIST_H
4 #define __CDS_INTRUSIVE_IMPL_LAZY_LIST_H
6 #include <mutex> // unique_lock
7 #include <cds/intrusive/details/lazy_list_base.h>
9 namespace cds { namespace intrusive {
11 /// Lazy ordered single-linked list
12 /** @ingroup cds_intrusive_list
13 \anchor cds_intrusive_LazyList_hp
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.
28 - \p GC - Garbage collector used. Note the \p GC must be the same as the GC used for item type \p T (see lazy_list::node).
29 - \p T - type to be stored in the list. The type must be based on lazy_list::node (for lazy_list::base_hook)
30 or it must have a member of type lazy_list::node (for lazy_list::member_hook).
31 - \p Traits - type traits. See lazy_list::traits for explanation.
32 It is possible to declare option-based list with cds::intrusive::lazy_list::make_traits metafunction istead of \p Traits template
33 argument. For example, the following traits-based declaration of \p gc::HP lazy list
35 #include <cds/intrusive/lazy_list_hp.h>
36 // Declare item stored in your list
37 struct item: public cds::intrusive::lazy_list::node< cds::gc::HP >
40 // Declare comparator for the item
41 struct my_compare { ... }
44 struct my_traits: public cds::intrusive::lazy_list::traits
46 typedef cds::intrusive::lazy_list::base_hook< cds::opt::gc< cds::gc::HP > > hook;
47 typedef my_compare compare;
50 // Declare traits-based list
51 typedef cds::intrusive::LazyList< cds::gc::HP, item, my_traits > traits_based_list;
53 is equivalent for the following option-based list
55 #include <cds/intrusive/lazy_list_hp.h>
57 // item struct and my_compare are the same
59 // Declare option-based list
60 typedef cds::intrusive::LazyList< cds::gc::HP, item,
61 typename cds::intrusive::lazy_list::make_traits<
62 cds::intrusive::opt::hook< cds::intrusive::lazy_list::base_hook< cds::opt::gc< cds::gc::HP > > > // hook option
63 ,cds::intrusive::opt::compare< my_compare > // item comparator option
69 There are different specializations of this template for each garbage collecting schema used.
70 You should select GC needed and include appropriate .h-file:
71 - for gc::HP: \code #include <cds/intrusive/lazy_list_hp.h> \endcode
72 - for gc::DHP: \code #include <cds/intrusive/lazy_list_dhp.h> \endcode
73 - for gc::nogc: \code #include <cds/intrusive/lazy_list_nogc.h> \endcode
74 - for \ref cds_urcu_type "RCU" - see \ref cds_intrusive_LazyList_rcu "LazyList RCU specialization"
76 Then, you should incorporate lazy_list::node into your struct \p T and provide
77 appropriate \p lazy_list::traits::hook in your \p Traits template parameters. Usually, for \p Traits
78 a struct based on \p lazy_list::traits should be defined.
80 Example for gc::DHP and base hook:
82 // Include GC-related lazy list specialization
83 #include <cds/intrusive/lazy_list_dhp.h>
85 // Data stored in lazy list
86 struct my_data: public cds::intrusive::lazy_list::node< cds::gc::DHP >
95 // my_data comparing functor
97 int operator()( const my_data& d1, const my_data& d2 )
99 return d1.strKey.compare( d2.strKey );
102 int operator()( const my_data& d, const std::string& s )
104 return d.strKey.compare(s);
107 int operator()( const std::string& s, const my_data& d )
109 return s.compare( d.strKey );
114 struct my_traits: public cds::intrusive::lazy_list::traits
116 typedef cds::intrusive::lazy_list::base_hook< cds::opt::gc< cds::gc::DHP > > hook;
117 typedef my_data_cmp compare;
121 typedef cds::intrusive::LazyList< cds::gc::DHP, my_data, my_traits > traits_based_list;
124 Equivalent option-based code:
126 // GC-related specialization
127 #include <cds/intrusive/lazy_list_dhp.h>
136 // Declare option-based list
137 typedef cds::intrusive::LazyList< cds::gc::DHP
139 , typename cds::intrusive::lazy_list::make_traits<
140 cds::intrusive::opt::hook< cds::intrusive::lazy_list::base_hook< cds::opt::gc< cds::gc::DHP > > >
141 ,cds::intrusive::opt::compare< my_data_cmp >
150 #ifdef CDS_DOXYGEN_INVOKED
151 ,class Traits = lazy_list::traits
159 typedef GC gc; ///< Garbage collector
160 typedef T value_type; ///< type of value stored in the list
161 typedef Traits traits; ///< Traits template parameter
163 typedef typename traits::hook hook; ///< hook type
164 typedef typename hook::node_type node_type; ///< node type
166 # ifdef CDS_DOXYGEN_INVOKED
167 typedef implementation_defined key_comparator ; ///< key comparison functor based on opt::compare and opt::less option setter.
169 typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
172 typedef typename traits::disposer disposer; ///< disposer
173 typedef typename get_node_traits< value_type, node_type, hook>::type node_traits; ///< node traits
174 typedef typename lazy_list::get_link_checker< node_type, traits::link_checker >::type link_checker; ///< link checker
176 typedef typename traits::back_off back_off ; ///< back-off strategy
177 typedef typename traits::item_counter item_counter ; ///< Item counting policy used
178 typedef typename traits::memory_model memory_model; ///< C++ memory ordering (see \p lazy_list::traits::memory_model)
180 typedef cds::gc::guarded_ptr< gc, value_type > guarded_ptr; ///< Guarded pointer
183 // Rebind traits (split-list support)
184 template <typename... Options>
185 struct rebind_traits {
189 , typename cds::opt::make_options< traits, Options...>::type
195 typedef typename node_type::marked_ptr marked_node_ptr; ///< Node marked pointer
196 typedef node_type * auxiliary_head; ///< Auxiliary head type (for split-list support)
203 item_counter m_ItemCounter ; ///< Item counter
206 struct clean_disposer {
207 void operator()( value_type * p )
209 lazy_list::node_cleaner<gc, node_type, memory_model>()( node_traits::to_node_ptr( p ) );
214 /// Position pointer for item search
216 node_type * pPred ; ///< Previous node
217 node_type * pCur ; ///< Current node
219 typename gc::template GuardArray<2> guards ; ///< Guards array
226 /// Locks nodes \p pPred and \p pCur
229 pPred->m_Lock.lock();
233 /// Unlocks nodes \p pPred and \p pCur
236 pCur->m_Lock.unlock();
237 pPred->m_Lock.unlock();
241 class auto_lock_position {
244 auto_lock_position( position& pos )
249 ~auto_lock_position()
258 void link_node( node_type * pNode, node_type * pPred, node_type * pCur )
260 assert( pPred->m_pNext.load(memory_model::memory_order_relaxed).ptr() == pCur );
262 pNode->m_pNext.store( marked_node_ptr(pCur), memory_model::memory_order_release );
263 pPred->m_pNext.store( marked_node_ptr(pNode), memory_model::memory_order_release );
266 void unlink_node( node_type * pPred, node_type * pCur, node_type * pHead )
268 assert( pPred->m_pNext.load(memory_model::memory_order_relaxed).ptr() == pCur );
270 node_type * pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed).ptr();
271 //pCur->m_pNext.store( marked_node_ptr( pNext, 1), memory_model::memory_order_release) ; // logically deleting
272 pCur->m_pNext.store( marked_node_ptr( pHead, 1 ), memory_model::memory_order_release ) ; // logical removal + back-link for search
273 pPred->m_pNext.store( marked_node_ptr( pNext ), memory_model::memory_order_release); // physically deleting
274 //pCur->m_pNext.store( marked_node_ptr( pHead, 1 ), memory_model::memory_order_release ) ; // back-link for search
277 void retire_node( node_type * pNode )
279 assert( pNode != nullptr );
280 gc::template retire<clean_disposer>( node_traits::to_value_ptr( *pNode ) );
286 template <bool IsConst>
289 friend class LazyList;
292 value_type * m_pNode;
293 typename gc::Guard m_Guard;
297 assert( m_pNode != nullptr );
300 typename gc::Guard g;
301 node_type * pCur = node_traits::to_node_ptr( m_pNode );
302 if ( pCur->m_pNext.load( memory_model::memory_order_relaxed ).ptr() != nullptr ) { // if pCur is not tail node
305 pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed).ptr();
306 g.assign( node_traits::to_value_ptr( pNext ));
307 } while ( pNext != pCur->m_pNext.load(memory_model::memory_order_relaxed).ptr() );
309 m_pNode = m_Guard.assign( g.template get<value_type>() );
316 if ( m_pNode != nullptr ) {
317 typename gc::Guard g;
318 node_type * pNode = node_traits::to_node_ptr( m_pNode );
320 // Dummy tail node could not be marked
321 while ( pNode->is_marked() ) {
322 node_type * p = pNode->m_pNext.load(memory_model::memory_order_relaxed).ptr();
323 g.assign( node_traits::to_value_ptr( p ));
324 if ( p == pNode->m_pNext.load(memory_model::memory_order_relaxed).ptr() )
327 if ( pNode != node_traits::to_node_ptr( m_pNode ) )
328 m_pNode = m_Guard.assign( g.template get<value_type>() );
332 iterator_type( node_type * pNode )
334 m_pNode = m_Guard.assign( node_traits::to_value_ptr( pNode ));
339 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
340 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
346 iterator_type( iterator_type const& src )
349 m_pNode = m_Guard.assign( src.m_pNode );
355 value_ptr operator ->() const
360 value_ref operator *() const
362 assert( m_pNode != nullptr );
367 iterator_type& operator ++()
374 iterator_type& operator = (iterator_type const& src)
376 m_pNode = src.m_pNode;
377 m_Guard.assign( m_pNode );
382 bool operator ==(iterator_type<C> const& i ) const
384 return m_pNode == i.m_pNode;
387 bool operator !=(iterator_type<C> const& i ) const
389 return m_pNode != i.m_pNode;
397 The forward iterator for lazy list has some features:
398 - it has no post-increment operator
399 - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
400 For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
401 may be thrown if a limit of guard count per thread is exceeded.
402 - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
403 - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
404 deleting operations it is no guarantee that you iterate all item in the list.
406 Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator on the concurrent container
407 for debug purpose only.
409 typedef iterator_type<false> iterator;
410 /// Const forward iterator
412 For iterator's features and requirements see \ref iterator
414 typedef iterator_type<true> const_iterator;
416 /// Returns a forward iterator addressing the first element in a list
418 For empty list \code begin() == end() \endcode
422 iterator it( &m_Head );
423 ++it ; // skip dummy head
427 /// Returns an iterator that addresses the location succeeding the last element in a list
429 Do not use the value returned by <tt>end</tt> function to access any item.
431 The returned value can be used only to control reaching the end of the list.
432 For empty list \code begin() == end() \endcode
436 return iterator( &m_Tail );
439 /// Returns a forward const iterator addressing the first element in a list
441 const_iterator begin() const
443 return get_const_begin();
445 const_iterator cbegin() const
447 return get_const_begin();
451 /// Returns an const iterator that addresses the location succeeding the last element in a list
453 const_iterator end() const
455 return get_const_end();
457 const_iterator cend() const
459 return get_const_end();
465 const_iterator get_const_begin() const
467 const_iterator it( const_cast<node_type *>( &m_Head ));
468 ++it ; // skip dummy head
471 const_iterator get_const_end() const
473 return const_iterator( const_cast<node_type *>(&m_Tail) );
478 /// Default constructor initializes empty list
481 static_assert( (std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type" );
482 m_Head.m_pNext.store( marked_node_ptr( &m_Tail ), memory_model::memory_order_relaxed );
485 /// Destroys the list object
489 assert( m_Head.m_pNext.load( memory_model::memory_order_relaxed ).ptr() == &m_Tail );
490 m_Head.m_pNext.store( marked_node_ptr(), memory_model::memory_order_relaxed );
495 The function inserts \p val in the list if the list does not contain
496 an item with key equal to \p val.
498 Returns \p true if \p val is linked into the list, \p false otherwise.
500 bool insert( value_type& val )
502 return insert_at( &m_Head, val );
507 This function is intended for derived non-intrusive containers.
509 The function allows to split new item creating into two part:
510 - create item with key only
511 - insert new item into the list
512 - if inserting is success, calls \p f functor to initialize value-field of \p val.
514 The functor signature is:
516 void func( value_type& val );
518 where \p val is the item inserted.
519 While the functor \p f is called the item \p val is locked so
520 the functor has an exclusive access to the item.
521 The user-defined functor is called only if the inserting is success.
523 template <typename Func>
524 bool insert( value_type& val, Func f )
526 return insert_at( &m_Head, val, f );
529 /// Ensures that the \p item exists in the list
531 The operation performs inserting or changing data with lock-free manner.
533 If the item \p val not found in the list, then \p val is inserted into the list.
534 Otherwise, the functor \p func is called with item found.
535 The functor signature is:
537 void func( bool bNew, value_type& item, value_type& val );
540 - \p bNew - \p true if the item has been inserted, \p false otherwise
541 - \p item - item of the list
542 - \p val - argument \p val passed into the \p ensure function
543 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
544 refer to the same thing.
546 The functor may change non-key fields of the \p item.
547 While the functor \p f is working the item \p item is locked,
548 so \p f has exclusive access to the item.
550 Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
551 \p second is \p true if new item has been added or \p false if the item with \p key
552 already is in the list.
554 template <typename Func>
555 std::pair<bool, bool> ensure( value_type& val, Func func )
557 return ensure_at( &m_Head, val, func );
560 /// Unlinks the item \p val from the list
562 The function searches the item \p val in the list and unlink it from the list
563 if it is found and it is equal to \p val.
565 Difference between \ref erase and \p unlink functions: \p erase finds <i>a key</i>
566 and deletes the item found. \p unlink finds an item by key and deletes it
567 only if \p val is an item of that list, i.e. the pointer to item found
568 is equal to <tt> &val </tt>.
570 The function returns \p true if success and \p false otherwise.
572 bool unlink( value_type& val )
574 return unlink_at( &m_Head, val );
577 /// Deletes the item from the list
578 /** \anchor cds_intrusive_LazyList_hp_erase_val
579 The function searches an item with key equal to \p key in the list,
580 unlinks it from the list, and returns \p true.
581 If the item with the key equal to \p key is not found the function return \p false.
583 template <typename Q>
584 bool erase( Q const& key )
586 return erase_at( &m_Head, key, key_comparator() );
589 /// Deletes the item from the list using \p pred predicate for searching
591 The function is an analog of \ref cds_intrusive_LazyList_hp_erase_val "erase(Q const&)"
592 but \p pred is used for key comparing.
593 \p Less functor has the interface like \p std::less.
594 \p pred must imply the same element order as the comparator used for building the list.
596 template <typename Q, typename Less>
597 bool erase_with( Q const& key, Less pred )
599 return erase_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>() );
602 /// Deletes the item from the list
603 /** \anchor cds_intrusive_LazyList_hp_erase_func
604 The function searches an item with key equal to \p key in the list,
605 call \p func functor with item found, unlinks it from the list, and returns \p true.
606 The \p Func interface is
609 void operator()( value_type const& item );
613 If \p key is not found the function return \p false.
615 template <typename Q, typename Func>
616 bool erase( const Q& key, Func func )
618 return erase_at( &m_Head, key, key_comparator(), func );
621 /// Deletes the item from the list using \p pred predicate for searching
623 The function is an analog of \ref cds_intrusive_LazyList_hp_erase_func "erase(Q const&, Func)"
624 but \p pred is used for key comparing.
625 \p Less functor has the interface like \p std::less.
626 \p pred must imply the same element order as the comparator used for building the list.
628 template <typename Q, typename Less, typename Func>
629 bool erase_with( const Q& key, Less pred, Func func )
631 return erase_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), func );
634 /// Extracts the item from the list with specified \p key
635 /** \anchor cds_intrusive_LazyList_hp_extract
636 The function searches an item with key equal to \p key,
637 unlinks it from the list, and returns it in \p dest parameter.
638 If the item with key equal to \p key is not found the function returns \p false.
640 Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
642 The \ref disposer specified in \p Traits class template parameter is called automatically
643 by garbage collector \p GC specified in class' template parameters when returned \ref guarded_ptr object
644 will be destroyed or released.
645 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
649 typedef cds::intrusive::LazyList< cds::gc::HP, foo, my_traits > ord_list;
653 ord_list::guarded_ptr gp;
654 theList.extract( gp, 5 );
658 // Destructor of gp releases internal HP guard
662 template <typename Q>
663 bool extract( guarded_ptr& dest, Q const& key )
665 return extract_at( &m_Head, dest.guard(), key, key_comparator() );
668 /// Extracts the item from the list with comparing functor \p pred
670 The function is an analog of \ref cds_intrusive_LazyList_hp_extract "extract(guarded_ptr&, Q const&)"
671 but \p pred predicate is used for key comparing.
673 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
675 \p pred must imply the same element order as the comparator used for building the list.
677 template <typename Q, typename Less>
678 bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
680 return extract_at( &m_Head, dest.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
683 /// Finds the key \p key
684 /** \anchor cds_intrusive_LazyList_hp_find
685 The function searches the item with key equal to \p key and calls the functor \p f for item found.
686 The interface of \p Func functor is:
689 void operator()( value_type& item, Q& key );
692 where \p item is the item found, \p key is the <tt>find</tt> function argument.
694 The functor may change non-key fields of \p item.
695 While the functor \p f is calling the item \p item is locked.
697 The function returns \p true if \p key is found, \p false otherwise.
699 template <typename Q, typename Func>
700 bool find( Q& key, Func f )
702 return find_at( &m_Head, key, key_comparator(), f );
705 template <typename Q, typename Func>
706 bool find( Q const& key, Func f )
708 return find_at( &m_Head, key, key_comparator(), f );
712 /// Finds the key \p key using \p pred predicate for searching
714 The function is an analog of \ref cds_intrusive_LazyList_hp_find "find(Q&, Func)"
715 but \p pred is used for key comparing.
716 \p Less functor has the interface like \p std::less.
717 \p pred must imply the same element order as the comparator used for building the list.
719 template <typename Q, typename Less, typename Func>
720 bool find_with( Q& key, Less pred, Func f )
722 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), f );
725 template <typename Q, typename Less, typename Func>
726 bool find_with( Q const& key, Less pred, Func f )
728 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), f );
732 /// Finds the key \p key
733 /** \anchor cds_intrusive_LazyList_hp_find_val
734 The function searches the item with key equal to \p key
735 and returns \p true if it is found, and \p false otherwise
737 template <typename Q>
738 bool find( Q const& key )
740 return find_at( &m_Head, key, key_comparator() );
743 /// Finds \p key using \p pred predicate for searching
745 The function is an analog of \ref cds_intrusive_LazyList_hp_find_val "find(Q const&)"
746 but \p pred is used for key comparing.
747 \p Less functor has the interface like \p std::less.
748 \p pred must imply the same element order as the comparator used for building the list.
750 template <typename Q, typename Less>
751 bool find_with( Q const& key, Less pred )
753 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>() );
756 /// Finds \p key and return the item found
757 /** \anchor cds_intrusive_LazyList_hp_get
758 The function searches the item with key equal to \p key
759 and assigns the item found to guarded pointer \p ptr.
760 The function returns \p true if \p key is found, and \p false otherwise.
761 If \p key is not found the \p ptr parameter is not changed.
763 The \ref disposer specified in \p Traits class template parameter is called
764 by garbage collector \p GC automatically when returned \ref guarded_ptr object
765 will be destroyed or released.
766 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
770 typedef cds::intrusive::LazyList< cds::gc::HP, foo, my_traits > ord_list;
774 ord_list::guarded_ptr gp;
775 if ( theList.get( gp, 5 )) {
779 // Destructor of guarded_ptr releases internal HP guard
783 Note the compare functor specified for class \p Traits template parameter
784 should accept a parameter of type \p Q that can be not the same as \p value_type.
786 template <typename Q>
787 bool get( guarded_ptr& ptr, Q const& key )
789 return get_at( &m_Head, ptr.guard(), key, key_comparator() );
792 /// Finds \p key and return the item found
794 The function is an analog of \ref cds_intrusive_LazyList_hp_get "get( guarded_ptr& ptr, Q const&)"
795 but \p pred is used for comparing the keys.
797 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
799 \p pred must imply the same element order as the comparator used for building the list.
801 template <typename Q, typename Less>
802 bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
804 return get_at( &m_Head, ptr.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
810 typename gc::Guard guard;
813 h = m_Head.m_pNext.load( memory_model::memory_order_relaxed );
814 guard.assign( node_traits::to_value_ptr( h.ptr() ));
815 if ( m_Head.m_pNext.load(memory_model::memory_order_acquire) == h ) {
816 m_Head.m_Lock.lock();
819 unlink_node( &m_Head, h.ptr(), &m_Head );
822 m_Head.m_Lock.unlock();
824 retire_node( h.ptr() ) ; // free node
829 /// Checks if the list is empty
832 return m_Head.m_pNext.load( memory_model::memory_order_relaxed ).ptr() == &m_Tail;
835 /// Returns list's item count
837 The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
838 this function always returns 0.
840 @note Even if you use real item counter and it returns 0, this fact does not mean that the list
841 is empty. To check list emptyness use \p empty() method.
845 return m_ItemCounter.value();
850 // split-list support
851 bool insert_aux_node( node_type * pNode )
853 return insert_aux_node( &m_Head, pNode );
856 // split-list support
857 bool insert_aux_node( node_type * pHead, node_type * pNode )
859 assert( pNode != nullptr );
861 // Hack: convert node_type to value_type.
862 // In principle, auxiliary node cannot be reducible to value_type
863 // We assume that internal comparator can correctly distinguish aux and regular node.
864 return insert_at( pHead, *node_traits::to_value_ptr( pNode ) );
867 bool insert_at( node_type * pHead, value_type& val )
869 link_checker::is_empty( node_traits::to_node_ptr( val ) );
874 search( pHead, val, pos, key_comparator() );
876 auto_lock_position alp( pos );
877 if ( validate( pos.pPred, pos.pCur )) {
878 if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
879 // failed: key already in list
883 link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
892 template <typename Func>
893 bool insert_at( node_type * pHead, value_type& val, Func f )
895 link_checker::is_empty( node_traits::to_node_ptr( val ) );
900 search( pHead, val, pos, key_comparator() );
902 auto_lock_position alp( pos );
903 if ( validate( pos.pPred, pos.pCur )) {
904 if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
905 // failed: key already in list
909 link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
919 template <typename Func>
920 std::pair<bool, bool> ensure_at( node_type * pHead, value_type& val, Func func )
926 search( pHead, val, pos, key_comparator() );
928 auto_lock_position alp( pos );
929 if ( validate( pos.pPred, pos.pCur )) {
930 if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
931 // key already in the list
933 func( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
934 return std::make_pair( true, false );
938 link_checker::is_empty( node_traits::to_node_ptr( val ) );
940 link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
941 func( true, val, val );
943 return std::make_pair( true, true );
950 bool unlink_at( node_type * pHead, value_type& val )
956 search( pHead, val, pos, key_comparator() );
960 auto_lock_position alp( pos );
961 if ( validate( pos.pPred, pos.pCur ) ) {
962 if ( pos.pCur != &m_Tail
963 && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0
964 && node_traits::to_value_ptr( pos.pCur ) == &val )
967 unlink_node( pos.pPred, pos.pCur, pHead );
977 retire_node( pos.pCur );
986 template <typename Q, typename Compare, typename Func>
987 bool erase_at( node_type * pHead, const Q& val, Compare cmp, Func f, position& pos )
990 search( pHead, val, pos, cmp );
994 auto_lock_position alp( pos );
995 if ( validate( pos.pPred, pos.pCur )) {
996 if ( pos.pCur != &m_Tail && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 ) {
998 unlink_node( pos.pPred, pos.pCur, pHead );
999 f( *node_traits::to_value_ptr( *pos.pCur ) );
1009 if ( nResult > 0 ) {
1010 retire_node( pos.pCur );
1019 template <typename Q, typename Compare, typename Func>
1020 bool erase_at( node_type * pHead, const Q& val, Compare cmp, Func f )
1023 return erase_at( pHead, val, cmp, f, pos );
1026 template <typename Q, typename Compare>
1027 bool erase_at( node_type * pHead, const Q& val, Compare cmp )
1030 return erase_at( pHead, val, cmp, [](value_type const &){}, pos );
1033 template <typename Q, typename Compare>
1034 bool extract_at( node_type * pHead, typename gc::Guard& gp, const Q& val, Compare cmp )
1037 if ( erase_at( pHead, val, cmp, [](value_type const &){}, pos )) {
1038 gp.assign( pos.guards.template get<value_type>(position::guard_current_item) );
1044 template <typename Q, typename Compare, typename Func>
1045 bool find_at( node_type * pHead, Q& val, Compare cmp, Func f )
1049 search( pHead, val, pos, cmp );
1050 if ( pos.pCur != &m_Tail ) {
1051 std::unique_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
1052 if ( !pos.pCur->is_marked()
1053 && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 )
1055 f( *node_traits::to_value_ptr( *pos.pCur ), val );
1062 template <typename Q, typename Compare>
1063 bool find_at( node_type * pHead, Q const& val, Compare cmp )
1067 search( pHead, val, pos, cmp );
1068 return pos.pCur != &m_Tail
1069 && !pos.pCur->is_marked()
1070 && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0;
1073 template <typename Q, typename Compare>
1074 bool get_at( node_type * pHead, typename gc::Guard& gp, Q const& val, Compare cmp )
1078 search( pHead, val, pos, cmp );
1079 if ( pos.pCur != &m_Tail
1080 && !pos.pCur->is_marked()
1081 && cmp( *node_traits::to_value_ptr( *pos.pCur ), val ) == 0 )
1083 gp.assign( pos.guards.template get<value_type>( position::guard_current_item ));
1093 template <typename Q, typename Compare>
1094 void search( node_type * pHead, const Q& key, position& pos, Compare cmp )
1096 const node_type * pTail = &m_Tail;
1098 marked_node_ptr pCur( pHead );
1099 marked_node_ptr pPrev( pHead );
1103 while ( pCur.ptr() != pTail )
1105 if ( pCur.ptr() != pHead ) {
1106 if ( cmp( *node_traits::to_value_ptr( *pCur.ptr() ), key ) >= 0 )
1110 pos.guards.copy( position::guard_prev_item, position::guard_current_item );
1114 pCur = pPrev->m_pNext.load(memory_model::memory_order_relaxed);
1115 pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ));
1116 if ( pCur == pPrev->m_pNext.load(memory_model::memory_order_acquire) )
1120 assert( pCur.ptr() != nullptr );
1123 pos.pCur = pCur.ptr();
1124 pos.pPred = pPrev.ptr();
1127 static bool validate( node_type * pPred, node_type * pCur )
1129 return !pPred->is_marked()
1130 && !pCur->is_marked()
1131 && pPred->m_pNext.load(memory_model::memory_order_relaxed) == pCur;
1136 }} // namespace cds::intrusive
1138 #endif // __CDS_INTRUSIVE_IMPL_LAZY_LIST_H