A work-stealing task scheduler. More...
#include "donner/base/tasks/TaskScheduler.h"
Public Member Functions | |
| TaskScheduler (TaskSchedulerOptions options) | |
| Starts the workers. | |
| TaskScheduler (const TaskScheduler &)=delete | |
| TaskScheduler (TaskScheduler &&)=delete | |
| TaskScheduler & | operator= (const TaskScheduler &)=delete |
| TaskScheduler & | operator= (TaskScheduler &&)=delete |
| ~TaskScheduler () | |
| Sets the stop flag, wakes and joins every worker, then closes the parker pool. Fails a release assertion if a TaskScope or AdoptedThread is live, if work remains, or if the awake count disagrees with the workers' state words. | |
| std::size_t | workerCount () const |
| Number of worker threads that successfully started. A seeded scheduler starts none. | |
| std::size_t | schedulingWidth () const |
| Logical width for clients deciding whether to use scoped scheduling. | |
| bool | canJoinWithoutRunningUnrelatedTasks () const |
| Whether the caller can join a synchronous scope without running tasks from other scopes. | |
| std::size_t | workerQosRefusals () const |
| Number of workers whose startup QoS hint the platform refused. These workers still run. | |
| std::size_t | workerStackBytes () const |
| Requested stack bytes per worker, before platform page rounding. | |
| bool | threadingDisabledByConfiguration () const |
| True when the options asked for zero workers. | |
| ScheduleAwaitable | schedule () noexcept |
| Suspends and queues the caller on this scheduler unless it has zero workers. | |
| AdoptedThread | adopt () |
| Claims a slot for the calling non-worker, which must have no live synchronous scope. Returns an empty guard in inline or seeded mode, or if all slots are already claimed. | |
Static Public Member Functions | |
| static std::optional< StackBounds > | TryCurrentThreadUsableStack () |
| Calling thread's usable stack, if measurable. Workers use their configured stack bounds. Compare against the current frame on this thread only. | |
Friends | |
| class | TaskSchedulerTestPeer |
| class | AdoptedThread |
| struct | detail::ParallelForAccess |
| class | AsyncTaskScope |
| class | ScheduleAwaitable |
| class | detail::TaskPromiseBase |
| class | TaskScope |
A work-stealing task scheduler.
A worker that waits on a TaskScope it created applies three rules in order: it runs an entry of its own deque above the scope's watermark (the deque's bottom index when the scope was created), returns once the scope's count is zero, and otherwise, while it has more than TaskSchedulerOptions::helpMarginBytes of stack left, helps by taking from the global queue or stealing. Past its margin, or when it finds nothing, it sleeps with the same steps as an idle worker, asleep deep when past its margin. The last awake worker does not sleep while work is queued: it hands the work to a worker asleep shallow if there is one, and otherwise runs it itself even past its margin.
Workers and adopted slots each own a bounded deque and record pool. Unadopted submitters use a global FIFO. A worker scans the FIFO and the adopted deques as separate outside sources in rotating order before stealing from its peers, and visits outside sources at least once per 61 consecutive local takes.
A worker that finds nothing for TaskSchedulerOptions::idleSpinRounds rounds sleeps on its parker: it drains its deque into the global queue, sets the parker's waiting bit, stores asleep in its state word and decrements the awake count, then re-checks the stop flag, global queue and adopted deques before it waits. A thread that pushes onto the global queue increments the count, then reads the awake count and, if no worker is awake, wakes an asleep worker. Both sides use sequentially consistent operations, so either the sleeper sees the work or the publisher sees no worker awake: whenever a task is queued, at least one worker is awake.
Work reaches the scheduler through a TaskScope, which counts its outstanding tasks: the thread that finishes a task decrements its scope's count and, on reaching zero, changes the parker the scope recorded, waking the scope's creator.
With a seed, submissions and joins take place on the constructing thread. A join runs queued tasks of the joined scope in seeded order; a nested wait therefore cannot recursively run its parent's siblings. Even seed zero selects this mode. Without a seed and with zero workers every spawn runs inline on the spawning thread.
|
explicit |
Starts the workers.
| options | The scheduler's options. |
|
nodiscard |
Whether the caller can join a synchronous scope without running tasks from other scopes.
Ordinary external and adopted callers qualify. A worker of this scheduler can help unrelated work during a join. A seeded scope is usable only on its constructing thread.
|
inlinenodiscard |
Logical width for clients deciding whether to use scoped scheduling.
Zero means work runs inline, including when no requested worker started. In ordinary mode this is the number of live workers. Seeded replay queues work on its constructing thread with a fixed width of four. A logical piece does not identify a worker or index per-thread storage.