Coverage Report

Created: 2025-11-16 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/pw_async_basic/fake_dispatcher.cc
Line
Count
Source
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
14.9k
    : dispatcher_(dispatcher) {}
26
27
14.9k
NativeFakeDispatcher::~NativeFakeDispatcher() {
28
14.9k
  RequestStop();
29
14.9k
  DrainTaskQueue();
30
14.9k
}
31
32
7.55M
bool NativeFakeDispatcher::RunUntilIdle() {
33
7.55M
  bool tasks_ran = ExecuteDueTasks();
34
7.55M
  if (stop_requested_) {
35
0
    tasks_ran |= DrainTaskQueue();
36
0
  }
37
7.55M
  return tasks_ran;
38
7.55M
}
39
40
5.37M
bool NativeFakeDispatcher::RunUntil(chrono::SystemClock::time_point end_time) {
41
5.37M
  bool tasks_ran = false;
42
12.6M
  while (!task_queue_.empty() && task_queue_.front().due_time() <= end_time &&
43
7.31M
         !stop_requested_) {
44
7.31M
    now_ = task_queue_.front().due_time();
45
7.31M
    tasks_ran |= ExecuteDueTasks();
46
7.31M
  }
47
48
5.37M
  if (stop_requested_) {
49
0
    tasks_ran |= DrainTaskQueue();
50
0
    return tasks_ran;
51
0
  }
52
53
5.37M
  if (now_ < end_time) {
54
1.35M
    now_ = end_time;
55
1.35M
  }
56
5.37M
  return tasks_ran;
57
5.37M
}
58
59
5.37M
bool NativeFakeDispatcher::RunFor(chrono::SystemClock::duration duration) {
60
5.37M
  return RunUntil(now() + duration);
61
5.37M
}
62
63
14.8M
bool NativeFakeDispatcher::ExecuteDueTasks() {
64
14.8M
  bool task_ran = false;
65
23.4M
  while (!task_queue_.empty() && task_queue_.front().due_time() <= now() &&
66
8.59M
         !stop_requested_) {
67
8.59M
    ::pw::async::backend::NativeTask& task = task_queue_.front();
68
8.59M
    task_queue_.pop_front();
69
70
8.59M
    Context ctx{&dispatcher_, &task.task_};
71
8.59M
    task(ctx, OkStatus());
72
73
8.59M
    task_ran = true;
74
8.59M
  }
75
14.8M
  return task_ran;
76
14.8M
}
77
78
14.9k
void NativeFakeDispatcher::RequestStop() {
79
14.9k
  PW_LOG_DEBUG("stop requested");
80
14.9k
  stop_requested_ = true;
81
14.9k
}
82
83
14.9k
bool NativeFakeDispatcher::DrainTaskQueue() {
84
14.9k
  bool task_ran = false;
85
14.9k
  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
14.9k
  return task_ran;
96
14.9k
}
97
98
0
void NativeFakeDispatcher::Post(Task& task) { PostAt(task, now()); }
99
100
void NativeFakeDispatcher::PostAfter(Task& task,
101
9.30M
                                     chrono::SystemClock::duration delay) {
102
9.30M
  PostAt(task, now() + delay);
103
9.30M
}
104
105
void NativeFakeDispatcher::PostAt(Task& task,
106
9.90M
                                  chrono::SystemClock::time_point time) {
107
9.90M
  PW_LOG_DEBUG("posting task");
108
9.90M
  PostTaskInternal(task.native_type(), time);
109
9.90M
}
110
111
9.43M
bool NativeFakeDispatcher::Cancel(Task& task) {
112
9.43M
  return task_queue_.remove(task.native_type());
113
9.43M
}
114
115
void NativeFakeDispatcher::PostTaskInternal(
116
    ::pw::async::backend::NativeTask& task,
117
9.90M
    chrono::SystemClock::time_point time_due) {
118
9.90M
  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
9.90M
  task.set_due_time(time_due);
128
9.90M
  auto it_front = task_queue_.begin();
129
9.90M
  auto it_behind = task_queue_.before_begin();
130
337M
  while (it_front != task_queue_.end() && time_due >= it_front->due_time()) {
131
327M
    ++it_front;
132
327M
    ++it_behind;
133
327M
  }
134
9.90M
  task_queue_.insert_after(it_behind, task);
135
9.90M
}
136
137
}  // namespace pw::async::test::backend