blob: d3bc66a6e8078089c2259900fecd1e8f3c6c81d9 [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_process_thread.h"
22#include "test/time_controller/simulated_task_queue.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010023#include "test/time_controller/simulated_thread.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010024
25namespace webrtc {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010026namespace {
27// Helper function to remove from a std container by value.
28template <class C>
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010029bool RemoveByValue(C* vec, typename C::value_type val) {
30 auto it = std::find(vec->begin(), vec->end(), val);
31 if (it == vec->end())
Sebastian Jansson7b6add32019-03-29 10:34:26 +010032 return false;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010033 vec->erase(it);
Sebastian Jansson7b6add32019-03-29 10:34:26 +010034 return true;
35}
36} // namespace
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010037
38namespace sim_time_impl {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010039
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010040SimulatedTimeControllerImpl::SimulatedTimeControllerImpl(Timestamp start_time)
41 : thread_id_(rtc::CurrentThreadId()), current_time_(start_time) {}
42
43SimulatedTimeControllerImpl::~SimulatedTimeControllerImpl() = default;
44
45std::unique_ptr<TaskQueueBase, TaskQueueDeleter>
46SimulatedTimeControllerImpl::CreateTaskQueue(
47 absl::string_view name,
48 TaskQueueFactory::Priority priority) const {
49 // TODO(srte): Remove the const cast when the interface is made mutable.
50 auto mutable_this = const_cast<SimulatedTimeControllerImpl*>(this);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010051 auto task_queue = std::unique_ptr<SimulatedTaskQueue, TaskQueueDeleter>(
52 new SimulatedTaskQueue(mutable_this, name));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010053 ;
54 mutable_this->Register(task_queue.get());
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010055 return task_queue;
56}
57
58std::unique_ptr<ProcessThread> SimulatedTimeControllerImpl::CreateProcessThread(
59 const char* thread_name) {
60 rtc::CritScope lock(&lock_);
61 auto process_thread =
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010062 std::make_unique<SimulatedProcessThread>(this, thread_name);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010063 Register(process_thread.get());
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010064 return process_thread;
65}
66
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010067std::unique_ptr<rtc::Thread> SimulatedTimeControllerImpl::CreateThread(
68 const std::string& name,
69 std::unique_ptr<rtc::SocketServer> socket_server) {
70 auto thread =
71 std::make_unique<SimulatedThread>(this, name, std::move(socket_server));
72 Register(thread.get());
73 return thread;
74}
75
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010076void SimulatedTimeControllerImpl::YieldExecution() {
77 if (rtc::CurrentThreadId() == thread_id_) {
Sebastian Jansson7b6add32019-03-29 10:34:26 +010078 TaskQueueBase* yielding_from = TaskQueueBase::Current();
79 // Since we might continue execution on a process thread, we should reset
80 // the thread local task queue reference. This ensures that thread checkers
81 // won't think we are executing on the yielding task queue. It also ensure
82 // that TaskQueueBase::Current() won't return the yielding task queue.
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010083 TokenTaskQueue::CurrentTaskQueueSetter reset_queue(nullptr);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010084 // When we yield, we don't want to risk executing further tasks on the
85 // currently executing task queue. If there's a ready task that also yields,
86 // it's added to this set as well and only tasks on the remaining task
87 // queues are executed.
Sebastian Jansson7b6add32019-03-29 10:34:26 +010088 auto inserted = yielded_.insert(yielding_from);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010089 RTC_DCHECK(inserted.second);
90 RunReadyRunners();
91 yielded_.erase(inserted.first);
92 }
93}
94
95void SimulatedTimeControllerImpl::RunReadyRunners() {
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010096 // Using a dummy thread rather than nullptr to avoid implicit thread creation
97 // by Thread::Current().
98 SimulatedThread::CurrentThreadSetter set_current(dummy_thread_.get());
Sebastian Jansson1b408232019-04-12 15:39:38 +020099 rtc::CritScope lock(&lock_);
Sebastian Jansson7b6add32019-03-29 10:34:26 +0100100 RTC_DCHECK_EQ(rtc::CurrentThreadId(), thread_id_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100101 Timestamp current_time = CurrentTime();
Sebastian Jansson1b408232019-04-12 15:39:38 +0200102 // Clearing |ready_runners_| in case this is a recursive call:
103 // RunReadyRunners -> Run -> Event::Wait -> Yield ->RunReadyRunners
104 ready_runners_.clear();
105
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100106 // We repeat until we have no ready left to handle tasks posted by ready
107 // runners.
108 while (true) {
Sebastian Jansson76540812019-04-11 17:48:30 +0200109 for (auto* runner : runners_) {
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100110 if (yielded_.find(runner->GetAsTaskQueue()) == yielded_.end() &&
Sebastian Jansson76540812019-04-11 17:48:30 +0200111 runner->GetNextRunTime() <= current_time) {
112 ready_runners_.push_back(runner);
113 }
114 }
115 if (ready_runners_.empty())
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100116 break;
Sebastian Jansson76540812019-04-11 17:48:30 +0200117 while (!ready_runners_.empty()) {
118 auto* runner = ready_runners_.front();
119 ready_runners_.pop_front();
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100120 // Note that the RunReady function might indirectly cause a call to
Sebastian Jansson76540812019-04-11 17:48:30 +0200121 // Unregister() which will recursively grab |lock_| again to remove items
122 // from |ready_runners_|.
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100123 runner->RunReady(current_time);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100124 }
125 }
126}
127
128Timestamp SimulatedTimeControllerImpl::CurrentTime() const {
129 rtc::CritScope lock(&time_lock_);
130 return current_time_;
131}
132
133Timestamp SimulatedTimeControllerImpl::NextRunTime() const {
134 Timestamp current_time = CurrentTime();
135 Timestamp next_time = Timestamp::PlusInfinity();
136 rtc::CritScope lock(&lock_);
137 for (auto* runner : runners_) {
138 Timestamp next_run_time = runner->GetNextRunTime();
139 if (next_run_time <= current_time)
140 return current_time;
141 next_time = std::min(next_time, next_run_time);
142 }
143 return next_time;
144}
145
146void SimulatedTimeControllerImpl::AdvanceTime(Timestamp target_time) {
147 rtc::CritScope time_lock(&time_lock_);
Sebastian Jansson5a000162019-04-12 11:21:32 +0200148 RTC_DCHECK_GE(target_time, current_time_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100149 current_time_ = target_time;
150}
151
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100152void SimulatedTimeControllerImpl::Register(SimulatedSequenceRunner* runner) {
153 rtc::CritScope lock(&lock_);
154 runners_.push_back(runner);
155}
156
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100157void SimulatedTimeControllerImpl::Unregister(SimulatedSequenceRunner* runner) {
158 rtc::CritScope lock(&lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100159 bool removed = RemoveByValue(&runners_, runner);
Sebastian Jansson7b6add32019-03-29 10:34:26 +0100160 RTC_CHECK(removed);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100161 RemoveByValue(&ready_runners_, runner);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100162}
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100163} // namespace sim_time_impl
164
165GlobalSimulatedTimeController::GlobalSimulatedTimeController(
166 Timestamp start_time)
Sebastian Jansson340af972019-12-04 10:07:48 +0100167 : sim_clock_(start_time.us()), impl_(start_time), yield_policy_(&impl_) {
Sebastian Janssond624c392019-04-17 10:36:03 +0200168 global_clock_.SetTime(start_time);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100169 auto main_thread = std::make_unique<SimulatedMainThread>(&impl_);
170 impl_.Register(main_thread.get());
171 main_thread_ = std::move(main_thread);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100172}
173
174GlobalSimulatedTimeController::~GlobalSimulatedTimeController() = default;
175
176Clock* GlobalSimulatedTimeController::GetClock() {
177 return &sim_clock_;
178}
179
180TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() {
181 return &impl_;
182}
183
184std::unique_ptr<ProcessThread>
185GlobalSimulatedTimeController::CreateProcessThread(const char* thread_name) {
186 return impl_.CreateProcessThread(thread_name);
187}
188
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100189std::unique_ptr<rtc::Thread> GlobalSimulatedTimeController::CreateThread(
190 const std::string& name,
191 std::unique_ptr<rtc::SocketServer> socket_server) {
192 return impl_.CreateThread(name, std::move(socket_server));
193}
194
195rtc::Thread* GlobalSimulatedTimeController::GetMainThread() {
196 return main_thread_.get();
197}
198
Markus Handell486cc552019-12-03 14:37:28 +0100199void GlobalSimulatedTimeController::AdvanceTime(TimeDelta duration) {
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100200 rtc::ScopedYieldPolicy yield_policy(&impl_);
201 Timestamp current_time = impl_.CurrentTime();
202 Timestamp target_time = current_time + duration;
203 RTC_DCHECK_EQ(current_time.us(), rtc::TimeMicros());
204 while (current_time < target_time) {
205 impl_.RunReadyRunners();
206 Timestamp next_time = std::min(impl_.NextRunTime(), target_time);
207 impl_.AdvanceTime(next_time);
208 auto delta = next_time - current_time;
209 current_time = next_time;
210 sim_clock_.AdvanceTimeMicroseconds(delta.us());
Sebastian Janssond624c392019-04-17 10:36:03 +0200211 global_clock_.AdvanceTime(delta);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100212 }
philipeld5727482020-01-03 14:43:10 +0100213 // After time has been simulated up until |target_time| we also need to run
214 // tasks meant to be executed at |target_time|.
215 impl_.RunReadyRunners();
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100216}
217
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100218} // namespace webrtc