1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/time_controller/external_time_controller.h"
#include <algorithm>
#include <map>
#include <memory>
#include <utility>
#include "absl/functional/any_invocable.h"
#include "api/task_queue/task_queue_base.h"
#include "api/task_queue/task_queue_factory.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "rtc_base/checks.h"
#include "rtc_base/synchronization/yield_policy.h"
#include "test/time_controller/simulated_time_controller.h"
namespace webrtc {
// Wraps a TaskQueue so that it can reschedule the time controller whenever
// an external call schedules a new task.
class ExternalTimeController::TaskQueueWrapper : public TaskQueueBase {
public:
TaskQueueWrapper(ExternalTimeController* parent,
std::unique_ptr<TaskQueueBase, TaskQueueDeleter> base)
: parent_(parent), base_(std::move(base)) {}
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) override {
parent_->UpdateTime();
base_->PostTask(TaskWrapper(std::move(task)));
parent_->ScheduleNext();
}
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) override {
parent_->UpdateTime();
if (traits.high_precision) {
base_->PostDelayedHighPrecisionTask(TaskWrapper(std::move(task)), delay);
} else {
base_->PostDelayedTask(TaskWrapper(std::move(task)), delay);
}
parent_->ScheduleNext();
}
void Delete() override { delete this; }
private:
absl::AnyInvocable<void() &&> TaskWrapper(
absl::AnyInvocable<void() &&> task) {
return [task = std::move(task), this]() mutable {
CurrentTaskQueueSetter current(this);
std::move(task)();
};
}
ExternalTimeController* const parent_;
std::unique_ptr<TaskQueueBase, TaskQueueDeleter> base_;
};
ExternalTimeController::ExternalTimeController(ControlledAlarmClock* alarm)
: alarm_(alarm),
impl_(alarm_->GetClock()->CurrentTime()),
yield_policy_(&impl_) {
global_clock_.SetTime(alarm_->GetClock()->CurrentTime());
alarm_->SetCallback([this] { Run(); });
}
Clock* ExternalTimeController::GetClock() {
return alarm_->GetClock();
}
TaskQueueFactory* ExternalTimeController::GetTaskQueueFactory() {
return this;
}
void ExternalTimeController::AdvanceTime(TimeDelta duration) {
alarm_->Sleep(duration);
}
std::unique_ptr<rtc::Thread> ExternalTimeController::CreateThread(
const std::string& name,
std::unique_ptr<rtc::SocketServer> socket_server) {
RTC_DCHECK_NOTREACHED();
return nullptr;
}
rtc::Thread* ExternalTimeController::GetMainThread() {
RTC_DCHECK_NOTREACHED();
return nullptr;
}
std::unique_ptr<TaskQueueBase, TaskQueueDeleter>
ExternalTimeController::CreateTaskQueue(
absl::string_view name,
TaskQueueFactory::Priority priority) const {
return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>(
new TaskQueueWrapper(const_cast<ExternalTimeController*>(this),
impl_.CreateTaskQueue(name, priority)));
}
void ExternalTimeController::Run() {
rtc::ScopedYieldPolicy yield_policy(&impl_);
UpdateTime();
impl_.RunReadyRunners();
ScheduleNext();
}
void ExternalTimeController::UpdateTime() {
Timestamp now = alarm_->GetClock()->CurrentTime();
impl_.AdvanceTime(now);
global_clock_.SetTime(now);
}
void ExternalTimeController::ScheduleNext() {
RTC_DCHECK_EQ(impl_.CurrentTime(), alarm_->GetClock()->CurrentTime());
TimeDelta delay =
std::max(impl_.NextRunTime() - impl_.CurrentTime(), TimeDelta::Zero());
if (delay.IsFinite()) {
alarm_->ScheduleAlarmAt(alarm_->GetClock()->CurrentTime() + delay);
}
}
} // namespace webrtc
|