-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
-#ifndef __CDS_INTRUSIVE_MICHAEL_LIST_NOGC_H
-#define __CDS_INTRUSIVE_MICHAEL_LIST_NOGC_H
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+ Source code repo: http://github.com/khizmax/libcds/
+ Download: http://sourceforge.net/projects/libcds/files/
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef CDSLIB_INTRUSIVE_MICHAEL_LIST_NOGC_H
+#define CDSLIB_INTRUSIVE_MICHAEL_LIST_NOGC_H
#include <cds/intrusive/details/michael_list_base.h>
#include <cds/gc/nogc.h>
#include <cds/details/make_const_type.h>
-
namespace cds { namespace intrusive {
namespace michael_list {
typedef typename get_node_traits< value_type, node_type, hook>::type node_traits ; ///< node traits
typedef typename michael_list::get_link_checker< node_type, traits::link_checker >::type link_checker; ///< link checker
- typedef typename traits::back_off back_off; ///< back-off strategy
- 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 typename traits::back_off back_off; ///< back-off strategy
+ 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 typename traits::stat stat; ///< Internal statistics
//@cond
+ static_assert((std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type");
+
// Rebind traits (split-list support)
template <typename... Options>
struct rebind_traits {
, typename cds::opt::make_options< traits, Options...>::type
> type;
};
+
+ // Stat selector
+ template <typename Stat>
+ using select_stat_wrapper = michael_list::select_stat_wrapper< Stat >;
//@endcond
protected:
atomic_node_ptr m_pHead; ///< Head pointer
item_counter m_ItemCounter; ///< Item counter
+ stat m_Stat; ///< Internal statistics
//@cond
/// Position pointer for item search
link_checker::is_empty( pNode );
pNode->m_pNext.store( pos.pCur, memory_model::memory_order_relaxed );
- return pos.pPrev->compare_exchange_strong( pos.pCur, pNode, memory_model::memory_order_release, atomics::memory_order_relaxed );
+ if ( cds_likely( pos.pPrev->compare_exchange_strong( pos.pCur, pNode, memory_model::memory_order_release, atomics::memory_order_relaxed )))
+ return true;
+
+ pNode->m_pNext.store( nullptr, memory_model::memory_order_relaxed );
+ return false;
}
//@endcond
*/
iterator begin()
{
- return iterator(m_pHead.load(memory_model::memory_order_relaxed) );
+ return iterator(m_pHead.load(memory_model::memory_order_relaxed));
}
/// Returns an iterator that addresses the location succeeding the last element in a list
/// Returns a forward const iterator addressing the first element in a list
const_iterator begin() const
{
- return const_iterator(m_pHead.load(memory_model::memory_order_relaxed) );
+ return const_iterator(m_pHead.load(memory_model::memory_order_relaxed));
}
/// Returns a forward const iterator addressing the first element in a list
const_iterator cbegin() const
{
- return const_iterator(m_pHead.load(memory_model::memory_order_relaxed) );
+ return const_iterator(m_pHead.load(memory_model::memory_order_relaxed));
}
/// Returns an const iterator that addresses the location succeeding the last element in a list
/// Default constructor initializes empty list
MichaelList()
: m_pHead( nullptr )
- {
- static_assert( (std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type" );
- }
+ {}
+
+ //@cond
+ template <typename Stat, typename = std::enable_if<std::is_same<stat, michael_list::wrapped_stat<Stat>>::value >>
+ explicit MichaelList( Stat& st )
+ : m_pHead( nullptr )
+ , m_Stat( st )
+ {}
+ //@endcond
/// Destroys the list objects
~MichaelList()
return insert_at( m_pHead, val );
}
- /// Ensures that the \p item exists in the list
+ /// Updates the item
/**
The operation performs inserting or changing data with lock-free manner.
- If the item \p val not found in the list, then \p val is inserted into the list.
+ If the item \p val not found in the list, then \p val is inserted into the list
+ iff \p bAllowInsert 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
refer to the same thing.
The functor may 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.
- Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
- \p second is true if new item has been added or \p false if the item with \p key
+ Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful,
+ \p second is \p true if new item has been added or \p false if the item with \p key
already is in the list.
*/
-
template <typename Func>
+ std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
+ {
+ return update_at( m_pHead, val, func, bAllowInsert );
+ }
+ //@cond
+ template <typename Func>
+ CDS_DEPRECATED("ensure() is deprecated, use update()")
std::pair<bool, bool> ensure( value_type& val, Func func )
{
- return ensure_at( m_pHead, val, func );
+ return update( val, func );
}
+ //@endcond
/// Finds the key \p val
/** \anchor cds_intrusive_MichaelList_nogc_find_func
}
//@endcond
- /// Finds \p key
- /** \anchor cds_intrusive_MichaelList_nogc_find_val
+ /// Checks whether the list contains \p key
+ /**
The function searches the item with key equal to \p key
- and returns pointer to value found or \p nullptr.
+ and returns \p true if it is found, and \p false otherwise.
*/
template <typename Q>
+ value_type * contains( Q const& key )
+ {
+ return find_at( m_pHead, key, key_comparator());
+ }
+ //@cond
+ template <typename Q>
+ CDS_DEPRECATED("deprecated, use contains()")
value_type * find( Q const& key )
{
- return find_at( m_pHead, key, key_comparator() );
+ return contains( key );
}
+ //@endcond
- /// Finds \p key using \p pred predicate for searching
+ /// Checks whether the map contains \p key using \p pred predicate for searching
/**
- The function is an analog of \ref cds_intrusive_MichaelList_nogc_find_val "find(Q const&, Func)"
- but \p pred is used for key comparing.
+ The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
\p Less functor has the interface like \p std::less.
- \p pred must imply the same element order as the comparator used for building the list.
+ \p Less must imply the same element order as the comparator used for building the list.
*/
template <typename Q, typename Less>
- value_type * find_with( Q const& key, Less pred )
+ value_type * contains( Q const& key, Less pred )
{
CDS_UNUSED( pred );
return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
}
+ //@cond
+ template <typename Q, typename Less>
+ CDS_DEPRECATED("deprecated, use contains()")
+ value_type * find_with( Q const& key, Less pred )
+ {
+ return contains( key, pred );
+ }
+ //@endcond
/// Clears the list
/**
void clear( Disposer disp )
{
node_type * pHead = m_pHead.load(memory_model::memory_order_relaxed);
- do {} while ( !m_pHead.compare_exchange_weak( pHead, nullptr, memory_model::memory_order_relaxed ) );
+ do {} while ( cds_unlikely( !m_pHead.compare_exchange_weak( pHead, nullptr, memory_model::memory_order_relaxed )));
while ( pHead ) {
node_type * p = pHead->m_pNext.load(memory_model::memory_order_relaxed);
*/
void clear()
{
- clear( disposer() );
+ clear( disposer());
}
/// Checks if the list is empty
return m_ItemCounter.value();
}
+ /// Returns const reference to internal statistics
+ stat const& statistics() const
+ {
+ return m_Stat;
+ }
+
protected:
//@cond
// split-list support
// Hack: convert node_type to value_type.
// In principle, auxiliary node can be non-reducible to value_type
// We assume that comparator can correctly distinguish aux and regular node.
- return insert_at( refHead, *node_traits::to_value_ptr( pNode ) );
+ return insert_at( refHead, *node_traits::to_value_ptr( pNode ));
}
bool insert_at( atomic_node_ptr& refHead, value_type& val )
{
- link_checker::is_empty( node_traits::to_node_ptr( val ) );
position pos;
while ( true ) {
- if ( search( refHead, val, key_comparator(), pos ) )
+ if ( search( refHead, val, key_comparator(), pos )) {
+ m_Stat.onInsertFailed();
return false;
+ }
- if ( link_node( node_traits::to_node_ptr( val ), pos ) ) {
+ if ( link_node( node_traits::to_node_ptr( val ), pos )) {
++m_ItemCounter;
+ m_Stat.onInsertSuccess();
return true;
}
+
+ m_Stat.onInsertRetry();
}
}
}
template <typename Func>
- std::pair<iterator, bool> ensure_at_( atomic_node_ptr& refHead, value_type& val, Func func )
+ std::pair<iterator, bool> update_at_( atomic_node_ptr& refHead, value_type& val, Func func, bool bAllowInsert )
{
position pos;
while ( true ) {
- if ( search( refHead, val, key_comparator(), pos ) ) {
- assert( key_comparator()( val, *node_traits::to_value_ptr( *pos.pCur ) ) == 0 );
+ if ( search( refHead, val, key_comparator(), pos )) {
+ assert( key_comparator()( val, *node_traits::to_value_ptr( *pos.pCur )) == 0 );
func( false, *node_traits::to_value_ptr( *pos.pCur ) , val );
+ m_Stat.onUpdateExisting();
return std::make_pair( iterator( pos.pCur ), false );
}
else {
- link_checker::is_empty( node_traits::to_node_ptr( val ) );
+ if ( !bAllowInsert ) {
+ m_Stat.onUpdateFailed();
+ return std::make_pair( end(), false );
+ }
- if ( link_node( node_traits::to_node_ptr( val ), pos ) ) {
+ if ( link_node( node_traits::to_node_ptr( val ), pos )) {
++m_ItemCounter;
func( true, val , val );
+ m_Stat.onUpdateNew();
return std::make_pair( iterator( node_traits::to_node_ptr( val )), true );
}
}
+
+ m_Stat.onUpdateRetry();
}
}
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 bAllowInsert )
{
- std::pair<iterator, bool> ret = ensure_at_( refHead, val, func );
+ std::pair<iterator, bool> ret = update_at_( refHead, val, func, bAllowInsert );
return std::make_pair( ret.first != end(), ret.second );
}
{
position pos;
- if ( search( refHead, val, cmp, pos ) ) {
+ if ( search( refHead, val, cmp, pos )) {
assert( pos.pCur != nullptr );
f( *node_traits::to_value_ptr( *pos.pCur ), val );
+ m_Stat.onFindSuccess();
return true;
}
+
+ m_Stat.onFindFailed();
return false;
}
value_type * find_at( atomic_node_ptr& refHead, Q const& val, Compare cmp )
{
iterator it = find_at_( refHead, val, cmp );
- if ( it != end() )
+ if ( it != end()) {
+ m_Stat.onFindSuccess();
return &*it;
+ }
+
+ m_Stat.onFindFailed();
return nullptr;
}
{
position pos;
- if ( search( refHead, val, cmp, pos ) ) {
+ if ( search( refHead, val, cmp, pos )) {
assert( pos.pCur != nullptr );
+ m_Stat.onFindSuccess();
return iterator( pos.pCur );
}
+
+ m_Stat.onFindFailed();
return end();
}
}
pNext = pCur->m_pNext.load(memory_model::memory_order_relaxed);
- if ( pCur->m_pNext.load(memory_model::memory_order_acquire) != pNext ) {
+ if ( cds_unlikely( pCur->m_pNext.load(memory_model::memory_order_acquire) != pNext )) {
bkoff();
goto try_again;
}
- if ( pPrev->load(memory_model::memory_order_acquire) != pCur ) {
+ if ( cds_unlikely( pPrev->load(memory_model::memory_order_acquire) != pCur )) {
bkoff();
goto try_again;
}
pCur = pNext;
}
}
+
+ // for split-list
+ template <typename Predicate>
+ void erase_for( Predicate pred )
+ {
+ node_type * pPred = nullptr;
+ node_type * pHead = m_pHead.load( memory_model::memory_order_relaxed );
+ while ( pHead ) {
+ node_type * p = pHead->m_pNext.load( memory_model::memory_order_relaxed );
+ if ( pred( *node_traits::to_value_ptr( pHead ))) {
+ assert( pPred != nullptr );
+ pPred->m_pNext.store( p, memory_model::memory_order_relaxed );
+ dispose_node( pHead, disposer());
+ }
+ else
+ pPred = pHead;
+ pHead = p;
+ }
+ }
//@endcond
};
}} // namespace cds::intrusive
-#endif // #ifndef __CDS_INTRUSIVE_MICHAEL_LIST_NOGC_H
+#endif // #ifndef CDSLIB_INTRUSIVE_MICHAEL_LIST_NOGC_H