cds  2.3.1
cds::intrusive::MichaelList< GC, T, Traits > Class Template Reference

Michael's lock-free ordered single-linked list. More...

#include <cds/intrusive/impl/michael_list.h>

Inheritance diagram for cds::intrusive::MichaelList< GC, T, Traits >:
cds::container::MichaelList< GC, T, Traits >

Public Types

typedef T value_type
 type of value stored in the list
 
typedef Traits traits
 Traits template parameter.
 
typedef traits::hook hook
 hook type
 
typedef hook::node_type node_type
 node type
 
typedef implementation_defined key_comparator
 key comparison functor based on opt::compare and opt::less option setter.
 
typedef traits::disposer disposer
 disposer used
 
typedef traits::stat stat
 Internal statistics.
 
typedef get_node_traits< value_type, node_type, hook >::type node_traits
 node traits
 
typedef michael_list::get_link_checker< node_type, traits::link_checker >::type link_checker
 link checker
 
typedef GC gc
 Garbage collector.
 
typedef traits::back_off back_off
 back-off strategy
 
typedef traits::item_counter item_counter
 Item counting policy used.
 
typedef traits::memory_model memory_model
 Memory ordering. See cds::opt::memory_model option.
 
typedef gc::template guarded_ptr< value_typeguarded_ptr
 Guarded pointer.
 

Public Member Functions

 MichaelList ()
 Default constructor initializes empty list.
 
 ~MichaelList ()
 Destroys the list object.
 
bool insert (value_type &val)
 Inserts new node. More...
 
template<typename Func >
bool insert (value_type &val, Func f)
 Inserts new node. More...
 
template<typename Func >
std::pair< bool, bool > update (value_type &val, Func func, bool bInsert=true)
 Updates the node. More...
 
bool unlink (value_type &val)
 Unlinks the item val from the list. More...
 
template<typename Q >
bool erase (Q const &key)
 Deletes the item from the list. More...
 
template<typename Q , typename Less >
bool erase_with (Q const &key, Less pred)
 Deletes the item from the list using pred predicate for searching. More...
 
template<typename Q , typename Func >
bool erase (Q const &key, Func func)
 Deletes the item from the list. More...
 
template<typename Q , typename Less , typename Func >
bool erase_with (Q const &key, Less pred, Func f)
 Deletes the item from the list using pred predicate for searching. More...
 
template<typename Q >
guarded_ptr extract (Q const &key)
 Extracts the item from the list with specified key. More...
 
template<typename Q , typename Less >
guarded_ptr extract_with (Q const &key, Less pred)
 Extracts the item using compare functor pred. More...
 
template<typename Q , typename Func >
bool find (Q &key, Func f)
 Finds key in the list. More...
 
template<typename Q , typename Less , typename Func >
bool find_with (Q &key, Less pred, Func f)
 Finds the key using pred predicate for searching. More...
 
template<typename Q >
bool contains (Q const &key)
 Checks whether the list contains key. More...
 
template<typename Q , typename Less >
bool contains (Q const &key, Less pred)
 Checks whether the list contains key using pred predicate for searching. More...
 
template<typename Q >
guarded_ptr get (Q const &key)
 Finds the key and return the item found. More...
 
template<typename Q , typename Less >
guarded_ptr get_with (Q const &key, Less pred)
 Finds the key and return the item found. More...
 
void clear ()
 Clears the list. More...
 
bool empty () const
 Checks whether the list is empty.
 
size_t size () const
 Returns list's item count. More...
 
stat const & statistics () const
 Returns const reference to internal statistics.
 

Static Public Attributes

static constexpr const size_t c_nHazardPtrCount = 4
 Count of hazard pointer required for the algorithm.
 

Protected Types

typedef node_type::atomic_marked_ptr atomic_node_ptr
 Atomic node pointer.
 
typedef node_type::marked_ptr marked_node_ptr
 Node marked pointer.
 
typedef atomic_node_ptr auxiliary_head
 Auxiliary head type (for split-list support)
 

Protected Attributes

atomic_node_ptr m_pHead
 Head pointer.
 
item_counter m_ItemCounter
 Item counter.
 
stat m_Stat
 Internal statistics.
 

Forward iterators (only for debugging purpose)

typedef iterator_type< false > iterator
 Forward iterator. More...
 
typedef iterator_type< true > const_iterator
 Const forward iterator. More...
 
