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 Posted task from starting.

Returns: true: the task was successfully canceled and will not be run by the dispatcher until Posted again. false: the task could not be cancelled because it either was not posted, already ran, or is currently running on the Dispatcher thread.

inline virtual chrono::SystemClock::time_point now() override#

Returns the current time.

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

-4

quorem

DEL

-197

__aeabi_dsub

-17

__floatundidf

DEL

-68

__extendsfdf2

+1

pw_assert_tokenized_HandleAssertFailure::_pw_tokenizer_string_entry_60_5

DEL

-36

__aeabi_i2d

DEL

-32

__aeabi_ui2d

+36

main

-2

pw_boot_PreMainInit

DEL

-2

pw_boot_PreStaticConstructorInit

NEW

+167

pw::containers::internal::CheckIntrusiveContainerIsEmpty()::_pw_tokenizer_string_entry_22_1

NEW

+126

pw::containers::internal::CheckIntrusiveItemIsUncontained()::_pw_tokenizer_string_entry_28_3

NEW

+56

pw_assert_HandleFailure::_pw_tokenizer_string_entry_35_1

NEW

+44

pw::async::backend::NativeTask::NativeTask()

NEW

+24

pw_assert_tokenized_HandleCheckFailure

NEW

+22

pw::async::backend::NativeTask::~NativeTask()

NEW

+22

pw::containers::internal::IntrusiveListItemBase<>::unlist()

NEW

+22

pw::containers::internal::IntrusiveListItemBase<>::~IntrusiveListItemBase()

NEW

+20

fit::internal::null_target<>::ops

NEW

+20

fit::internal::target<>::ops

NEW

+20

pw::containers::internal::CheckIntrusiveItemIsUncontained()

NEW

+20

pw::containers::internal::LegacyIntrusiveList<>::Item::~Item()

NEW

+14

pw::containers::internal::IntrusiveForwardListItem::DoGetPrevious()

NEW

+8

fit::internal::inline_trivial_target_move<>()

NEW

+8

pw::async::Task::Task()

NEW

+6

fit::internal::null_target<>::invoke()

NEW

+4

fit::internal::null_target_get()

NEW

+4

fit::internal::unshared_target_type_id()

NEW

+2

fit::internal::target<>::invoke()

+288