//$$CDS-header$$
-#ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
-#define __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#ifndef CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#define CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H
#include <cds/intrusive/details/michael_list_base.h>
-#include <cds/gc/guarded_ptr.h>
#include <cds/details/make_const_type.h>
namespace cds { namespace intrusive {
typedef typename traits::item_counter item_counter; ///< Item counting policy used
typedef typename traits::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
- typedef cds::gc::guarded_ptr< gc, value_type > guarded_ptr; ///< Guarded pointer
+ typedef typename gc::template guarded_ptr< value_type > guarded_ptr; ///< Guarded pointer
//@cond
// Rebind traits (split-list support)
link_checker::is_empty( pNode );
marked_node_ptr cur(pos.pCur);
- pNode->m_pNext.store( cur, memory_model::memory_order_relaxed );
+ pNode->m_pNext.store( cur, memory_model::memory_order_release );
return pos.pPrev->compare_exchange_strong( cur, marked_node_ptr(pNode), memory_model::memory_order_release, atomics::memory_order_relaxed );
}
// physical deletion may be performed by search function if it detects that a node is logically deleted (marked)
// CAS may be successful here or in other thread that searching something
marked_node_ptr cur(pos.pCur);
- if ( pos.pPrev->compare_exchange_strong( cur, marked_node_ptr( pos.pNext ), memory_model::memory_order_release, atomics::memory_order_relaxed ))
+ if ( pos.pPrev->compare_exchange_strong( cur, marked_node_ptr( pos.pNext ), memory_model::memory_order_acquire, atomics::memory_order_relaxed ))
retire_node( pos.pCur );
return true;
}
return insert_at( m_pHead, val, f );
}
- /// Ensures that the \p val exists in the list
+ /// Updates the node
/**
The operation performs inserting or changing data with lock-free manner.
- If the item \p val is not found in the list, then \p val is inserted.
+ If the item \p val is not found in the list, then \p val is inserted
+ iff \p bInsert is \p true.
Otherwise, the functor \p func is called with item found.
The functor signature is:
\code
with arguments:
- \p bNew - \p true if the item has been inserted, \p false otherwise
- \p item - item of the list
- - \p val - argument \p val passed into the \p ensure function
+ - \p val - argument \p val passed into the \p update() function
If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
refers to the same thing.
@warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
*/
template <typename Func>
+ std::pair<bool, bool> update( value_type& val, Func func, bool bInsert = true )
+ {
+ return update_at( m_pHead, val, func, bInsert );
+ }
+
+ //@cond
+ // Deprecated, use update()
+ template <typename Func>
std::pair<bool, bool> ensure( value_type& val, Func func )
{
- return ensure_at( m_pHead, val, func );
+ return update( val, func, true );
}
+ //@endcond
/// Unlinks the item \p val from the list
/**
template <typename Q, typename Less>
bool erase_with( Q const& key, Less pred )
{
+ CDS_UNUSED( pred );
return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
}
template <typename Q, typename Less, typename Func>
bool erase_with( Q const& key, Less pred, Func f )
{
+ CDS_UNUSED( pred );
return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
}
/// Extracts the item from the list with specified \p key
/** \anchor cds_intrusive_MichaelList_hp_extract
The function searches an item with key equal to \p key,
- unlinks it from the list, and returns it in \p dest parameter.
- If the item with key equal to \p key is not found the function returns \p false.
+ unlinks it from the list, and returns it as \p guarded_ptr.
+ If \p key is not found returns an empty guarded pointer.
Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
ord_list theList;
// ...
{
- ord_list::guarded_ptr gp;
- theList.extract( gp, 5 );
- // Deal with gp
- // ...
-
+ ord_list::guarded_ptr gp(theList.extract( 5 ));
+ if ( gp ) {
+ // Deal with gp
+ // ...
+ }
// Destructor of gp releases internal HP guard
}
\endcode
*/
template <typename Q>
- bool extract( guarded_ptr& dest, Q const& key )
+ guarded_ptr extract( Q const& key )
{
- return extract_at( m_pHead, dest.guard(), key, key_comparator() );
+ guarded_ptr gp;
+ extract_at( m_pHead, gp.guard(), key, key_comparator() );
+ return gp;
}
/// Extracts the item using compare functor \p pred
/**
- The function is an analog of \ref cds_intrusive_MichaelList_hp_extract "extract(guarded_ptr&, Q const&)"
+ The function is an analog of \ref cds_intrusive_MichaelList_hp_extract "extract(Q const&)"
but \p pred predicate is used for key comparing.
\p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
\p pred must imply the same element order as the comparator used for building the list.
*/
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_at( m_pHead, dest.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+ CDS_UNUSED( pred );
+ guarded_ptr gp;
+ extract_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+ return gp;
}
/// Finds \p key in the list
{
return find_at( m_pHead, key, key_comparator(), f );
}
+ //@cond
+ template <typename Q, typename Func>
+ bool find( Q const& key, Func f )
+ {
+ return find_at( m_pHead, key, key_comparator(), f );
+ }
+ //@endcond
/// Finds the \p key using \p pred predicate for searching
/**
template <typename Q, typename Less, typename Func>
bool find_with( Q& key, Less pred, Func f )
{
+ CDS_UNUSED( pred );
+ return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
+ }
+ //@cond
+ template <typename Q, typename Less, typename Func>
+ bool find_with( Q const& key, Less pred, Func f )
+ {
+ CDS_UNUSED( pred );
return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
}
+ //@endcond
/// Finds the \p key
/** \anchor cds_intrusive_MichaelList_hp_find_val
template <typename Q, typename Less>
bool find_with( Q const& key, Less pred )
{
+ CDS_UNUSED( pred );
return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>() );
}
/// Finds the \p key and return the item found
/** \anchor cds_intrusive_MichaelList_hp_get
The function searches the item with key equal to \p key
- and assigns the item found to guarded pointer \p ptr.
- The function returns \p true if \p key is found, and \p false otherwise.
- If \p key is not found the \p ptr parameter is not changed.
+ and returns it as \p guarded_ptr.
+ If \p key is not found the function returns an empty guarded pointer.
The \ref disposer specified in \p Traits class template parameter is called
by garbage collector \p GC automatically when returned \ref guarded_ptr object
ord_list theList;
// ...
{
- ord_list::guarded_ptr gp;
- if ( theList.get( gp, 5 )) {
+ ord_list::guarded_ptr gp(theList.get( 5 ));
+ if ( gp ) {
// Deal with gp
//...
}
should accept a parameter of type \p Q that can be not the same as \p value_type.
*/
template <typename Q>
- bool get( guarded_ptr& ptr, Q const& key )
+ guarded_ptr get( Q const& key )
{
- return get_at( m_pHead, ptr.guard(), key, key_comparator() );
+ guarded_ptr gp;
+ get_at( m_pHead, gp.guard(), key, key_comparator() );
+ return gp;
}
/// Finds the \p key and return the item found
/**
- The function is an analog of \ref cds_intrusive_MichaelList_hp_get "get( guarded_ptr& ptr, Q const&)"
+ The function is an analog of \ref cds_intrusive_MichaelList_hp_get "get( Q const&)"
but \p pred is used for comparing the keys.
\p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
\p pred must imply the same element order as the comparator used for building the list.
*/
template <typename Q, typename Less>
- bool get_with( guarded_ptr& ptr, Q const& key, Less pred )
+ guarded_ptr get_with( Q const& key, Less pred )
{
- return get_at( m_pHead, ptr.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+ CDS_UNUSED( pred );
+ guarded_ptr gp;
+ get_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>() );
+ return gp;
}
/// Clears the list
}
template <typename Func>
- std::pair<bool, bool> ensure_at( atomic_node_ptr& refHead, value_type& val, Func func )
+ std::pair<bool, bool> update_at( atomic_node_ptr& refHead, value_type& val, Func func, bool bInsert )
{
position pos;
return std::make_pair( true, false );
}
else {
+ if ( !bInsert )
+ return std::make_pair( false, false );
+
typename gc::Guard guard;
guard.assign( &val );
if ( link_node( pNode, pos ) ) {
}
}
+ template <typename Func>
+ std::pair<bool, bool> ensure_at( atomic_node_ptr& refHead, value_type& val, Func func )
+ {
+ return update_at( refHead, val, func, true );
+ }
+
bool unlink_at( atomic_node_ptr& refHead, value_type& val )
{
position pos;
}
template <typename Q, typename Compare>
- bool extract_at( atomic_node_ptr& refHead, typename gc::Guard& dest, Q const& val, Compare cmp )
+ bool extract_at( atomic_node_ptr& refHead, typename guarded_ptr::native_guard& dest, Q const& val, Compare cmp )
{
position pos;
back_off bkoff;
while ( search( refHead, val, pos, cmp )) {
if ( unlink_node( pos ) ) {
- dest.assign( pos.guards.template get<value_type>( position::guard_current_item ) );
+ dest.set( pos.guards.template get<value_type>( position::guard_current_item ) );
--m_ItemCounter;
return true;
}
}
template <typename Q, typename Compare>
- bool get_at( atomic_node_ptr& refHead, typename gc::Guard& guard, Q const& val, Compare cmp )
+ bool get_at( atomic_node_ptr& refHead, typename guarded_ptr::native_guard& guard, Q const& val, Compare cmp )
{
position pos;
if ( search( refHead, val, pos, cmp )) {
- guard.assign( pos.guards.template get<value_type>( position::guard_current_item ));
+ guard.set( pos.guards.template get<value_type>( position::guard_current_item ));
return true;
}
return false;
back_off bkoff;
-try_again:
+ try_again:
pPrev = &refHead;
pNext = nullptr;
- pCur = pPrev->load(memory_model::memory_order_relaxed);
- pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ) );
- if ( pPrev->load(memory_model::memory_order_acquire) != pCur.ptr() )
- goto try_again;
+ pCur = pos.guards.protect( position::guard_current_item, *pPrev,
+ [](marked_node_ptr p) -> value_type *
+ {
+ return node_traits::to_value_ptr( p.ptr() );
+ });
while ( true ) {
if ( pCur.ptr() == nullptr ) {
pos.pPrev = pPrev;
- pos.pCur = pCur.ptr();
- pos.pNext = pNext.ptr();
+ pos.pCur = nullptr;
+ pos.pNext = nullptr;
return false;
}
- pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed);
- pos.guards.assign( position::guard_next_item, node_traits::to_value_ptr( pNext.ptr() ));
- if ( pCur->m_pNext.load(memory_model::memory_order_relaxed).all() != pNext.all() ) {
- bkoff();
- goto try_again;
- }
-
- if ( pPrev->load(memory_model::memory_order_relaxed).all() != pCur.ptr() ) {
+ pNext = pos.guards.protect( position::guard_next_item, pCur->m_pNext,
+ [](marked_node_ptr p ) -> value_type *
+ {
+ return node_traits::to_value_ptr( p.ptr() );
+ });
+ if ( pPrev->load(memory_model::memory_order_acquire).all() != pCur.ptr() ) {
bkoff();
goto try_again;
}
if ( pNext.bits() == 1 ) {
// pCur marked i.e. logically deleted. Help the erase/unlink function to unlink pCur node
marked_node_ptr cur( pCur.ptr());
- if ( pPrev->compare_exchange_strong( cur, marked_node_ptr( pNext.ptr() ), memory_model::memory_order_release, atomics::memory_order_relaxed )) {
+ if ( pPrev->compare_exchange_strong( cur, marked_node_ptr( pNext.ptr() ), memory_model::memory_order_acquire, atomics::memory_order_relaxed )) {
retire_node( pCur.ptr() );
}
else {
return nCmp == 0;
}
pPrev = &( pCur->m_pNext );
- pos.guards.assign( position::guard_prev_item, node_traits::to_value_ptr( pCur.ptr() ) );
+ pos.guards.copy( position::guard_prev_item, position::guard_current_item );
}
pCur = pNext;
- pos.guards.assign( position::guard_current_item, node_traits::to_value_ptr( pCur.ptr() ));
+ pos.guards.copy( position::guard_current_item, position::guard_next_item );
}
}
//@endcond
};
}} // namespace cds::intrusive
-#endif // #ifndef __CDS_INTRUSIVE_IMPL_MICHAEL_LIST_H
+#endif // #ifndef CDSLIB_INTRUSIVE_IMPL_MICHAEL_LIST_H