A parker: one atomic word holding a waiting bit and an epoch, that one thread, its sleeper, waits on and any thread changes to wake it. More...
#include "donner/base/tasks/Parker.h"
Public Types | |
| using | Word = typename Hooks::Word |
| The parker word. | |
| using | UnsignedWord = std::make_unsigned_t<Word> |
| The word's bits read as unsigned, as epochs are. | |
Public Member Functions | |
| Parker (Word initialWord) | |
Starts from initialWord instead of zero, so tests can reach the epoch's wrap. | |
| Parker (const Parker &)=delete | |
| Parker (Parker &&)=delete | |
| Parker & | operator= (const Parker &)=delete |
| Parker & | operator= (Parker &&)=delete |
| Word | prepareWait () |
| Sets the waiting bit. | |
| void | wait (Word token) |
Blocks until the word differs from token. | |
| void | finishWait () |
| Clears the waiting bit. Sleeper only, after waking or after a re-check that made waiting unnecessary. A read-modify-write keeps any epoch change that lands meanwhile; a stale bit only causes a notify that nothing waits for. | |
| bool | unpark () |
| Advances the epoch and wakes the sleeper if it set the waiting bit. | |
| template<typename Done> | |
| void | parkUntil (Done &&done) |
Sleeps until done returns true. | |
| Word | word () const |
| A snapshot of the word, for tests and diagnostics. Relaxed: it orders nothing. | |
Static Public Member Functions | |
| static constexpr UnsignedWord | EpochOf (Word word) |
| The epoch of a parker word. | |
Static Public Attributes | |
| static constexpr Word | kWaitingBit = 1 |
| The bit the sleeper sets before it waits. | |
| static constexpr Word | kEpochStep = 2 |
| What one change adds to the word: one epoch step, leaving the waiting bit alone. | |
A parker: one atomic word holding a waiting bit and an epoch, that one thread, its sleeper, waits on and any thread changes to wake it.
The word is NativeParkerWord, 64 bits on Apple platforms and 32 bits elsewhere.
The waiting bit is bit 0 and the epoch is the remaining bits, so a change adds 2 and wraps (atomic arithmetic on a signed integer wraps in two's complement) without touching the bit. Only the sleeper sets and clears the bit. A sleeper:
A waker first makes the condition true, then calls unpark, a release read-modify-write that advances the epoch and notifies only if the word it replaced had the waiting bit set, so a waker whose sleeper is not waiting pays no system call. Both sides are read-modify-writes on the one word, so they fall in its single modification order: a change ordered before the sleeper's prepareWait is read by it, with everything the waker wrote before the change, so the re-check in step 2 sees the condition; a change ordered after it returns a word with the bit set, so the waker notifies. A notify is therefore never skipped for a thread that will wait. The sleeper misses a change only if a multiple of 2^(bits - 1) changes land between its prepareWait and its wait: 2^31 for a 32-bit word, 2^63 for a 64-bit one.
parkUntil runs those steps in a loop. A wake that is not for the condition being waited for is a spurious wake: the sleeper re-checks and waits again.
The parker must outlive every unpark call in progress, because unpark notifies after its change, when the sleeper may already have returned.
| Hooks | Platform policy and test seam, see PlatformParkerHooks; it names the word type. |
|
inline |
Sleeps until done returns true.
Sleeper only.
| done | Called before each wait, and again after setting the waiting bit, so a change that made it true before the sleeper began waiting is never missed. A re-check that follows a prepareWait or a wait that read the change sees the condition whatever ordering done uses; the first call, before any of them, orders nothing, so a caller that reads data the waker published reads the condition with acquire ordering. |
|
inlinenodiscard |
Sets the waiting bit.
Sleeper only.
Sequentially consistent: it acquires a change ordered before it, and with the changes it is in the one modification order that decides whether a waker notifies.
Fails a release assertion if the waiting bit was already set: a parker has one sleeper, and each prepareWait is followed by a finishWait before the next.
|
inline |
Advances the epoch and wakes the sleeper if it set the waiting bit.
Any thread; call it after making the sleeper's condition true.
|
inline |
Blocks until the word differs from token.
Sleeper only, after prepareWait and a re-check. Returns at once if a change landed after the prepareWait that produced token.
| token | The value prepareWait returned. |