C/C++ API Reference
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 value_type = PendOutputOf<Pendable>;
29 PendableAsTaskWithOutput(Pendable& pendable)
30 : pendable_(pendable), output_(Pending()) {}
31
32 Poll<value_type> 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<value_type> output_;
41};
42
43} // namespace internal
44
46
49 public:
51 Dispatcher() = default;
52 Dispatcher(Dispatcher&) = delete;
53 Dispatcher(Dispatcher&&) = delete;
54 Dispatcher& operator=(Dispatcher&) = delete;
55 Dispatcher& operator=(Dispatcher&&) = delete;
56 ~Dispatcher() { native_.Deregister(); }
57
68 native_.Post(task);
69 }
70
72 Poll<> RunUntilStalled() PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
73 return native_.DoRunUntilStalled(*this, nullptr);
74 }
75
82 return native_.DoRunUntilStalled(*this, &task);
83 }
84
89 template <typename Pendable>
93 Post(task);
94 if (RunUntilStalled(task).IsReady()) {
95 return task.TakePoll();
96 }
97 // Ensure that the task is no longer registered, as it will be destroyed
98 // once we return.
99 //
100 // This operation will not block because we are on the dispatcher thread
101 // and the dispatcher is not currently running (we just ran it).
102 task.Deregister();
103 return Pending();
104 }
105
107 void RunToCompletion() PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
108 native_.DoRunToCompletion(*this, nullptr);
109 }
110
113 native_.DoRunToCompletion(*this, &task);
114 }
115
117 template <typename Pendable>
118 PendOutputOf<Pendable> RunPendableToCompletion(Pendable& pendable)
121 Post(task);
122 native_.DoRunToCompletion(*this, &task);
123 return task.TakePoll().value();
124 }
125
128 void LogRegisteredTasks() { native_.LogRegisteredTasks(); }
129
132 uint32_t tasks_polled() const { return native_.tasks_polled(); }
133
135 uint32_t tasks_completed() const { return native_.tasks_completed(); }
136
139
140 private:
142};
143
145
146} // namespace pw::async2
Definition: context.h:55
A single-threaded cooperatively scheduled runtime for async tasks.
Definition: dispatcher.h:48
pw::async2::backend::NativeDispatcher & native()
Returns a reference to the native backend-specific dispatcher type.
Definition: dispatcher.h:138
Poll RunUntilStalled()
Runs tasks until none are able to make immediate progress.
Definition: dispatcher.h:72
Dispatcher()=default
Constructs a new async Dispatcher.
void RunToCompletion()
Runs until all tasks complete.
Definition: dispatcher.h:107
Poll< PendOutputOf< Pendable > > RunPendableUntilStalled(Pendable &pendable)
Definition: dispatcher.h:90
uint32_t tasks_completed() const
Returns the total number of tasks the dispatcher has run to completion.
Definition: dispatcher.h:135
void LogRegisteredTasks()
Definition: dispatcher.h:128
void Post(Task &task)
Definition: dispatcher.h:67
void RunToCompletion(Task &task)
Runs until task completes.
Definition: dispatcher.h:112
Poll RunUntilStalled(Task &task)
Definition: dispatcher.h:80
uint32_t tasks_polled() const
Definition: dispatcher.h:132
PendOutputOf< Pendable > RunPendableToCompletion(Pendable &pendable)
Runs until pendable completes, returning the output of pendable.
Definition: dispatcher.h:118
Definition: poll.h:60
Definition: task.h:63
Definition: dispatcher_native.h:39
Poll DoPend(Context &cx) final
Definition: dispatcher.h:35
pw::sync::InterruptSpinLock & dispatcher_lock()
Definition: lock.h:33
constexpr Poll Readiness() const noexcept
Definition: poll.h:140
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr value_type & value() &noexcept
Definition: poll.h:151
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176