2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_INTRUSIVE_LAZY_LIST_NOGC_H
32 #define CDSLIB_INTRUSIVE_LAZY_LIST_NOGC_H
34 #include <mutex> // unique_lock
35 #include <cds/intrusive/details/lazy_list_base.h>
36 #include <cds/gc/nogc.h>
38 namespace cds { namespace intrusive {
40 /// Lazy list node for \p gc::nogc
43 - Lock - lock type. Default is \p cds::sync::spin
44 - Tag - a \ref cds_intrusive_hook_tag "tag"
47 #ifdef CDS_DOXYGEN_INVOKED
48 typename Lock = cds::sync::spin,
49 typename Tag = opt::none
55 struct node<gc::nogc, Lock, Tag>
57 typedef gc::nogc gc; ///< Garbage collector
58 typedef Lock lock_type; ///< Lock type
59 typedef Tag tag; ///< tag
61 atomics::atomic<node *> m_pNext; ///< pointer to the next node in the list
62 mutable lock_type m_Lock; ///< Node lock
68 } // namespace lazy_list
71 /// Lazy single-linked list (template specialization for \p gc::nogc)
72 /** @ingroup cds_intrusive_list
73 \anchor cds_intrusive_LazyList_nogc
75 This specialization is append-only list when no item
76 reclamation may be performed. The class does not support deleting of list item.
78 The list can be ordered if \p Traits::sort is \p true that is default
79 or unordered otherwise. Unordered list can be maintained by \p equal_to
80 relationship (\p Traits::equal_to), but for the ordered list \p less
81 or \p compare relations should be specified in \p Traits.
83 See \ref cds_intrusive_LazyList_hp "LazyList" for description of template parameters.
87 #ifdef CDS_DOXYGEN_INVOKED
88 ,class Traits = lazy_list::traits
93 class LazyList<gc::nogc, T, Traits>
96 typedef gc::nogc gc; ///< Garbage collector
97 typedef T value_type; ///< type of value stored in the list
98 typedef Traits traits; ///< Traits template parameter
100 typedef typename traits::hook hook; ///< hook type
101 typedef typename hook::node_type node_type; ///< node type
102 static CDS_CONSTEXPR bool const c_bSort = traits::sort; ///< List type: ordered (\p true) or unordered (\p false)
104 # ifdef CDS_DOXYGEN_INVOKED
105 /// Key comparing functor
107 - for ordered list, the functor is based on \p traits::compare or \p traits::less
108 - for unordered list, the functor is based on \p traits::equal_to, \p traits::compare or \p traits::less
110 typedef implementation_defined key_comparator;
112 typedef typename std::conditional< c_bSort,
113 typename opt::details::make_comparator< value_type, traits >::type,
114 typename opt::details::make_equal_to< value_type, traits >::type
115 >::type key_comparator;
117 typedef typename traits::back_off back_off; ///< Back-off strategy
118 typedef typename traits::disposer disposer; ///< disposer
119 typedef typename get_node_traits< value_type, node_type, hook>::type node_traits; ///< node traits
120 typedef typename lazy_list::get_link_checker< node_type, traits::link_checker >::type link_checker; ///< link checker
122 typedef typename traits::item_counter item_counter; ///< Item counting policy used
123 typedef typename traits::memory_model memory_model; ///< C++ memory ordering (see lazy_list::traits::memory_model)
126 // Rebind traits (split-list support)
127 template <typename... Options>
128 struct rebind_traits {
132 , typename cds::opt::make_options< traits, Options...>::type
138 typedef node_type * auxiliary_head ; ///< Auxiliary head type (for split-list support)
141 node_type m_Head; ///< List head (dummy node)
142 node_type m_Tail; ///< List tail (dummy node)
143 item_counter m_ItemCounter; ///< Item counter
147 /// Position pointer for item search
149 node_type * pPred ; ///< Previous node
150 node_type * pCur ; ///< Current node
152 /// Locks nodes \p pPred and \p pCur
155 pPred->m_Lock.lock();
159 /// Unlocks nodes \p pPred and \p pCur
162 pCur->m_Lock.unlock();
163 pPred->m_Lock.unlock();
167 class auto_lock_position {
170 auto_lock_position( position& pos )
175 ~auto_lock_position()
184 void clear_links( node_type * pNode )
186 pNode->m_pNext.store( nullptr, memory_model::memory_order_relaxed );
189 template <class Disposer>
190 void dispose_node( node_type * pNode, Disposer disp )
192 clear_links( pNode );
193 disp( node_traits::to_value_ptr( *pNode ));
196 template <class Disposer>
197 void dispose_value( value_type& val, Disposer disp )
199 dispose_node( node_traits::to_node_ptr( val ), disp );
202 void link_node( node_type * pNode, node_type * pPred, node_type * pCur )
204 assert( pPred->m_pNext.load(memory_model::memory_order_relaxed) == pCur );
206 pNode->m_pNext.store( pCur, memory_model::memory_order_release );
207 pPred->m_pNext.store( pNode, memory_model::memory_order_release );
213 template <bool IsConst>
216 friend class LazyList;
219 value_type * m_pNode;
223 assert( m_pNode != nullptr );
225 node_type * pNode = node_traits::to_node_ptr( m_pNode );
226 node_type * pNext = pNode->m_pNext.load(memory_model::memory_order_relaxed);
227 if ( pNext != nullptr )
228 m_pNode = node_traits::to_value_ptr( pNext );
231 iterator_type( node_type * pNode )
233 m_pNode = node_traits::to_value_ptr( pNode );
237 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
238 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
244 iterator_type( const iterator_type& src )
245 : m_pNode( src.m_pNode )
248 value_ptr operator ->() const
253 value_ref operator *() const
255 assert( m_pNode != nullptr );
260 iterator_type& operator ++()
267 iterator_type operator ++(int)
269 iterator_type i(*this);
274 iterator_type& operator = (const iterator_type& src)
276 m_pNode = src.m_pNode;
281 bool operator ==(iterator_type<C> const& i ) const
283 return m_pNode == i.m_pNode;
286 bool operator !=(iterator_type<C> const& i ) const
288 return m_pNode != i.m_pNode;
295 typedef iterator_type<false> iterator;
296 /// Const forward iterator
297 typedef iterator_type<true> const_iterator;
299 /// Returns a forward iterator addressing the first element in a list
301 For empty list \code begin() == end() \endcode
305 iterator it( &m_Head );
306 ++it ; // skip dummy head
310 /// Returns an iterator that addresses the location succeeding the last element in a list
312 Do not use the value returned by <tt>end</tt> function to access any item.
314 The returned value can be used only to control reaching the end of the list.
315 For empty list \code begin() == end() \endcode
319 return iterator( &m_Tail );
322 /// Returns a forward const iterator addressing the first element in a list
323 const_iterator begin() const
327 /// Returns a forward const iterator addressing the first element in a list
328 const_iterator cbegin() const
330 const_iterator it( const_cast<node_type *>(&m_Head) );
331 ++it; // skip dummy head
335 /// Returns an const iterator that addresses the location succeeding the last element in a list
336 const_iterator end() const
340 /// Returns an const iterator that addresses the location succeeding the last element in a list
341 const_iterator cend() const
343 return const_iterator( const_cast<node_type *>(&m_Tail) );
347 /// Default constructor initializes empty list
350 static_assert( (std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type" );
351 m_Head.m_pNext.store( &m_Tail, memory_model::memory_order_relaxed );
354 /// Destroys the list object
358 assert( m_Head.m_pNext.load(memory_model::memory_order_relaxed) == &m_Tail );
359 m_Head.m_pNext.store( nullptr, memory_model::memory_order_relaxed );
364 The function inserts \p val in the list if the list does not contain
365 an item with key equal to \p val.
367 Returns \p true if \p val is linked into the list, \p false otherwise.
369 bool insert( value_type& val )
371 return insert_at( &m_Head, val );
376 The operation performs inserting or changing data with lock-free manner.
378 If the item \p val not found in the list, then \p val is inserted into the list
379 iff \p bAllowInsert is \p true.
380 Otherwise, the functor \p func is called with item found.
381 The functor signature is:
384 void operator()( bool bNew, value_type& item, value_type& val );
388 - \p bNew - \p true if the item has been inserted, \p false otherwise
389 - \p item - item of the list
390 - \p val - argument \p val passed into the \p update() function
391 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
392 refer to the same thing.
394 The functor may change non-key fields of the \p item.
395 While the functor \p f is calling the item \p item is locked.
397 Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
398 \p second is \p true if new item has been added or \p false if the item with \p key
399 already is in the list.
401 template <typename Func>
402 std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
404 return update_at( &m_Head, val, func, bAllowInsert );
407 template <typename Func>
408 CDS_DEPRECATED("ensure() is deprecated, use update()")
409 std::pair<bool, bool> ensure( value_type& val, Func func )
411 return update( val, func, true );
415 /// Finds the key \p key
416 /** \anchor cds_intrusive_LazyList_nogc_find_func
417 The function searches the item with key equal to \p key
418 and calls the functor \p f for item found.
419 The interface of \p Func functor is:
422 void operator()( value_type& item, Q& key );
425 where \p item is the item found, \p key is the <tt>find</tt> function argument.
427 The functor may change non-key fields of \p item.
428 While the functor \p f is calling the item found \p item is locked.
430 The function returns \p true if \p key is found, \p false otherwise.
432 template <typename Q, typename Func>
433 bool find( Q& key, Func f )
435 return find_at( &m_Head, key, key_comparator(), f );
438 template <typename Q, typename Func>
439 bool find( Q const& key, Func f )
441 return find_at( &m_Head, key, key_comparator(), f );
445 /// Finds the key \p key using \p less predicate for searching. Disabled for unordered lists.
447 The function is an analog of \ref cds_intrusive_LazyList_nogc_find_func "find(Q&, Func)"
448 but \p pred is used for key comparing.
449 \p Less functor has the interface like \p std::less.
450 \p pred must imply the same element order as the comparator used for building the list.
452 template <typename Q, typename Less, typename Func, bool Sort = c_bSort>
453 typename std::enable_if<Sort, bool>::type find_with( Q& key, Less less, Func f )
456 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), f );
459 /// Finds the key \p key using \p equal predicate for searching. Disabled for ordered lists.
461 The function is an analog of \ref cds_intrusive_LazyList_nogc_find_func "find(Q&, Func)"
462 but \p equal is used for key comparing.
463 \p Equal functor has the interface like \p std::equal_to.
465 template <typename Q, typename Equal, typename Func, bool Sort = c_bSort>
466 typename std::enable_if<!Sort, bool>::type find_with( Q& key, Equal equal, Func f )
469 return find_at( &m_Head, key, equal, f );
472 template <typename Q, typename Less, typename Func, bool Sort = c_bSort>
473 typename std::enable_if<Sort, bool>::type find_with( Q const& key, Less pred, Func f )
476 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>(), f );
479 template <typename Q, typename Equal, typename Func, bool Sort = c_bSort>
480 typename std::enable_if<!Sort, bool>::type find_with( Q const& key, Equal equal, Func f )
483 return find_at( &m_Head, key, equal, f );
487 /// Checks whether the list contains \p key
489 The function searches the item with key equal to \p key
490 and returns \p true if it is found, and \p false otherwise.
492 template <typename Q>
493 value_type * contains( Q const& key )
495 return find_at( &m_Head, key, key_comparator() );
498 template <typename Q>
499 CDS_DEPRECATED("deprecated, use contains()")
500 value_type * find( Q const& key )
502 return contains( key );
506 /// Checks whether the map contains \p key using \p pred predicate for searching (ordered list version)
508 The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
509 \p Less functor has the interface like \p std::less.
510 \p Less must imply the same element order as the comparator used for building the list.
512 template <typename Q, typename Less, bool Sort = c_bSort>
513 typename std::enable_if<Sort, value_type *>::type contains( Q const& key, Less pred )
516 return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>() );
519 template <typename Q, typename Less, bool Sort = c_bSort>
520 CDS_DEPRECATED("deprecated, use contains()")
521 typename std::enable_if<Sort, value_type *>::type find_with( Q const& key, Less pred )
523 return contains( key, pred );
527 /// Checks whether the map contains \p key using \p equal predicate for searching (unordered list version)
529 The function is an analog of <tt>contains( key )</tt> but \p equal is used for key comparing.
530 \p Equal functor has the interface like \p std::equal_to.
532 template <typename Q, typename Equal, bool Sort = c_bSort>
533 typename std::enable_if<!Sort, value_type *>::type contains( Q const& key, Equal equal )
535 return find_at( &m_Head, key, equal );
538 template <typename Q, typename Equal, bool Sort = c_bSort>
539 CDS_DEPRECATED("deprecated, use contains()")
540 typename std::enable_if<!Sort, value_type *>::type find_with( Q const& key, Equal equal )
542 return contains( key, equal );
548 The function unlink all items from the list.
549 For each unlinked item the item disposer \p disp is called after unlinking.
551 This function is not thread-safe.
553 template <typename Disposer>
554 void clear( Disposer disp )
556 node_type * pHead = m_Head.m_pNext.exchange( &m_Tail, memory_model::memory_order_release );
558 while ( pHead != &m_Tail ) {
559 node_type * p = pHead->m_pNext.load(memory_model::memory_order_relaxed);
560 dispose_node( pHead, disp );
565 /// Clears the list using default disposer
567 The function clears the list using default (provided in class template) disposer functor.
574 /// Checks if the list is empty
577 return m_Head.m_pNext.load(memory_model::memory_order_relaxed) == &m_Tail;
580 /// Returns list's item count
582 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
583 this function always returns 0.
585 <b>Warning</b>: even if you use real item counter and it returns 0, this fact is not mean that the list
586 is empty. To check list emptyness use \ref empty() method.
590 return m_ItemCounter.value();
595 // split-list support
596 bool insert_aux_node( node_type * pNode )
598 return insert_aux_node( &m_Head, pNode );
601 // split-list support
602 bool insert_aux_node( node_type * pHead, node_type * pNode )
604 assert( pHead != nullptr );
605 assert( pNode != nullptr );
607 // Hack: convert node_type to value_type.
608 // In principle, auxiliary node can be non-reducible to value_type
609 // We assume that comparator can correctly distinguish aux and regular node.
610 return insert_at( pHead, *node_traits::to_value_ptr( pNode ) );
613 bool insert_at( node_type * pHead, value_type& val )
615 link_checker::is_empty( node_traits::to_node_ptr( val ) );
620 search( pHead, val, pos, pred );
622 auto_lock_position alp( pos );
623 if ( validate( pos.pPred, pos.pCur )) {
624 if ( pos.pCur != &m_Tail && equal( *node_traits::to_value_ptr( *pos.pCur ), val, pred ) ) {
625 // failed: key already in list
629 link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
638 iterator insert_at_( node_type * pHead, value_type& val )
640 if ( insert_at( pHead, val ))
641 return iterator( node_traits::to_node_ptr( val ));
646 template <typename Func>
647 std::pair<iterator, bool> update_at_( node_type * pHead, value_type& val, Func func, bool bAllowInsert )
653 search( pHead, val, pos, pred );
655 auto_lock_position alp( pos );
656 if ( validate( pos.pPred, pos.pCur )) {
657 if ( pos.pCur != &m_Tail && equal( *node_traits::to_value_ptr( *pos.pCur ), val, pred )) {
658 // key already in the list
660 func( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
661 return std::make_pair( iterator( pos.pCur ), false );
666 return std::make_pair( end(), false );
668 link_checker::is_empty( node_traits::to_node_ptr( val ) );
670 link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );
671 func( true, val, val );
673 return std::make_pair( iterator( node_traits::to_node_ptr( val )), true );
680 template <typename Func>
681 std::pair<bool, bool> update_at( node_type * pHead, value_type& val, Func func, bool bAllowInsert )
683 std::pair<iterator, bool> ret = update_at_( pHead, val, func, bAllowInsert );
684 return std::make_pair( ret.first != end(), ret.second );
687 template <typename Q, typename Pred, typename Func>
688 bool find_at( node_type * pHead, Q& val, Pred pred, Func f )
692 search( pHead, val, pos, pred );
693 if ( pos.pCur != &m_Tail ) {
694 std::unique_lock< typename node_type::lock_type> al( pos.pCur->m_Lock );
695 if ( equal( *node_traits::to_value_ptr( *pos.pCur ), val, pred ) )
697 f( *node_traits::to_value_ptr( *pos.pCur ), val );
704 template <typename Q, typename Pred>
705 value_type * find_at( node_type * pHead, Q& val, Pred pred)
707 iterator it = find_at_( pHead, val, pred );
713 template <typename Q, typename Pred>
714 iterator find_at_( node_type * pHead, Q& val, Pred pred)
718 search( pHead, val, pos, pred );
719 if ( pos.pCur != &m_Tail ) {
720 if ( equal( *node_traits::to_value_ptr( *pos.pCur ), val, pred ))
721 return iterator( pos.pCur );
730 template <typename Q, typename Equal, bool Sort = c_bSort>
731 typename std::enable_if<!Sort, void>::type search( node_type * pHead, const Q& key, position& pos, Equal eq )
733 const node_type * pTail = &m_Tail;
735 node_type * pCur = pHead;
736 node_type * pPrev = pHead;
738 while ( pCur != pTail && ( pCur == pHead || !equal( *node_traits::to_value_ptr( *pCur ), key, eq ) )) {
740 pCur = pCur->m_pNext.load(memory_model::memory_order_acquire);
747 template <typename Q, typename Compare, bool Sort = c_bSort>
748 typename std::enable_if<Sort, void>::type search( node_type * pHead, const Q& key, position& pos, Compare cmp )
750 const node_type * pTail = &m_Tail;
752 node_type * pCur = pHead;
753 node_type * pPrev = pHead;
755 while ( pCur != pTail && ( pCur == pHead || cmp( *node_traits::to_value_ptr( *pCur ), key ) < 0 )) {
757 pCur = pCur->m_pNext.load(memory_model::memory_order_acquire);
764 template <typename L, typename R, typename Equal, bool Sort = c_bSort>
765 static typename std::enable_if<!Sort, bool>::type equal( L const& l, R const& r, Equal eq )
770 template <typename L, typename R, typename Compare, bool Sort = c_bSort>
771 static typename std::enable_if<Sort, bool>::type equal( L const& l, R const& r, Compare cmp )
773 return cmp(l, r) == 0;
776 static bool validate( node_type * pPred, node_type * pCur )
778 return pPred->m_pNext.load(memory_model::memory_order_acquire) == pCur;
784 }} // namespace cds::intrusive
786 #endif // #ifndef CDSLIB_INTRUSIVE_LAZY_LIST_NOGC_H