• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "pw_async_basic/dispatcher.h"
15 
16 #include <mutex>
17 
18 #include "pw_chrono/system_clock.h"
19 
20 using namespace std::chrono_literals;
21 
22 namespace pw::async {
23 
~BasicDispatcher()24 BasicDispatcher::~BasicDispatcher() {
25   RequestStop();
26   lock_.lock();
27   DrainTaskQueue();
28   lock_.unlock();
29 }
30 
Run()31 void BasicDispatcher::Run() {
32   lock_.lock();
33   while (!stop_requested_) {
34     MaybeSleep();
35     ExecuteDueTasks();
36   }
37   DrainTaskQueue();
38   lock_.unlock();
39 }
40 
RunUntilIdle()41 void BasicDispatcher::RunUntilIdle() {
42   lock_.lock();
43   ExecuteDueTasks();
44   if (stop_requested_) {
45     DrainTaskQueue();
46   }
47   lock_.unlock();
48 }
49 
RunUntil(chrono::SystemClock::time_point end_time)50 void BasicDispatcher::RunUntil(chrono::SystemClock::time_point end_time) {
51   lock_.lock();
52   while (end_time < now() && !stop_requested_) {
53     MaybeSleep();
54     ExecuteDueTasks();
55   }
56   if (stop_requested_) {
57     DrainTaskQueue();
58   }
59   lock_.unlock();
60 }
61 
RunFor(chrono::SystemClock::duration duration)62 void BasicDispatcher::RunFor(chrono::SystemClock::duration duration) {
63   RunUntil(now() + duration);
64 }
65 
MaybeSleep()66 void BasicDispatcher::MaybeSleep() {
67   if (task_queue_.empty() || task_queue_.front().due_time_ > now()) {
68     // Sleep until a notification is received or until the due time of the
69     // next task. Notifications are sent when tasks are posted or 'stop' is
70     // requested.
71     std::optional<chrono::SystemClock::time_point> wake_time = std::nullopt;
72     if (!task_queue_.empty()) {
73       wake_time = task_queue_.front().due_time_;
74     }
75     lock_.unlock();
76     if (wake_time.has_value()) {
77       timed_notification_.try_acquire_until(*wake_time);
78     } else {
79       timed_notification_.acquire();
80     }
81     lock_.lock();
82   }
83 }
84 
ExecuteDueTasks()85 void BasicDispatcher::ExecuteDueTasks() {
86   while (!task_queue_.empty() && task_queue_.front().due_time_ <= now() &&
87          !stop_requested_) {
88     backend::NativeTask& task = task_queue_.front();
89     task_queue_.pop_front();
90 
91     lock_.unlock();
92     Context ctx{this, &task.task_};
93     task(ctx, OkStatus());
94     lock_.lock();
95   }
96 }
97 
RequestStop()98 void BasicDispatcher::RequestStop() {
99   {
100     std::lock_guard lock(lock_);
101     stop_requested_ = true;
102   }
103   timed_notification_.release();
104 }
105 
DrainTaskQueue()106 void BasicDispatcher::DrainTaskQueue() {
107   while (!task_queue_.empty()) {
108     backend::NativeTask& task = task_queue_.front();
109     task_queue_.pop_front();
110 
111     lock_.unlock();
112     Context ctx{this, &task.task_};
113     task(ctx, Status::Cancelled());
114     lock_.lock();
115   }
116 }
117 
PostAt(Task & task,chrono::SystemClock::time_point time)118 void BasicDispatcher::PostAt(Task& task, chrono::SystemClock::time_point time) {
119   PostTaskInternal(task.native_type(), time);
120 }
121 
Cancel(Task & task)122 bool BasicDispatcher::Cancel(Task& task) {
123   std::lock_guard lock(lock_);
124   return task_queue_.remove(task.native_type());
125 }
126 
PostTaskInternal(backend::NativeTask & task,chrono::SystemClock::time_point time_due)127 void BasicDispatcher::PostTaskInternal(
128     backend::NativeTask& task, chrono::SystemClock::time_point time_due) {
129   lock_.lock();
130   task.due_time_ = time_due;
131   // Insert the new task in the queue after all tasks with the same or earlier
132   // deadline to ensure FIFO execution order.
133   auto it_front = task_queue_.begin();
134   auto it_behind = task_queue_.before_begin();
135   while (it_front != task_queue_.end() && time_due >= it_front->due_time_) {
136     ++it_front;
137     ++it_behind;
138   }
139   task_queue_.insert_after(it_behind, task);
140   lock_.unlock();
141   timed_notification_.release();
142 }
143 
144 }  // namespace pw::async
145