Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::tasks::WorkStealingDeque< T, kOrdering, Hooks > Class Template Reference

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.

Detailed Description

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
class donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >

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.

Template Parameters
TEntry 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.
kOrderingThe index ordering form, see DequeOrdering.
HooksTest seam, see NoDequeHooks.

Constructor & Destructor Documentation

◆ WorkStealingDeque()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::WorkStealingDeque ( std::size_t capacity)
inlineexplicit

Allocates a ring of capacity slots.

Parameters
capacityNumber of entries the deque holds at most; a power of two, at most kMaxCapacity. Any other value fails a release assertion.

Member Function Documentation

◆ bottom()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
Index donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::bottom ( ) const
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.

◆ full()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
bool donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::full ( ) const
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.

◆ push()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
bool donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::push ( T item)
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.

Parameters
itemEntry to push.
Returns
True if the entry was pushed, false if the deque was full.

◆ steal()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
StealResult< T > donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::steal ( )
inlinenodiscard

Steals the oldest entry from the top.

Any thread other than the owner.

Returns
The entry with StealStatus::Taken; StealStatus::Empty if the deque held none; or StealStatus::Lost if another thread claimed it first, in which case the deque may still hold entries and the caller may retry.

◆ take()

template<typename T, DequeOrdering kOrdering = kDefaultDequeOrdering, typename Hooks = NoDequeHooks>
std::optional< T > donner::tasks::WorkStealingDeque< T, kOrdering, Hooks >::take ( )
inlinenodiscard

Takes the newest entry from the bottom.

Owner only.

Returns
The entry, or std::nullopt if the deque was empty or a thief claimed its last entry.

The documentation for this class was generated from the following file: