1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_async2/join.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include "pw_async2/dispatcher.h"
18*61c4878aSAndroid Build Coastguard Worker #include "pw_unit_test/framework.h"
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker namespace {
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Context;
23*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Dispatcher;
24*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Join;
25*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Pending;
26*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Poll;
27*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Waker;
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker // Windows GCC emits a bogs uninitialized error for the
30*61c4878aSAndroid Build Coastguard Worker // move constructor below.
31*61c4878aSAndroid Build Coastguard Worker PW_MODIFY_DIAGNOSTICS_PUSH();
32*61c4878aSAndroid Build Coastguard Worker PW_MODIFY_DIAGNOSTIC_GCC(ignored, "-Wmaybe-uninitialized");
33*61c4878aSAndroid Build Coastguard Worker struct SomeMoveOnlyValue {
SomeMoveOnlyValue__anon0eae9da30111::SomeMoveOnlyValue34*61c4878aSAndroid Build Coastguard Worker explicit SomeMoveOnlyValue(int result) : result_(result), move_count_(0) {}
35*61c4878aSAndroid Build Coastguard Worker SomeMoveOnlyValue(const SomeMoveOnlyValue&) = delete;
36*61c4878aSAndroid Build Coastguard Worker SomeMoveOnlyValue& operator=(const SomeMoveOnlyValue&) = delete;
SomeMoveOnlyValue__anon0eae9da30111::SomeMoveOnlyValue37*61c4878aSAndroid Build Coastguard Worker SomeMoveOnlyValue(SomeMoveOnlyValue&& other)
38*61c4878aSAndroid Build Coastguard Worker : result_(other.result_), move_count_(other.move_count_ + 1) {
39*61c4878aSAndroid Build Coastguard Worker other.result_ = -1;
40*61c4878aSAndroid Build Coastguard Worker }
operator =__anon0eae9da30111::SomeMoveOnlyValue41*61c4878aSAndroid Build Coastguard Worker SomeMoveOnlyValue& operator=(SomeMoveOnlyValue&& other) {
42*61c4878aSAndroid Build Coastguard Worker result_ = other.result_;
43*61c4878aSAndroid Build Coastguard Worker other.result_ = -1;
44*61c4878aSAndroid Build Coastguard Worker move_count_ = other.move_count_ + 1;
45*61c4878aSAndroid Build Coastguard Worker return *this;
46*61c4878aSAndroid Build Coastguard Worker }
47*61c4878aSAndroid Build Coastguard Worker ~SomeMoveOnlyValue() = default;
48*61c4878aSAndroid Build Coastguard Worker int result_;
49*61c4878aSAndroid Build Coastguard Worker int move_count_;
50*61c4878aSAndroid Build Coastguard Worker };
51*61c4878aSAndroid Build Coastguard Worker PW_MODIFY_DIAGNOSTICS_POP();
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker struct PendableController {
PendableController__anon0eae9da30111::PendableController54*61c4878aSAndroid Build Coastguard Worker PendableController() : poll_count_(0), allow_completion_(false), waker_() {}
55*61c4878aSAndroid Build Coastguard Worker PendableController(PendableController&) = delete;
56*61c4878aSAndroid Build Coastguard Worker PendableController(PendableController&&) = delete;
57*61c4878aSAndroid Build Coastguard Worker PendableController& operator=(const PendableController&) = delete;
58*61c4878aSAndroid Build Coastguard Worker PendableController& operator=(PendableController&&) = delete;
59*61c4878aSAndroid Build Coastguard Worker ~PendableController() = default;
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker int poll_count_;
62*61c4878aSAndroid Build Coastguard Worker bool allow_completion_;
63*61c4878aSAndroid Build Coastguard Worker Waker waker_;
64*61c4878aSAndroid Build Coastguard Worker };
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker struct StructWithPendMethod {
StructWithPendMethod__anon0eae9da30111::StructWithPendMethod67*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod(int result, PendableController& controller)
68*61c4878aSAndroid Build Coastguard Worker : result_(result), controller_(&controller) {}
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod(const StructWithPendMethod&) = delete;
71*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod& operator=(const StructWithPendMethod&) = delete;
72*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod(StructWithPendMethod&&) = default;
73*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod& operator=(StructWithPendMethod&&) = default;
74*61c4878aSAndroid Build Coastguard Worker ~StructWithPendMethod() = default;
75*61c4878aSAndroid Build Coastguard Worker
Pend__anon0eae9da30111::StructWithPendMethod76*61c4878aSAndroid Build Coastguard Worker Poll<SomeMoveOnlyValue> Pend(Context& cx) {
77*61c4878aSAndroid Build Coastguard Worker ++controller_->poll_count_;
78*61c4878aSAndroid Build Coastguard Worker if (controller_->allow_completion_) {
79*61c4878aSAndroid Build Coastguard Worker return Poll<SomeMoveOnlyValue>(result_);
80*61c4878aSAndroid Build Coastguard Worker }
81*61c4878aSAndroid Build Coastguard Worker PW_ASYNC_STORE_WAKER(
82*61c4878aSAndroid Build Coastguard Worker cx,
83*61c4878aSAndroid Build Coastguard Worker controller_->waker_,
84*61c4878aSAndroid Build Coastguard Worker "StructWithPendMethod is waiting for PendableController's waker");
85*61c4878aSAndroid Build Coastguard Worker return Pending();
86*61c4878aSAndroid Build Coastguard Worker }
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard Worker int result_;
89*61c4878aSAndroid Build Coastguard Worker PendableController* controller_;
90*61c4878aSAndroid Build Coastguard Worker };
91*61c4878aSAndroid Build Coastguard Worker
TEST(Join,PendDelegatesToPendables)92*61c4878aSAndroid Build Coastguard Worker TEST(Join, PendDelegatesToPendables) {
93*61c4878aSAndroid Build Coastguard Worker Dispatcher dispatcher;
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker PendableController controller_1;
96*61c4878aSAndroid Build Coastguard Worker PendableController controller_2;
97*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod pendable_1(1, controller_1);
98*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod pendable_2(2, controller_2);
99*61c4878aSAndroid Build Coastguard Worker Join join(std::move(pendable_1), std::move(pendable_2));
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(dispatcher.RunPendableUntilStalled(join), Pending());
102*61c4878aSAndroid Build Coastguard Worker controller_2.allow_completion_ = true;
103*61c4878aSAndroid Build Coastguard Worker std::move(controller_2.waker_).Wake();
104*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(dispatcher.RunPendableUntilStalled(join), Pending());
105*61c4878aSAndroid Build Coastguard Worker controller_1.allow_completion_ = true;
106*61c4878aSAndroid Build Coastguard Worker std::move(controller_1.waker_).Wake();
107*61c4878aSAndroid Build Coastguard Worker auto&& result = dispatcher.RunPendableUntilStalled(join);
108*61c4878aSAndroid Build Coastguard Worker ASSERT_TRUE(result.IsReady());
109*61c4878aSAndroid Build Coastguard Worker auto&& [v1, v2] = std::move(*result);
110*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v1.result_, 1);
111*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v2.result_, 2);
112*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v1.move_count_, 1);
113*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v2.move_count_, 1);
114*61c4878aSAndroid Build Coastguard Worker }
115*61c4878aSAndroid Build Coastguard Worker
TEST(Join,BindsDirectly)116*61c4878aSAndroid Build Coastguard Worker TEST(Join, BindsDirectly) {
117*61c4878aSAndroid Build Coastguard Worker Dispatcher dispatcher;
118*61c4878aSAndroid Build Coastguard Worker PendableController controller_1;
119*61c4878aSAndroid Build Coastguard Worker controller_1.allow_completion_ = true;
120*61c4878aSAndroid Build Coastguard Worker PendableController controller_2;
121*61c4878aSAndroid Build Coastguard Worker controller_2.allow_completion_ = true;
122*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod pendable_1(1, controller_1);
123*61c4878aSAndroid Build Coastguard Worker StructWithPendMethod pendable_2(2, controller_2);
124*61c4878aSAndroid Build Coastguard Worker Join join(std::move(pendable_1), std::move(pendable_2));
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard Worker auto&& [v1, v2] = dispatcher.RunPendableToCompletion(join);
127*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v1.result_, 1);
128*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v2.result_, 2);
129*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v1.move_count_, 1);
130*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(v2.move_count_, 1);
131*61c4878aSAndroid Build Coastguard Worker }
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker } // namespace
134