blob: 266b76fb549a39ebbe7bf2c9859157b1ed82df48 [file] [log] [blame]
Sebastian Janssonfc8279d2020-01-16 11:45:59 +01001/*
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 Janssonfc8279d2020-01-16 11:45:59 +010015namespace webrtc {
16namespace {
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.
21class DummySocketServer : public rtc::SocketServer {
22 public:
23 rtc::Socket* CreateSocket(int family, int type) override {
Artem Titovd3251962021-11-15 16:57:07 +010024 RTC_DCHECK_NOTREACHED();
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010025 return nullptr;
26 }
Markus Handell9a21c492022-08-25 11:40:13 +000027 bool Wait(TimeDelta max_wait_duration, bool process_io) override {
28 RTC_CHECK(max_wait_duration.IsZero());
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010029 return true;
30 }
31 void WakeUp() override {}
32};
33
34} // namespace
35
36SimulatedThread::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
47SimulatedThread::~SimulatedThread() {
48 handler_->Unregister(this);
49 delete[] name_;
50}
51
52void SimulatedThread::RunReady(Timestamp at_time) {
53 CurrentThreadSetter set_current(this);
54 ProcessMessages(0);
55 int delay_ms = GetDelay();
Markus Handelle56976d2020-07-08 17:34:37 +020056 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010057 if (delay_ms == kForever) {
58 next_run_time_ = Timestamp::PlusInfinity();
59 } else {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010060 next_run_time_ = at_time + TimeDelta::Millis(delay_ms);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010061 }
62}
63
Danil Chapovalov7c323ad2022-09-08 13:13:53 +020064void SimulatedThread::BlockingCall(rtc::FunctionView<void()> functor) {
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010065 if (IsQuitting())
66 return;
Danil Chapovalov7c323ad2022-09-08 13:13:53 +020067
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010068 if (IsCurrent()) {
Danil Chapovalov7c323ad2022-09-08 13:13:53 +020069 functor();
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010070 } else {
Sebastian Jansson274cc7f2020-01-17 13:58:54 +010071 TaskQueueBase* yielding_from = TaskQueueBase::Current();
72 handler_->StartYield(yielding_from);
Artem Titov7ade6592020-07-24 21:32:38 +020073 RunReady(Timestamp::MinusInfinity());
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010074 CurrentThreadSetter set_current(this);
Danil Chapovalov7c323ad2022-09-08 13:13:53 +020075 functor();
Sebastian Jansson274cc7f2020-01-17 13:58:54 +010076 handler_->StopYield(yielding_from);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010077 }
78}
79
80void SimulatedThread::Post(const rtc::Location& posted_from,
81 rtc::MessageHandler* phandler,
82 uint32_t id,
83 rtc::MessageData* pdata,
84 bool time_sensitive) {
85 rtc::Thread::Post(posted_from, phandler, id, pdata, time_sensitive);
Markus Handelle56976d2020-07-08 17:34:37 +020086 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010087 next_run_time_ = Timestamp::MinusInfinity();
88}
89
90void SimulatedThread::PostDelayed(const rtc::Location& posted_from,
91 int delay_ms,
92 rtc::MessageHandler* phandler,
93 uint32_t id,
94 rtc::MessageData* pdata) {
95 rtc::Thread::PostDelayed(posted_from, delay_ms, phandler, id, pdata);
Markus Handelle56976d2020-07-08 17:34:37 +020096 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010097 next_run_time_ =
Danil Chapovalov0c626af2020-02-10 11:16:00 +010098 std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis() + delay_ms));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010099}
100
101void SimulatedThread::PostAt(const rtc::Location& posted_from,
102 int64_t target_time_ms,
103 rtc::MessageHandler* phandler,
104 uint32_t id,
105 rtc::MessageData* pdata) {
106 rtc::Thread::PostAt(posted_from, target_time_ms, phandler, id, pdata);
Markus Handelle56976d2020-07-08 17:34:37 +0200107 MutexLock lock(&lock_);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100108 next_run_time_ = std::min(next_run_time_, Timestamp::Millis(target_time_ms));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100109}
110
111void SimulatedThread::Stop() {
112 Thread::Quit();
113}
114
115SimulatedMainThread::SimulatedMainThread(
116 sim_time_impl::SimulatedTimeControllerImpl* handler)
117 : SimulatedThread(handler, "main", nullptr), current_setter_(this) {}
118
119SimulatedMainThread::~SimulatedMainThread() {
120 // Removes pending tasks in case they keep shared pointer references to
121 // objects whose destructor expects to run before the Thread destructor.
122 Stop();
123 DoDestroy();
124}
125
126} // namespace webrtc