blob: 1ed2b30dc85d4b6500843d6fc5f308727fcddd59 [file] [log] [blame]
Sebastian Jansson0d617cc2019-03-22 15:22:16 +01001/*
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/simulated_time_controller.h"
11
12#include <algorithm>
13#include <deque>
14#include <list>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010016#include <string>
17#include <thread>
18#include <vector>
19
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010020#include "absl/strings/string_view.h"
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010021#include "test/time_controller/simulated_task_queue.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010022#include "test/time_controller/simulated_thread.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010023
24namespace webrtc {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010025namespace {
26// Helper function to remove from a std container by value.
27template <class C>
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010028bool RemoveByValue(C* vec, typename C::value_type val) {
29 auto it = std::find(vec->begin(), vec->end(), val);
30 if (it == vec->end())
Sebastian Jansson7b6add32019-03-29 10:34:26 +010031 return false;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010032 vec->erase(it);
Sebastian Jansson7b6add32019-03-29 10:34:26 +010033 return true;
34}
35} // namespace
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010036
37namespace sim_time_impl {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010038
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010039SimulatedTimeControllerImpl::SimulatedTimeControllerImpl(Timestamp start_time)
40 : thread_id_(rtc::CurrentThreadId()), current_time_(start_time) {}
41
42SimulatedTimeControllerImpl::~SimulatedTimeControllerImpl() = default;
43
44std::unique_ptr<TaskQueueBase, TaskQueueDeleter>
45SimulatedTimeControllerImpl::CreateTaskQueue(
46 absl::string_view name,
47 TaskQueueFactory::Priority priority) const {
48 // TODO(srte): Remove the const cast when the interface is made mutable.
49 auto mutable_this = const_cast<SimulatedTimeControllerImpl*>(this);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010050 auto task_queue = std::unique_ptr<SimulatedTaskQueue, TaskQueueDeleter>(
51 new SimulatedTaskQueue(mutable_this, name));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010052 mutable_this->Register(task_queue.get());
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010053 return task_queue;
54}
55
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010056std::unique_ptr<rtc::Thread> SimulatedTimeControllerImpl::CreateThread(
57 const std::string& name,
58 std::unique_ptr<rtc::SocketServer> socket_server) {
59 auto thread =
60 std::make_unique<SimulatedThread>(this, name, std::move(socket_server));
61 Register(thread.get());
62 return thread;
63}
64
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010065void SimulatedTimeControllerImpl::YieldExecution() {
66 if (rtc::CurrentThreadId() == thread_id_) {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010067 TaskQueueBase* yielding_from = TaskQueueBase::Current();
68 // Since we might continue execution on a process thread, we should reset
69 // the thread local task queue reference. This ensures that thread checkers
70 // won't think we are executing on the yielding task queue. It also ensure
71 // that TaskQueueBase::Current() won't return the yielding task queue.
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010072 TokenTaskQueue::CurrentTaskQueueSetter reset_queue(nullptr);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010073 // When we yield, we don't want to risk executing further tasks on the
74 // currently executing task queue. If there's a ready task that also yields,
75 // it's added to this set as well and only tasks on the remaining task
76 // queues are executed.
Sebastian Jansson7b6add32019-03-29 10:34:26 +010077 auto inserted = yielded_.insert(yielding_from);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010078 RTC_DCHECK(inserted.second);
79 RunReadyRunners();
80 yielded_.erase(inserted.first);
81 }
82}
83
84void SimulatedTimeControllerImpl::RunReadyRunners() {
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010085 // Using a dummy thread rather than nullptr to avoid implicit thread creation
86 // by Thread::Current().
87 SimulatedThread::CurrentThreadSetter set_current(dummy_thread_.get());
Markus Handelle56976d2020-07-08 17:34:37 +020088 MutexLock lock(&lock_);
Sebastian Jansson7b6add32019-03-29 10:34:26 +010089 RTC_DCHECK_EQ(rtc::CurrentThreadId(), thread_id_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010090 Timestamp current_time = CurrentTime();
Artem Titov1ee563d2021-07-27 12:46:29 +020091 // Clearing `ready_runners_` in case this is a recursive call:
Sebastian Jansson1b408232019-04-12 15:39:38 +020092 // RunReadyRunners -> Run -> Event::Wait -> Yield ->RunReadyRunners
93 ready_runners_.clear();
94
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010095 // We repeat until we have no ready left to handle tasks posted by ready
96 // runners.
97 while (true) {
Sebastian Jansson76540812019-04-11 17:48:30 +020098 for (auto* runner : runners_) {
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010099 if (yielded_.find(runner->GetAsTaskQueue()) == yielded_.end() &&
Sebastian Jansson76540812019-04-11 17:48:30 +0200100 runner->GetNextRunTime() <= current_time) {
101 ready_runners_.push_back(runner);
102 }
103 }
104 if (ready_runners_.empty())
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100105 break;
Sebastian Jansson76540812019-04-11 17:48:30 +0200106 while (!ready_runners_.empty()) {
107 auto* runner = ready_runners_.front();
108 ready_runners_.pop_front();
Markus Handelle56976d2020-07-08 17:34:37 +0200109 lock_.Unlock();
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100110 // Note that the RunReady function might indirectly cause a call to
Artem Titov1ee563d2021-07-27 12:46:29 +0200111 // Unregister() which will grab `lock_` again to remove items from
112 // `ready_runners_`.
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100113 runner->RunReady(current_time);
Markus Handelle56976d2020-07-08 17:34:37 +0200114 lock_.Lock();
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100115 }
116 }
117}
118
119Timestamp SimulatedTimeControllerImpl::CurrentTime() const {
Markus Handelle56976d2020-07-08 17:34:37 +0200120 MutexLock lock(&time_lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100121 return current_time_;
122}
123
124Timestamp SimulatedTimeControllerImpl::NextRunTime() const {
125 Timestamp current_time = CurrentTime();
126 Timestamp next_time = Timestamp::PlusInfinity();
Markus Handelle56976d2020-07-08 17:34:37 +0200127 MutexLock lock(&lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100128 for (auto* runner : runners_) {
129 Timestamp next_run_time = runner->GetNextRunTime();
130 if (next_run_time <= current_time)
131 return current_time;
132 next_time = std::min(next_time, next_run_time);
133 }
134 return next_time;
135}
136
137void SimulatedTimeControllerImpl::AdvanceTime(Timestamp target_time) {
Markus Handelle56976d2020-07-08 17:34:37 +0200138 MutexLock time_lock(&time_lock_);
Sebastian Jansson5a000162019-04-12 11:21:32 +0200139 RTC_DCHECK_GE(target_time, current_time_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100140 current_time_ = target_time;
141}
142
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100143void SimulatedTimeControllerImpl::Register(SimulatedSequenceRunner* runner) {
Markus Handelle56976d2020-07-08 17:34:37 +0200144 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100145 runners_.push_back(runner);
146}
147
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100148void SimulatedTimeControllerImpl::Unregister(SimulatedSequenceRunner* runner) {
Markus Handelle56976d2020-07-08 17:34:37 +0200149 MutexLock lock(&lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100150 bool removed = RemoveByValue(&runners_, runner);
Sebastian Jansson7b6add32019-03-29 10:34:26 +0100151 RTC_CHECK(removed);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100152 RemoveByValue(&ready_runners_, runner);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100153}
Sebastian Jansson274cc7f2020-01-17 13:58:54 +0100154
155void SimulatedTimeControllerImpl::StartYield(TaskQueueBase* yielding_from) {
156 auto inserted = yielded_.insert(yielding_from);
157 RTC_DCHECK(inserted.second);
158}
159
160void SimulatedTimeControllerImpl::StopYield(TaskQueueBase* yielding_from) {
161 yielded_.erase(yielding_from);
162}
Markus Handell222598d2020-05-15 18:43:05 +0200163
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100164} // namespace sim_time_impl
165
166GlobalSimulatedTimeController::GlobalSimulatedTimeController(
167 Timestamp start_time)
Sebastian Jansson340af972019-12-04 10:07:48 +0100168 : sim_clock_(start_time.us()), impl_(start_time), yield_policy_(&impl_) {
Sebastian Janssond624c392019-04-17 10:36:03 +0200169 global_clock_.SetTime(start_time);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100170 auto main_thread = std::make_unique<SimulatedMainThread>(&impl_);
171 impl_.Register(main_thread.get());
172 main_thread_ = std::move(main_thread);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100173}
174
175GlobalSimulatedTimeController::~GlobalSimulatedTimeController() = default;
176
177Clock* GlobalSimulatedTimeController::GetClock() {
178 return &sim_clock_;
179}
180
181TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() {
182 return &impl_;
183}
184
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100185std::unique_ptr<rtc::Thread> GlobalSimulatedTimeController::CreateThread(
186 const std::string& name,
187 std::unique_ptr<rtc::SocketServer> socket_server) {
188 return impl_.CreateThread(name, std::move(socket_server));
189}
190
191rtc::Thread* GlobalSimulatedTimeController::GetMainThread() {
192 return main_thread_.get();
193}
194
Markus Handell486cc552019-12-03 14:37:28 +0100195void GlobalSimulatedTimeController::AdvanceTime(TimeDelta duration) {
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100196 rtc::ScopedYieldPolicy yield_policy(&impl_);
197 Timestamp current_time = impl_.CurrentTime();
198 Timestamp target_time = current_time + duration;
199 RTC_DCHECK_EQ(current_time.us(), rtc::TimeMicros());
200 while (current_time < target_time) {
201 impl_.RunReadyRunners();
202 Timestamp next_time = std::min(impl_.NextRunTime(), target_time);
203 impl_.AdvanceTime(next_time);
204 auto delta = next_time - current_time;
205 current_time = next_time;
206 sim_clock_.AdvanceTimeMicroseconds(delta.us());
Sebastian Janssond624c392019-04-17 10:36:03 +0200207 global_clock_.AdvanceTime(delta);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100208 }
Artem Titov1ee563d2021-07-27 12:46:29 +0200209 // After time has been simulated up until `target_time` we also need to run
210 // tasks meant to be executed at `target_time`.
philipeld5727482020-01-03 14:43:10 +0100211 impl_.RunReadyRunners();
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100212}
213
Markus Handell049ed442021-06-18 13:45:50 +0200214void GlobalSimulatedTimeController::Register(
215 sim_time_impl::SimulatedSequenceRunner* runner) {
216 impl_.Register(runner);
217}
218
219void GlobalSimulatedTimeController::Unregister(
220 sim_time_impl::SimulatedSequenceRunner* runner) {
221 impl_.Unregister(runner);
222}
223
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100224} // namespace webrtc