Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [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/simulated_time_controller.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <deque> |
| 14 | #include <list> |
| 15 | #include <map> |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <thread> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "absl/memory/memory.h" |
| 21 | #include "absl/strings/string_view.h" |
| 22 | |
| 23 | namespace webrtc { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 24 | namespace { |
| 25 | // Helper function to remove from a std container by value. |
| 26 | template <class C> |
| 27 | bool RemoveByValue(C& vec, typename C::value_type val) { |
| 28 | auto it = std::find(vec.begin(), vec.end(), val); |
| 29 | if (it == vec.end()) |
| 30 | return false; |
| 31 | vec.erase(it); |
| 32 | return true; |
| 33 | } |
| 34 | } // namespace |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 35 | |
| 36 | namespace sim_time_impl { |
| 37 | class SimulatedSequenceRunner : public ProcessThread, public TaskQueueBase { |
| 38 | public: |
| 39 | SimulatedSequenceRunner(SimulatedTimeControllerImpl* handler, |
| 40 | absl::string_view queue_name) |
| 41 | : handler_(handler), name_(queue_name) {} |
| 42 | ~SimulatedSequenceRunner() override { handler_->Unregister(this); } |
| 43 | |
| 44 | // Provides next run time. |
| 45 | Timestamp GetNextRunTime() const; |
| 46 | |
| 47 | // Iterates through delayed tasks and modules and moves them to the ready set |
| 48 | // if they are supposed to execute by |at time|. |
| 49 | void UpdateReady(Timestamp at_time); |
| 50 | // Runs all ready tasks and modules and updates next run time. |
| 51 | void Run(Timestamp at_time); |
| 52 | |
| 53 | // TaskQueueBase interface |
| 54 | void Delete() override; |
| 55 | // Note: PostTask is also in ProcessThread interface. |
| 56 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
| 57 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 58 | uint32_t milliseconds) override; |
| 59 | |
| 60 | // ProcessThread interface |
| 61 | void Start() override; |
| 62 | void Stop() override; |
| 63 | void WakeUp(Module* module) override; |
| 64 | void RegisterModule(Module* module, const rtc::Location& from) override; |
| 65 | void DeRegisterModule(Module* module) override; |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 66 | // Promoted to public for use in SimulatedTimeControllerImpl::YieldExecution. |
| 67 | using CurrentTaskQueueSetter = TaskQueueBase::CurrentTaskQueueSetter; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | Timestamp GetCurrentTime() const { return handler_->CurrentTime(); } |
| 71 | void RunReadyTasks(Timestamp at_time) RTC_LOCKS_EXCLUDED(lock_); |
| 72 | void RunReadyModules(Timestamp at_time) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 73 | void UpdateNextRunTime() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 74 | Timestamp GetNextTime(Module* module, Timestamp at_time); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 75 | |
| 76 | SimulatedTimeControllerImpl* const handler_; |
| 77 | const std::string name_; |
| 78 | |
| 79 | rtc::CriticalSection lock_; |
| 80 | |
| 81 | std::deque<std::unique_ptr<QueuedTask>> ready_tasks_ RTC_GUARDED_BY(lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 82 | std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_ |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 83 | RTC_GUARDED_BY(lock_); |
| 84 | |
| 85 | bool process_thread_running_ RTC_GUARDED_BY(lock_) = false; |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 86 | std::vector<Module*> stopped_modules_ RTC_GUARDED_BY(lock_); |
| 87 | std::vector<Module*> ready_modules_ RTC_GUARDED_BY(lock_); |
| 88 | std::map<Timestamp, std::list<Module*>> delayed_modules_ |
| 89 | RTC_GUARDED_BY(lock_); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 90 | |
| 91 | Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); |
| 92 | }; |
| 93 | |
| 94 | Timestamp SimulatedSequenceRunner::GetNextRunTime() const { |
| 95 | rtc::CritScope lock(&lock_); |
| 96 | return next_run_time_; |
| 97 | } |
| 98 | |
| 99 | void SimulatedSequenceRunner::UpdateReady(Timestamp at_time) { |
| 100 | rtc::CritScope lock(&lock_); |
| 101 | for (auto it = delayed_tasks_.begin(); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 102 | it != delayed_tasks_.end() && it->first <= at_time; |
| 103 | it = delayed_tasks_.erase(it)) { |
| 104 | for (auto& task : it->second) { |
| 105 | ready_tasks_.emplace_back(std::move(task)); |
| 106 | } |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 107 | } |
| 108 | for (auto it = delayed_modules_.begin(); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 109 | it != delayed_modules_.end() && it->first <= at_time; |
| 110 | it = delayed_modules_.erase(it)) { |
| 111 | for (auto module : it->second) { |
| 112 | ready_modules_.push_back(module); |
| 113 | } |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void SimulatedSequenceRunner::Run(Timestamp at_time) { |
| 118 | RunReadyTasks(at_time); |
| 119 | rtc::CritScope lock(&lock_); |
| 120 | RunReadyModules(at_time); |
| 121 | UpdateNextRunTime(); |
| 122 | } |
| 123 | |
| 124 | void SimulatedSequenceRunner::Delete() { |
| 125 | { |
| 126 | rtc::CritScope lock(&lock_); |
| 127 | ready_tasks_.clear(); |
| 128 | delayed_tasks_.clear(); |
| 129 | } |
| 130 | delete this; |
| 131 | } |
| 132 | |
| 133 | void SimulatedSequenceRunner::RunReadyTasks(Timestamp at_time) { |
| 134 | std::deque<std::unique_ptr<QueuedTask>> ready_tasks; |
| 135 | { |
| 136 | rtc::CritScope lock(&lock_); |
| 137 | ready_tasks.swap(ready_tasks_); |
| 138 | } |
| 139 | if (!ready_tasks.empty()) { |
| 140 | CurrentTaskQueueSetter set_current(this); |
| 141 | for (auto& ready : ready_tasks) { |
| 142 | bool delete_task = ready->Run(); |
| 143 | if (delete_task) { |
| 144 | ready.reset(); |
| 145 | } else { |
| 146 | ready.release(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void SimulatedSequenceRunner::RunReadyModules(Timestamp at_time) { |
| 153 | if (!ready_modules_.empty()) { |
| 154 | CurrentTaskQueueSetter set_current(this); |
| 155 | for (auto* module : ready_modules_) { |
| 156 | module->Process(); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 157 | delayed_modules_[GetNextTime(module, at_time)].push_back(module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | ready_modules_.clear(); |
| 161 | } |
| 162 | |
| 163 | void SimulatedSequenceRunner::UpdateNextRunTime() { |
| 164 | if (!ready_tasks_.empty() || !ready_modules_.empty()) { |
| 165 | next_run_time_ = Timestamp::MinusInfinity(); |
| 166 | } else { |
| 167 | next_run_time_ = Timestamp::PlusInfinity(); |
| 168 | if (!delayed_tasks_.empty()) |
| 169 | next_run_time_ = std::min(next_run_time_, delayed_tasks_.begin()->first); |
| 170 | if (!delayed_modules_.empty()) |
| 171 | next_run_time_ = |
| 172 | std::min(next_run_time_, delayed_modules_.begin()->first); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void SimulatedSequenceRunner::PostTask(std::unique_ptr<QueuedTask> task) { |
| 177 | rtc::CritScope lock(&lock_); |
| 178 | ready_tasks_.emplace_back(std::move(task)); |
| 179 | next_run_time_ = Timestamp::MinusInfinity(); |
| 180 | } |
| 181 | |
| 182 | void SimulatedSequenceRunner::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 183 | uint32_t milliseconds) { |
| 184 | rtc::CritScope lock(&lock_); |
| 185 | Timestamp target_time = GetCurrentTime() + TimeDelta::ms(milliseconds); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 186 | delayed_tasks_[target_time].push_back(std::move(task)); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 187 | next_run_time_ = std::min(next_run_time_, target_time); |
| 188 | } |
| 189 | |
| 190 | void SimulatedSequenceRunner::Start() { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 191 | std::vector<Module*> starting; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 192 | { |
| 193 | rtc::CritScope lock(&lock_); |
| 194 | if (process_thread_running_) |
| 195 | return; |
| 196 | process_thread_running_ = true; |
| 197 | starting.swap(stopped_modules_); |
| 198 | } |
| 199 | for (auto& module : starting) |
| 200 | module->ProcessThreadAttached(this); |
| 201 | |
| 202 | Timestamp at_time = GetCurrentTime(); |
| 203 | rtc::CritScope lock(&lock_); |
| 204 | for (auto& module : starting) |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 205 | delayed_modules_[GetNextTime(module, at_time)].push_back(module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 206 | UpdateNextRunTime(); |
| 207 | } |
| 208 | |
| 209 | void SimulatedSequenceRunner::Stop() { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 210 | std::vector<Module*> stopping; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 211 | { |
| 212 | rtc::CritScope lock(&lock_); |
| 213 | process_thread_running_ = false; |
| 214 | |
| 215 | for (auto* ready : ready_modules_) |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 216 | stopped_modules_.push_back(ready); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 217 | ready_modules_.clear(); |
| 218 | |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 219 | for (auto& delayed : delayed_modules_) { |
| 220 | for (auto mod : delayed.second) |
| 221 | stopped_modules_.push_back(mod); |
| 222 | } |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 223 | delayed_modules_.clear(); |
| 224 | |
| 225 | stopping = stopped_modules_; |
| 226 | } |
| 227 | for (auto& module : stopping) |
| 228 | module->ProcessThreadAttached(nullptr); |
| 229 | } |
| 230 | |
| 231 | void SimulatedSequenceRunner::WakeUp(Module* module) { |
| 232 | rtc::CritScope lock(&lock_); |
| 233 | // If we already are planning to run this module as soon as possible, we don't |
| 234 | // need to do anything. |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 235 | for (auto mod : ready_modules_) |
| 236 | if (mod == module) |
| 237 | return; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 238 | |
| 239 | for (auto it = delayed_modules_.begin(); it != delayed_modules_.end(); ++it) { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 240 | if (RemoveByValue(it->second, module)) |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 241 | break; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 242 | } |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 243 | Timestamp next_time = GetNextTime(module, GetCurrentTime()); |
| 244 | delayed_modules_[next_time].push_back(module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 245 | next_run_time_ = std::min(next_run_time_, next_time); |
| 246 | } |
| 247 | |
| 248 | void SimulatedSequenceRunner::RegisterModule(Module* module, |
| 249 | const rtc::Location& from) { |
| 250 | module->ProcessThreadAttached(this); |
| 251 | rtc::CritScope lock(&lock_); |
| 252 | if (!process_thread_running_) { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 253 | stopped_modules_.push_back(module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 254 | } else { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 255 | Timestamp next_time = GetNextTime(module, GetCurrentTime()); |
| 256 | delayed_modules_[next_time].push_back(module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 257 | next_run_time_ = std::min(next_run_time_, next_time); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void SimulatedSequenceRunner::DeRegisterModule(Module* module) { |
| 262 | bool modules_running; |
| 263 | { |
| 264 | rtc::CritScope lock(&lock_); |
| 265 | if (!process_thread_running_) { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 266 | RemoveByValue(stopped_modules_, module); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 267 | } else { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 268 | bool removed = RemoveByValue(ready_modules_, module); |
| 269 | if (!removed) { |
| 270 | for (auto& pair : delayed_modules_) { |
| 271 | if (RemoveByValue(pair.second, module)) |
| 272 | break; |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | modules_running = process_thread_running_; |
| 277 | } |
| 278 | if (modules_running) |
| 279 | module->ProcessThreadAttached(nullptr); |
| 280 | } |
| 281 | |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 282 | Timestamp SimulatedSequenceRunner::GetNextTime(Module* module, |
| 283 | Timestamp at_time) { |
| 284 | CurrentTaskQueueSetter set_current(this); |
| 285 | return at_time + TimeDelta::ms(module->TimeUntilNextProcess()); |
| 286 | } |
| 287 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 288 | SimulatedTimeControllerImpl::SimulatedTimeControllerImpl(Timestamp start_time) |
| 289 | : thread_id_(rtc::CurrentThreadId()), current_time_(start_time) {} |
| 290 | |
| 291 | SimulatedTimeControllerImpl::~SimulatedTimeControllerImpl() = default; |
| 292 | |
| 293 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> |
| 294 | SimulatedTimeControllerImpl::CreateTaskQueue( |
| 295 | absl::string_view name, |
| 296 | TaskQueueFactory::Priority priority) const { |
| 297 | // TODO(srte): Remove the const cast when the interface is made mutable. |
| 298 | auto mutable_this = const_cast<SimulatedTimeControllerImpl*>(this); |
| 299 | auto task_queue = std::unique_ptr<SimulatedSequenceRunner, TaskQueueDeleter>( |
| 300 | new SimulatedSequenceRunner(mutable_this, name)); |
| 301 | rtc::CritScope lock(&mutable_this->lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 302 | mutable_this->runners_.push_back(task_queue.get()); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 303 | return task_queue; |
| 304 | } |
| 305 | |
| 306 | std::unique_ptr<ProcessThread> SimulatedTimeControllerImpl::CreateProcessThread( |
| 307 | const char* thread_name) { |
| 308 | rtc::CritScope lock(&lock_); |
| 309 | auto process_thread = |
| 310 | absl::make_unique<SimulatedSequenceRunner>(this, thread_name); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 311 | runners_.push_back(process_thread.get()); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 312 | return process_thread; |
| 313 | } |
| 314 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 315 | |
| 316 | void SimulatedTimeControllerImpl::YieldExecution() { |
| 317 | if (rtc::CurrentThreadId() == thread_id_) { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 318 | TaskQueueBase* yielding_from = TaskQueueBase::Current(); |
| 319 | // Since we might continue execution on a process thread, we should reset |
| 320 | // the thread local task queue reference. This ensures that thread checkers |
| 321 | // won't think we are executing on the yielding task queue. It also ensure |
| 322 | // that TaskQueueBase::Current() won't return the yielding task queue. |
| 323 | SimulatedSequenceRunner::CurrentTaskQueueSetter reset_queue(nullptr); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 324 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 325 | // When we yield, we don't want to risk executing further tasks on the |
| 326 | // currently executing task queue. If there's a ready task that also yields, |
| 327 | // it's added to this set as well and only tasks on the remaining task |
| 328 | // queues are executed. |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 329 | auto inserted = yielded_.insert(yielding_from); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 330 | RTC_DCHECK(inserted.second); |
| 331 | RunReadyRunners(); |
| 332 | yielded_.erase(inserted.first); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void SimulatedTimeControllerImpl::RunReadyRunners() { |
| 337 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Sebastian Jansson | 1b40823 | 2019-04-12 15:39:38 +0200 | [diff] [blame] | 338 | rtc::CritScope lock(&lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 339 | RTC_DCHECK_EQ(rtc::CurrentThreadId(), thread_id_); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 340 | Timestamp current_time = CurrentTime(); |
Sebastian Jansson | 1b40823 | 2019-04-12 15:39:38 +0200 | [diff] [blame] | 341 | // Clearing |ready_runners_| in case this is a recursive call: |
| 342 | // RunReadyRunners -> Run -> Event::Wait -> Yield ->RunReadyRunners |
| 343 | ready_runners_.clear(); |
| 344 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 345 | // We repeat until we have no ready left to handle tasks posted by ready |
| 346 | // runners. |
| 347 | while (true) { |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 348 | for (auto* runner : runners_) { |
| 349 | if (yielded_.find(runner) == yielded_.end() && |
| 350 | runner->GetNextRunTime() <= current_time) { |
| 351 | ready_runners_.push_back(runner); |
| 352 | } |
| 353 | } |
| 354 | if (ready_runners_.empty()) |
| 355 | return; |
| 356 | while (!ready_runners_.empty()) { |
| 357 | auto* runner = ready_runners_.front(); |
| 358 | ready_runners_.pop_front(); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 359 | runner->UpdateReady(current_time); |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 360 | // Note that the Run function might indirectly cause a call to |
| 361 | // Unregister() which will recursively grab |lock_| again to remove items |
| 362 | // from |ready_runners_|. |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 363 | runner->Run(current_time); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | Timestamp SimulatedTimeControllerImpl::CurrentTime() const { |
| 369 | rtc::CritScope lock(&time_lock_); |
| 370 | return current_time_; |
| 371 | } |
| 372 | |
| 373 | Timestamp SimulatedTimeControllerImpl::NextRunTime() const { |
| 374 | Timestamp current_time = CurrentTime(); |
| 375 | Timestamp next_time = Timestamp::PlusInfinity(); |
| 376 | rtc::CritScope lock(&lock_); |
| 377 | for (auto* runner : runners_) { |
| 378 | Timestamp next_run_time = runner->GetNextRunTime(); |
| 379 | if (next_run_time <= current_time) |
| 380 | return current_time; |
| 381 | next_time = std::min(next_time, next_run_time); |
| 382 | } |
| 383 | return next_time; |
| 384 | } |
| 385 | |
| 386 | void SimulatedTimeControllerImpl::AdvanceTime(Timestamp target_time) { |
| 387 | rtc::CritScope time_lock(&time_lock_); |
Sebastian Jansson | 5a00016 | 2019-04-12 11:21:32 +0200 | [diff] [blame] | 388 | RTC_DCHECK_GE(target_time, current_time_); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 389 | current_time_ = target_time; |
| 390 | } |
| 391 | |
| 392 | void SimulatedTimeControllerImpl::Unregister(SimulatedSequenceRunner* runner) { |
| 393 | rtc::CritScope lock(&lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 394 | bool removed = RemoveByValue(runners_, runner); |
| 395 | RTC_CHECK(removed); |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 396 | RemoveByValue(ready_runners_, runner); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | } // namespace sim_time_impl |
| 400 | |
| 401 | GlobalSimulatedTimeController::GlobalSimulatedTimeController( |
| 402 | Timestamp start_time) |
| 403 | : sim_clock_(start_time.us()), impl_(start_time) { |
| 404 | global_clock_.SetTimeMicros(start_time.us()); |
| 405 | } |
| 406 | |
| 407 | GlobalSimulatedTimeController::~GlobalSimulatedTimeController() = default; |
| 408 | |
| 409 | Clock* GlobalSimulatedTimeController::GetClock() { |
| 410 | return &sim_clock_; |
| 411 | } |
| 412 | |
| 413 | TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() { |
| 414 | return &impl_; |
| 415 | } |
| 416 | |
| 417 | std::unique_ptr<ProcessThread> |
| 418 | GlobalSimulatedTimeController::CreateProcessThread(const char* thread_name) { |
| 419 | return impl_.CreateProcessThread(thread_name); |
| 420 | } |
| 421 | |
| 422 | void GlobalSimulatedTimeController::Sleep(TimeDelta duration) { |
| 423 | rtc::ScopedYieldPolicy yield_policy(&impl_); |
| 424 | Timestamp current_time = impl_.CurrentTime(); |
| 425 | Timestamp target_time = current_time + duration; |
| 426 | RTC_DCHECK_EQ(current_time.us(), rtc::TimeMicros()); |
| 427 | while (current_time < target_time) { |
| 428 | impl_.RunReadyRunners(); |
| 429 | Timestamp next_time = std::min(impl_.NextRunTime(), target_time); |
| 430 | impl_.AdvanceTime(next_time); |
| 431 | auto delta = next_time - current_time; |
| 432 | current_time = next_time; |
| 433 | sim_clock_.AdvanceTimeMicroseconds(delta.us()); |
| 434 | global_clock_.AdvanceTimeMicros(delta.us()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void GlobalSimulatedTimeController::InvokeWithControlledYield( |
| 439 | std::function<void()> closure) { |
| 440 | rtc::ScopedYieldPolicy yield_policy(&impl_); |
| 441 | closure(); |
| 442 | } |
| 443 | |
| 444 | // namespace sim_time_impl |
| 445 | |
| 446 | } // namespace webrtc |