xref: /aosp_15_r20/external/webrtc/test/time_controller/simulated_time_controller.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/simulated_time_controller.h"
11 
12 #include <algorithm>
13 #include <deque>
14 #include <list>
15 #include <memory>
16 #include <string>
17 #include <thread>
18 #include <vector>
19 
20 #include "absl/strings/string_view.h"
21 #include "test/time_controller/simulated_task_queue.h"
22 #include "test/time_controller/simulated_thread.h"
23 
24 namespace webrtc {
25 namespace {
26 // Helper function to remove from a std container by value.
27 template <class C>
RemoveByValue(C * vec,typename C::value_type val)28 bool RemoveByValue(C* vec, typename C::value_type val) {
29   auto it = std::find(vec->begin(), vec->end(), val);
30   if (it == vec->end())
31     return false;
32   vec->erase(it);
33   return true;
34 }
35 }  // namespace
36 
37 namespace sim_time_impl {
38 
SimulatedTimeControllerImpl(Timestamp start_time)39 SimulatedTimeControllerImpl::SimulatedTimeControllerImpl(Timestamp start_time)
40     : thread_id_(rtc::CurrentThreadId()), current_time_(start_time) {}
41 
42 SimulatedTimeControllerImpl::~SimulatedTimeControllerImpl() = default;
43 
44 std::unique_ptr<TaskQueueBase, TaskQueueDeleter>
CreateTaskQueue(absl::string_view name,TaskQueueFactory::Priority priority) const45 SimulatedTimeControllerImpl::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);
50   auto task_queue = std::unique_ptr<SimulatedTaskQueue, TaskQueueDeleter>(
51       new SimulatedTaskQueue(mutable_this, name));
52   mutable_this->Register(task_queue.get());
53   return task_queue;
54 }
55 
CreateThread(const std::string & name,std::unique_ptr<rtc::SocketServer> socket_server)56 std::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 
YieldExecution()65 void SimulatedTimeControllerImpl::YieldExecution() {
66   if (rtc::CurrentThreadId() == thread_id_) {
67     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.
72     TokenTaskQueue::CurrentTaskQueueSetter reset_queue(nullptr);
73     // 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.
77     auto inserted = yielded_.insert(yielding_from);
78     RTC_DCHECK(inserted.second);
79     RunReadyRunners();
80     yielded_.erase(inserted.first);
81   }
82 }
83 
RunReadyRunners()84 void SimulatedTimeControllerImpl::RunReadyRunners() {
85   // Using a dummy thread rather than nullptr to avoid implicit thread creation
86   // by Thread::Current().
87   SimulatedThread::CurrentThreadSetter set_current(dummy_thread_.get());
88   MutexLock lock(&lock_);
89   RTC_DCHECK_EQ(rtc::CurrentThreadId(), thread_id_);
90   Timestamp current_time = CurrentTime();
91   // Clearing `ready_runners_` in case this is a recursive call:
92   // RunReadyRunners -> Run -> Event::Wait -> Yield ->RunReadyRunners
93   ready_runners_.clear();
94 
95   // We repeat until we have no ready left to handle tasks posted by ready
96   // runners.
97   while (true) {
98     for (auto* runner : runners_) {
99       if (yielded_.find(runner->GetAsTaskQueue()) == yielded_.end() &&
100           runner->GetNextRunTime() <= current_time) {
101         ready_runners_.push_back(runner);
102       }
103     }
104     if (ready_runners_.empty())
105       break;
106     while (!ready_runners_.empty()) {
107       auto* runner = ready_runners_.front();
108       ready_runners_.pop_front();
109       lock_.Unlock();
110       // Note that the RunReady function might indirectly cause a call to
111       // Unregister() which will grab `lock_` again to remove items from
112       // `ready_runners_`.
113       runner->RunReady(current_time);
114       lock_.Lock();
115     }
116   }
117 }
118 
CurrentTime() const119 Timestamp SimulatedTimeControllerImpl::CurrentTime() const {
120   MutexLock lock(&time_lock_);
121   return current_time_;
122 }
123 
NextRunTime() const124 Timestamp SimulatedTimeControllerImpl::NextRunTime() const {
125   Timestamp current_time = CurrentTime();
126   Timestamp next_time = Timestamp::PlusInfinity();
127   MutexLock lock(&lock_);
128   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 
AdvanceTime(Timestamp target_time)137 void SimulatedTimeControllerImpl::AdvanceTime(Timestamp target_time) {
138   MutexLock time_lock(&time_lock_);
139   RTC_DCHECK_GE(target_time, current_time_);
140   current_time_ = target_time;
141 }
142 
Register(SimulatedSequenceRunner * runner)143 void SimulatedTimeControllerImpl::Register(SimulatedSequenceRunner* runner) {
144   MutexLock lock(&lock_);
145   runners_.push_back(runner);
146 }
147 
Unregister(SimulatedSequenceRunner * runner)148 void SimulatedTimeControllerImpl::Unregister(SimulatedSequenceRunner* runner) {
149   MutexLock lock(&lock_);
150   bool removed = RemoveByValue(&runners_, runner);
151   RTC_CHECK(removed);
152   RemoveByValue(&ready_runners_, runner);
153 }
154 
StartYield(TaskQueueBase * yielding_from)155 void SimulatedTimeControllerImpl::StartYield(TaskQueueBase* yielding_from) {
156   auto inserted = yielded_.insert(yielding_from);
157   RTC_DCHECK(inserted.second);
158 }
159 
StopYield(TaskQueueBase * yielding_from)160 void SimulatedTimeControllerImpl::StopYield(TaskQueueBase* yielding_from) {
161   yielded_.erase(yielding_from);
162 }
163 
164 }  // namespace sim_time_impl
165 
GlobalSimulatedTimeController(Timestamp start_time)166 GlobalSimulatedTimeController::GlobalSimulatedTimeController(
167     Timestamp start_time)
168     : sim_clock_(start_time.us()), impl_(start_time), yield_policy_(&impl_) {
169   global_clock_.SetTime(start_time);
170   auto main_thread = std::make_unique<SimulatedMainThread>(&impl_);
171   impl_.Register(main_thread.get());
172   main_thread_ = std::move(main_thread);
173 }
174 
175 GlobalSimulatedTimeController::~GlobalSimulatedTimeController() = default;
176 
GetClock()177 Clock* GlobalSimulatedTimeController::GetClock() {
178   return &sim_clock_;
179 }
180 
GetTaskQueueFactory()181 TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() {
182   return &impl_;
183 }
184 
CreateThread(const std::string & name,std::unique_ptr<rtc::SocketServer> socket_server)185 std::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 
GetMainThread()191 rtc::Thread* GlobalSimulatedTimeController::GetMainThread() {
192   return main_thread_.get();
193 }
194 
AdvanceTime(TimeDelta duration)195 void GlobalSimulatedTimeController::AdvanceTime(TimeDelta duration) {
196   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());
207     global_clock_.AdvanceTime(delta);
208   }
209   // After time has been simulated up until `target_time` we also need to run
210   // tasks meant to be executed at `target_time`.
211   impl_.RunReadyRunners();
212 }
213 
Register(sim_time_impl::SimulatedSequenceRunner * runner)214 void GlobalSimulatedTimeController::Register(
215     sim_time_impl::SimulatedSequenceRunner* runner) {
216   impl_.Register(runner);
217 }
218 
Unregister(sim_time_impl::SimulatedSequenceRunner * runner)219 void GlobalSimulatedTimeController::Unregister(
220     sim_time_impl::SimulatedSequenceRunner* runner) {
221   impl_.Unregister(runner);
222 }
223 
224 }  // namespace webrtc
225