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