A Chase-Lev work-stealing deque on a fixed ring of slots. More...
#include "donner/base/tasks/WorkStealingDeque.h"
Public Types | |
| using | Index = std::int64_t |
| Logical position of an entry. It never wraps: a signed 64-bit index overflows only after 2^63 pushes. | |
Public Member Functions | |
| WorkStealingDeque (std::size_t capacity) | |
Allocates a ring of capacity slots. | |
| WorkStealingDeque (const WorkStealingDeque &)=delete | |
| WorkStealingDeque (WorkStealingDeque &&)=delete | |
| WorkStealingDeque & | operator= (const WorkStealingDeque &)=delete |
| WorkStealingDeque & | operator= (WorkStealingDeque &&)=delete |
| std::size_t | capacity () const |
| The number of entries the deque holds at most. | |
| bool | push (T item) |
Pushes item at the bottom. | |
| bool | pushPublished (T item) |
| Pushes from a blocking owner, publishing the new bottom in the sleepers' SC order. | |
| bool | publishedWorkVisible () const |
| Tests the adopted-deque half of the publish/sleep handshake without touching a slot. | |
| bool | full () const |
| Whether a push would fail now. | |
| Index | bottom () const |
| The bottom index: the logical position the next push uses. | |
| std::optional< T > | take () |
| Takes the newest entry from the bottom. | |
| StealResult< T > | steal () |
| Steals the oldest entry from the top. | |
| template<typename BeforeClaim> | |
| StealResult< T > | stealWithHook (BeforeClaim beforeClaim) |
Steals as in steal, holding at beforeClaim after reading a nonempty slot and before its CAS. The ordinary path instantiates no runtime callback. | |
Static Public Attributes | |
| static constexpr std::size_t | kMaxCapacity = std::size_t{1} << 30 |
| The largest supported capacity. | |
A Chase-Lev work-stealing deque on a fixed ring of slots.
One thread, the owner, pushes and takes at the bottom; any number of other threads, thieves, steal from the top. The owner takes the newest entry and thieves take the oldest. The owner may change between threads only across a happens-before edge, such as a thread start or join.
The top and bottom indices are signed 64-bit logical positions that are never wrapped or reset; they would overflow only after 2^63 pushes. The top only grows, and the bottom grows on a push and shrinks on a take. An entry's slot is its logical index modulo the capacity. The bottom is below the top only for a moment inside take, which signed indices allow without underflow even on a new empty deque. Only the last entry is contended between the owner and a thief, and it is claimed by a compare-and-swap on the top index.
The ring is allocated once and never resized, so no thief can read a retired array. A push that would exceed the capacity fails and leaves the deque unchanged; the caller keeps the entry.
| T | Entry type, trivially copyable with lock-free atomics, normally a pointer. Slots are relaxed atomics, so a thief reading a slot the owner is rewriting is not a data race. |
| kOrdering | The index ordering form, see DequeOrdering. |
| Hooks | Test seam, see NoDequeHooks. |
|
inlineexplicit |
Allocates a ring of capacity slots.
| capacity | Number of entries the deque holds at most; a power of two, at most kMaxCapacity. Any other value fails a release assertion. |
|
inlinenodiscard |
The bottom index: the logical position the next push uses.
Owner only, and only outside take, whose transient lowering it never sees. Every entry pushed after this read has a position at or above it, until a take lowers the bottom past it.
|
inlinenodiscard |
Whether a push would fail now.
Owner only. Only the owner pushes and a thief only frees slots, so a push the owner makes after this returned false succeeds.
|
inlinenodiscard |
Pushes item at the bottom.
Owner only.
The fullness check reads the top index, which a concurrent steal may be advancing, so a push racing a steal can fail even though the steal is freeing a slot. It never succeeds past the capacity.
| item | Entry to push. |
|
inlinenodiscard |
Steals the oldest entry from the top.
Any thread other than the owner.
|
inlinenodiscard |
Takes the newest entry from the bottom.
Owner only.