A task which may complete one or more asynchronous operations.
The Task interface is commonly implemented by users wishing to schedule work on an asynchronous Dispatcher. To do this, users may subclass Task, providing an implementation of the DoPend method which advances the state of the Task as far as possible before yielding back to the Dispatcher.
This process works similarly to cooperatively-scheduled green threads or coroutines, with a Task representing a single logical "thread" of execution. Unlike some green thread or coroutine implementations, Task does not imply a separately-allocated stack: Task state is most commonly stored in fields of the Task subclass.
Once defined by a user, Tasks may be run by passing them to a Dispatcher via Dispatcher::Post. The Dispatcher will then Pend the Task every time that the Task indicates it is able to make progress.
Note that Task objects must not be destroyed while they are actively being Pend'd by a Dispatcher. To protect against this, be sure to do one of the following:
Task objects whose stack-based lifetimes outlive their associated Dispatcher.Deregister on the Task prior to its destruction. NOTE that Deregister may not be called from inside the Task's own Pend method.SharedPtr<Task> can be posted with Dispatcher::PostShared.
Public Member Functions | |
| constexpr | Task (log::Token name=kDefaultName) |
| Task (const Task &)=delete | |
| Task (Task &&)=delete | |
| Task & | operator= (const Task &)=delete |
| Task & | operator= (Task &&)=delete |
| virtual | ~Task () |
| Poll | Pend (Context &cx) |
| bool | IsRegistered () const |
| void | Deregister () |
| void | Join () |
Protected Types | |
| using | Context = ::pw::async2::Context |
Private Member Functions | |
| virtual Poll | DoPend (Context &)=0 |
Friends | |
| class | Dispatcher |
| class | Waker |