|
ADS
Algorithms and Data Structures
|
the sleep of threads until a condition holds, woken by notify() More...
#include <ADS/concurrent/EventCount.h>
Public types | |
| using | Key = std::uint32_t |
| the epoch a waiter saw | |
Public member functions | |
| EventCount (void)=default | |
| the event count of epoch 0 and no waiter | |
| EventCount (const EventCount &)=delete | |
| no copy, since the waiters sleep on its address | |
| EventCount & | operator= (const EventCount &)=delete |
| no copy, since the waiters sleep on its address | |
| Key | prepareWait (void) noexcept |
| announces a waiter and returns the epoch, before the last look at the condition | |
| void | cancelWait (Key k) noexcept |
| withdraws the waiter, the condition being true, unless a notifier already withdrew it (the epoch is no longer \( k \)) | |
| void | commitWait (Key k) noexcept |
| sleeps while the epoch is \( k \); the notifier that advanced it withdrew the waiter | |
| void | notify (void) noexcept |
| wakes the waiters, after the condition was made true: it advances the epoch and withdraws them all at once, so that the notifications that follow, before they run, find nobody to wake; when nobody waits, a fence and a read | |
| template<class P> | |
| void | await (P &&p, std::int64_t spin=20000) |
| returns when p() is true: it looks again and again for up to spin nanoseconds, with a pause of the processor between the looks, and then sleeps between them | |
| std::uint32_t | waiters (void) const noexcept |
| the number of the threads announced as waiters at some moment | |
the sleep of threads until a condition holds, woken by notify()
| using ADS::EventCount::Key = std::uint32_t |
the epoch a waiter saw
|
default |
the event count of epoch 0 and no waiter
|
delete |
no copy, since the waiters sleep on its address
|
inline |
returns when p() is true: it looks again and again for up to spin nanoseconds, with a pause of the processor between the looks, and then sleeps between them
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{await}( p , \mathit{spin} ) \cr 2 & \quad \textbf{repeat}\ \textbf{if}\ p()\ \textbf{then return}; \ \text{a pause}\ \textbf{until}\ \mathit{spin} \text{ ns have} \text{ passed} \cr 3 & \quad \textbf{while not}\ p()\ \textbf{do} \cr 4 & \qquad k \gets \mathrm{prepareWait}() \cr 5 & \qquad \textbf{if}\ p()\ \textbf{then}\ \mathrm{cancelWait}( k ); \ \textbf{return} \cr 6 & \qquad \mathrm{commitWait}( k ) \end{array}} \]
|
inlinenoexcept |
withdraws the waiter, the condition being true, unless a notifier already withdrew it (the epoch is no longer \( k \))
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{cancelWait}( k ) \cr 2 & \quad \textbf{repeat}\ ( e' , w' ) \gets ( e , w ) \ \textbf{until}\ e' \ne k\ \textbf{or}\ \mathrm{CAS}( ( e , w ) , ( e' , w' ) , ( e' , w' - 1 ) ) \end{array}} \]
|
inlinenoexcept |
sleeps while the epoch is \( k \); the notifier that advanced it withdrew the waiter
In pseudocode (beyond the book), \( E \) the copy of the epoch on which the waiters sleep:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{commitWait}( k ) \cr 2 & \quad \textbf{while}\ E = k\ \textbf{do}\ \text{sleep until } E \text{ changes}\quad \triangleright\ \text{acquire} \end{array}} \]
|
inlinenoexcept |
wakes the waiters, after the condition was made true: it advances the epoch and withdraws them all at once, so that the notifications that follow, before they run, find nobody to wake; when nobody waits, a fence and a read
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{notify}() \cr 2 & \quad \text{a full fence};\ ( e' , w' ) \gets ( e , w )\quad \triangleright\ \text{or a read-modify-write that adds 0} \cr 3 & \quad \textbf{while}\ w' > 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathrm{CAS}( ( e , w ) , ( e' , w' ) , ( e' + 1 , 0 ) )\ \textbf{then}\quad \triangleright\ \text{acquire and} \text{ release} \cr 5 & \qquad\quad \text{raise } E \text{ to } e' + 1 \text{ if it is} \text{ behind}\quad \triangleright\ \text{release}; \ \text{wake all the threads asleep on } E \cr 6 & \qquad\quad \textbf{return} \cr 7 & \qquad ( e' , w' ) \gets ( e , w )\quad \triangleright\ \text{read} \text{ by the failed CAS} \end{array}} \]
|
delete |
no copy, since the waiters sleep on its address
|
inlinenodiscardnoexcept |
announces a waiter and returns the epoch, before the last look at the condition
In pseudocode (beyond the book), \( ( e , w ) \) the word of the epoch and of the waiters:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prepareWait}() \cr 2 & \quad ( e' , w' ) \gets \mathrm{fetchAdd}( ( e , w ) , 1 )\quad \triangleright\ \text{sequentially consistent} \cr 3 & \quad \text{a full fence (none with a read-modify-write in} \text{ the notifier)} \cr 4 & \quad \textbf{return}\ e' \end{array}} \]
|
inlinenodiscardnoexcept |
the number of the threads announced as waiters at some moment