3 #ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
4 #define __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
6 #include <cds/intrusive/details/michael_list_base.h>
7 #include <cds/gc/guarded_ptr.h>
8 #include <cds/details/make_const_type.h>
10 namespace cds { namespace intrusive {
12 /// Michael's lock-free ordered single-linked list
13 /** @ingroup cds_intrusive_list
14 \anchor cds_intrusive_MichaelList_hp
16 Usually, ordered single-linked list is used as a building block for the hash table implementation.
17 The complexity of searching is <tt>O(N)</tt>.
20 - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
23 - \p GC - Garbage collector used. Note the \p GC must be the same as the GC used for item type \p T (see \p michael_list::node).
24 - \p T - type to be stored in the list. The type must be based on \p michael_list::node (for \p michael_list::base_hook)
25 or it must have a member of type \p michael_list::node (for \p michael_list::member_hook).
26 - \p Traits - type traits, default is \p michael_list::traits. It is possible to declare option-based
27 list with \p cds::intrusive::michael_list::make_traits metafunction:
28 For example, the following traits-based declaration of \p gc::HP Michael's list
30 #include <cds/intrusive/michael_list_hp.h>
31 // Declare item stored in your list
32 struct item: public cds::intrusive::michael_list::node< cds::gc::HP >
38 // Declare comparator for the item
40 int operator()( item const& i1, item const& i2 ) const
42 return i1.nKey - i2.nKey;
47 struct my_traits: public cds::intrusive::michael_list::traits
49 typedef cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > hook;
50 typedef my_compare compare;
53 // Declare traits-based list
54 typedef cds::intrusive::MichaelList< cds::gc::HP, item, my_traits > traits_based_list;
56 is equivalent for the following option-based list
58 #include <cds/intrusive/michael_list_hp.h>
60 // item struct and my_compare are the same
62 // Declare option-based list
63 typedef cds::intrusive::MichaelList< cds::gc::HP, item,
64 typename cds::intrusive::michael_list::make_traits<
65 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > > // hook option
66 ,cds::intrusive::opt::compare< my_compare > // item comparator option
72 There are different specializations of this template for each garbage collecting schema.
73 You should select GC needed and include appropriate .h-file:
74 - for \p gc::HP: <tt> <cds/intrusive/michael_list_hp.h> </tt>
75 - for \p gc::DHP: <tt> <cds/intrusive/michael_list_dhp.h> </tt>
76 - for \ref cds_urcu_gc "RCU type" - see \ref cds_intrusive_MichaelList_rcu "RCU-based MichaelList"
77 - for \p gc::nogc: <tt> <cds/intrusive/michael_list_nogc.h> </tt>
78 See \ref cds_intrusive_MichaelList_nogc "non-GC MichaelList"
80 Then, you should incorporate \p michael_list::node into your struct \p T and provide
81 appropriate \p michael_list::traits::hook in your \p Traits template parameters. Usually, for \p Traits you
82 define a struct based on \p michael_list::traits.
84 Example for \p gc::DHP and base hook:
86 // Include GC-related Michael's list specialization
87 #include <cds/intrusive/michael_list_dhp.h>
89 // Data stored in Michael's list
90 struct my_data: public cds::intrusive::michael_list::node< cds::gc::DHP >
99 // my_data comparing functor
101 int operator()( const my_data& d1, const my_data& d2 )
103 return d1.strKey.compare( d2.strKey );
106 int operator()( const my_data& d, const std::string& s )
108 return d.strKey.compare(s);
111 int operator()( const std::string& s, const my_data& d )
113 return s.compare( d.strKey );
119 struct my_traits: public cds::intrusive::michael_list::traits
121 typedef cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP > > hook;
122 typedef my_data_cmp compare;
126 typedef cds::intrusive::MichaelList< cds::gc::DHP, my_data, my_traits > traits_based_list;
129 Equivalent option-based code:
131 // GC-related specialization
132 #include <cds/intrusive/michael_list_dhp.h>
141 // Declare option-based list
142 typedef cds::intrusive::MichaelList< cds::gc::DHP
144 , typename cds::intrusive::michael_list::make_traits<
145 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP > > >
146 ,cds::intrusive::opt::compare< my_data_cmp >
155 #ifdef CDS_DOXYGEN_INVOKED
156 ,class Traits = michael_list::traits
164 typedef T value_type; ///< type of value stored in the list
165 typedef Traits traits; ///< Traits template parameter
167 typedef typename traits::hook hook; ///< hook type
168 typedef typename hook::node_type node_type; ///< node type
170 # ifdef CDS_DOXYGEN_INVOKED
171 typedef implementation_defined key_comparator ; ///< key comparison functor based on opt::compare and opt::less option setter.
173 typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
176 typedef typename traits::disposer disposer; ///< disposer used
177 typedef typename get_node_traits< value_type, node_type, hook>::type node_traits ; ///< node traits
178 typedef typename michael_list::get_link_checker< node_type, traits::link_checker >::type link_checker; ///< link checker
180 typedef GC gc ; ///< Garbage collector
181 typedef typename traits::back_off back_off; ///< back-off strategy
182 typedef typename traits::item_counter item_counter; ///< Item counting policy used
183 typedef typename traits::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
185 typedef cds::gc::guarded_ptr< gc, value_type > guarded_ptr; ///< Guarded pointer
188 // Rebind traits (split-list support)
189 template <typename... Options>
190 struct rebind_traits {
194 , typename cds::opt::make_options< traits, Options...>::type
200 typedef typename node_type::atomic_marked_ptr atomic_node_ptr; ///< Atomic node pointer
201 typedef typename node_type::marked_ptr marked_node_ptr; ///< Node marked pointer
203 typedef atomic_node_ptr auxiliary_head; ///< Auxiliary head type (for split-list support)
205 atomic_node_ptr m_pHead; ///< Head pointer
206 item_counter m_ItemCounter; ///< Item counter
209 /// Position pointer for item search
211 atomic_node_ptr * pPrev ; ///< Previous node
212 node_type * pCur ; ///< Current node
213 node_type * pNext ; ///< Next node
215 typename gc::template GuardArray<3> guards ; ///< Guards array
224 struct clean_disposer {
225 void operator()( value_type * p )
227 michael_list::node_cleaner<gc, node_type, memory_model>()( node_traits::to_node_ptr( p ) );
235 static void retire_node( node_type * pNode )
237 assert( pNode != nullptr );
238 gc::template retire<clean_disposer>( node_traits::to_value_ptr( *pNode ) );
241 static bool link_node( node_type * pNode, position& pos )
243 assert( pNode != nullptr );
244 link_checker::is_empty( pNode );
246 marked_node_ptr cur(pos.pCur);
247 pNode->m_pNext.store( cur, memory_model::memory_order_relaxed );
248 return pos.pPrev->compare_exchange_strong( cur, marked_node_ptr(pNode), memory_model::memory_order_release, atomics::memory_order_relaxed );
251 static bool unlink_node( position& pos )
253 assert( pos.pPrev != nullptr );
254 assert( pos.pCur != nullptr );
256 // Mark the node (logical deleting)
257 marked_node_ptr next(pos.pNext, 0);
258 if ( pos.pCur->m_pNext.compare_exchange_strong( next, marked_node_ptr(pos.pNext, 1), memory_model::memory_order_release, atomics::memory_order_relaxed )) {
259 // physical deletion may be performed by search function if it detects that a node is logically deleted (marked)
260 // CAS may be successful here or in other thread that searching something
261 marked_node_ptr cur(pos.pCur);
262 if ( pos.pPrev->compare_exchange_strong( cur, marked_node_ptr( pos.pNext ), memory_model::memory_order_release, atomics::memory_order_relaxed ))
263 retire_node( pos.pCur );
272 template <bool IsConst>
275 friend class MichaelList;
278 value_type * m_pNode;
279 typename gc::Guard m_Guard;
284 typename gc::Guard g;
285 node_type * pCur = node_traits::to_node_ptr( *m_pNode );
287 marked_node_ptr pNext;
289 pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed);
290 g.assign( node_traits::to_value_ptr( pNext.ptr() ));
291 } while ( pNext != pCur->m_pNext.load(memory_model::memory_order_acquire) );
294 m_pNode = m_Guard.assign( g.template get<value_type>() );
303 iterator_type( atomic_node_ptr const& pNode )
306 marked_node_ptr p = pNode.load(memory_model::memory_order_relaxed);
308 m_pNode = m_Guard.assign( node_traits::to_value_ptr( p.ptr() ) );
314 if ( p == pNode.load(memory_model::memory_order_acquire) )
320 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
321 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
327 iterator_type( iterator_type const& src )
330 m_pNode = m_Guard.assign( src.m_pNode );
336 value_ptr operator ->() const
341 value_ref operator *() const
343 assert( m_pNode != nullptr );
348 iterator_type& operator ++()
354 iterator_type& operator = (iterator_type const& src)
356 m_pNode = src.m_pNode;
357 m_Guard.assign( m_pNode );
363 void operator ++(int)
370 bool operator ==(iterator_type<C> const& i ) const
372 return m_pNode == i.m_pNode;
375 bool operator !=(iterator_type<C> const& i ) const
377 return m_pNode != i.m_pNode;
385 The forward iterator for Michael's list has some features:
386 - it has no post-increment operator
387 - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
388 For some GC (like as \p gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard"
389 may be thrown if the limit of guard count per thread is exceeded.
390 - The iterator cannot be moved across thread boundary since it contains thread-private GC's guard.
391 - Iterator ensures thread-safety even if you delete the item the iterator points to. However, in case of concurrent
392 deleting operations there is no guarantee that you iterate all item in the list.
394 Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator on the concurrent container
395 for debug purpose only.
397 The iterator interface:
401 // Default constructor
405 iterator( iterator const& src );
407 // Dereference operator
408 value_type * operator ->() const;
410 // Dereference operator
411 value_type& operator *() const;
413 // Preincrement operator
414 iterator& operator ++();
416 // Assignment operator
417 iterator& operator = (iterator const& src);
419 // Equality operators
420 bool operator ==(iterator const& i ) const;
421 bool operator !=(iterator const& i ) const;
425 typedef iterator_type<false> iterator;
426 /// Const forward iterator
428 For iterator's features and requirements see \ref iterator
430 typedef iterator_type<true> const_iterator;
432 /// Returns a forward iterator addressing the first element in a list
434 For empty list \code begin() == end() \endcode
438 return iterator( m_pHead );
441 /// Returns an iterator that addresses the location succeeding the last element in a list
443 Do not use the value returned by <tt>end</tt> function to access any item.
444 Internally, <tt>end</tt> returning value equals to \p nullptr.
446 The returned value can be used only to control reaching the end of the list.
447 For empty list <tt>begin() == end()</tt>
454 /// Returns a forward const iterator addressing the first element in a list
455 const_iterator cbegin()
457 return const_iterator( m_pHead );
460 /// Returns a forward const iterator addressing the first element in a list
461 const_iterator begin() const
463 return const_iterator( m_pHead );
466 /// Returns an const iterator that addresses the location succeeding the last element in a list
467 const_iterator end() const
469 return const_iterator();
472 /// Returns an const iterator that addresses the location succeeding the last element in a list
473 const_iterator cend()
475 return const_iterator();
479 /// Default constructor initializes empty list
483 static_assert( (std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type" );
486 /// Destroys the list object
494 The function inserts \p val into the list if the list does not contain
495 an item with key equal to \p val.
497 Returns \p true if \p val has been linked to the list, \p false otherwise.
499 bool insert( value_type& val )
501 return insert_at( m_pHead, val );
506 This function is intended for derived non-intrusive containers.
508 The function allows to split new item creating into two part:
509 - create item with key only
510 - insert new item into the list
511 - if inserting is success, calls \p f functor to initialize value-field of \p val.
513 The functor signature is:
515 void func( value_type& val );
517 where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
518 \p val no any other changes could be made on this list's item by concurrent threads.
519 The user-defined functor is called only if the inserting is success.
521 template <typename Func>
522 bool insert( value_type& val, Func f )
524 return insert_at( m_pHead, val, f );
527 /// Ensures that the \p val exists in the list
529 The operation performs inserting or changing data with lock-free manner.
531 If the item \p val is not found in the list, then \p val is inserted.
532 Otherwise, the functor \p func is called with item found.
533 The functor signature is:
535 void func( bool bNew, value_type& item, value_type& val );
538 - \p bNew - \p true if the item has been inserted, \p false otherwise
539 - \p item - item of the list
540 - \p val - argument \p val passed into the \p ensure function
541 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
542 refers to the same thing.
544 The functor may change non-key fields of the \p item; however, \p func must guarantee
545 that during changing no any other modifications could be made on this item by concurrent threads.
547 Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
548 \p second is \p true if new item has been added or \p false if the item with \p key
549 already is in the list.
551 template <typename Func>
552 std::pair<bool, bool> ensure( value_type& val, Func func )
554 return ensure_at( m_pHead, val, func );
557 /// Unlinks the item \p val from the list
559 The function searches the item \p val in the list and unlinks it from the list
560 if it is found and it is equal to \p val.
562 Difference between \p erase() and \p %unlink(): \p %erase() finds <i>a key</i>
563 and deletes the item found. \p %unlink() finds an item by key and deletes it
564 only if \p val is an item of the list, i.e. the pointer to item found
565 is equal to <tt> &val </tt>.
567 The function returns \p true if success and \p false otherwise.
569 bool unlink( value_type& val )
571 return unlink_at( m_pHead, val );
574 /// Deletes the item from the list
575 /** \anchor cds_intrusive_MichaelList_hp_erase_val
576 The function searches an item with key equal to \p key in the list,
577 unlinks it from the list, and returns \p true.
578 If \p key is not found the function return \p false.
580 template <typename Q>
581 bool erase( Q const& key )
583 return erase_at( m_pHead, key, key_comparator() );
586 /// Deletes the item from the list using \p pred predicate for searching
588 The function is an analog of \ref cds_intrusive_MichaelList_hp_erase_val "erase(Q const&)"
589 but \p pred is used for key comparing.
590 \p Less functor has the interface like \p std::less.
591 \p pred must imply the same element order as the comparator used for building the list.
593 template <typename Q, typename Less>
594 bool erase_with( Q const& key, Less pred )
596 return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
599 /// Deletes the item from the list
600 /** \anchor cds_intrusive_MichaelList_hp_erase_func
601 The function searches an item with key equal to \p key in the list,
602 call \p func functor with item found, unlinks it from the list, and returns \p true.
603 The \p Func interface is
606 void operator()( value_type const& item );
609 If \p key is not found the function return \p false, \p func is not called.
611 template <typename Q, typename Func>
612 bool erase( Q const& key, Func func )
614 return erase_at( m_pHead, key, key_comparator(), func );
617 /// Deletes the item from the list using \p pred predicate for searching
619 The function is an analog of \ref cds_intrusive_MichaelList_hp_erase_func "erase(Q const&, Func)"
620 but \p pred is used for key comparing.
621 \p Less functor has the interface like \p std::less.
622 \p pred must imply the same element order as the comparator used for building the list.
624 template <typename Q, typename Less, typename Func>
625 bool erase_with( Q const& key, Less pred, Func f )
627 return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
630 /// Extracts the item from the list with specified \p key
631 /** \anchor cds_intrusive_MichaelList_hp_extract
632 The function searches an item with key equal to \p key,
633 unlinks it from the list, and returns it in \p dest parameter.
634 If the item with key equal to \p key is not found the function returns \p false.
636 Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
638 The \ref disposer specified in \p Traits class template parameter is called automatically
639 by garbage collector \p GC when returned \ref guarded_ptr object will be destroyed or released.
640 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
644 typedef cds::intrusive::MichaelList< cds::gc::HP, foo, my_traits > ord_list;
648 ord_list::guarded_ptr gp;
649 theList.extract( gp, 5 );
653 // Destructor of gp releases internal HP guard
657 template <typename Q>
658 bool extract( guarded_ptr& dest, Q const& key )
660 return extract_at( m_pHead, dest.guard(), key, key_comparator() );
663 /// Extracts the item using compare functor \p pred
665 The function is an analog of \ref cds_intrusive_MichaelList_hp_extract "extract(guarded_ptr&, Q const&)"
666 but \p pred predicate is used for key comparing.
668 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
670 \p pred must imply the same element order as the comparator used for building the list.
672 template <typename Q, typename Less>
673 bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
675 return extract_at( m_pHead, dest.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
678 /// Finds \p key in the list
679 /** \anchor cds_intrusive_MichaelList_hp_find_func
680 The function searches the item with key equal to \p key and calls the functor \p f for item found.
681 The interface of \p Func functor is:
684 void operator()( value_type& item, Q& key );
687 where \p item is the item found, \p key is the <tt>find</tt> function argument.
689 The functor may change non-key fields of \p item. Note that the function is only guarantee
690 that \p item cannot be disposed during functor is executing.
691 The function does not serialize simultaneous access to the \p item. If such access is
692 possible you must provide your own synchronization schema to keep out unsafe item modifications.
694 The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
695 may modify both arguments.
697 The function returns \p true if \p val is found, \p false otherwise.
699 template <typename Q, typename Func>
700 bool find( Q& key, Func f )
702 return find_at( m_pHead, key, key_comparator(), f );
705 /// Finds the \p key using \p pred predicate for searching
707 The function is an analog of \ref cds_intrusive_MichaelList_hp_find_func "find(Q&, Func)"
708 but \p pred is used for key comparing.
709 \p Less functor has the interface like \p std::less.
710 \p pred must imply the same element order as the comparator used for building the list.
712 template <typename Q, typename Less, typename Func>
713 bool find_with( Q& key, Less pred, Func f )
715 return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
719 /** \anchor cds_intrusive_MichaelList_hp_find_val
720 The function searches the item with key equal to \p key
721 and returns \p true if it is found, and \p false otherwise
723 template <typename Q>
724 bool find( Q const& key )
726 return find_at( m_pHead, key, key_comparator() );
729 /// Finds the key \p val using \p pred predicate for searching
731 The function is an analog of \ref cds_intrusive_MichaelList_hp_find_val "find(Q const&)"
732 but \p pred is used for key comparing.
733 \p Less functor has the interface like \p std::less.
734 \p pred must imply the same element order as the comparator used for building the list.
736 template <typename Q, typename Less>
737 bool find_with( Q const& key, Less pred )
739 return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>() );
742 /// Finds the \p key and return the item found
743 /** \anchor cds_intrusive_MichaelList_hp_get
744 The function searches the item with key equal to \p key
745 and assigns the item found to guarded pointer \p ptr.
746 The function returns \p true if \p key is found, and \p false otherwise.
747 If \p key is not found the \p ptr parameter is not changed.
749 The \ref disposer specified in \p Traits class template parameter is called
750 by garbage collector \p GC automatically when returned \ref guarded_ptr object
751 will be destroyed or released.
752 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
756 typedef cds::intrusive::MichaelList< cds::gc::HP, foo, my_traits > ord_list;
760 ord_list::guarded_ptr gp;
761 if ( theList.get( gp, 5 )) {
765 // Destructor of guarded_ptr releases internal HP guard
769 Note the compare functor specified for \p Traits template parameter
770 should accept a parameter of type \p Q that can be not the same as \p value_type.
772 template <typename Q>
773 bool get( guarded_ptr& ptr, Q const& key )
775 return get_at( m_pHead, ptr.guard(), key, key_comparator() );
778 /// Finds the \p key and return the item found
780 The function is an analog of \ref cds_intrusive_MichaelList_hp_get "get( guarded_ptr& ptr, Q const&)"
781 but \p pred is used for comparing the keys.
783 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
785 \p pred must imply the same element order as the comparator used for building the list.
787 template <typename Q, typename Less>
788 bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
790 return get_at( m_pHead, ptr.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
795 The function unlink all items from the list.
799 typename gc::Guard guard;
800 marked_node_ptr head;
802 head = m_pHead.load(memory_model::memory_order_relaxed);
804 guard.assign( node_traits::to_value_ptr( *head.ptr() ));
805 if ( m_pHead.load(memory_model::memory_order_acquire) == head ) {
806 if ( head.ptr() == nullptr )
808 value_type& val = *node_traits::to_value_ptr( *head.ptr() );
814 /// Checks whether the list is empty
817 return m_pHead.load( memory_model::memory_order_relaxed ).all() == nullptr;
820 /// Returns list's item count
822 The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
823 this function always returns 0.
825 @note Even if you use real item counter and it returns 0, this fact does not mean that the list
826 is empty. To check list emptyness use \p empty() method.
830 return m_ItemCounter.value();
835 // split-list support
836 bool insert_aux_node( node_type * pNode )
838 return insert_aux_node( m_pHead, pNode );
841 // split-list support
842 bool insert_aux_node( atomic_node_ptr& refHead, node_type * pNode )
844 assert( pNode != nullptr );
846 // Hack: convert node_type to value_type.
847 // In principle, auxiliary node can be non-reducible to value_type
848 // We assume that comparator can correctly distinguish aux and regular node.
849 return insert_at( refHead, *node_traits::to_value_ptr( pNode ) );
852 bool insert_at( atomic_node_ptr& refHead, value_type& val )
854 node_type * pNode = node_traits::to_node_ptr( val );
855 link_checker::is_empty( pNode );
859 if ( search( refHead, val, pos, key_comparator() ) )
862 if ( link_node( pNode, pos ) ) {
868 pNode->m_pNext.store( marked_node_ptr(), memory_model::memory_order_relaxed );
872 template <typename Func>
873 bool insert_at( atomic_node_ptr& refHead, value_type& val, Func f )
875 node_type * pNode = node_traits::to_node_ptr( val );
876 link_checker::is_empty( pNode );
880 if ( search( refHead, val, pos, key_comparator() ) )
883 typename gc::Guard guard;
884 guard.assign( &val );
885 if ( link_node( pNode, pos ) ) {
892 pNode->m_pNext.store( marked_node_ptr(), memory_model::memory_order_relaxed );
896 template <typename Func>
897 std::pair<bool, bool> ensure_at( atomic_node_ptr& refHead, value_type& val, Func func )
901 node_type * pNode = node_traits::to_node_ptr( val );
903 if ( search( refHead, val, pos, key_comparator() ) ) {
904 if ( pos.pCur->m_pNext.load(memory_model::memory_order_acquire).bits() ) {
906 continue ; // the node found is marked as deleted
908 assert( key_comparator()( val, *node_traits::to_value_ptr( *pos.pCur ) ) == 0 );
910 func( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
911 return std::make_pair( true, false );
914 typename gc::Guard guard;
915 guard.assign( &val );
916 if ( link_node( pNode, pos ) ) {
918 func( true, val, val );
919 return std::make_pair( true, true );
922 pNode->m_pNext.store( marked_node_ptr(), memory_model::memory_order_relaxed );
927 bool unlink_at( atomic_node_ptr& refHead, value_type& val )
932 while ( search( refHead, val, pos, key_comparator() ) ) {
933 if ( node_traits::to_value_ptr( *pos.pCur ) == &val ) {
934 if ( unlink_node( pos ) ) {
947 template <typename Q, typename Compare, typename Func>
948 bool erase_at( atomic_node_ptr& refHead, const Q& val, Compare cmp, Func f, position& pos )
951 while ( search( refHead, val, pos, cmp )) {
952 if ( unlink_node( pos ) ) {
953 f( *node_traits::to_value_ptr( *pos.pCur ) );
963 template <typename Q, typename Compare, typename Func>
964 bool erase_at( atomic_node_ptr& refHead, const Q& val, Compare cmp, Func f )
967 return erase_at( refHead, val, cmp, f, pos );
970 template <typename Q, typename Compare>
971 bool erase_at( atomic_node_ptr& refHead, Q const& val, Compare cmp )
974 return erase_at( refHead, val, cmp, [](value_type const&){}, pos );
977 template <typename Q, typename Compare>
978 bool extract_at( atomic_node_ptr& refHead, typename gc::Guard& dest, Q const& val, Compare cmp )
982 while ( search( refHead, val, pos, cmp )) {
983 if ( unlink_node( pos ) ) {
984 dest.assign( pos.guards.template get<value_type>( position::guard_current_item ) );
994 template <typename Q, typename Compare>
995 bool find_at( atomic_node_ptr& refHead, Q const& val, Compare cmp )
998 return search( refHead, val, pos, cmp );
1001 template <typename Q, typename Compare, typename Func>
1002 bool find_at( atomic_node_ptr& refHead, Q& val, Compare cmp, Func f )
1005 if ( search( refHead, val, pos, cmp )) {
1006 f( *node_traits::to_value_ptr( *pos.pCur ), val );
1012 template <typename Q, typename Compare>
1013 bool get_at( atomic_node_ptr& refHead, typename gc::Guard& guard, Q const& val, Compare cmp )
1016 if ( search( refHead, val, pos, cmp )) {
1017 guard.assign( pos.guards.template get<value_type>( position::guard_current_item ));
1028 template <typename Q, typename Compare >
1029 bool search( atomic_node_ptr& refHead, const Q& val, position& pos, Compare cmp )
1031 atomic_node_ptr * pPrev;
1032 marked_node_ptr pNext;
1033 marked_node_ptr pCur;
1041 pCur = pPrev->load(memory_model::memory_order_relaxed);
1042 pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ) );
1043 if ( pPrev->load(memory_model::memory_order_acquire) != pCur.ptr() )
1047 if ( pCur.ptr() == nullptr ) {
1049 pos.pCur = pCur.ptr();
1050 pos.pNext = pNext.ptr();
1054 pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed);
1055 pos.guards.assign( position::guard_next_item, node_traits::to_value_ptr( pNext.ptr() ));
1056 if ( pCur->m_pNext.load(memory_model::memory_order_relaxed).all() != pNext.all() ) {
1061 if ( pPrev->load(memory_model::memory_order_relaxed).all() != pCur.ptr() ) {
1066 // pNext contains deletion mark for pCur
1067 if ( pNext.bits() == 1 ) {
1068 // pCur marked i.e. logically deleted. Help the erase/unlink function to unlink pCur node
1069 marked_node_ptr cur( pCur.ptr());
1070 if ( pPrev->compare_exchange_strong( cur, marked_node_ptr( pNext.ptr() ), memory_model::memory_order_release, atomics::memory_order_relaxed )) {
1071 retire_node( pCur.ptr() );
1079 assert( pCur.ptr() != nullptr );
1080 int nCmp = cmp( *node_traits::to_value_ptr( pCur.ptr() ), val );
1083 pos.pCur = pCur.ptr();
1084 pos.pNext = pNext.ptr();
1087 pPrev = &( pCur->m_pNext );
1088 pos.guards.assign( position::guard_prev_item, node_traits::to_value_ptr( pCur.ptr() ) );
1091 pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ));
1096 }} // namespace cds::intrusive
1098 #endif // #ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H