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::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

No backend is selected.

(ALL)

0