Counts the tasks spawned into it, and is joined, by wait or the destructor, on the thread that created it. More...
#include "donner/base/tasks/TaskScope.h"
Public Member Functions | |
| TaskScope (TaskScheduler &scheduler) | |
| Records the calling thread as the scope's creator, and its parker. | |
| TaskScope (const TaskScope &)=delete | |
| TaskScope (TaskScope &&)=delete | |
| TaskScope & | operator= (const TaskScope &)=delete |
| TaskScope & | operator= (TaskScope &&)=delete |
| ~TaskScope () | |
| Joins: returns once every task spawned into the scope has finished. On the creating thread. | |
| template<typename F> | |
| void | spawn (F &&work) |
Spawns work as a task of this scope, or runs it inline on the calling thread when the scheduler cannot queue it (zero workers, a record bound, a full deque). | |
| template<typename F> | |
| bool | trySpawn (F &&work) |
Spawns work as a task of this scope, or returns false and leaves work with the caller when the scheduler cannot queue it. | |
| void | wait () |
| Returns once every task spawned into the scope has finished; on return, everything those tasks wrote is visible. | |
| void | requestStop () |
| Requests cooperative cancellation from any thread. | |
| bool | stopRequested () const |
| Whether requestStop was called; tasks that run long poll it. | |
Friends | |
| class | TaskScopeTestPeer |
Counts the tasks spawned into it, and is joined, by wait or the destructor, on the thread that created it.
The scope records its creating thread and that thread's parker when it is constructed. A task spawned into it, by the creator or by any task of the scope, carries the scope's counter and the recorded parker in its task record; the thread that finishes the task destroys its closure, returns its record, then decrements the counter, and the finisher whose decrement reaches zero changes the parker. That decrement is the finisher's last access to the scope, and the parker belongs to the scheduler, so the creator may destroy the scope as soon as it sees the count at zero.
On a worker, the scope also records the worker's deque bottom index, its watermark: every entry the creator, or a task run on top of its wait, pushes from then on lies at or above it. The creator's spawns go to its own deque, and its wait runs entries above the watermark, returns once the count is zero, and otherwise helps with other work while it has more than TaskSchedulerOptions::helpMarginBytes of stack left, then sleeps on the worker's parker (see TaskScheduler). Nested scopes must be joined in last-in, first-out order. A coroutine must not suspend while a synchronous scope is live.
On an adopted thread, the creator runs only its own scope's entries above the watermark and otherwise waits on the slot's parker. While a nested synchronous scope is live, submitting to an outer scope fails a release assertion, including for a spawn that would run inline.
On an unadopted thread that is not a worker, the creator never runs tasks. Its spawns go to the scheduler's global queue, and its wait re-checks the count and sleeps on its parker, from the scheduler's ParkerPool, until a finisher changes it. Without a seed and with zero workers, every spawn runs inline and a wait returns at once.
On a seeded scheduler the creator queues tasks in the replay pool and runs them during its wait.
requestStop asks the scope's queued tasks not to run: each is destroyed unrun when a thread takes it, and still completes its scope's accounting, so the wait returns. Running tasks may poll stopRequested.
Creating a scope while the calling thread's own thread-local objects are being destroyed (from another thread-local's destructor, at thread exit) is undefined: the thread's parker entry is a thread-local object and may already be gone.
|
explicit |
Records the calling thread as the scope's creator, and its parker.
| scheduler | The scheduler that runs the scope's tasks; must outlive the scope. |
|
inline |
Requests cooperative cancellation from any thread.
A queued task that observes the request discards its closure and counts as finished. Inline fallback work still runs.
|
inline |
Spawns work as a task of this scope, or runs it inline on the calling thread when the scheduler cannot queue it (zero workers, a record bound, a full deque).
From the creator or from any task of this scope.
| work | The task's closure, copied or moved into the task as it is passed. |
|
inlinenodiscard |
Spawns work as a task of this scope, or returns false and leaves work with the caller when the scheduler cannot queue it.
From the creator or from any task of this scope.
| work | The task's closure, copied or moved into the task as it is passed, and only when the spawn succeeds. |
| void donner::tasks::TaskScope::wait | ( | ) |
Returns once every task spawned into the scope has finished; on return, everything those tasks wrote is visible.
On the creating thread only. A worker can help elsewhere. An adopted creator runs only its local entries above the watermark and otherwise waits on its slot.