cds
2.3.2
|
Split-ordered list. More...
#include <cds/intrusive/split_list.h>
Public Types | |
typedef GC | gc |
Garbage collector. | |
typedef Traits | traits |
Set traits. | |
typedef OrderedList | ordered_list |
type of ordered list used as a base for split-list | |
typedef ordered_list::value_type | value_type |
type of value stored in the split-list | |
typedef ordered_list::key_comparator | key_comparator |
key comparison functor | |
typedef ordered_list::disposer | disposer |
Node disposer functor. | |
typedef cds::opt::v::hash_selector< typename traits::hash >::type | hash |
Hash functor for value_type and all its derivatives you use. | |
typedef traits::bit_reversal | bit_reversal |
Bit reversal algorithm, see split_list::traits::bit_reversal . | |
typedef traits::item_counter | item_counter |
Item counter type. | |
typedef traits::back_off | back_off |
back-off strategy for spinning | |
typedef traits::memory_model | memory_model |
Memory ordering. See cds::opt::memory_model option. | |
typedef traits::stat | stat |
Internal statistics, see spit_list::stat . | |
typedef ordered_list::guarded_ptr | guarded_ptr |
Guarded pointer. | |
Public Member Functions | |
SplitListSet () | |
Initialize split-ordered list of default capacity. More... | |
SplitListSet (size_t nItemCount, size_t nLoadFactor=1) | |
Initialize split-ordered list. More... | |
~SplitListSet () | |
Destroys split-list set. | |
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 bAllowInsert=true) |
Updates the node. More... | |
std::pair< bool, bool > | upsert (value_type &val, bool bAllowInsert=true) |
Inserts or updates the node (only for IterableList ) More... | |
bool | unlink (value_type &val) |
Unlinks the item val from the set. More... | |
template<typename Q > | |
bool | erase (Q const &key) |
Deletes the item from the set. More... | |
template<typename Q , typename Less > | |
bool | erase_with (const Q &key, Less pred) |
Deletes the item from the set with comparing functor pred . More... | |
template<typename Q , typename Func > | |
bool | erase (Q const &key, Func f) |
Deletes the item from the set. More... | |
template<typename Q , typename Less , typename Func > | |
bool | erase_with (Q const &key, Less pred, Func f) |
Deletes the item from the set with comparing functor pred . More... | |
bool | erase_at (iterator const &iter) |
Deletes the item pointed by iterator iter (only for IterableList based set) More... | |
template<typename Q > | |
guarded_ptr | extract (Q const &key) |
Extracts the item 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 the key key . More... | |
template<typename Q > | |
iterator | find (Q &key) |
Finds key and returns iterator pointed to the item found (only for IterableList ) More... | |
template<typename Q , typename Less , typename Func > | |
bool | find_with (Q &key, Less pred, Func f) |
Finds the key key with pred predicate for comparing. More... | |
template<typename Q , typename Less > | |
iterator | find_with (Q &key, Less pred) |
Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList ) More... | |
template<typename Q > | |
bool | contains (Q const &key) |
Checks whether the set contains key . More... | |
template<typename Q , typename Less > | |
bool | contains (Q const &key, Less pred) |
Checks whether the set contains key using pred predicate for searching. More... | |
template<typename Q > | |
guarded_ptr | get (Q const &key) |
Finds the key key and return the item found. More... | |
template<typename Q , typename Less > | |
guarded_ptr | get_with (Q const &key, Less pred) |
Finds the key key and return the item found. More... | |
size_t | size () const |
Returns item count in the set. | |
bool | empty () const |
Checks if the set is empty. More... | |
void | clear () |
Clears the set (non-atomic) More... | |
stat const & | statistics () const |
Returns internal statistics. | |
OrderedList::stat const & | list_statistics () const |
Returns internal statistics for OrderedList . | |
Static Public Attributes | |
static constexpr const size_t | c_nHazardPtrCount = ordered_list::c_nHazardPtrCount + 4 |
Count of hazard pointer required. | |
Forward iterators | |
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 split-list. More... | |
iterator | end () |
Returns an iterator that addresses the location succeeding the last element in a split-list. More... | |
const_iterator | begin () const |
Returns a forward const iterator addressing the first element in a split-list. | |
const_iterator | cbegin () const |
Returns a forward const iterator addressing the first element in a split-list. | |
const_iterator | end () const |
Returns an const iterator that addresses the location succeeding the last element in a split-list. | |
const_iterator | cend () const |
Returns an const iterator that addresses the location succeeding the last element in a split-list. | |
Split-ordered list.
Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
The split-ordered list is a lock-free implementation of an extensible unbounded hash table. It uses original recursive split-ordering algorithm discovered by Ori Shalev and Nir Shavit that allows to split buckets without item moving on resizing.
Short description [from [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"]
The algorithm keeps all the items in one lock-free linked list, and gradually assigns the bucket pointers to the places in the list where a sublist of 'correct' items can be found. A bucket is initialized upon first access by assigning it to a new 'dummy' node (dashed contour) in the list, preceding all items that should be in that bucket. A newly created bucket splits an older bucket's chain, reducing the access cost to its items. The table uses a modulo 2**i hash (there are known techniques for 'pre-hashing' before a modulo 2**i hash to overcome possible binary correlations among values). The table starts at size 2 and repeatedly doubles in size.
Unlike moving an item, the operation of directing a bucket pointer can be done in a single CAS operation, and since items are not moved, they are never 'lost'. However, to make this approach work, one must be able to keep the items in the list sorted in such a way that any bucket's sublist can be 'split' by directing a new bucket pointer within it. This operation must be recursively repeatable, as every split bucket may be split again and again as the hash table grows. To achieve this goal the authors introduced recursive split-ordering, a new ordering on keys that keeps items in a given bucket adjacent in the list throughout the repeated splitting process.
Magically, yet perhaps not surprisingly, recursive split-ordering is achieved by simple binary reversal: reversing the bits of the hash key so that the new key's most significant bits (MSB) are those that were originally its least significant. The split-order keys of regular nodes are exactly the bit-reverse image of the original keys after turning on their MSB. For example, items 9 and 13 are in the 1 mod 4
bucket, which can be recursively split in two by inserting a new node between them.
To insert (respectively delete or search for) an item in the hash table, hash its key to the appropriate bucket using recursive split-ordering, follow the pointer to the appropriate location in the sorted items list, and traverse the list until the key's proper location in the split-ordering (respectively until the key or a key indicating the item is not in the list is found). Because of the combinatorial structure induced by the split-ordering, this will require traversal of no more than an expected constant number of items.
The design is modular: to implement the ordered items list, you can use one of several non-blocking list-based set algorithms: MichaelList, LazyList.
Implementation
Template parameters are:
GC
- Garbage collector. Note the GC
must be the same as the GC
used for OrderedList
OrderedList
- ordered list implementation used as a bucket for hash set, for example, MichaelList
, LazyList
. The intrusive ordered list implementation specifies the type T
stored in the split-list set, the comparison functor for the type T
and other features specific for the ordered list.Traits
- split-list traits, default is split_list::traits
. Instead of defining Traits
struct you can use option-based syntax provided by split_list::make_traits
metafunction.There are several specialization of the split-list class for different GC:
<cds/intrusive/split_list_rcu.h>
- see RCU-based split-list<cds/intrusive/split_list_nogc.h>
- see persistent SplitListSet.Some member functions of split-ordered list accept the key parameter of type Q
which differs from value_type
. It is expected that type Q
contains full key of value_type
, and for equal keys of type Q
and value_type
the hash values of these keys must be equal too. The hash functor Traits::hash
should accept parameters of both type:
How to use
Split-list based on IterableList
differs from split-list based on MichaelList
or LazyList
because IterableList
stores data "as is" - it cannot use any hook.
Suppose, your split-list contains values of type Foo
. For MichaelList
and LazyList
, Foo
declaration should be based on ordered-list node:
MichaelList
: LazyList
: For IterableList
, Foo
should be based on void:
Everything else is the same. Consider split-list based on MichaelList
.
First, you should choose ordered list type to use in your split-list set:
Second, you should declare split-list set container:
Now, you can use Foo_set
in your application.
typedef iterator_type<true> cds::intrusive::SplitListSet< GC, OrderedList, Traits >::const_iterator |
Const forward iterator.
For iterator's features and requirements see iterator
typedef iterator_type<false> cds::intrusive::SplitListSet< GC, OrderedList, Traits >::iterator |
Forward iterator.
The forward iterator is based on OrderedList
forward iterator and has some features:
Iterator thread safety depends on type of OrderedList:
MichaelList
and LazyList:
iterator guarantees safety even if you delete the item that iterator points to because that item is guarded by hazard pointer. However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the set. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread. Use this iterator on the concurrent container for debugging purpose only.IterableList:
iterator is thread-safe. You may use it freely in concurrent environment.
|
inline |
Initialize split-ordered list of default capacity.
The default capacity is defined in bucket table constructor. See split_list::expandable_bucket_table
, split_list::static_bucket_table
which selects by split_list::dynamic_bucket_table
option.
|
inline |
Initialize split-ordered list.
nItemCount | estimate average of item count |
nLoadFactor | load factor - average item count per bucket. Small integer up to 8, default is 1. |
|
inline |
|
inline |
Clears the set (non-atomic)
The function unlink all items from the set. The function is not atomic. After call the split-list can be non-empty.
For each item the disposer
is called after unlinking.
|
inline |
Checks whether the set contains key
.
The function searches the item with key equal to key
and returns true
if it is found, and false
otherwise.
Note the hash functor specified for class Traits
template parameter should accept a parameter of type Q
that can be not the same as value_type
. Otherwise, you may use contains( Q const&, Less pred )
functions with explicit predicate for key comparing.
|
inline |
Checks whether the set 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 set.
|
inline |
Checks if the set is empty.
Emptiness is checked by item counting: if item count is zero then the set is empty. Thus, the correct item counting feature is an important part of split-list set implementation.
|
inline |
|
inline |
Deletes the item from the set.
The function searches an item with key equal to key
in the set, unlinks it from the set, and returns true
. If the item with key equal to key
is not found the function return false
.
Difference between erase and unlink
functions: erase
finds a key and deletes the item found. unlink
finds an item by key and deletes it only if key
is an item of that set, i.e. the pointer to item found is equal to &key
.
Note the hash functor should accept a parameter of type Q
that can be not the same as value_type
.
|
inline |
Deletes the item from the set.
The function searches an item with key equal to key
in the set, call f
functor with item found, unlinks it from the set, and returns true
. The disposer specified by OrderedList
class template parameter is called by garbage collector GC
asynchronously.
The Func
interface is
If the item with key equal to key
is not found the function return false
.
Note the hash functor should accept a parameter of type Q
that can be not the same as value_type
.
|
inline |
Deletes the item pointed by iterator iter
(only for IterableList
based set)
Returns true
if the operation is successful, false
otherwise. The function can return false
if the node the iterator points to has already been deleted by other thread.
The function does not invalidate the iterator, it remains valid and can be used for further traversing.
erase_at
() is supported only for SplitListSet
based on IterableList
.
|
inline |
Deletes the item from the set with comparing functor pred
.
The function is an analog of erase(Q const&) but pred
predicate is used for key comparing. Less
has the interface like std::less
. pred
must imply the same element order as the comparator used for building the set.
|
inline |
Deletes the item from the set with comparing functor pred
.
The function is an analog of erase(Q const&, Func) but pred
predicate is used for key comparing. Less
has the interface like std::less
. pred
must imply the same element order as the comparator used for building the set.
|
inline |
Extracts the item with specified key
.
The function searches an item with key equal to key
, unlinks it from the set, and returns it as guarded_ptr
. If key
is not found the function returns an empty guarded pointer.
Note the compare functor should accept a parameter of type Q
that may be not the same as value_type
.
The disposer
specified in OrderedList
class' template parameter is called automatically by garbage collector GC
when returned guarded_ptr
object will be destroyed or released.
guarded_ptr
object uses the GC's guard that can be limited resource.Usage:
|
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 set.
|
inline |
Finds the key key
.
The function searches the item with key equal to key
and calls the functor f
for item found. The interface of Func
functor is:
where item
is the item found, key
is the find
function argument.
The functor can change non-key fields of item
. Note that the functor is only guarantee that item
cannot be disposed during functor is executing. The functor does not serialize simultaneous access to the set item
. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
Note the hash functor specified for class Traits
template parameter should accept a parameter of type Q
that can be not the same as value_type
.
The function returns true
if key
is found, false
otherwise.
|
inline |
Finds key
and returns iterator pointed to the item found (only for IterableList
)
If key
is not found the function returns end()
.
IterableList
|
inline |
Finds the key key
with pred
predicate for comparing.
The function is an analog of find(Q&, Func) but cmp
is used for key compare. Less
has the interface like std::less
. cmp
must imply the same element order as the comparator used for building the set.
|
inline |
Finds key
using pred
predicate and returns iterator pointed to the item found (only for IterableList
)
The function is an analog of find(Q&)
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 set.
If key
is not found the function returns end()
.
IterableList
|
inline |
Finds the key key
and return the item found.
The function searches the item with key equal to key
and returns the item found as guarded_ptr
. If key
is not found the function returns an empty guarded pointer.
The disposer
specified in OrderedList
class' template parameter is called by garbage collector GC
automatically when returned guarded_ptr
object will be destroyed or released.
guarded_ptr
object uses one GC's guard which can be limited resource.Usage:
Note the compare functor specified for OrderedList
template parameter should accept a parameter of type Q
that can be not the same as value_type
.
|
inline |
Finds the key 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 set.
|
inline |
Inserts new node.
The function inserts val
in the set if it does not contain an item with key equal to val
.
Returns true
if val
is placed into the set, false
otherwise.
|
inline |
Inserts new node.
This function is intended for derived non-intrusive containers.
The function allows to split creating of new item into two part:
f
functor to initialize value-field of val
.The functor signature is:
where val
is the item inserted. The user-defined functor is called only if the inserting is success.
|
inline |
Unlinks the item val
from the set.
The function searches the item val
in the set and unlinks it from the set if it is found and is equal to val
.
Difference between erase and unlink
functions: 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 that set, i.e. the pointer to item found is equal to &val
.
The function returns true
if success and false
otherwise.
|
inline |
Updates the node.
The operation performs inserting or changing data with lock-free manner.
If the item val
is not found in the set, then val
is inserted iff bAllowInsert
is true
. Otherwise, the functor func
is called with item found.
The functor signature depends of the type of OrderedList:
for MichaelList
, LazyList
with arguments:
bNew
- true
if the item has been inserted, false
otherwiseitem
- item of the setval
- 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
.
for IterableList
where
val
- argument val
passed into the update
() functionold
- old value that will be retired. If new item has been inserted then old
is nullptr
.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 val
already is in the list.
|
inline |
Inserts or updates the node (only for IterableList
)
The operation performs inserting or changing data with lock-free manner.
If the item val
is not found in the set, then val
is inserted iff bAllowInsert
is true
. Otherwise, the current element is changed to val
, the old element will be retired later by call Traits::disposer
.
Returns std::pair<bool, bool> where first
is true
if operation is successful, second
is true
if val
has been added or false
if the item with that key already in the set.