pw_async_basic#
This module includes basic implementations of pw_async’s Dispatcher and FakeDispatcher.
API#
-
class BasicDispatcher : public pw::async::Dispatcher, public thread::ThreadCore#
BasicDispatcher is a generic implementation of Dispatcher.
Public Functions
-
void RunUntilIdle()#
Execute all runnable tasks and return without waiting.
-
void RunUntil(chrono::SystemClock::time_point end_time)#
Run the dispatcher until Now() has reached
end_time
, executing all tasks that come due before then.
-
void RunFor(chrono::SystemClock::duration duration)#
Run the dispatcher until
duration
has elapsed, executing all tasks that come due in that period.
-
void RequestStop()#
Stop processing tasks. If the dispatcher is serving a task loop, break out of the loop, dequeue all waiting tasks, and call their TaskFunctions with a PW_STATUS_CANCELLED status. If no task loop is being served, execute the dequeueing procedure the next time the Dispatcher is run.
-
void Run() override#
Run the dispatcher until RequestStop() is called. Overrides ThreadCore::Run() so that BasicDispatcher is compatible with pw::Thread.
-
virtual void PostAt(Task &task, chrono::SystemClock::time_point time) override#
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.
-
virtual bool Cancel(Task &task) override#
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 theDispatcher
thread.
-
inline virtual chrono::SystemClock::time_point now() override#
Returns the current time.
-
void RunUntilIdle()#
Usage#
First, set the following GN variables:
pw_async_TASK_BACKEND="$dir_pw_async_basic:task"
pw_async_FAKE_DISPATCHER_BACKEND="$dir_pw_async_basic:fake_dispatcher"
Next, create a target that depends on //pw_async_basic:dispatcher
:
pw_executable("hello_world") {
sources = [ "hello_world.cc" ]
deps = [
"//pw_async_basic:dispatcher",
]
}
Next, construct and use a BasicDispatcher
.
#include "pw_async_basic/dispatcher.h"
void DelayedPrint(pw::async::Dispatcher& dispatcher) {
dispatcher.PostAfter([](auto&){
printf("hello world\n");
}, 5s);
}
int main() {
pw::async::BasicDispatcher dispatcher;
DelayedPrint(dispatcher);
dispatcher.RunFor(10s);
return 0;
}
Size Report#
Label |
Segment |
Delta |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Construct a Task |
FLASH
|
+288 |