Portable APIs for asynchronous code. Main docs: https://pigweed.dev/pw_async.
Classes | |
struct | pw::async::Context |
Contextual information provided by a Dispatcher to a running task. More... | |
class | pw::async::Dispatcher |
class | pw::async::test::FakeDispatcherFixture |
class | pw::async::FunctionDispatcher |
class | pw::async::HeapDispatcher |
class | pw::async::Task |
Typedefs | |
using | pw::async::TaskFunction = Function< void(Context &, Status)> |
Functions | |
virtual void | pw::async::Dispatcher::Post (Task &task) |
virtual void | pw::async::Dispatcher::PostAfter (Task &task, chrono::SystemClock::duration delay) |
virtual void | pw::async::Dispatcher::PostAt (Task &task, chrono::SystemClock::time_point time)=0 |
virtual bool | pw::async::Dispatcher::Cancel (Task &task)=0 |
FakeDispatcher & | pw::async::test::FakeDispatcherFixture::dispatcher () |
Returns the FakeDispatcher that should be used for dependency injection. | |
chrono::SystemClock::time_point | pw::async::test::FakeDispatcherFixture::now () |
Returns the current fake time. | |
bool | pw::async::test::FakeDispatcherFixture::RunUntilIdle () |
bool | pw::async::test::FakeDispatcherFixture::RunUntil (chrono::SystemClock::time_point end_time) |
bool | pw::async::test::FakeDispatcherFixture::RunFor (chrono::SystemClock::duration duration) |
virtual Status | pw::async::FunctionDispatcher::Post (TaskFunction &&task_func) |
Post dispatcher owned |task_func| function. | |
virtual Status | pw::async::FunctionDispatcher::PostAfter (TaskFunction &&task_func, chrono::SystemClock::duration delay) |
Post dispatcher owned |task_func| function to be run after |delay|. | |
virtual Status | pw::async::FunctionDispatcher::PostAt (TaskFunction &&task_func, chrono::SystemClock::time_point time)=0 |
Post dispatcher owned |task_func| function to be run at |time|. | |
pw::async::HeapDispatcher::HeapDispatcher (Dispatcher &dispatcher) | |
Status | pw::async::HeapDispatcher::PostAt (TaskFunction &&task_func, chrono::SystemClock::time_point time) override |
Post dispatcher owned |task_func| function to be run at |time|. | |
void | pw::async::HeapDispatcher::PostAt (Task &task, chrono::SystemClock::time_point time) override |
bool | pw::async::HeapDispatcher::Cancel (Task &task) override |
chrono::SystemClock::time_point | pw::async::HeapDispatcher::now () override |
Returns the current time. | |
pw::async::Task::Task () | |
pw::async::Task::Task (TaskFunction &&f) | |
Constructs a Task that calls f when executed on a Dispatcher . | |
pw::async::Task::Task (const Task &)=delete | |
Task & | pw::async::Task::operator= (const Task &)=delete |
pw::async::Task::Task (Task &&)=delete | |
Task & | pw::async::Task::operator= (Task &&)=delete |
void | pw::async::Task::set_function (TaskFunction &&f) |
void | pw::async::Task::operator() (Context &ctx, Status status) |
Executes this task. | |
backend::NativeTask & | pw::async::Task::native_type () |
const backend::NativeTask & | pw::async::Task::native_type () const |
Variables | |
Dispatcher * | pw::async::Context::dispatcher |
The Dispatcher running the current Task . | |
Task * | pw::async::Context::task |
The current Task being executed. | |
using pw::async::TaskFunction = typedef Function<void(Context&, Status)> |
A TaskFunction
is a unit of work that is wrapped by a Task and executed on a Dispatcher
.
TaskFunction
s take a Context
as their first argument. Before executing a Task
, the Dispatcher
sets the pointer to itself and to the Task
in Context
.
TaskFunction
s take a Status
as their second argument. When a Task is running as normal, |status| is PW_STATUS_OK
. If a Task
will not be able to run as scheduled, the Dispatcher
will still invoke the TaskFunction
with |status| PW_STATUS_CANCELLED
. This provides an opportunity to reclaim resources held by the Task.
A Task
will not run as scheduled if, for example, it is still waiting when the Dispatcher
shuts down.
|
inlineoverridevirtual |
Prevent a Post
ed task from starting.
Returns: true: the task was successfully canceled and will not be run by the dispatcher until Post
ed again. false: the task could not be cancelled because it either was not posted, already ran, or is currently running on the Dispatcher
thread.
Implements pw::async::Dispatcher.
|
pure virtual |
Prevent a Post
ed task from starting.
Returns: true: the task was successfully canceled and will not be run by the dispatcher until Post
ed again. false: the task could not be cancelled because it either was not posted, already ran, or is currently running on the Dispatcher
thread.
Implemented in pw::async::HeapDispatcher, and pw::async::BasicDispatcher.
|
inline |
Returns the inner NativeTask
containing backend-specific state. Only Dispatcher
backends or non-portable code should call these methods!
|
inlineoverridevirtual |
Returns the current time.
Implements pw::chrono::VirtualClock< SystemClock >.
|
inlinevirtual |
Post caller-owned |task| to be run on the dispatch loop.
Posted tasks execute in the order they are posted. This ensures that tasks can re-post themselves and yield in order to allow other tasks the opportunity to execute.
A given |task| must only be posted to a single Dispatcher
.
Reimplemented in pw::async::FunctionDispatcher.
|
inlinevirtual |
Post caller owned |task| to be run after |delay|.
If |task| was already posted to run at an earlier time (before |delay| would expire), |task| must be run at the earlier time, and |task| may also be run at the later time.
Reimplemented in pw::async::FunctionDispatcher.
|
inlineoverridevirtual |
Post caller owned |task| to be run at |time|.
If |task| was already posted to run before |time|, |task| must be run at the earlier time, and |task| may also be run at the later time.
Implements pw::async::Dispatcher.
|
pure virtual |
Post caller owned |task| to be run at |time|.
If |task| was already posted to run before |time|, |task| must be run at the earlier time, and |task| may also be run at the later time.
Implemented in pw::async::HeapDispatcher, pw::async::BasicDispatcher, and pw::async::FunctionDispatcher.
|
overridevirtual |
Post dispatcher owned |task_func| function to be run at |time|.
Implements pw::async::FunctionDispatcher.
|
pure virtual |
Post dispatcher owned |task_func| function to be run at |time|.
Implemented in pw::async::HeapDispatcher.
|
inline |
Dispatches all tasks with due times up to now() + duration
, progressively advancing the fake clock. Returns true iff any tasks were invoked during the run.
|
inline |
Dispatches all tasks with due times up to end_time
, progressively advancing the fake clock. Returns true iff any tasks were invoked during the run.
|
inline |
Dispatches all tasks with due times up until now()
. Returns true iff any tasks were invoked during the run.
|
inline |
Configure the TaskFunction
after construction. This MUST NOT be called while this Task
is pending in a Dispatcher
.
|
inline |
The default constructor creates a Task
without a function. set_function()
must be called before posting the Task
.