A single-threaded cooperatively scheduled runtime for async tasks.
Dispatcher implementations must pop and run tasks with one of the following:
|
|
class | Task |
| |
|
class | Waker |
| |
|
template<typename > |
| class | DispatcherForTestFacade |
| |
◆ RunTaskResult
Result from Dispatcher::RunTask. Reports the state of the task when it finished running.
| Enumerator |
|---|
| kActive | The task is still posted to the dispatcher.
|
| kDeregistered | The task was removed from the dispatcher by another thread.
|
| kCompleted | The task finished running.
|
◆ DoWake()
| virtual void pw::async2::Dispatcher::DoWake |
( |
| ) |
|
|
privatepure virtual |
◆ LogRegisteredTasks()
| void pw::async2::Dispatcher::LogRegisteredTasks |
( |
| ) |
|
Outputs log statements about the tasks currently registered with this dispatcher.
◆ PopAndRunAllReadyTasks()
| bool pw::async2::Dispatcher::PopAndRunAllReadyTasks |
( |
| ) |
|
|
protected |
Pops and runs tasks until there are no tasks ready to run.
This function may be called by dispatcher implementations to run tasks. This is a high-level function that runs all ready tasks without logging or metrics. For more control, use PopTaskToRun and RunTask.
- Return values
-
| true | The dispatcher has posted tasks, but they are sleeping. |
| false | The dispatcher has no posted tasks. |
◆ PopSingleTaskForThisWake()
| Task * pw::async2::Dispatcher::PopSingleTaskForThisWake |
( |
| ) |
|
|
inlineprotected |
Pop a single task to run. Each call to PopSingleTaskForThisWake can result in up to one DoWake() call, so use PopTaskToRun or PopAndRunAllReadyTasks to run multiple tasks.
◆ PopTaskToRun() [1/2]
| Task * pw::async2::Dispatcher::PopTaskToRun |
( |
| ) |
|
|
inlineprotected |
Pops a task and marks it as running. The task must be passed to RunTask.
PopTaskToRun MUST be called repeatedly until it returns nullptr, at which point the dispatcher will request a wake.
◆ PopTaskToRun() [2/2]
| Task * pw::async2::Dispatcher::PopTaskToRun |
( |
bool & |
has_posted_tasks | ) |
|
|
inlineprotected |
PopTaskToRun overload that optionally reports the whether the Dispatcher has registered tasks. This allows callers to distinguish between there being no woken tasks and no posted tasks at all.
Like the no-argument overload, PopTaskToRun MUST be called repeatedly until it returns nullptr.
- Parameters
-
| [out] | has_posted_tasks | Set to true if the dispatcher has at least one task posted, potentially including the task that was popped. Set to false if the dispatcher has no posted tasks. |
- Returns
- A pointer to a task that is ready to run, or
nullptr if there are no ready tasks.
◆ Post()
| void pw::async2::Dispatcher::Post |
( |
Task & |
task | ) |
|
Tells the Dispatcher to run Task to completion. This method does not block.
After Post is called, Task::Pend will be invoked once. If Task::Pend does not complete, the Dispatcher will wait until the Task is "awoken", at which point it will call Pend again until the Task completes.
This method is thread-safe and interrupt-safe.
◆ RunTask()
Runs the task that was returned from PopTaskToRun.
- Warning
- Do NOT access the
Task object after RunTask returns! The task could have destroyed, either by the dispatcher or another thread, even if RunTask returns kActive. It is only safe to access a popped task before calling RunTask, since it is marked as running and will not be destroyed until after it runs.
The documentation for this class was generated from the following file: