#include <memory>
#include <cds/intrusive/details/ellen_bintree_base.h>
#include <cds/opt/compare.h>
-#include <cds/ref.h>
#include <cds/details/binary_functor_wrapper.h>
#include <cds/urcu/details/check_deadlock.h>
-#include <cds/gc/guarded_ptr.h>
namespace cds { namespace intrusive {
Source:
- [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
- %EllenBinTree is an unbalanced leaf-oriented binary search tree that implements the <i>set</i>
+ %EllenBinTree is an <i>unbalanced</i> leaf-oriented binary search tree that implements the <i>set</i>
abstract data type. Nodes maintains child pointers but not parent pointers.
Every internal node has exactly two children, and all data of type \p T currently in
- the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find
+ the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find()
operation along the path to the correct leaf. The keys (of \p Key type) stored in internal nodes
may or may not be in the set. \p Key type is a subset of \p T type.
There should be exactly defined a key extracting functor for converting object of type \p T to
object of type \p Key.
- Due to \p extract_min and \p extract_max member functions the \p %EllenBinTree can act as
+ Due to \p extract_min() and \p extract_max() member functions the \p %EllenBinTree can act as
a <i>priority queue</i>. In this case you should provide unique compound key, for example,
the priority value plus some uniformly distributed random value.
@note Do not include <tt><cds/intrusive/impl/ellen_bintree.h></tt> header file explicitly.
There are header file for each GC type:
- - <tt><cds/intrusive/ellen_bintree_hp.h></tt> - for Hazard Pointer GC cds::gc::HP
- - <tt><cds/intrusive/ellen_bintree_ptb.h></tt> - for Pass-the-Buck GC cds::gc::PTB
- - <tt><cds/intrusive/ellen_bintree_rcu.h></tt> - for RCU GC
- (see \ref cds_intrusive_EllenBinTree_rcu "RCU-based EllenBinTree")
+ - <tt><cds/intrusive/ellen_bintree_hp.h></tt> - for Hazard Pointer GC \p cds::gc::HP
+ - <tt><cds/intrusive/ellen_bintree_dhp.h></tt> - for Dynamic Hazard Pointer GC \p cds::gc::DHP
+ - <tt><cds/intrusive/ellen_bintree_rcu.h></tt> - for RCU (see \ref cds_intrusive_EllenBinTree_rcu "RCU-based EllenBinTree")
<b>Template arguments</b> :
- - \p GC - garbage collector used, possible types are cds::gc::HP, cds::gc::PTB.
- Note that cds::gc::HRC is not supported.
+ - \p GC - garbage collector, possible types are cds::gc::HP, cds::gc::DHP.
- \p Key - key type, a subset of \p T
- - \p T - type to be stored in tree's leaf nodes. The type must be based on ellen_bintree::node
- (for ellen_bintree::base_hook) or it must have a member of type ellen_bintree::node
- (for ellen_bintree::member_hook).
- - \p Traits - type traits. See ellen_bintree::type_traits for explanation.
-
- It is possible to declare option-based tree with cds::intrusive::ellen_bintree::make_traits metafunction
- instead of \p Traits template argument.
- Template argument list \p Options of cds::intrusive::ellen_bintree::make_traits metafunction are:
- - opt::hook - hook used. Possible values are: ellen_bintree::base_hook, ellen_bintree::member_hook, ellen_bintree::traits_hook.
- If the option is not specified, <tt>ellen_bintree::base_hook<></tt> is used.
- - ellen_bintree::key_extractor - key extracting functor, mandatory option. The functor has the following prototype:
- \code
- struct key_extractor {
- void operator ()( Key& dest, T const& src );
- };
- \endcode
- It should initialize \p dest key from \p src data. The functor is used to initialize internal nodes.
- - opt::compare - key compare functor. No default functor is provided.
- If the option is not specified, \p %opt::less is used.
- - opt::less - specifies binary predicate used for key compare. At least \p %opt::compare or \p %opt::less should be defined.
- - opt::disposer - the functor used for dispose removed nodes. Default is opt::v::empty_disposer. Due the nature
- of GC schema the disposer may be called asynchronously. The disposer is used only for leaf nodes.
- - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that means no item counting.
- - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
- or opt::v::sequential_consistent (sequentially consisnent memory model).
- - ellen_bintree::update_desc_allocator - an allocator of \ref ellen_bintree::update_desc "update descriptors",
- default is CDS_DEFAULT_ALLOCATOR.
- Note that update descriptor is helping data structure with short lifetime and it is good candidate for pooling.
- The number of simultaneously existing descriptors is bounded and depends on the number of threads
- working with the tree and GC internals.
- A bounded lock-free container like \p cds::container::VyukovMPMCCycleQueue is good candidate
- for the free-list of update descriptors, see cds::memory::vyukov_queue_pool free-list implementation.
- Also notice that size of update descriptor is constant and not dependent on the type of data
- stored in the tree so single free-list object can be used for all \p %EllenBinTree objects.
- - opt::node_allocator - the allocator used for internal nodes. Default is \ref CDS_DEFAULT_ALLOCATOR.
- - opt::stat - internal statistics. Available types: ellen_bintree::stat, ellen_bintree::empty_stat (the default)
+ - \p T - type to be stored in tree's leaf nodes. The type must be based on \p ellen_bintree::node
+ (for \p ellen_bintree::base_hook) or it must have a member of type \p ellen_bintree::node
+ (for \p ellen_bintree::member_hook).
+ - \p Traits - tree traits, default is \p ellen_bintree::traits
+ It is possible to declare option-based tree with \p ellen_bintree::make_traits metafunction
+ instead of \p Traits template argument.
@anchor cds_intrusive_EllenBinTree_less
<b>Predicate requirements</b>
- opt::less, opt::compare and other predicates using with member fuctions should accept at least parameters
+ \p Traits::less, \p Traits::compare and other predicates using with member fuctions should accept at least parameters
of type \p T and \p Key in any combination.
For example, for \p Foo struct with \p std::string key field the appropiate \p less functor is:
\code
{ return v.m_strKey.compare(p) > 0; }
};
\endcode
+
+ Usage examples see \ref cds_intrusive_EllenBinTree_usage "here"
*/
template < class GC,
typename Key,
typename T,
#ifdef CDS_DOXYGEN_INVOKED
- class Traits = ellen_bintree::type_traits
+ class Traits = ellen_bintree::traits
#else
class Traits
#endif
class EllenBinTree
{
public:
- typedef GC gc ; ///< Garbage collector used
- typedef Key key_type ; ///< type of a key stored in internal nodes; key is a part of \p value_type
- typedef T value_type ; ///< type of value stored in the binary tree
- typedef Traits options ; ///< Traits template parameter
+ typedef GC gc; ///< Garbage collector
+ typedef Key key_type; ///< type of a key to be stored in internal nodes; key is a part of \p value_type
+ typedef T value_type; ///< type of value stored in the binary tree
+ typedef Traits traits; ///< Traits template parameter
- typedef typename options::hook hook ; ///< hook type
- typedef typename hook::node_type node_type ; ///< node type
- typedef typename options::disposer disposer ; ///< leaf node disposer
+ typedef typename traits::hook hook; ///< hook type
+ typedef typename hook::node_type node_type; ///< node type
+ typedef typename traits::disposer disposer; ///< leaf node disposer
+ typedef typename traits::back_off back_off; ///< back-off strategy
- typedef cds::gc::guarded_ptr< gc, value_type > guarded_ptr; ///< Guarded pointer
+ typedef typename gc::template guarded_ptr< value_type > guarded_ptr; ///< Guarded pointer
protected:
//@cond
- typedef ellen_bintree::base_node< gc > tree_node ; ///< Base type of tree node
- typedef node_type leaf_node ; ///< Leaf node type
+ typedef ellen_bintree::base_node< gc > tree_node; ///< Base type of tree node
+ typedef node_type leaf_node; ///< Leaf node type
typedef ellen_bintree::node_types< gc, key_type, typename leaf_node::tag > node_factory;
- typedef typename node_factory::internal_node_type internal_node ; ///< Internal node type
- typedef typename node_factory::update_desc_type update_desc ; ///< Update descriptor
- typedef typename update_desc::update_ptr update_ptr ; ///< Marked pointer to update descriptor
+ typedef typename node_factory::internal_node_type internal_node; ///< Internal node type
+ typedef typename node_factory::update_desc_type update_desc; ///< Update descriptor
+ typedef typename update_desc::update_ptr update_ptr; ///< Marked pointer to update descriptor
//@endcond
public:
# ifdef CDS_DOXYGEN_INVOKED
- typedef implementation_defined key_comparator ; ///< key compare functor based on opt::compare and opt::less option setter.
- typedef typename get_node_traits< value_type, node_type, hook>::type node_traits ; ///< Node traits
+ typedef implementation_defined key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
+ typedef typename get_node_traits< value_type, node_type, hook>::type node_traits; ///< Node traits
# else
- typedef typename opt::details::make_comparator< value_type, options >::type key_comparator;
+ typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
struct node_traits: public get_node_traits< value_type, node_type, hook>::type
{
static internal_node const& to_internal_node( tree_node const& n )
};
# endif
- typedef typename options::item_counter item_counter; ///< Item counting policy used
- typedef typename options::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
- typedef typename options::stat stat ; ///< internal statistics type
- typedef typename options::key_extractor key_extractor ; ///< key extracting functor
+ typedef typename traits::item_counter item_counter; ///< Item counting policy
+ typedef typename traits::memory_model memory_model; ///< Memory ordering, see \p cds::opt::memory_model
+ typedef typename traits::stat stat; ///< internal statistics type
+ typedef typename traits::key_extractor key_extractor; ///< key extracting functor
- typedef typename options::node_allocator node_allocator ; ///< Internal node allocator
- typedef typename options::update_desc_allocator update_desc_allocator ; ///< Update descriptor allocator
+ typedef typename traits::node_allocator node_allocator; ///< Allocator for internal node
+ typedef typename traits::update_desc_allocator update_desc_allocator; ///< Update descriptor allocator
protected:
//@cond
leaf_node * pLeaf;
update_ptr updParent;
update_ptr updGrandParent;
- bool bRightLeaf ; // true if pLeaf is right child of pParent, false otherwise
- bool bRightParent ; // true if pParent is right child of pGrandParent, false otherwise
+ bool bRightLeaf; // true if pLeaf is right child of pParent, false otherwise
+ bool bRightParent; // true if pParent is right child of pGrandParent, false otherwise
search_result()
:pGrandParent( nullptr )
- , pParent( nullptr )
- , pLeaf( nullptr )
+ ,pParent( nullptr )
+ ,pLeaf( nullptr )
,bRightLeaf( false )
,bRightParent( false )
{}
protected:
//@cond
- internal_node m_Root ; ///< Tree root node (key= Infinite2)
- leaf_node m_LeafInf1 ; ///< Infinite leaf 1 (key= Infinite1)
- leaf_node m_LeafInf2 ; ///< Infinite leaf 2 (key= Infinite2)
+ internal_node m_Root; ///< Tree root node (key= Infinite2)
+ leaf_node m_LeafInf1; ///< Infinite leaf 1 (key= Infinite1)
+ leaf_node m_LeafInf2; ///< Infinite leaf 2 (key= Infinite2)
//@endcond
- item_counter m_ItemCounter ; ///< item counter
- mutable stat m_Stat ; ///< internal statistics
+ item_counter m_ItemCounter; ///< item counter
+ mutable stat m_Stat; ///< internal statistics
protected:
//@cond
/// Default constructor
EllenBinTree()
{
- static_assert( (!std::is_same< key_extractor, opt::none >::value), "The key extractor option must be specified" );
+ static_assert( !std::is_same< key_extractor, opt::none >::value, "The key extractor option must be specified" );
make_empty_tree();
}
\endcode
where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
\p val no any other changes could be made on this tree's item by concurrent threads.
- The user-defined functor is called only if the inserting is success and may be passed by reference
- using <tt>boost::ref</tt>
+ The user-defined functor is called only if the inserting is success.
*/
template <typename Func>
bool insert( value_type& val, Func f )
guardInsert.assign( &val );
unique_internal_node_ptr pNewInternal;
-
search_result res;
+ back_off bkoff;
+
for ( ;; ) {
if ( search( res, val, node_compare() )) {
if ( pNewInternal.get() )
pNewInternal.reset( alloc_internal_node() );
if ( try_insert( val, pNewInternal.get(), res )) {
- cds::unref(f)( val );
+ f( val );
pNewInternal.release(); // internal node is linked into the tree and should not be deleted
break;
}
}
+ bkoff();
m_Stat.onInsertRetry();
}
\endcode
with arguments:
- \p bNew - \p true if the item has been inserted, \p false otherwise
- - \p item - item of the tree
- - \p val - argument \p val passed into the \p ensure function
+ - \p item - an item of the tree
+ - \p val - the argument \p val passed to the \p ensure function
If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
refer to the same thing.
The functor can change non-key fields of the \p item; however, \p func must guarantee
that during changing no any other modifications could be made on this item by concurrent threads.
- You can pass \p func argument by value or by reference using <tt>boost::ref</tt> or cds::ref.
-
Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
\p second is \p true if new item has been added or \p false if the item with \p key
already is in the tree.
+
+ @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
*/
template <typename Func>
std::pair<bool, bool> ensure( value_type& val, Func func )
guardInsert.assign( &val );
unique_internal_node_ptr pNewInternal;
-
search_result res;
+ back_off bkoff;
+
for ( ;; ) {
if ( search( res, val, node_compare() )) {
- cds::unref(func)( false, *node_traits::to_value_ptr( res.pLeaf ), val );
+ func( false, *node_traits::to_value_ptr( res.pLeaf ), val );
if ( pNewInternal.get() )
m_Stat.onInternalNodeDeleted() ; // unique_internal_node_ptr deletes internal node
m_Stat.onEnsureExist();
pNewInternal.reset( alloc_internal_node() );
if ( try_insert( val, pNewInternal.get(), res )) {
- cds::unref(func)( true, val, val );
+ func( true, val, val );
pNewInternal.release() ; // internal node has been linked into the tree and should not be deleted
break;
}
}
+
+ bkoff();
m_Stat.onEnsureRetry();
}
Difference between \ref erase and \p unlink functions: \p erase finds <i>a key</i>
and deletes the item found. \p unlink finds an item by key and deletes it
- only if \p val is an item of the tree, i.e. the pointer to item found
- is equal to <tt> &val </tt>.
+ only if \p val is a node, i.e. the pointer to item found is equal to <tt> &val </tt>.
- The \ref disposer specified in \p Traits class template parameter is called
+ The \p disposer specified in \p Traits class template parameter is called
by garbage collector \p GC asynchronously.
The function returns \p true if success and \p false otherwise.
/// Deletes the item from the tree
/** \anchor cds_intrusive_EllenBinTree_erase
- The function searches an item with key equal to \p val in the tree,
+ The function searches an item with key equal to \p key in the tree,
unlinks it from the tree, and returns \p true.
- If the item with key equal to \p val is not found the function return \p false.
+ If the item with key equal to \p key is not found the function return \p false.
Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
*/
template <typename Q>
- bool erase( const Q& val )
+ bool erase( const Q& key )
{
- return erase_( val, node_compare(),
+ return erase_( key, node_compare(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
[](value_type const&) {} );
}
\p pred must imply the same element order as the comparator used for building the tree.
*/
template <typename Q, typename Less>
- bool erase_with( const Q& val, Less pred )
+ bool erase_with( const Q& key, Less pred )
{
+ CDS_UNUSED( pred );
typedef ellen_bintree::details::compare<
key_type,
value_type,
node_traits
> compare_functor;
- return erase_( val, compare_functor(),
+ return erase_( key, compare_functor(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
[](value_type const&) {} );
}
/// Deletes the item from the tree
/** \anchor cds_intrusive_EllenBinTree_erase_func
- The function searches an item with key equal to \p val in the tree,
+ The function searches an item with key equal to \p key in the tree,
call \p f functor with item found, unlinks it from the tree, and returns \p true.
The \ref disposer specified in \p Traits class template parameter is called
by garbage collector \p GC asynchronously.
void operator()( value_type const& item );
};
\endcode
- The functor can be passed by reference with <tt>boost:ref</tt>
- If the item with key equal to \p val is not found the function return \p false.
+ If the item with key equal to \p key is not found the function return \p false.
Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
*/
template <typename Q, typename Func>
- bool erase( Q const& val, Func f )
+ bool erase( Q const& key, Func f )
{
- return erase_( val, node_compare(),
+ return erase_( key, node_compare(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
f );
}
\p pred must imply the same element order as the comparator used for building the tree.
*/
template <typename Q, typename Less, typename Func>
- bool erase_with( Q const& val, Less pred, Func f )
+ bool erase_with( Q const& key, Less pred, Func f )
{
+ CDS_UNUSED( pred );
typedef ellen_bintree::details::compare<
key_type,
value_type,
node_traits
> compare_functor;
- return erase_( val, compare_functor(),
+ return erase_( key, compare_functor(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
f );
}
/// Extracts an item with minimal key from the tree
/**
- The function searches an item with minimal key, unlinks it, and returns pointer to an item found in \p dest parameter.
- If the tree is empty the function returns \p false.
+ The function searches an item with minimal key, unlinks it, and returns a guarded pointer to an item found.
+ If the tree is empty the function returns an empty guarded pointer.
@note Due the concurrent nature of the tree, the function extracts <i>nearly</i> minimum key.
It means that the function gets leftmost leaf of the tree and tries to unlink it.
During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
So, the function returns the item with minimum key at the moment of tree traversing.
- The guarded pointer \p dest prevents disposer invocation for returned item,
- see cds::gc::guarded_ptr for explanation.
+ The returned \p guarded_ptr prevents disposer invocation for returned item,
+ see \p cds::gc::guarded_ptr for explanation.
@note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
*/
- bool extract_min( guarded_ptr& dest )
+ guarded_ptr extract_min()
{
- return extract_min_( dest.guard());
+ guarded_ptr gp;
+ extract_min_( gp.guard() );
+ return gp;
}
/// Extracts an item with maximal key from the tree
/**
- The function searches an item with maximal key, unlinks it, and returns pointer to an item found in \p dest parameter.
- If the tree is empty the function returns \p false.
+ The function searches an item with maximal key, unlinks it, and returns a guarded pointer to an item found.
+ If the tree is empty the function returns an empty \p guarded_ptr.
@note Due the concurrent nature of the tree, the function extracts <i>nearly</i> maximal key.
It means that the function gets rightmost leaf of the tree and tries to unlink it.
During unlinking, a concurrent thread may insert an item with key great than rightmost item's key.
So, the function returns the item with maximal key at the moment of tree traversing.
- The guarded pointer \p dest prevents disposer invocation for returned item,
+ The returned \p guarded_ptr prevents disposer invocation for returned item,
see cds::gc::guarded_ptr for explanation.
@note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
*/
- bool extract_max( guarded_ptr& dest )
+ guarded_ptr extract_max()
{
- return extract_max_( dest.guard() );
+ guarded_ptr gp;
+ extract_max_( gp.guard());
+ return gp;
}
/// Extracts an item from the tree
/** \anchor cds_intrusive_EllenBinTree_extract
The function searches an item with key equal to \p key in the tree,
- unlinks it, and returns pointer to an item found in \p dest parameter.
- If the item is not found the function returns \p false.
+ unlinks it, and returns a guarded pointer to an item found.
+ If the item is not found the function returns an empty \p guarded_ptr.
- The guarded pointer \p dest prevents disposer invocation for returned item,
+ \p guarded_ptr prevents disposer invocation for returned item,
see cds::gc::guarded_ptr for explanation.
@note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
*/
template <typename Q>
- bool extract( guarded_ptr& dest, Q const& key )
+ guarded_ptr extract( Q const& key )
{
- return extract_( dest.guard(), key );
+ guarded_ptr gp;
+ extract_( gp.guard(), key );
+ return gp;
}
/// Extracts an item from the tree using \p pred for searching
/**
- The function is an analog of \ref cds_intrusive_EllenBinTree_extract "extract(guarded_ptr& dest, Q const&)"
+ The function is an analog of \ref cds_intrusive_EllenBinTree_extract "extract(Q const&)"
but \p pred is used for key compare.
\p Less has the interface like \p std::less and should meet \ref cds_intrusive_EllenBinTree_less
"Predicate requirements".
\p pred must imply the same element order as the comparator used for building the tree.
*/
template <typename Q, typename Less>
- bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
+ guarded_ptr extract_with( Q const& key, Less pred )
{
- return extract_with_( dest.guard(), key, pred );
+ guarded_ptr gp;
+ extract_with_( gp.guard(), key, pred );
+ return gp;
}
- /// Finds the key \p val
+ /// Finds the key \p key
/** @anchor cds_intrusive_EllenBinTree_find_val
- The function searches the item with key equal to \p val
+ The function searches the item with key equal to \p key
and returns \p true if it is found, and \p false otherwise.
Note the hash functor specified for class \p Traits template parameter
should accept a parameter of type \p Q that can be not the same as \p value_type.
*/
template <typename Q>
- bool find( Q const& val ) const
+ bool find( Q const& key ) const
{
search_result res;
- if ( search( res, val, node_compare() )) {
+ if ( search( res, key, node_compare() )) {
m_Stat.onFindSuccess();
return true;
}
return false;
}
- /// Finds the key \p val with comparing functor \p pred
+ /// Finds the key \p key with comparing functor \p pred
/**
The function is an analog of \ref cds_intrusive_EllenBinTree_find_val "find(Q const&)"
but \p pred is used for key compare.
\p pred should accept arguments of type \p Q, \p key_type, \p value_type in any combination.
*/
template <typename Q, typename Less>
- bool find_with( Q const& val, Less pred ) const
+ bool find_with( Q const& key, Less pred ) const
{
+ CDS_UNUSED( pred );
typedef ellen_bintree::details::compare<
key_type,
value_type,
> compare_functor;
search_result res;
- if ( search( res, val, compare_functor() )) {
+ if ( search( res, key, compare_functor() )) {
m_Stat.onFindSuccess();
return true;
}
return false;
}
- /// Finds the key \p val
+ /// Finds the key \p key
/** @anchor cds_intrusive_EllenBinTree_find_func
- The function searches the item with key equal to \p val and calls the functor \p f for item found.
+ The function searches the item with key equal to \p key and calls the functor \p f for item found.
The interface of \p Func functor is:
\code
struct functor {
- void operator()( value_type& item, Q& val );
+ void operator()( value_type& item, Q& key );
};
\endcode
- where \p item is the item found, \p val is the <tt>find</tt> function argument.
-
- You can pass \p f argument by value or by reference using <tt>boost::ref</tt> or cds::ref.
+ where \p item is the item found, \p key is the <tt>find</tt> function argument.
The functor can change non-key fields of \p item. Note that the functor is only guarantee
that \p item cannot be disposed during functor is executing.
The functor does not serialize simultaneous access to the tree \p item. If such access is
possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
- The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
- can modify both arguments.
-
- The function returns \p true if \p val is found, \p false otherwise.
+ The function returns \p true if \p key is found, \p false otherwise.
*/
template <typename Q, typename Func>
- bool find( Q& val, Func f ) const
+ bool find( Q& key, Func f ) const
{
- return find_( val, f );
+ return find_( key, f );
}
+ //@cond
+ template <typename Q, typename Func>
+ bool find( Q const& key, Func f ) const
+ {
+ return find_( key, f );
+ }
+ //@endcond
- /// Finds the key \p val with comparing functor \p pred
+ /// Finds the key \p key with comparing functor \p pred
/**
The function is an analog of \ref cds_intrusive_EllenBinTree_find_func "find(Q&, Func)"
but \p pred is used for key comparison.
\p pred must imply the same element order as the comparator used for building the tree.
*/
template <typename Q, typename Less, typename Func>
- bool find_with( Q& val, Less pred, Func f ) const
+ bool find_with( Q& key, Less pred, Func f ) const
{
- return find_with_( val, pred, f );
+ return find_with_( key, pred, f );
}
-
- /// Finds the key \p val
- /** @anchor cds_intrusive_EllenBinTree_find_cfunc
- The function searches the item with key equal to \p val and calls the functor \p f for item found.
- The interface of \p Func functor is:
- \code
- struct functor {
- void operator()( value_type& item, Q const& val );
- };
- \endcode
- where \p item is the item found, \p val is the <tt>find</tt> function argument.
-
- You can pass \p f argument by value or by reference using <tt>boost::ref</tt> or cds::ref.
-
- The functor can change non-key fields of \p item. Note that the functor is only guarantee
- that \p item cannot be disposed during functor is executing.
- The functor does not serialize simultaneous access to the tree \p item. If such access is
- possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
-
- The function returns \p true if \p val is found, \p false otherwise.
- */
- template <typename Q, typename Func>
- bool find( Q const& val, Func f ) const
- {
- return find_( val, f );
- }
-
- /// Finds the key \p val with comparing functor \p pred
- /**
- The function is an analog of \ref cds_intrusive_EllenBinTree_find_cfunc "find(Q const&, Func)"
- but \p pred is used for key compare.
- \p Less functor has the interface like \p std::less and should meet \ref cds_intrusive_EllenBinTree_less
- "Predicate requirements".
- \p pred must imply the same element order as the comparator used for building the tree.
- */
+ //@cond
template <typename Q, typename Less, typename Func>
- bool find_with( Q const& val, Less pred, Func f ) const
+ bool find_with( Q const& key, Less pred, Func f ) const
{
- return find_with_( val, pred, f );
+ return find_with_( key, pred, f );
}
+ //@endcond
/// Finds \p key and returns the item found
/** @anchor cds_intrusive_EllenBinTree_get
- The function searches the item with key equal to \p key and returns the item found in \p dest parameter.
- The function returns \p true if \p key is found, \p false otherwise.
+ The function searches the item with key equal to \p key and returns the item found as \p guarded_ptr object.
+ The function returns an empty guarded pointer is \p key is not found.
- The guarded pointer \p dest prevents disposer invocation for returned item,
- see cds::gc::guarded_ptr for explanation.
+ \p guarded_ptr prevents disposer invocation for returned item,
+ see \p cds::gc::guarded_ptr for explanation.
@note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
*/
template <typename Q>
- bool get( guarded_ptr& dest, Q const& key ) const
+ guarded_ptr get( Q const& key ) const
{
- return get_( dest.guard(), key );
+ guarded_ptr gp;
+ get_( gp.guard(), key );
+ return gp;
}
/// Finds \p key with predicate \p pred and returns the item found
/**
- The function is an analog of \ref cds_intrusive_EllenBinTree_get "get(guarded_ptr&, Q const&)"
+ The function is an analog of \ref cds_intrusive_EllenBinTree_get "get(Q const&)"
but \p pred is used for key comparing.
\p Less functor has the interface like \p std::less and should meet \ref cds_intrusive_EllenBinTree_less
"Predicate requirements".
\p pred must imply the same element order as the comparator used for building the tree.
*/
template <typename Q, typename Less>
- bool get_with( guarded_ptr& dest, Q const& key, Less pred ) const
+ guarded_ptr get_with( Q const& key, Less pred ) const
{
- return get_with_( dest.guard(), key, pred );
+ guarded_ptr gp;
+ get_with_( gp.guard(), key, pred );
+ return gp;
}
/// Checks if the tree is empty
return m_Root.m_pLeft.load( memory_model::memory_order_relaxed )->is_leaf();
}
- /// Clears the tree (thread safe, non-atomic)
+ /// Clears the tree (thread safe, not atomic)
/**
The function unlink all items from the tree.
The function is thread safe but not atomic: in multi-threaded environment with parallel insertions
\endcode
the assertion could be raised.
- For each leaf the \ref disposer will be called after unlinking.
+ For each leaf the \p disposer will be called after unlinking.
*/
void clear()
{
guarded_ptr gp;
- while ( extract_min(gp));
+ do {
+ gp = extract_min();
+ } while ( gp );
}
/// Clears the tree (not thread safe)
Only leaf nodes containing user data are counted.
The value returned depends on item counter type provided by \p Traits template parameter.
- If it is atomicity::empty_item_counter this function always returns 0.
- Therefore, the function is not suitable for checking the tree emptiness, use \ref empty
+ If it is \p atomicity::empty_item_counter this function always returns 0.
+ The function is not suitable for checking the tree emptiness, use \p empty()
member function for this purpose.
*/
size_t size() const
return true;
}
+ /*
void help( update_ptr pUpdate )
{
// pUpdate must be guarded!
break;
}
}
+ */
void help_insert( update_desc * pOp )
{
assert( res.pLeaf->is_leaf() );
// check search result
- if ( ( res.bRightLeaf
+ if ( (res.bRightLeaf
? res.pParent->m_pRight.load( memory_model::memory_order_acquire )
- : res.pParent->m_pLeft.load( memory_model::memory_order_acquire ) ) == res.pLeaf )
- {
+ : res.pParent->m_pLeft.load( memory_model::memory_order_acquire )) == res.pLeaf ) {
leaf_node * pNewLeaf = node_traits::to_node_ptr( val );
- int nCmp = node_compare()( val, *res.pLeaf );
+ int nCmp = node_compare()(val, *res.pLeaf);
if ( nCmp < 0 ) {
if ( res.pGrandParent ) {
assert( !res.pLeaf->infinite_key() );
pNewInternal->infinite_key( 0 );
- key_extractor()( pNewInternal->m_Key, *node_traits::to_value_ptr( res.pLeaf ) );
+ key_extractor()(pNewInternal->m_Key, *node_traits::to_value_ptr( res.pLeaf ));
}
else {
assert( res.pLeaf->infinite_key() == tree_node::key_infinite1 );
assert( !res.pLeaf->is_internal() );
pNewInternal->infinite_key( 0 );
- key_extractor()( pNewInternal->m_Key, val );
+ key_extractor()(pNewInternal->m_Key, val);
pNewInternal->m_pLeft.store( static_cast<tree_node *>(res.pLeaf), memory_model::memory_order_relaxed );
pNewInternal->m_pRight.store( static_cast<tree_node *>(pNewLeaf), memory_model::memory_order_release );
- assert( !res.pLeaf->infinite_key());
+ assert( !res.pLeaf->infinite_key() );
}
typename gc::Guard guard;
update_ptr updCur( res.updParent.ptr() );
if ( res.pParent->m_pUpdate.compare_exchange_strong( updCur, update_ptr( pOp, update_desc::IFlag ),
- memory_model::memory_order_acquire, atomics::memory_order_relaxed ))
- {
+ memory_model::memory_order_acquire, atomics::memory_order_relaxed ) ) {
// do insert
help_insert( pOp );
retire_update_desc( pOp );
free_update_desc( pOp );
}
}
+
return false;
}
{
update_desc * pOp = nullptr;
search_result res;
+ back_off bkoff;
for ( ;; ) {
if ( !search( res, val, cmp ) || !eq( val, *res.pLeaf ) ) {
update_ptr updGP( res.updGrandParent.ptr() );
if ( res.pGrandParent->m_pUpdate.compare_exchange_strong( updGP, update_ptr( pOp, update_desc::DFlag ),
- memory_model::memory_order_acquire, atomics::memory_order_relaxed ))
- {
- if ( help_delete( pOp )) {
+ memory_model::memory_order_acquire, atomics::memory_order_relaxed ) ) {
+ if ( help_delete( pOp ) ) {
// res.pLeaf is not deleted yet since it is guarded
- cds::unref(f)( *node_traits::to_value_ptr( res.pLeaf ));
+ f( *node_traits::to_value_ptr( res.pLeaf ) );
break;
}
pOp = nullptr;
}
}
+ bkoff();
m_Stat.onEraseRetry();
}
}
template <typename Q>
- bool extract_( typename gc::Guard& guard, Q const& key )
+ bool extract_( typename guarded_ptr::native_guard& guard, Q const& key )
{
return erase_( key, node_compare(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
- [&guard]( value_type& found ) { guard.assign( &found ); } );
+ [&guard]( value_type& found ) { guard.set( &found ); } );
}
template <typename Q, typename Less>
- bool extract_with_( typename gc::Guard& guard, Q const& key, Less pred )
+ bool extract_with_( typename guarded_ptr::native_guard& guard, Q const& key, Less /*pred*/ )
{
typedef ellen_bintree::details::compare<
key_type,
return erase_( key, compare_functor(),
[]( Q const&, leaf_node const& ) -> bool { return true; },
- [&guard]( value_type& found ) { guard.assign( &found ); } );
+ [&guard]( value_type& found ) { guard.set( &found ); } );
}
- bool extract_max_( typename gc::Guard& guard )
+ bool extract_max_( typename guarded_ptr::native_guard& gp )
{
update_desc * pOp = nullptr;
search_result res;
+ back_off bkoff;
for ( ;; ) {
if ( !search_max( res )) {
return false;
}
- if ( res.updGrandParent.bits() == update_desc::Clean && res.updParent.bits() == update_desc::Clean ) {
+ if ( res.updGrandParent.bits() == update_desc::Clean && res.updParent.bits() == update_desc::Clean ) {
if ( !pOp )
pOp = alloc_update_desc();
if ( check_delete_precondition( res ) ) {
update_ptr updGP( res.updGrandParent.ptr() );
if ( res.pGrandParent->m_pUpdate.compare_exchange_strong( updGP, update_ptr( pOp, update_desc::DFlag ),
- memory_model::memory_order_acquire, atomics::memory_order_relaxed ))
+ memory_model::memory_order_acquire, atomics::memory_order_relaxed ) )
{
- if ( help_delete( pOp ))
+ if ( help_delete( pOp ) )
break;
pOp = nullptr;
}
}
}
+ bkoff();
m_Stat.onExtractMaxRetry();
}
--m_ItemCounter;
m_Stat.onExtractMaxSuccess();
- guard.assign( node_traits::to_value_ptr( res.pLeaf ) );
+ gp.set( node_traits::to_value_ptr( res.pLeaf ));
return true;
}
- bool extract_min_( typename gc::Guard& guard )
+ bool extract_min_( typename guarded_ptr::native_guard& gp )
{
update_desc * pOp = nullptr;
search_result res;
+ back_off bkoff;
for ( ;; ) {
if ( !search_min( res )) {
}
}
+ bkoff();
m_Stat.onExtractMinRetry();
}
--m_ItemCounter;
m_Stat.onExtractMinSuccess();
- guard.assign( node_traits::to_value_ptr( res.pLeaf ));
+ gp.set( node_traits::to_value_ptr( res.pLeaf ));
return true;
}
search_result res;
if ( search( res, val, node_compare() )) {
assert( res.pLeaf );
- cds::unref(f)( *node_traits::to_value_ptr( res.pLeaf ), val );
+ f( *node_traits::to_value_ptr( res.pLeaf ), val );
m_Stat.onFindSuccess();
return true;
}
template <typename Q, typename Less, typename Func>
- bool find_with_( Q& val, Less pred, Func f ) const
+ bool find_with_( Q& val, Less /*pred*/, Func f ) const
{
typedef ellen_bintree::details::compare<
key_type,
search_result res;
if ( search( res, val, compare_functor() )) {
assert( res.pLeaf );
- cds::unref(f)( *node_traits::to_value_ptr( res.pLeaf ), val );
+ f( *node_traits::to_value_ptr( res.pLeaf ), val );
m_Stat.onFindSuccess();
return true;
}
template <typename Q>
- bool get_( typename gc::Guard& guard, Q const& val ) const
+ bool get_( typename guarded_ptr::native_guard& guard, Q const& val ) const
{
- return find_( val, [&guard]( value_type& found, Q const& ) { guard.assign( &found ); } );
+ return find_( val, [&guard]( value_type& found, Q const& ) { guard.set( &found ); } );
}
template <typename Q, typename Less>
- bool get_with_( typename gc::Guard& guard, Q const& val, Less pred ) const
+ bool get_with_( typename guarded_ptr::native_guard& guard, Q const& val, Less pred ) const
{
- return find_with_( val, pred, [&guard]( value_type& found, Q const& ) { guard.assign( &found ); } );
+ return find_with_( val, pred, [&guard]( value_type& found, Q const& ) { guard.set( &found ); } );
}
//@endcond