iterator begin ()
 Returns a forward iterator addressing the first element in a list. More...
 
iterator end ()
 Returns an iterator that addresses the location succeeding the last element in a list. More...
 
const_iterator cbegin () const
 Returns a forward const iterator addressing the first element in a list.
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a list.
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a list.
 
const_iterator cend () const
 Returns an const iterator that addresses the location succeeding the last element in a list.
 

Detailed Description

template<class GC, typename T, class Traits = michael_list::traits>
class cds::intrusive::MichaelList< GC, T, Traits >

Michael's lock-free ordered single-linked list.

Usually, ordered single-linked list is used as a building block for the hash table implementation. The complexity of searching is O(N).

Source:

  • [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"

Template arguments:

Usage
There are different specializations of this template for each garbage collecting schema. You should select GC needed and include appropriate .h-file:

Then, you should incorporate michael_list::node into your struct T and provide appropriate michael_list::traits::hook in your Traits template parameters. Usually, for Traits you define a struct based on michael_list::traits.

Example for gc::DHP and base hook:

// Include GC-related Michael's list specialization
#include <cds/intrusive/michael_list_dhp.h>
// Data stored in Michael's list
struct my_data: public cds::intrusive::michael_list::node< cds::gc::DHP >
{
// key field
std::string strKey;
// other data
// ...
};
// my_data comparing functor
struct my_data_cmp {
int operator()( const my_data& d1, const my_data& d2 )
{
return d1.strKey.compare( d2.strKey );
}
int operator()( const my_data& d, const std::string& s )
{
return d.strKey.compare(s);
}
int operator()( const std::string& s, const my_data& d )
{
return s.compare( d.strKey );
}
};
// Declare traits
struct my_traits: public cds::intrusive::michael_list::traits
{
typedef my_data_cmp compare;
};
// Declare list type

Equivalent option-based code:

// GC-related specialization
#include <cds/intrusive/michael_list_dhp.h>
struct my_data {
// see above
};
struct compare {
// see above
};
// Declare option-based list
typedef cds::intrusive::MichaelList< cds::gc::DHP
,my_data
cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP > > >
>::type
> option_based_list;

Member Typedef Documentation

◆ const_iterator

template<class GC, typename T, class Traits = michael_list::traits>
typedef iterator_type<true> cds::intrusive::MichaelList< GC, T, Traits >::const_iterator

Const forward iterator.

For iterator's features and requirements see iterator

◆ iterator

template<class GC, typename T, class Traits = michael_list::traits>
typedef iterator_type<false> cds::intrusive::MichaelList< GC, T, Traits >::iterator

Forward iterator.

The forward iterator for Michael's list has some features:

  • it has no post-increment operator
  • to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator. For some GC (like as gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard" may be thrown if the limit of guard count per thread is exceeded.
  • The iterator cannot be moved across thread boundary since it contains thread-private GC's guard.
  • Iterator ensures thread-safety even if you delete the item the iterator points to. However, in case of concurrent deleting operations there is no guarantee that you iterate all item in the list. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
Warning
Use this iterator on the concurrent container for debugging purpose only.

The iterator interface:

class iterator {
public:
// Default constructor
// Copy construtor
iterator( iterator const& src );
// Dereference operator
value_type * operator ->() const;
// Dereference operator
value_type& operator *() const;
// Preincrement operator
iterator& operator ++();
// Assignment operator
iterator& operator = (iterator const& src);
// Equality operators
bool operator ==(iterator const& i ) const;
bool operator !=(iterator const& i ) const;
};

Member Function Documentation

◆ begin()

template<class GC, typename T, class Traits = michael_list::traits>
iterator cds::intrusive::MichaelList< GC, T, Traits >::begin ( )
inline

Returns a forward iterator addressing the first element in a list.

For empty list

begin() == end()

◆ clear()

template<class GC, typename T, class Traits = michael_list::traits>
void cds::intrusive::MichaelList< GC, T, Traits >::clear ( )
inline

Clears the list.

The function unlink all items from the list.

◆ contains() [1/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q >
bool cds::intrusive::MichaelList< GC, T, Traits >::contains ( Q const &  key)
inline

Checks whether the list contains key.

The function searches the item with key equal to key and returns true if it is found, and false otherwise.

◆ contains() [2/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less >
bool cds::intrusive::MichaelList< GC, T, Traits >::contains ( Q const &  key,
Less  pred 
)
inline

Checks whether the list contains key using pred predicate for searching.

The function is an analog of contains( key ) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the list.

◆ end()

template<class GC, typename T, class Traits = michael_list::traits>
iterator cds::intrusive::MichaelList< GC, T, Traits >::end ( )
inline

Returns an iterator that addresses the location succeeding the last element in a list.

Do not use the value returned by end function to access any item. Internally, end returning value equals to nullptr.

The returned value can be used only to control reaching the end of the list. For empty list begin() == end()

◆ erase() [1/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q >
bool cds::intrusive::MichaelList< GC, T, Traits >::erase ( Q const &  key)
inline

Deletes the item from the list.

The function searches an item with key equal to key in the list, unlinks it from the list, and returns true. If key is not found the function return false.

disposer specified in Traits is called for deleted item.

◆ erase() [2/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Func >
bool cds::intrusive::MichaelList< GC, T, Traits >::erase ( Q const &  key,
Func  func 
)
inline

Deletes the item from the list.

The function searches an item with key equal to key in the list, call func functor with item found, unlinks it from the list, and returns true. The Func interface is

struct functor {
void operator()( value_type const& item );
};

If key is not found the function return false, func is not called.

disposer specified in Traits is called for deleted item.

◆ erase_with() [1/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less >
bool cds::intrusive::MichaelList< GC, T, Traits >::erase_with ( Q const &  key,
Less  pred 
)
inline

Deletes the item from the list using pred predicate for searching.

The function is an analog of erase(Q const&) but pred is used for key comparing. Less functor has the interface like std::less. pred must imply the same element order as the comparator used for building the list.

disposer specified in Traits is called for deleted item.

◆ erase_with() [2/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less , typename Func >
bool cds::intrusive::MichaelList< GC, T, Traits >::erase_with ( Q const &  key,
Less  pred,
Func  f 
)
inline

Deletes the item from the list using pred predicate for searching.

The function is an analog of erase(Q const&, Func) but pred is used for key comparing. Less functor has the interface like std::less. pred must imply the same element order as the comparator used for building the list.

disposer specified in Traits is called for deleted item.

◆ extract()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q >
guarded_ptr cds::intrusive::MichaelList< GC, T, Traits >::extract ( Q const &  key)
inline

Extracts the item from the list with specified key.

The function searches an item with key equal to key, unlinks it from the list, and returns it as guarded_ptr. If key is not found returns an empty guarded pointer.

Note the compare functor should accept a parameter of type Q that can be not the same as value_type.

The disposer specified in Traits class template parameter is called automatically by garbage collector GC when returned guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses the GC's guard that can be limited resource.

Usage:

ord_list theList;
// ...
{
ord_list::guarded_ptr gp(theList.extract( 5 ));
if ( gp ) {
// Deal with gp
// ...
}
// Destructor of gp releases internal HP guard
}

◆ extract_with()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less >
guarded_ptr cds::intrusive::MichaelList< GC, T, Traits >::extract_with ( Q const &  key,
Less  pred 
)
inline

Extracts the item using compare functor pred.

The function is an analog of extract(Q const&) but pred predicate is used for key comparing.

Less functor has the semantics like std::less but should take arguments of type value_type and Q in any order. pred must imply the same element order as the comparator used for building the list.

◆ find()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Func >
bool cds::intrusive::MichaelList< GC, T, Traits >::find ( Q &  key,
Func  f 
)
inline

Finds key in the list.

The function searches the item with key equal to key and calls the functor f for item found. The interface of Func functor is:

struct functor {
void operator()( value_type& item, Q& key );
};

where item is the item found, key is the find function argument.

The functor may change non-key fields of item. Note that the function is only guarantee that item cannot be disposed during functor is executing. The function does not serialize simultaneous access to the item. If such access is possible you must provide your own synchronization schema to keep out unsafe item modifications.

The key argument is non-const since it can be used as f functor destination i.e., the functor may modify both arguments.

The function returns true if val is found, false otherwise.

◆ find_with()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less , typename Func >
bool cds::intrusive::MichaelList< GC, T, Traits >::find_with ( Q &  key,
Less  pred,
Func  f 
)
inline

Finds the key using pred predicate for searching.

The function is an analog of find(Q&, Func) but pred is used for key comparing. Less functor has the interface like std::less. pred must imply the same element order as the comparator used for building the list.

◆ get()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q >
guarded_ptr cds::intrusive::MichaelList< GC, T, Traits >::get ( Q const &  key)
inline

Finds the key and return the item found.

The function searches the item with key equal to key and returns it as guarded_ptr. If key is not found the function returns an empty guarded pointer.

The disposer specified in Traits class template parameter is called by garbage collector GC automatically when returned guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses one GC's guard which can be limited resource.

Usage:

ord_list theList;
// ...
{
ord_list::guarded_ptr gp(theList.get( 5 ));
if ( gp ) {
// Deal with gp
//...
}
// Destructor of guarded_ptr releases internal HP guard
}

Note the compare functor specified for Traits template parameter should accept a parameter of type Q that can be not the same as value_type.

◆ get_with()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Q , typename Less >
guarded_ptr cds::intrusive::MichaelList< GC, T, Traits >::get_with ( Q const &  key,
Less  pred 
)
inline

Finds the key and return the item found.

The function is an analog of get( Q const&) but pred is used for comparing the keys.

Less functor has the semantics like std::less but should take arguments of type value_type and Q in any order. pred must imply the same element order as the comparator used for building the list.

◆ insert() [1/2]

template<class GC, typename T, class Traits = michael_list::traits>
bool cds::intrusive::MichaelList< GC, T, Traits >::insert ( value_type val)
inline

Inserts new node.

The function inserts val into the list if the list does not contain an item with key equal to val.

Returns true if val has been linked to the list, false otherwise.

◆ insert() [2/2]

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Func >
bool cds::intrusive::MichaelList< GC, T, Traits >::insert ( value_type val,
Func  f 
)
inline

Inserts new node.

This function is intended for derived non-intrusive containers.

The function allows to split new item creating into two part:

  • create item with key only
  • insert new item into the list
  • if inserting is success, calls f functor to initialize value-field of val.

The functor signature is:

void func( value_type& val );

where val is the item inserted. User-defined functor f should guarantee that during changing val no any other changes could be made on this list's item by concurrent threads. The user-defined functor is called only if the inserting is success.

Warning
See insert item troubleshooting

◆ size()

template<class GC, typename T, class Traits = michael_list::traits>
size_t cds::intrusive::MichaelList< GC, T, Traits >::size ( ) const
inline

Returns list's item count.

The value returned depends on item counter provided by Traits. For atomicity::empty_item_counter, this function always returns 0.

Note
Even if you use real item counter and it returns 0, this fact does not mean that the list is empty. To check list emptiness use empty() method.

◆ unlink()

template<class GC, typename T, class Traits = michael_list::traits>
bool cds::intrusive::MichaelList< GC, T, Traits >::unlink ( value_type val)
inline

Unlinks the item val from the list.

The function searches the item val in the list and unlinks it from the list if it is found and it is equal to val.

Difference between erase() and unlink(): erase() finds a key and deletes the item found. unlink() finds an item by key and deletes it only if val is an item of the list, i.e. the pointer to item found is equal to &val .

disposer specified in Traits is called for deleted item.

The function returns true if success and false otherwise.

◆ update()

template<class GC, typename T, class Traits = michael_list::traits>
template<typename Func >
std::pair<bool, bool> cds::intrusive::MichaelList< GC, T, Traits >::update ( value_type val,
Func  func,
bool  bInsert = true 
)
inline

Updates the node.

The operation performs inserting or changing data with lock-free manner.

If the item val is not found in the list, then val is inserted iff bInsert is true. Otherwise, the functor func is called with item found. The functor signature is:

void func( bool bNew, value_type& item, value_type& val );

with arguments:

  • bNew - true if the item has been inserted, false otherwise
  • item - item of the list
  • val - argument val passed into the update() function If new item has been inserted (i.e. bNew is true) then item and val arguments refers to the same thing.

The functor may change non-key fields of the item; however, func must guarantee that during changing no any other modifications could be made on this item by concurrent threads.

Returns std::pair<bool, bool> where first is true if operation is successful, second is true if new item has been added or false if the item with that key already in the list.

Warning
See insert item troubleshooting

The documentation for this class was generated from the following file:

cds 2.3.1 Developed by Maxim Khizhinsky aka khizmax 2007 - 2017
Autogenerated Fri Sep 1 2017 08:47:22 by Doxygen 1.8.13