2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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_CONTAINER_BRONSON_AVLTREE_MAP_RCU_H
32 #define CDSLIB_CONTAINER_BRONSON_AVLTREE_MAP_RCU_H
35 #include <cds/container/impl/bronson_avltree_map_rcu.h>
37 namespace cds { namespace container {
39 namespace bronson_avltree {
42 template < class RCU, typename Key, typename T, typename Traits>
46 typedef T mapped_type;
47 typedef Traits original_traits;
49 typedef cds::details::Allocator< mapped_type, typename original_traits::allocator > cxx_allocator;
51 struct traits : public original_traits
54 void operator()( mapped_type * p ) const
56 cxx_allocator().Delete( p );
61 // Metafunction result
62 typedef BronsonAVLTreeMap< RCU, Key, mapped_type *, traits > type;
64 } // namespace details
66 } // namespace bronson_avltree
68 /// Bronson et al AVL-tree (RCU specialization)
69 /** @ingroup cds_nonintrusive_map
70 @ingroup cds_nonintrusive_tree
71 @anchor cds_container_BronsonAVLTreeMap_rcu
74 - [2010] N.Bronson, J.Casper, H.Chafi, K.Olukotun "A Practical Concurrent Binary Search Tree"
75 - <a href="http://github.com/nbronson/snaptree">Java implementation</a>
77 This is a concurrent AVL tree algorithm that uses hand-over-hand optimistic validation,
78 a concurrency control mechanism for searching and navigating a binary search tree.
79 This mechanism minimizes spurious retries when concurrent structural changes cannot
80 affect the correctness of the search or navigation result.
81 The algorithm is based on partially external trees, a simple scheme that simplifies deletions
82 by leaving a routing node in the tree when deleting a node that has two children,
83 then opportunistically unlinking routing nodes during rebalancing. As in external trees,
84 which store values only in leaf nodes, deletions can be performed locally while holding
85 a fixed number of locks. Partially external trees, however, require far fewer routing nodes
86 than an external tree for most sequences of insertions and deletions.
87 The algorithm uses optimistic concurrency control, but carefully manage the
88 tree in such a way that all atomic regions have fixed read and write sets
89 that are known ahead of time. This allows to reduce practical overheads by embedding
90 the concurrency control directly. To perform tree operations using only fixed sized
91 atomic regions the algo uses the following mechanisms: search operations overlap atomic blocks as
92 in the hand-over-hand locking technique; mutations perform rebalancing separately;
93 and deletions occasionally leave a routing node in the tree.
95 <b>Template arguments</b>:
96 - \p RCU - one of \ref cds_urcu_gc "RCU type"
98 - \p T - value type to be stored in tree's nodes.
99 - \p Traits - tree traits, default is \p bronson_avltree::traits
100 It is possible to declare option-based tree with \p bronson_avltree::make_traits metafunction
101 instead of \p Traits template argument.
103 There is \ref cds_container_BronsonAVLTreeMap_rcu_ptr "a specialization" for "key -> value pointer" map.
105 @note Before including <tt><cds/container/bronson_avltree_map_rcu.h></tt> you should include appropriate RCU header file,
106 see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
112 # ifdef CDS_DOXYGEN_INVOKED
113 typename Traits = bronson_avltree::traits
118 class BronsonAVLTreeMap< cds::urcu::gc<RCU>, Key, T, Traits >
119 #ifdef CDS_DOXYGEN_INVOKED
120 : private BronsonAVLTreeMap< cds::urcu::gc<RCU>, Key, T*, Traits >
122 : private bronson_avltree::details::make_map< cds::urcu::gc<RCU>, Key, T, Traits >::type
126 typedef bronson_avltree::details::make_map< cds::urcu::gc<RCU>, Key, T, Traits > maker;
127 typedef typename maker::type base_class;
131 typedef cds::urcu::gc<RCU> gc; ///< RCU Garbage collector
132 typedef Key key_type; ///< type of a key stored in the map
133 typedef T mapped_type; ///< type of value stored in the map
134 typedef Traits traits; ///< Traits template parameter
136 typedef typename base_class::key_comparator key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
137 typedef typename traits::item_counter item_counter; ///< Item counting policy
138 typedef typename traits::memory_model memory_model; ///< Memory ordering, see \p cds::opt::memory_model option
139 typedef typename traits::allocator allocator_type; ///< allocator for value
140 typedef typename traits::node_allocator node_allocator_type;///< allocator for maintaining internal nodes
141 typedef typename traits::stat stat; ///< internal statistics
142 typedef typename traits::rcu_check_deadlock rcu_check_deadlock; ///< Deadlock checking policy
143 typedef typename traits::back_off back_off; ///< Back-off strategy
144 typedef typename traits::sync_monitor sync_monitor; ///< @ref cds_sync_monitor "Synchronization monitor" type for node-level locking
146 /// Enabled or disabled @ref bronson_avltree::relaxed_insert "relaxed insertion"
147 static bool const c_bRelaxedInsert = traits::relaxed_insert;
149 /// Group of \p extract_xxx functions does not require external locking
150 static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal;
152 typedef typename base_class::rcu_lock rcu_lock; ///< RCU scoped lock
154 /// Returned pointer to \p mapped_type of extracted node
155 typedef typename base_class::exempt_ptr exempt_ptr;
159 typedef typename base_class::node_type node_type;
160 typedef typename base_class::node_scoped_lock node_scoped_lock;
161 typedef typename maker::cxx_allocator cxx_allocator;
163 typedef typename base_class::update_flags update_flags;
167 /// Creates empty map
175 /// Inserts new node with \p key and default value
177 The function creates a node with \p key and default value, and then inserts the node created into the map.
180 - The \p key_type should be constructible from a value of type \p K.
181 - The \p mapped_type should be default-constructible.
183 RCU \p synchronize() can be called. RCU should not be locked.
185 Returns \p true if inserting successful, \p false otherwise.
187 template <typename K>
188 bool insert( K const& key )
190 return base_class::do_update(key, key_comparator(),
191 []( node_type * pNode ) -> mapped_type*
193 assert( pNode->m_pValue.load( memory_model::memory_order_relaxed ) == nullptr );
195 return cxx_allocator().New();
197 update_flags::allow_insert
198 ) == update_flags::result_inserted;
203 The function creates a node with copy of \p val value
204 and then inserts the node created into the map.
207 - The \p key_type should be constructible from \p key of type \p K.
208 - The \p mapped_type should be constructible from \p val of type \p V.
210 RCU \p synchronize() method can be called. RCU should not be locked.
212 Returns \p true if \p val is inserted into the map, \p false otherwise.
214 template <typename K, typename V>
215 bool insert( K const& key, V const& val )
217 return base_class::do_update( key, key_comparator(),
218 [&val]( node_type * pNode ) -> mapped_type*
220 assert( pNode->m_pValue.load( memory_model::memory_order_relaxed ) == nullptr );
222 return cxx_allocator().New( val );
224 update_flags::allow_insert
225 ) == update_flags::result_inserted;
228 /// Inserts new node and initialize it by a functor
230 This function inserts new node with key \p key and if inserting is successful then it calls
231 \p func functor with signature
234 void operator()( key_type const& key, mapped_type& item );
238 The key_type should be constructible from value of type \p K.
240 The function allows to split creating of new item into two part:
241 - create item from \p key;
242 - insert new item into the map;
243 - if inserting is successful, initialize the value of item by calling \p func functor
245 This can be useful if complete initialization of object of \p value_type is heavyweight and
246 it is preferable that the initialization should be completed only if inserting is successful.
247 The functor is called under the node lock.
249 RCU \p synchronize() method can be called. RCU should not be locked.
251 template <typename K, typename Func>
252 bool insert_with( K const& key, Func func )
254 return base_class::do_update( key, key_comparator(),
255 [&func]( node_type * pNode ) -> mapped_type*
257 assert( pNode->m_pValue.load( memory_model::memory_order_relaxed ) == nullptr );
258 mapped_type * pVal = cxx_allocator().New();
259 func( pNode->m_key, *pVal );
262 update_flags::allow_insert
263 ) == update_flags::result_inserted;
266 /// For \p key inserts data of type \p mapped_type created in-place from \p args
268 Returns \p true if inserting successful, \p false otherwise.
270 RCU \p synchronize() method can be called. RCU should not be locked.
272 template <typename K, typename... Args>
273 bool emplace( K&& key, Args&&... args )
278 scoped_ptr( mapped_type * p ): pVal( p ) {}
279 ~scoped_ptr() { if ( pVal ) cxx_allocator().Delete( pVal ); }
280 void release() { pVal = nullptr; }
283 scoped_ptr p( cxx_allocator().MoveNew( std::forward<Args>( args )... ));
284 if ( base_class::insert( std::forward<K>( key ), p.pVal )) {
291 /// Updates the value for \p key
293 The operation performs inserting or changing data with lock-free manner.
295 If the \p key not found in the map, then the new item created from \p key
296 will be inserted into the map iff \p bAllowInsert is \p true
297 (note that in this case the \ref key_type should be constructible from type \p K).
298 Otherwise, the functor \p func is called with item found.
299 The functor \p Func signature is:
302 void operator()( bool bNew, key_type const& key, mapped_type& item );
307 - \p bNew - \p true if the item has been inserted, \p false otherwise
310 The functor may change any fields of the \p item. The functor is called under the node lock,
311 the caller can change any field of \p item.
313 RCU \p synchronize() method can be called. RCU should not be locked.
315 Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful,
316 \p second is \p true if new item has been added or \p false if the item with \p key
319 template <typename K, typename Func>
320 std::pair<bool, bool> update( K const& key, Func func, bool bAllowInsert = true )
322 int result = base_class::do_update( key, key_comparator(),
323 [&func]( node_type * pNode ) -> mapped_type*
325 mapped_type * pVal = pNode->m_pValue.load( memory_model::memory_order_relaxed );
327 pVal = cxx_allocator().New();
328 func( true, pNode->m_key, *pVal );
331 func( false, pNode->m_key, *pVal );
334 (bAllowInsert ? update_flags::allow_insert : 0) | update_flags::allow_update
336 return std::make_pair( result != 0, (result & update_flags::result_inserted) != 0 );
340 /// Delete \p key from the map
342 RCU \p synchronize() method can be called. RCU should not be locked.
344 Return \p true if \p key is found and deleted, \p false otherwise
346 template <typename K>
347 bool erase( K const& key )
349 return base_class::erase( key );
352 /// Deletes the item from the map using \p pred predicate for searching
354 The function is an analog of \p erase(K const&)
355 but \p pred is used for key comparing.
356 \p Less functor has the interface like \p std::less.
357 \p Less must imply the same element order as the comparator used for building the map.
359 template <typename K, typename Less>
360 bool erase_with( K const& key, Less pred )
362 return base_class::erase_with( key, pred );
365 /// Delete \p key from the map
366 /** \anchor cds_nonintrusive_BronsonAVLTreeMap_rcu_erase_func
368 The function searches an item with key \p key, calls \p f functor
369 and deletes the item. If \p key is not found, the functor is not called.
371 The functor \p Func interface:
374 void operator()(key_type const& key, mapped_type& item) { ... }
378 RCU \p synchronize method can be called. RCU should not be locked.
380 Return \p true if key is found and deleted, \p false otherwise
382 template <typename K, typename Func>
383 bool erase( K const& key, Func f )
385 return base_class::erase( key, f );
388 /// Deletes the item from the map using \p pred predicate for searching
390 The function is an analog of \ref cds_nonintrusive_BronsonAVLTreeMap_rcu_erase_func "erase(K const&, Func)"
391 but \p pred is used for key comparing.
392 \p Less functor has the interface like \p std::less.
393 \p Less must imply the same element order as the comparator used for building the map.
395 template <typename K, typename Less, typename Func>
396 bool erase_with( K const& key, Less pred, Func f )
398 return base_class::erase_with( key, pred, f );
401 /// Extracts a value with minimal key from the map
403 Returns \p exempt_ptr pointer to the leftmost item.
404 If the set is empty, returns empty \p exempt_ptr.
406 Note that the function returns only the value for minimal key.
407 To retrieve its key use \p extract_min( Func ) member function.
409 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
410 It means that the function gets leftmost leaf of the tree and tries to unlink it.
411 During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
412 So, the function returns the item with minimum key at the moment of tree traversing.
414 RCU \p synchronize method can be called. RCU should NOT be locked.
415 The function does not free the item.
416 The deallocator will be implicitly invoked when the returned object is destroyed or when
417 its \p release() member function is called.
419 exempt_ptr extract_min()
421 return base_class::extract_min();
424 /// Extracts minimal key and corresponding value
426 Returns \p exempt_ptr to the leftmost item.
427 If the tree is empty, returns empty \p exempt_ptr.
429 \p Func functor is used to store minimal key.
430 \p Func has the following signature:
433 void operator()( key_type const& key );
436 If the tree is empty, \p f is not called.
437 Otherwise, is it called with minimal key, the pointer to corresponding value is returned
440 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
441 It means that the function gets leftmost leaf of the tree and tries to unlink it.
442 During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
443 So, the function returns the item with minimum key at the moment of tree traversing.
445 RCU \p synchronize method can be called. RCU should NOT be locked.
446 The function does not free the item.
447 The deallocator will be implicitly invoked when the returned object is destroyed or when
448 its \p release() member function is called.
450 template <typename Func>
451 exempt_ptr extract_min( Func f )
453 return base_class::extract_min( f );
456 /// Extracts minimal key and corresponding value
458 This function is a shortcut for the following call:
461 exempt_ptr xp = theTree.extract_min( [&key]( key_type const& k ) { key = k; } );
463 \p key_type should be copy-assignable. The copy of minimal key
464 is returned in \p min_key argument.
466 typename std::enable_if< std::is_copy_assignable<key_type>::value, exempt_ptr >::type
467 extract_min_key( key_type& min_key )
469 return base_class::extract_min_key( min_key );
472 /// Extracts an item with maximal key from the map
474 Returns \p exempt_ptr pointer to the rightmost item.
475 If the set is empty, returns empty \p exempt_ptr.
477 Note that the function returns only the value for maximal key.
478 To retrieve its key use \p extract_max( Func ) or \p extract_max_key(key_type&) member function.
480 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
481 It means that the function gets rightmost leaf of the tree and tries to unlink it.
482 During unlinking, a concurrent thread may insert an item with key greater than rightmost item's key.
483 So, the function returns the item with maximum key at the moment of tree traversing.
485 RCU \p synchronize method can be called. RCU should NOT be locked.
486 The function does not free the item.
487 The deallocator will be implicitly invoked when the returned object is destroyed or when
488 its \p release() is called.
490 exempt_ptr extract_max()
492 return base_class::extract_max();
495 /// Extracts the maximal key and corresponding value
497 Returns \p exempt_ptr pointer to the rightmost item.
498 If the set is empty, returns empty \p exempt_ptr.
500 \p Func functor is used to store maximal key.
501 \p Func has the following signature:
504 void operator()( key_type const& key );
507 If the tree is empty, \p f is not called.
508 Otherwise, is it called with maximal key, the pointer to corresponding value is returned
511 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
512 It means that the function gets rightmost leaf of the tree and tries to unlink it.
513 During unlinking, a concurrent thread may insert an item with key greater than rightmost item's key.
514 So, the function returns the item with maximum key at the moment of tree traversing.
516 RCU \p synchronize method can be called. RCU should NOT be locked.
517 The function does not free the item.
518 The deallocator will be implicitly invoked when the returned object is destroyed or when
519 its \p release() is called.
521 template <typename Func>
522 exempt_ptr extract_max( Func f )
524 return base_class::extract_max( f );
527 /// Extracts the maximal key and corresponding value
529 This function is a shortcut for the following call:
532 exempt_ptr xp = theTree.extract_max( [&key]( key_type const& k ) { key = k; } );
534 \p key_type should be copy-assignable. The copy of maximal key
535 is returned in \p max_key argument.
537 typename std::enable_if< std::is_copy_assignable<key_type>::value, exempt_ptr >::type
538 extract_max_key( key_type& max_key )
540 return base_class::extract_max_key( max_key );
543 /// Extracts an item from the map
545 The function searches an item with key equal to \p key in the tree,
546 unlinks it, and returns \p exempt_ptr pointer to a value found.
547 If \p key is not found the function returns an empty \p exempt_ptr.
549 RCU \p synchronize method can be called. RCU should NOT be locked.
550 The function does not destroy the value found.
551 The dealloctor will be implicitly invoked when the returned object is destroyed or when
552 its \p release() member function is called.
554 template <typename Q>
555 exempt_ptr extract( Q const& key )
557 return base_class::extract( key );
560 /// Extracts an item from the map using \p pred for searching
562 The function is an analog of \p extract(Q const&)
563 but \p pred is used for key compare.
564 \p Less has the interface like \p std::less.
565 \p pred must imply the same element order as the comparator used for building the map.
567 template <typename Q, typename Less>
568 exempt_ptr extract_with( Q const& key, Less pred )
570 return base_class::extract_with( key, pred );
573 /// Find the key \p key
575 The function searches the item with key equal to \p key and calls the functor \p f for item found.
576 The interface of \p Func functor is:
579 void operator()( key_type const& key, mapped_type& val );
582 where \p val is the item found for \p key
583 The functor is called under node-level lock.
585 The function applies RCU lock internally.
587 The function returns \p true if \p key is found, \p false otherwise.
589 template <typename K, typename Func>
590 bool find( K const& key, Func f )
592 return base_class::find( key, f );
595 /// Finds the key \p val using \p pred predicate for searching
597 The function is an analog of \p find(K const&, Func)
598 but \p pred is used for key comparing.
599 \p Less functor has the interface like \p std::less.
600 \p Less must imply the same element order as the comparator used for building the map.
602 template <typename K, typename Less, typename Func>
603 bool find_with( K const& key, Less pred, Func f )
605 return base_class::find_with( key, pred, f );
608 /// Checks whether the map contains \p key
610 The function searches the item with key equal to \p key
611 and returns \p true if it is found, and \p false otherwise.
613 The function applies RCU lock internally.
615 template <typename K>
616 bool contains( K const& key )
618 return base_class::contains( key );
621 /// Checks whether the map contains \p key using \p pred predicate for searching
623 The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
624 \p Less functor has the interface like \p std::less.
625 \p Less must imply the same element order as the comparator used for building the set.
627 template <typename K, typename Less>
628 bool contains( K const& key, Less pred )
630 return base_class::contains( key, pred );
639 /// Checks if the map is empty
642 return base_class::empty();
645 /// Returns item count in the map
647 Only leaf nodes containing user data are counted.
649 The value returned depends on item counter type provided by \p Traits template parameter.
650 If it is \p atomicity::empty_item_counter this function always returns 0.
652 The function is not suitable for checking the tree emptiness, use \p empty()
653 member function for this purpose.
657 return base_class::size();
660 /// Returns const reference to internal statistics
661 stat const& statistics() const
663 return base_class::statistics();
666 /// Returns reference to \p sync_monitor object
667 sync_monitor& monitor()
669 return base_class::monitor();
672 sync_monitor const& monitor() const
674 return base_class::monitor();
678 /// Checks internal consistency (not atomic, not thread-safe)
680 The debugging function to check internal consistency of the tree.
682 bool check_consistency() const
684 return base_class::check_consistency();
687 /// Checks internal consistency (not atomic, not thread-safe)
689 The debugging function to check internal consistency of the tree.
690 The functor \p Func is called if a violation of internal tree structure
694 void operator()( size_t nLevel, size_t hLeft, size_t hRight );
698 - \p nLevel - the level where the violation is found
699 - \p hLeft - the height of left subtree
700 - \p hRight - the height of right subtree
702 The functor is called for each violation found.
704 template <typename Func>
705 bool check_consistency( Func f ) const
707 return base_class::check_consistency( f );
710 }} // namespace cds::container
712 #endif // #ifndef CDSLIB_CONTAINER_IMPL_BRONSON_AVLTREE_MAP_RCU_H