Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
dispatcher.h
1// Copyright 2023 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_async2/context.h"
17#include "pw_async2/dispatcher_native.h"
18#include "pw_async2/lock.h"
19#include "pw_async2/task.h"
20#include "pw_async2/waker.h"
21
22namespace pw::async2 {
23namespace internal {
24
25template <typename Pendable>
27 public:
28 using OutputType = PendOutputOf<Pendable>;
29 PendableAsTaskWithOutput(Pendable& pendable)
30 : pendable_(pendable), output_(Pending()) {}
31
32 Poll<OutputType> TakePoll() { return std::move(output_); }
33
34 private:
35 Poll<> DoPend(Context& cx) final {
36 output_ = pendable_.Pend(cx);
37 return output_.Readiness();
38 }
39 Pendable& pendable_;
40 Poll<OutputType> output_;
41};
42
43} // namespace internal
44
47 public:
49 Dispatcher() = default;
50 Dispatcher(Dispatcher&) = delete;
51 Dispatcher(Dispatcher&&) = delete;
52 Dispatcher& operator=(Dispatcher&) = delete;
53 Dispatcher& operator=(Dispatcher&&) = delete;
54 ~Dispatcher() { native_.Deregister(); }
55
65 void Post(Task& task) PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
66 native_.Post(task);
67 }
68
70 Poll<> RunUntilStalled() PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
71 return native_.DoRunUntilStalled(*this, nullptr);
72 }
73
79 PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
80 return native_.DoRunUntilStalled(*this, &task);
81 }
82
87 template <typename Pendable>
89 PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
91 Post(task);
92 if (RunUntilStalled(task).IsReady()) {
93 return task.TakePoll();
94 }
95 // Ensure that the task is no longer registered, as it will be destroyed
96 // once we return.
97 //
98 // This operation will not block because we are on the dispatcher thread
99 // and the dispatcher is not currently running (we just ran it).
100 task.Deregister();
101 return Pending();
102 }
103
105 void RunToCompletion() PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
106 native_.DoRunToCompletion(*this, nullptr);
107 }
108
110 void RunToCompletion(Task& task) PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
111 native_.DoRunToCompletion(*this, &task);
112 }
113
115 template <typename Pendable>
116 PendOutputOf<Pendable> RunPendableToCompletion(Pendable& pendable)
117 PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
119 Post(task);
120 native_.DoRunToCompletion(*this, &task);
121 return task.TakePoll().value();
122 }
123
126 void LogRegisteredTasks() { native_.LogRegisteredTasks(); }
127
130 uint32_t tasks_polled() const { return native_.tasks_polled(); }
131
133 uint32_t tasks_completed() const { return native_.tasks_completed(); }
134
137
138 private:
140};
141
142} // namespace pw::async2
Definition: context.h:53
A single-threaded cooperatively-scheduled runtime for async tasks.
Definition: dispatcher.h:46
pw::async2::backend::NativeDispatcher & native()
Returns a reference to the native backend-specific dispatcher type.
Definition: dispatcher.h:136
Poll RunUntilStalled()
Runs tasks until none are able to make immediate progress.
Definition: dispatcher.h:70
Dispatcher()=default
Constructs a new async Dispatcher.
void RunToCompletion()
Runs until all tasks complete.
Definition: dispatcher.h:105
Poll< PendOutputOf< Pendable > > RunPendableUntilStalled(Pendable &pendable)
Definition: dispatcher.h:88
uint32_t tasks_completed() const
Returns the total number of tasks the dispatcher has run to completion.
Definition: dispatcher.h:133
void LogRegisteredTasks()
Definition: dispatcher.h:126
void Post(Task &task)
Definition: dispatcher.h:65
void RunToCompletion(Task &task)
Runs until task completes.
Definition: dispatcher.h:110
Poll RunUntilStalled(Task &task)
Definition: dispatcher.h:78
uint32_t tasks_polled() const
Definition: dispatcher.h:130
PendOutputOf< Pendable > RunPendableToCompletion(Pendable &pendable)
Runs until pendable completes, returning the output of pendable.
Definition: dispatcher.h:116
Definition: poll.h:54
constexpr Poll Readiness() const noexcept
Definition: poll.h:132
constexpr T & value() &noexcept
Definition: poll.h:143
Definition: task.h:61
Definition: dispatcher_native.h:39
Poll DoPend(Context &cx) final
Definition: dispatcher.h:35