aboutsummaryrefslogtreecommitdiff
path: root/pw_async_basic/fake_dispatcher.cc
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2024-01-17 22:13:58 -0800
committerXin Li <delphij@google.com>2024-01-17 22:13:58 -0800
commit28d03a2a1cabbe01d7bcb6cf5166c10e50d3c2c6 (patch)
treec1643be8ab17fc607cea748a8bb1d621a5964873 /pw_async_basic/fake_dispatcher.cc
parentec2628a6ba2d0ecbe3ac10c8c772f6fc6acc345d (diff)
parentf054515492af5132f685cb23fe11891ee77104c9 (diff)
downloadpigweed-28d03a2a1cabbe01d7bcb6cf5166c10e50d3c2c6.tar.gz
Merge Android 24Q1 Release (ab/11220357)temp_319669529
Bug: 319669529 Merged-In: Iba357b308a79d0c8b560acd4f72b5423c9c83294 Change-Id: Icdf552029fb97a34e83c6dd7799433fc473a2506
Diffstat (limited to 'pw_async_basic/fake_dispatcher.cc')
-rw-r--r--pw_async_basic/fake_dispatcher.cc61
1 files changed, 32 insertions, 29 deletions
diff --git a/pw_async_basic/fake_dispatcher.cc b/pw_async_basic/fake_dispatcher.cc
index cbba89e98..6e649faee 100644
--- a/pw_async_basic/fake_dispatcher.cc
+++ b/pw_async_basic/fake_dispatcher.cc
@@ -29,47 +29,50 @@ NativeFakeDispatcher::~NativeFakeDispatcher() {
DrainTaskQueue();
}
-void NativeFakeDispatcher::RunUntilIdle() {
- ExecuteDueTasks();
+bool NativeFakeDispatcher::RunUntilIdle() {
+ bool tasks_ran = ExecuteDueTasks();
if (stop_requested_) {
- DrainTaskQueue();
+ tasks_ran |= DrainTaskQueue();
}
+ return tasks_ran;
}
-void NativeFakeDispatcher::RunUntil(chrono::SystemClock::time_point end_time) {
+bool NativeFakeDispatcher::RunUntil(chrono::SystemClock::time_point end_time) {
+ bool tasks_ran = false;
while (!task_queue_.empty() && task_queue_.front().due_time() <= end_time &&
!stop_requested_) {
now_ = task_queue_.front().due_time();
- ExecuteDueTasks();
+ tasks_ran |= ExecuteDueTasks();
}
if (stop_requested_) {
- DrainTaskQueue();
- return;
+ tasks_ran |= DrainTaskQueue();
+ return tasks_ran;
}
if (now_ < end_time) {
now_ = end_time;
}
+ return tasks_ran;
}
-void NativeFakeDispatcher::RunFor(chrono::SystemClock::duration duration) {
- RunUntil(now() + duration);
+bool NativeFakeDispatcher::RunFor(chrono::SystemClock::duration duration) {
+ return RunUntil(now() + duration);
}
-void NativeFakeDispatcher::ExecuteDueTasks() {
+bool NativeFakeDispatcher::ExecuteDueTasks() {
+ bool task_ran = false;
while (!task_queue_.empty() && task_queue_.front().due_time() <= now() &&
!stop_requested_) {
::pw::async::backend::NativeTask& task = task_queue_.front();
task_queue_.pop_front();
- if (task.interval().has_value()) {
- PostTaskInternal(task, task.due_time() + task.interval().value());
- }
-
Context ctx{&dispatcher_, &task.task_};
task(ctx, OkStatus());
+
+ task_ran = true;
}
+ return task_ran;
}
void NativeFakeDispatcher::RequestStop() {
@@ -77,7 +80,8 @@ void NativeFakeDispatcher::RequestStop() {
stop_requested_ = true;
}
-void NativeFakeDispatcher::DrainTaskQueue() {
+bool NativeFakeDispatcher::DrainTaskQueue() {
+ bool task_ran = false;
while (!task_queue_.empty()) {
::pw::async::backend::NativeTask& task = task_queue_.front();
task_queue_.pop_front();
@@ -85,7 +89,10 @@ void NativeFakeDispatcher::DrainTaskQueue() {
PW_LOG_DEBUG("running cancelled task");
Context ctx{&dispatcher_, &task.task_};
task(ctx, Status::Cancelled());
+
+ task_ran = true;
}
+ return task_ran;
}
void NativeFakeDispatcher::Post(Task& task) { PostAt(task, now()); }
@@ -101,19 +108,6 @@ void NativeFakeDispatcher::PostAt(Task& task,
PostTaskInternal(task.native_type(), time);
}
-void NativeFakeDispatcher::PostPeriodic(
- Task& task, chrono::SystemClock::duration interval) {
- PostPeriodicAt(task, interval, now());
-}
-
-void NativeFakeDispatcher::PostPeriodicAt(
- Task& task,
- chrono::SystemClock::duration interval,
- chrono::SystemClock::time_point start_time) {
- task.native_type().set_interval(interval);
- PostAt(task, start_time);
-}
-
bool NativeFakeDispatcher::Cancel(Task& task) {
return task_queue_.remove(task.native_type());
}
@@ -121,10 +115,19 @@ bool NativeFakeDispatcher::Cancel(Task& task) {
void NativeFakeDispatcher::PostTaskInternal(
::pw::async::backend::NativeTask& task,
chrono::SystemClock::time_point time_due) {
+ if (!task.unlisted()) {
+ if (task.due_time() <= time_due) {
+ // No need to repost a task that was already queued to run.
+ return;
+ }
+ // The task needs its time updated, so we have to move it to
+ // a different part of the list.
+ task.unlist();
+ }
task.set_due_time(time_due);
auto it_front = task_queue_.begin();
auto it_behind = task_queue_.before_begin();
- while (it_front != task_queue_.end() && time_due > it_front->due_time()) {
+ while (it_front != task_queue_.end() && time_due >= it_front->due_time()) {
++it_front;
++it_behind;
}