a DICTIONARY shared by threads, by linear probing on atomic words, that grows while they work and never removes; cleared in \( O( 1 ) \) by the policy EpochClear
More...
|
| | ConcurrentFlatTable (std::size_t n=0, H h=H(), Eq eq=Eq()) |
| | the empty table, with room for \( n \) pairs (and their nodes) before it grows, the hash h and the equality eq
|
| | ConcurrentFlatTable (const ConcurrentFlatTable &)=delete |
| | no copy, since the threads share it by its address
|
| ConcurrentFlatTable & | operator= (const ConcurrentFlatTable &)=delete |
| | no copy, since the threads share it by its address
|
| | ~ConcurrentFlatTable () |
| | the frees of the pairs and of the vectors
|
| std::uint64_t | hash (const K &k) const |
| | the hash of 64 bits of the key \( k \) that the operations take
|
| std::pair< Ref, bool > | findOrInsert (const K &k, std::uint64_t h, const V &v) |
| | the pair of the key \( k \), of hash \( h \), inserted with the value \( v \) if absent; true if it was inserted
|
| std::pair< Ref, bool > | findOrInsert (const K &k, const V &v) |
| | findOrInsert() above, with the hash computed
|
| std::pair< K, bool > | findOrInsertKey (const K &k, std::uint64_t h) |
| | the key held equal to \( k \), of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a packed set (an empty value)
|
| bool | insert (const K &k, const V &v) |
| | inserts the pair \( ( k , v ) \) if \( k \) is absent; true if it was, as findOrInsert()
|
| std::optional< V > | find (const K &k, std::uint64_t h) const |
| | a copy of the value of the key \( k \) of hash \( h \), or nothing
|
| std::optional< V > | find (const K &k) const |
| | find() above, with the hash computed
|
| std::optional< K > | findKey (const K &k, std::uint64_t h) const |
| | a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing; never inserts
|
| std::optional< K > | findKey (const K &k) const |
| | findKey() above, with the hash computed
|
| bool | contains (const K &k, std::uint64_t h) const |
| | whether the key \( k \) of hash \( h \) is there, as find()
|
| bool | contains (const K &k) const |
| | contains() above, with the hash computed
|
| void | prefetch (std::uint64_t h) const |
| | the lines that a lookup of the hash \( h \) reads first (its tags and its word) fetched into the cache, with no wait: a thread about to look up or insert several keys prefetches them all first, so that their misses overlap
|
| Entry * | entry (const K &k, std::uint64_t h) const |
| | the pair of the key \( k \) of hash \( h \), or nil, as find(); for a table neither packed nor inlined
|
template<class F>
requires ( packed || inlined ) |
| bool | update (const K &k, F &&f) |
| | sets the value \( x \) of the key \( k \) to f( x ), by a compare-and-swap of its word (of its place), for a packed (an inlined) table; true if \( k \) is there
|
| std::size_t | size (void) const |
| | the number of the pairs, the sum of the counters, exact when no operation runs
|
| bool | empty (void) const |
| | whether size() is 0
|
| std::size_t | capacity (void) const |
| | the number of the places of the vector in use
|
| std::size_t | rehashes (void) const |
| | the number of the growths of the table so far
|
| void | reserve (std::size_t n) |
| | room for \( n \) pairs before the table grows: the vector grown to \( 2 n \) places at least, as by an insertion, and the nodes of \( n \) pairs allocated
|
| void | clear (void) |
| | removes every pair, keeping the capacity; with no other operation running.
|
| std::uint64_t | epoch (void) const |
| | the epoch of the table, raised by each clear() and back to 1 after the largest; always 1 without EpochClear
|
| template<class F> |
| void | forEach (F &&f) const |
| | applies f( const K & , const V & ) to every pair; with no insertion running
|
| bool | consistent (void) const |
| | each key once, reached from its place without a free place in between, and the counters summing to the pairs; with no other operation running
|
template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
class ADS::ConcurrentFlatTable< K, V, H, Eq, C >
a DICTIONARY shared by threads, by linear probing on atomic words, that grows while they work and never removes; cleared in \( O( 1 ) \) by the policy EpochClear
template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
a copy of the value of the key \( k \) of hash \( h \), or nothing
In pseudocode (beyond the book), with the notation of findOrInsert() above:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{find}( k , h ) \cr
2 & \quad \textbf{loop} \cr
3 & \qquad T \gets \text{the vector in use } W\quad \triangleright
\ \text{acquire} \cr
4 & \qquad i \gets \lfloor h / 2^{64 - l} \rfloor \cr
5 & \qquad \textbf{while}\ T[ i ] \ne 0\ \textbf{do}\quad
\triangleright\ \text{acquire} \cr
6 & \qquad\quad \textbf{if}\ T[ i ] \text{ holds } k\ \textbf{then}
\ \textbf{return}\ \text{its value} \cr
7 & \qquad\quad i \gets ( i + 1 ) \bmod 2^l \cr
8 & \qquad \textbf{if}\ T \text{ is still the vector in use}
\ \textbf{then return nil}
\end{array}}
\]
- Precondition
- hash( k ) is \( h \) (audit)
template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing; never inserts
The lookup of find(), which returns the key of the word found instead of its value: in a set of pointers to objects compared by Eq as the objects themselves, a thread reads the pointer that the table holds for a state equal to that of \( k \), so that the set keeps one word per pointer and no copy of it in the value. As find(), it may run with insertions and growths of any thread, takes no lock and writes nothing, is wait-free while the table does not grow, and linearizable at the read of the word that holds the key or of the free place; the key returned was inserted, whole, before that read.
- Precondition
- hash( k ) is \( h \) (audit)
template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
the pair of the key \( k \), of hash \( h \), inserted with the value \( v \) if absent; true if it was inserted
In pseudocode (the folklore table of Maier et al. that grows, beyond the book), \( W[ 0 .. 2^l - 1 ] \) the words of the vector in use, \( c_t \) the counter of the thread and \( g \) the flag of the growth:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{findOrInsert}( k , h , v ) \cr
2 & \quad \textbf{loop} \cr
3 & \qquad c_t \gets c_t + 1\quad \triangleright\ \text{sequentially}
\text{ consistent} \cr
4 & \qquad \textbf{if}\ g\ \textbf{then}\ c_t \gets c_t - 1;
\ \mathrm{help}();\ \textbf{continue} \cr
5 & \qquad w \gets \text{the word of } ( k , v ) \text{, or of a new}
\text{ node of } ( h , k , v ) \cr
6 & \qquad i \gets \lfloor h / 2^{64 - l} \rfloor;\ r \gets 0 \cr
7 & \qquad \textbf{loop} \cr
8 & \qquad\quad x \gets W[ i ] \cr
9 & \qquad\quad \textbf{if}\ x = 0\ \textbf{and}\ \mathrm{CAS}( W[ i ] ,
0 , w )\ \textbf{then} \cr
10 & \qquad\qquad c_t \gets c_t - 1 + 2^{16}\quad \triangleright
\ \text{the pairs of the thread from the bit 16} \cr
11 & \qquad\qquad \textbf{if}\ \text{they are a multiple of } 2^{\min(
\max( l - 10 , 0 ) , 10 )} \text{ and the pairs of all the threads}
\text{ pass } 2^{l-1} \ \textbf{then}\ \mathrm{grow}( l + 1 ) \cr
12 & \qquad\qquad \textbf{return}\ ( w , \textbf{true} ) \cr
13 & \qquad\quad \textbf{if}\ x = 0\ \textbf{then}\ x \gets W[ i ]
\quad \triangleright\ \text{written by another thread} \cr
14 & \qquad\quad \textbf{if}\ x \text{ holds } k\ \textbf{then}
\ c_t \gets c_t - 1;\ \textbf{return}\ ( x , \textbf{false} ) \cr
15 & \qquad\quad i \gets ( i + 1 ) \bmod 2^l;\ r \gets r + 1 \cr
16 & \qquad\quad \textbf{if}\ r > 128\ \textbf{and}\ \text{the table}
\text{ is } 1/8 \text{ full}\ \textbf{then break} \cr
17 & \qquad c_t \gets c_t - 1;\ \mathrm{grow}( l + 1 ) \cr
18 & \textbf{procedure}\ \mathrm{grow}( l' ) \cr
19 & \quad \textbf{if}\ \mathrm{CAS}( g , \textbf{false} , \textbf{true}
)\ \textbf{then} \cr
20 & \qquad \textbf{while}\ \text{some } c_t > 0\ \textbf{do}
\ \text{a pause} \cr
21 & \qquad W' \gets 2^{l'} \text{ words } 0;\ \text{publish } W'
\text{ for the helpers} \cr
22 & \qquad \mathrm{help}();\ \textbf{wait until}\ \text{all the}
\text{ blocks are copied} \cr
23 & \qquad W \gets W';\ g \gets \textbf{false}\quad \triangleright
\ \text{release} \cr
24 & \quad \textbf{else}\ \mathrm{help}() \cr
25 & \textbf{procedure}\ \mathrm{help}() \cr
26 & \quad \textbf{while}\ g\ \textbf{do} \cr
27 & \qquad \textbf{foreach}\ \text{block of } 4096 \text{ words of } W
\text{ claimed by a counter}\ \textbf{do} \cr
28 & \qquad\quad \textbf{foreach}\ x \ne 0 \text{ in the block}
\ \textbf{do}\ \text{put } x \text{ at the first free place of its}
\text{ key in } W' \text{, by a CAS}
\end{array}}
\]
- Precondition
- hash( k ) is \( h \) (audit)
template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
the key held equal to \( k \), of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a packed set (an empty value)
The key held is equal to \( k \) by Eq, not by its bytes: in a set of pointers to objects compared by Eq as the objects themselves, the threads that insert at once pointers to equal objects all get the pointer of the one that inserted it, the intrusive set that a parallel search keeps of its nodes, a word of 8 bytes for each.
- Precondition
- hash( k ) is \( h \) (audit)