Pigweed
 
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/dispatcher_base.h" // IWYU pragma: export
17#include "pw_async2/dispatcher_native.h"
18
19namespace pw::async2 {
20namespace internal {
21
22template <typename Pendable>
24 public:
25 using OutputType = PendOutputOf<Pendable>;
26 PendableAsTaskWithOutput(Pendable& pendable)
27 : pendable_(pendable), output_(Pending()) {}
28 OutputType&& TakeOutput() { return std::move(*output_); }
29
30 private:
31 Poll<> DoPend(Context& cx) final {
32 output_ = pendable_.Pend(cx);
33 return output_.Readiness();
34 }
35 Pendable& pendable_;
36 Poll<OutputType> output_;
37};
38
39} // namespace internal
40
43 public:
45 Dispatcher() = default;
46 Dispatcher(Dispatcher&) = delete;
47 Dispatcher(Dispatcher&&) = delete;
48 Dispatcher& operator=(Dispatcher&) = delete;
49 Dispatcher& operator=(Dispatcher&&) = delete;
50 ~Dispatcher() { native_.Deregister(); }
51
61 void Post(Task& task) PW_LOCKS_EXCLUDED(dispatcher_lock()) {
62 native_.Post(task);
63 }
64
66 Poll<> RunUntilStalled() PW_LOCKS_EXCLUDED(dispatcher_lock()) {
67 return native_.DoRunUntilStalled(*this, nullptr);
68 }
69
74 Poll<> RunUntilStalled(Task& task) PW_LOCKS_EXCLUDED(dispatcher_lock()) {
75 return native_.DoRunUntilStalled(*this, &task);
76 }
77
82 template <typename Pendable>
84 PW_LOCKS_EXCLUDED(dispatcher_lock()) {
86 Post(task);
87 if (RunUntilStalled(task).IsReady()) {
88 return task.TakeOutput();
89 }
90 // Ensure that the task is no longer registered, as it will be destroyed
91 // once we return.
92 //
93 // This operation will not block because we are on the dispatcher thread
94 // and the dispatcher is not currently running (we just ran it).
95 task.Deregister();
96 return Pending();
97 }
98
100 void RunToCompletion() PW_LOCKS_EXCLUDED(dispatcher_lock()) {
101 native_.DoRunToCompletion(*this, nullptr);
102 }
103
105 void RunToCompletion(Task& task) PW_LOCKS_EXCLUDED(dispatcher_lock()) {
106 native_.DoRunToCompletion(*this, &task);
107 }
108
110 template <typename Pendable>
111 PendOutputOf<Pendable> RunPendableToCompletion(Pendable& pendable)
112 PW_LOCKS_EXCLUDED(dispatcher_lock()) {
114 Post(task);
115 native_.DoRunToCompletion(*this, &task);
116 return task.TakeOutput();
117 }
118
121
122 private:
124};
125
126} // namespace pw::async2
Definition: dispatcher_base.h:52
A single-threaded cooperatively-scheduled runtime for async tasks.
Definition: dispatcher.h:42
pw::async2::backend::NativeDispatcher & native()
Returns a reference to the native backend-specific dispatcher type.
Definition: dispatcher.h:120
Poll RunUntilStalled()
Runs tasks until none are able to make immediate progress.
Definition: dispatcher.h:66
Dispatcher()=default
Constructs a new async Dispatcher.
void RunToCompletion()
Runs until all tasks complete.
Definition: dispatcher.h:100
Poll< PendOutputOf< Pendable > > RunPendableUntilStalled(Pendable &pendable)
Definition: dispatcher.h:83
void Post(Task &task)
Definition: dispatcher.h:61
void RunToCompletion(Task &task)
Runs until task completes.
Definition: dispatcher.h:105
Poll RunUntilStalled(Task &task)
Definition: dispatcher.h:74
PendOutputOf< Pendable > RunPendableToCompletion(Pendable &pendable)
Runs until pendable completes, returning the output of pendable.
Definition: dispatcher.h:111
Definition: poll.h:54
constexpr Poll Readiness() const noexcept
Definition: poll.h:132
Definition: dispatcher_base.h:158
Definition: dispatcher_native.h:39
Poll DoPend(Context &cx) final
Definition: dispatcher.h:31