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
15 #include "pw_async/fake_dispatcher.h"
16
17 #include "pw_async/task.h"
18 #include "pw_log/log.h"
19
20 using namespace std::chrono_literals;
21
22 namespace pw::async::test::backend {
23
NativeFakeDispatcher(Dispatcher & dispatcher)24 NativeFakeDispatcher::NativeFakeDispatcher(Dispatcher& dispatcher)
25 : dispatcher_(dispatcher) {}
26
~NativeFakeDispatcher()27 NativeFakeDispatcher::~NativeFakeDispatcher() {
28 RequestStop();
29 DrainTaskQueue();
30 }
31
RunUntilIdle()32 bool NativeFakeDispatcher::RunUntilIdle() {
33 bool tasks_ran = ExecuteDueTasks();
34 if (stop_requested_) {
35 tasks_ran |= DrainTaskQueue();
36 }
37 return tasks_ran;
38 }
39
RunUntil(chrono::SystemClock::time_point end_time)40 bool NativeFakeDispatcher::RunUntil(chrono::SystemClock::time_point end_time) {
41 bool tasks_ran = false;
42 while (!task_queue_.empty() && task_queue_.front().due_time() <= end_time &&
43 !stop_requested_) {
44 now_ = task_queue_.front().due_time();
45 tasks_ran |= ExecuteDueTasks();
46 }
47
48 if (stop_requested_) {
49 tasks_ran |= DrainTaskQueue();
50 return tasks_ran;
51 }
52
53 if (now_ < end_time) {
54 now_ = end_time;
55 }
56 return tasks_ran;
57 }
58
RunFor(chrono::SystemClock::duration duration)59 bool NativeFakeDispatcher::RunFor(chrono::SystemClock::duration duration) {
60 return RunUntil(now() + duration);
61 }
62
ExecuteDueTasks()63 bool NativeFakeDispatcher::ExecuteDueTasks() {
64 bool task_ran = false;
65 while (!task_queue_.empty() && task_queue_.front().due_time() <= now() &&
66 !stop_requested_) {
67 ::pw::async::backend::NativeTask& task = task_queue_.front();
68 task_queue_.pop_front();
69
70 Context ctx{&dispatcher_, &task.task_};
71 task(ctx, OkStatus());
72
73 task_ran = true;
74 }
75 return task_ran;
76 }
77
RequestStop()78 void NativeFakeDispatcher::RequestStop() {
79 PW_LOG_DEBUG("stop requested");
80 stop_requested_ = true;
81 }
82
DrainTaskQueue()83 bool NativeFakeDispatcher::DrainTaskQueue() {
84 bool task_ran = false;
85 while (!task_queue_.empty()) {
86 ::pw::async::backend::NativeTask& task = task_queue_.front();
87 task_queue_.pop_front();
88
89 PW_LOG_DEBUG("running cancelled task");
90 Context ctx{&dispatcher_, &task.task_};
91 task(ctx, Status::Cancelled());
92
93 task_ran = true;
94 }
95 return task_ran;
96 }
97
Post(Task & task)98 void NativeFakeDispatcher::Post(Task& task) { PostAt(task, now()); }
99
PostAfter(Task & task,chrono::SystemClock::duration delay)100 void NativeFakeDispatcher::PostAfter(Task& task,
101 chrono::SystemClock::duration delay) {
102 PostAt(task, now() + delay);
103 }
104
PostAt(Task & task,chrono::SystemClock::time_point time)105 void NativeFakeDispatcher::PostAt(Task& task,
106 chrono::SystemClock::time_point time) {
107 PW_LOG_DEBUG("posting task");
108 PostTaskInternal(task.native_type(), time);
109 }
110
Cancel(Task & task)111 bool NativeFakeDispatcher::Cancel(Task& task) {
112 return task_queue_.remove(task.native_type());
113 }
114
PostTaskInternal(::pw::async::backend::NativeTask & task,chrono::SystemClock::time_point time_due)115 void NativeFakeDispatcher::PostTaskInternal(
116 ::pw::async::backend::NativeTask& task,
117 chrono::SystemClock::time_point time_due) {
118 if (!task.unlisted()) {
119 if (task.due_time() <= time_due) {
120 // No need to repost a task that was already queued to run.
121 return;
122 }
123 // The task needs its time updated, so we have to move it to
124 // a different part of the list.
125 task.unlist();
126 }
127 task.set_due_time(time_due);
128 auto it_front = task_queue_.begin();
129 auto it_behind = task_queue_.before_begin();
130 while (it_front != task_queue_.end() && time_due >= it_front->due_time()) {
131 ++it_front;
132 ++it_behind;
133 }
134 task_queue_.insert_after(it_behind, task);
135 }
136
137 } // namespace pw::async::test::backend
138