Coverage Report

Created: 2025-08-28 06:26

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