1 // Copyright 2024 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
15 #include "pw_async2/allocate_task.h"
16
17 #include "pw_allocator/testing.h"
18
19 namespace {
20
21 using ::pw::allocator::test::AllocatorForTest;
22 using ::pw::async2::AllocateTask;
23 using ::pw::async2::Context;
24 using ::pw::async2::Dispatcher;
25 using ::pw::async2::Pending;
26 using ::pw::async2::Poll;
27 using ::pw::async2::Ready;
28 using ::pw::async2::Task;
29 using ::pw::async2::Waker;
30
31 struct PendableStatus {
32 Waker last_waker = {};
33 bool should_finish = false;
34 int created = 0;
35 int polled = 0;
36 int destroyed = 0;
37 };
38
39 class Pendable {
40 public:
Pendable(PendableStatus & status)41 Pendable(PendableStatus& status) : status_(&status) { ++status_->created; }
42 Pendable() = delete;
Pendable(Pendable && other)43 Pendable(Pendable&& other) : status_(other.status_) {
44 other.status_ = nullptr;
45 }
operator =(Pendable && other)46 Pendable& operator=(Pendable&& other) {
47 Reset();
48 status_ = other.status_;
49 other.status_ = nullptr;
50 return *this;
51 }
~Pendable()52 ~Pendable() { Reset(); }
Pend(Context & cx)53 Poll<> Pend(Context& cx) {
54 if (status_ == nullptr) {
55 return Pending();
56 }
57 PW_ASYNC_STORE_WAKER(
58 cx, status_->last_waker, "Pendable is waiting for last_waker");
59 ++status_->polled;
60 if (status_->should_finish) {
61 return Ready();
62 }
63 return Pending();
64 }
65
66 private:
Reset()67 void Reset() {
68 if (status_ != nullptr) {
69 ++status_->destroyed;
70 }
71 }
72 PendableStatus* status_;
73 };
74
TEST(AllocateTask,AllocatesWithRvalue)75 TEST(AllocateTask, AllocatesWithRvalue) {
76 AllocatorForTest<256> alloc;
77 Dispatcher dispatcher;
78 PendableStatus status = {};
79 Pendable pendable(status);
80 Task* task = AllocateTask(alloc, std::move(pendable));
81 ASSERT_NE(task, nullptr);
82 EXPECT_NE(alloc.allocate_size(), alloc.deallocate_size());
83 task->Destroy();
84 EXPECT_EQ(alloc.allocate_size(), alloc.deallocate_size());
85 }
86
TEST(AllocateTask,AllocatesWithArgs)87 TEST(AllocateTask, AllocatesWithArgs) {
88 AllocatorForTest<256> alloc;
89 Dispatcher dispatcher;
90 PendableStatus status = {};
91 Task* task = AllocateTask<Pendable>(alloc, status);
92 ASSERT_NE(task, nullptr);
93 EXPECT_NE(alloc.allocate_size(), alloc.deallocate_size());
94 task->Destroy();
95 EXPECT_EQ(alloc.allocate_size(), alloc.deallocate_size());
96 }
97
TEST(AllocateTask,DestroysOnceAfterPendReturnsReady)98 TEST(AllocateTask, DestroysOnceAfterPendReturnsReady) {
99 AllocatorForTest<256> alloc;
100 Dispatcher dispatcher;
101 PendableStatus status = {};
102 Task* task = AllocateTask<Pendable>(alloc, status);
103 ASSERT_NE(task, nullptr);
104 dispatcher.Post(*task);
105
106 EXPECT_EQ(dispatcher.RunUntilStalled(), Pending());
107 EXPECT_EQ(status.polled, 1);
108 EXPECT_EQ(status.destroyed, 0);
109
110 std::move(status.last_waker).Wake();
111 status.should_finish = true;
112
113 EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
114 EXPECT_EQ(status.polled, 2);
115 EXPECT_EQ(status.destroyed, 1);
116
117 // Ensure that the allocated task is not polled or destroyed again after being
118 // deallocated.
119 std::move(status.last_waker).Wake();
120 EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
121 EXPECT_EQ(status.polled, 2);
122 EXPECT_EQ(status.destroyed, 1);
123 }
124
125 } // namespace
126