C/C++ API Reference
Loading...
Searching...
No Matches
dispatcher_for_test.h
1// Copyright 2025 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/runnable_dispatcher.h"
17
18namespace pw::async2 {
19namespace internal {
20
21// This should be updated to work for futures instead of generic pendables.
22template <typename Pendable>
24 public:
25 using value_type = PendOutputOf<Pendable>;
26 PendableAsTaskWithOutput(Pendable& pendable)
27 : pendable_(pendable), output_(Pending()) {}
28
29 Poll<value_type> TakePoll() { return std::move(output_); }
30
31 private:
32 Poll<> DoPend(Context& cx) final {
33 output_ = pendable_.Pend(cx);
34 return output_.Readiness();
35 }
36 Pendable& pendable_;
37 Poll<value_type> output_;
38};
39
40} // namespace internal
41
43
52template <typename Native>
54 public:
57
59 DispatcherForTestFacade& operator=(const DispatcherForTestFacade&) = delete;
60
63
66 void AllowBlocking() { blocking_is_allowed_ = true; }
67
68 template <typename Pendable>
69 Poll<internal::PendOutputOf<Pendable>> RunInTaskUntilStalled(
70 Pendable& pendable) PW_LOCKS_EXCLUDED(impl::dispatcher_lock()) {
72 native().Post(task);
73 native().RunUntilStalled();
74
75 // Ensure that the task is no longer registered, as it will be destroyed
76 // once we return.
77 //
78 // This operation will not block because we are on the dispatcher thread
79 // and the dispatcher is not currently running (we just ran it).
80 task.Deregister();
81
82 return task.TakePoll();
83 }
84
87 uint32_t tasks_polled() const { return tasks_polled_; }
88
90 uint32_t tasks_completed() const { return tasks_completed_; }
91
93 uint32_t wake_count() const { return wake_count_; }
94
95 private:
96 // These functions are implemented in dispatcher_for_test.cc for the
97 // NativeDispatcherForTest specialization only.
98 bool DoRunUntilStalled() override;
99
100 void DoWake() override;
101
102 void DoWaitForWake() override;
103
104 RunnableDispatcher& native() { return native_; }
105
106 Native native_;
107 bool blocking_is_allowed_ = false;
108
109 // TODO: b/401049619 - Optionally provide metrics for production dispatchers.
110 uint32_t tasks_polled_ = 0u;
111 uint32_t tasks_completed_ = 0u;
112 uint32_t wake_count_ = 0u;
113};
114
115} // namespace pw::async2
116
117#include "pw_async2_backend/native_dispatcher_for_test.h"
118
119namespace pw::async2 {
120
125
126} // namespace pw::async2
Definition: context.h:54
Definition: dispatcher_for_test.h:53
void Post(Task &task)
Definition: poll.h:60
Definition: runnable_dispatcher.h:24
Definition: task.h:62
Definition: dispatcher_for_test.h:23
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
bool RunUntilStalled()
Definition: runnable_dispatcher.h:30
uint32_t wake_count() const
Returns the total number of times the dispatcher has been woken.
Definition: dispatcher_for_test.h:93
DispatcherForTestFacade()=default
DispatcherForTest is default constructible.
uint32_t tasks_polled() const
Definition: dispatcher_for_test.h:87
Poll DoPend(Context &cx) final
Definition: dispatcher_for_test.h:32
uint32_t tasks_completed() const
Returns the total number of tasks the dispatcher has run to completion.
Definition: dispatcher_for_test.h:90
void AllowBlocking()
Definition: dispatcher_for_test.h:66
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176