• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     status_->last_waker = cx.GetWaker(pw::async2::WaitReason::Unspecified());
58     ++status_->polled;
59     if (status_->should_finish) {
60       return Ready();
61     }
62     return Pending();
63   }
64 
65  private:
Reset()66   void Reset() {
67     if (status_ != nullptr) {
68       ++status_->destroyed;
69     }
70   }
71   PendableStatus* status_;
72 };
73 
TEST(AllocateTask,AllocatesWithRvalue)74 TEST(AllocateTask, AllocatesWithRvalue) {
75   AllocatorForTest<256> alloc;
76   Dispatcher dispatcher;
77   PendableStatus status = {};
78   Pendable pendable(status);
79   Task* task = AllocateTask(alloc, std::move(pendable));
80   ASSERT_NE(task, nullptr);
81   EXPECT_NE(alloc.allocate_size(), alloc.deallocate_size());
82   task->Destroy();
83   EXPECT_EQ(alloc.allocate_size(), alloc.deallocate_size());
84 }
85 
TEST(AllocateTask,AllocatesWithArgs)86 TEST(AllocateTask, AllocatesWithArgs) {
87   AllocatorForTest<256> alloc;
88   Dispatcher dispatcher;
89   PendableStatus status = {};
90   Task* task = AllocateTask<Pendable>(alloc, status);
91   ASSERT_NE(task, nullptr);
92   EXPECT_NE(alloc.allocate_size(), alloc.deallocate_size());
93   task->Destroy();
94   EXPECT_EQ(alloc.allocate_size(), alloc.deallocate_size());
95 }
96 
TEST(AllocateTask,DestroysOnceAfterPendReturnsReady)97 TEST(AllocateTask, DestroysOnceAfterPendReturnsReady) {
98   AllocatorForTest<256> alloc;
99   Dispatcher dispatcher;
100   PendableStatus status = {};
101   Task* task = AllocateTask<Pendable>(alloc, status);
102   ASSERT_NE(task, nullptr);
103   dispatcher.Post(*task);
104 
105   EXPECT_EQ(dispatcher.RunUntilStalled(), Pending());
106   EXPECT_EQ(status.polled, 1);
107   EXPECT_EQ(status.destroyed, 0);
108 
109   std::move(status.last_waker).Wake();
110   status.should_finish = true;
111 
112   EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
113   EXPECT_EQ(status.polled, 2);
114   EXPECT_EQ(status.destroyed, 1);
115 
116   // Ensure that the allocated task is not polled or destroyed again after being
117   // deallocated.
118   std::move(status.last_waker).Wake();
119   EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
120   EXPECT_EQ(status.polled, 2);
121   EXPECT_EQ(status.destroyed, 1);
122 }
123 
124 }  // namespace
125