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