blob: 54493285fc9926df89227e19ea1274e9a7e05fb8 [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 }
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010027 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
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
64void 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 Jansson274cc7f2020-01-17 13:58:54 +010078 TaskQueueBase* yielding_from = TaskQueueBase::Current();
79 handler_->StartYield(yielding_from);
Artem Titov7ade6592020-07-24 21:32:38 +020080 RunReady(Timestamp::MinusInfinity());
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010081 CurrentThreadSetter set_current(this);
82 msg.phandler->OnMessage(&msg);
Sebastian Jansson274cc7f2020-01-17 13:58:54 +010083 handler_->StopYield(yielding_from);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010084 }
85}
86
87void 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 Handelle56976d2020-07-08 17:34:37 +020093 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010094 next_run_time_ = Timestamp::MinusInfinity();
95}
96
97void 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 Handelle56976d2020-07-08 17:34:37 +0200103 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100104 next_run_time_ =
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100105 std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis() + delay_ms));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100106}
107
108void 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 Handelle56976d2020-07-08 17:34:37 +0200114 MutexLock lock(&lock_);
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100115 next_run_time_ = std::min(next_run_time_, Timestamp::Millis(target_time_ms));
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100116}
117
118void SimulatedThread::Stop() {
119 Thread::Quit();
120}
121
122SimulatedMainThread::SimulatedMainThread(
123 sim_time_impl::SimulatedTimeControllerImpl* handler)
124 : SimulatedThread(handler, "main", nullptr), current_setter_(this) {}
125
126SimulatedMainThread::~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