Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [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 11:45:59 +0100 | [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 16:57:07 +0100 | [diff] [blame] | 24 | RTC_DCHECK_NOTREACHED(); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 25 | return nullptr; |
| 26 | } |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 27 | bool Wait(int cms, bool process_io) override { |
| 28 | RTC_CHECK_EQ(cms, 0); |
| 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 17:34:37 +0200 | [diff] [blame] | 56 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 57 | if (delay_ms == kForever) { |
| 58 | next_run_time_ = Timestamp::PlusInfinity(); |
| 59 | } else { |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 60 | next_run_time_ = at_time + TimeDelta::Millis(delay_ms); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | void SimulatedThread::Send(const rtc::Location& posted_from, |
| 65 | rtc::MessageHandler* phandler, |
| 66 | uint32_t id, |
| 67 | rtc::MessageData* pdata) { |
| 68 | if (IsQuitting()) |
| 69 | return; |
| 70 | rtc::Message msg; |
| 71 | msg.posted_from = posted_from; |
| 72 | msg.phandler = phandler; |
| 73 | msg.message_id = id; |
| 74 | msg.pdata = pdata; |
| 75 | if (IsCurrent()) { |
| 76 | msg.phandler->OnMessage(&msg); |
| 77 | } else { |
Sebastian Jansson | 274cc7f | 2020-01-17 13:58:54 +0100 | [diff] [blame] | 78 | TaskQueueBase* yielding_from = TaskQueueBase::Current(); |
| 79 | handler_->StartYield(yielding_from); |
Artem Titov | 7ade659 | 2020-07-24 21:32:38 +0200 | [diff] [blame] | 80 | RunReady(Timestamp::MinusInfinity()); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 81 | CurrentThreadSetter set_current(this); |
| 82 | msg.phandler->OnMessage(&msg); |
Sebastian Jansson | 274cc7f | 2020-01-17 13:58:54 +0100 | [diff] [blame] | 83 | handler_->StopYield(yielding_from); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void SimulatedThread::Post(const rtc::Location& posted_from, |
| 88 | rtc::MessageHandler* phandler, |
| 89 | uint32_t id, |
| 90 | rtc::MessageData* pdata, |
| 91 | bool time_sensitive) { |
| 92 | rtc::Thread::Post(posted_from, phandler, id, pdata, time_sensitive); |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 93 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 94 | next_run_time_ = Timestamp::MinusInfinity(); |
| 95 | } |
| 96 | |
| 97 | void SimulatedThread::PostDelayed(const rtc::Location& posted_from, |
| 98 | int delay_ms, |
| 99 | rtc::MessageHandler* phandler, |
| 100 | uint32_t id, |
| 101 | rtc::MessageData* pdata) { |
| 102 | rtc::Thread::PostDelayed(posted_from, delay_ms, phandler, id, pdata); |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 103 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 104 | next_run_time_ = |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 105 | std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis() + delay_ms)); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void SimulatedThread::PostAt(const rtc::Location& posted_from, |
| 109 | int64_t target_time_ms, |
| 110 | rtc::MessageHandler* phandler, |
| 111 | uint32_t id, |
| 112 | rtc::MessageData* pdata) { |
| 113 | rtc::Thread::PostAt(posted_from, target_time_ms, phandler, id, pdata); |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 114 | MutexLock lock(&lock_); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 115 | next_run_time_ = std::min(next_run_time_, Timestamp::Millis(target_time_ms)); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void SimulatedThread::Stop() { |
| 119 | Thread::Quit(); |
| 120 | } |
| 121 | |
| 122 | SimulatedMainThread::SimulatedMainThread( |
| 123 | sim_time_impl::SimulatedTimeControllerImpl* handler) |
| 124 | : SimulatedThread(handler, "main", nullptr), current_setter_(this) {} |
| 125 | |
| 126 | SimulatedMainThread::~SimulatedMainThread() { |
| 127 | // Removes pending tasks in case they keep shared pointer references to |
| 128 | // objects whose destructor expects to run before the Thread destructor. |
| 129 | Stop(); |
| 130 | DoDestroy(); |
| 131 | } |
| 132 | |
| 133 | } // namespace webrtc |