Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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_thread.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <utility> |
| 14 | |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | namespace { |
| 17 | |
| 18 | // A socket server that does nothing. It's different from NullSocketServer in |
| 19 | // that it does allow sleep/wakeup. This avoids usage of an Event instance which |
| 20 | // otherwise would cause issues with the simulated Yeild behavior. |
| 21 | class DummySocketServer : public rtc::SocketServer { |
| 22 | public: |
| 23 | rtc::Socket* CreateSocket(int family, int type) override { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 24 | RTC_DCHECK_NOTREACHED(); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 25 | return nullptr; |
| 26 | } |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 27 | bool Wait(TimeDelta max_wait_duration, bool process_io) override { |
| 28 | RTC_CHECK(max_wait_duration.IsZero()); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 29 | return true; |
| 30 | } |
| 31 | void WakeUp() override {} |
| 32 | }; |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | SimulatedThread::SimulatedThread( |
| 37 | sim_time_impl::SimulatedTimeControllerImpl* handler, |
| 38 | absl::string_view name, |
| 39 | std::unique_ptr<rtc::SocketServer> socket_server) |
| 40 | : rtc::Thread(socket_server ? std::move(socket_server) |
| 41 | : std::make_unique<DummySocketServer>()), |
| 42 | handler_(handler), |
| 43 | name_(new char[name.size()]) { |
| 44 | std::copy_n(name.begin(), name.size(), name_); |
| 45 | } |
| 46 | |
| 47 | SimulatedThread::~SimulatedThread() { |
| 48 | handler_->Unregister(this); |
| 49 | delete[] name_; |
| 50 | } |
| 51 | |
| 52 | void SimulatedThread::RunReady(Timestamp at_time) { |
| 53 | CurrentThreadSetter set_current(this); |
| 54 | ProcessMessages(0); |
| 55 | int delay_ms = GetDelay(); |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 56 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 57 | if (delay_ms == kForever) { |
| 58 | next_run_time_ = Timestamp::PlusInfinity(); |
| 59 | } else { |
Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 60 | next_run_time_ = at_time + TimeDelta::Millis(delay_ms); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Markus Handell | 8cb31cf | 2023-03-01 13:55:50 | [diff] [blame] | 64 | void SimulatedThread::BlockingCallImpl(rtc::FunctionView<void()> functor, |
| 65 | const Location& /*location*/) { |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 66 | if (IsQuitting()) |
| 67 | return; |
Danil Chapovalov | 7c323ad | 2022-09-08 11:13:53 | [diff] [blame] | 68 | |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 69 | if (IsCurrent()) { |
Danil Chapovalov | 7c323ad | 2022-09-08 11:13:53 | [diff] [blame] | 70 | functor(); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 71 | } else { |
Sebastian Jansson | 274cc7f | 2020-01-17 12:58:54 | [diff] [blame] | 72 | TaskQueueBase* yielding_from = TaskQueueBase::Current(); |
| 73 | handler_->StartYield(yielding_from); |
Artem Titov | 7ade659 | 2020-07-24 19:32:38 | [diff] [blame] | 74 | RunReady(Timestamp::MinusInfinity()); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 75 | CurrentThreadSetter set_current(this); |
Danil Chapovalov | 7c323ad | 2022-09-08 11:13:53 | [diff] [blame] | 76 | functor(); |
Sebastian Jansson | 274cc7f | 2020-01-17 12:58:54 | [diff] [blame] | 77 | handler_->StopYield(yielding_from); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Markus Handell | a1ceae2 | 2023-03-01 09:13:28 | [diff] [blame] | 81 | void SimulatedThread::PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 82 | const PostTaskTraits& traits, |
| 83 | const Location& location) { |
| 84 | rtc::Thread::PostTaskImpl(std::move(task), traits, location); |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 85 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 86 | next_run_time_ = Timestamp::MinusInfinity(); |
| 87 | } |
| 88 | |
Markus Handell | a1ceae2 | 2023-03-01 09:13:28 | [diff] [blame] | 89 | void SimulatedThread::PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 90 | TimeDelta delay, |
| 91 | const PostDelayedTaskTraits& traits, |
| 92 | const Location& location) { |
| 93 | rtc::Thread::PostDelayedTaskImpl(std::move(task), delay, traits, location); |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 94 | MutexLock lock(&lock_); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 95 | next_run_time_ = |
| 96 | std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis()) + delay); |
Sebastian Jansson | fc8279d | 2020-01-16 10:45:59 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void SimulatedThread::Stop() { |
| 100 | Thread::Quit(); |
| 101 | } |
| 102 | |
| 103 | SimulatedMainThread::SimulatedMainThread( |
| 104 | sim_time_impl::SimulatedTimeControllerImpl* handler) |
| 105 | : SimulatedThread(handler, "main", nullptr), current_setter_(this) {} |
| 106 | |
| 107 | SimulatedMainThread::~SimulatedMainThread() { |
| 108 | // Removes pending tasks in case they keep shared pointer references to |
| 109 | // objects whose destructor expects to run before the Thread destructor. |
| 110 | Stop(); |
| 111 | DoDestroy(); |
| 112 | } |
| 113 | |
| 114 | } // namespace webrtc |