blob: e8a5a22a71771135382c7b360a6478c4d6ce9c86 [file] [log] [blame]
Sebastian Janssonfc8279d2020-01-16 10:45:591/*
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 10:45:5915namespace 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 15:57:0724 RTC_DCHECK_NOTREACHED();
Sebastian Janssonfc8279d2020-01-16 10:45:5925 return nullptr;
26 }
Markus Handell9a21c492022-08-25 11:40:1327 bool Wait(TimeDelta max_wait_duration, bool process_io) override {
28 RTC_CHECK(max_wait_duration.IsZero());
Sebastian Janssonfc8279d2020-01-16 10:45:5929 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 15:34:3756 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 10:45:5957 if (delay_ms == kForever) {
58 next_run_time_ = Timestamp::PlusInfinity();
59 } else {
Danil Chapovalov0c626af2020-02-10 10:16:0060 next_run_time_ = at_time + TimeDelta::Millis(delay_ms);
Sebastian Janssonfc8279d2020-01-16 10:45:5961 }
62}
63
Markus Handell8cb31cf2023-03-01 13:55:5064void SimulatedThread::BlockingCallImpl(rtc::FunctionView<void()> functor,
65 const Location& /*location*/) {
Sebastian Janssonfc8279d2020-01-16 10:45:5966 if (IsQuitting())
67 return;
Danil Chapovalov7c323ad2022-09-08 11:13:5368
Sebastian Janssonfc8279d2020-01-16 10:45:5969 if (IsCurrent()) {
Danil Chapovalov7c323ad2022-09-08 11:13:5370 functor();
Sebastian Janssonfc8279d2020-01-16 10:45:5971 } else {
Sebastian Jansson274cc7f2020-01-17 12:58:5472 TaskQueueBase* yielding_from = TaskQueueBase::Current();
73 handler_->StartYield(yielding_from);
Artem Titov7ade6592020-07-24 19:32:3874 RunReady(Timestamp::MinusInfinity());
Sebastian Janssonfc8279d2020-01-16 10:45:5975 CurrentThreadSetter set_current(this);
Danil Chapovalov7c323ad2022-09-08 11:13:5376 functor();
Sebastian Jansson274cc7f2020-01-17 12:58:5477 handler_->StopYield(yielding_from);
Sebastian Janssonfc8279d2020-01-16 10:45:5978 }
79}
80
Markus Handella1ceae22023-03-01 09:13:2881void SimulatedThread::PostTaskImpl(absl::AnyInvocable<void() &&> task,
82 const PostTaskTraits& traits,
83 const Location& location) {
84 rtc::Thread::PostTaskImpl(std::move(task), traits, location);
Markus Handelle56976d2020-07-08 15:34:3785 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 10:45:5986 next_run_time_ = Timestamp::MinusInfinity();
87}
88
Markus Handella1ceae22023-03-01 09:13:2889void 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 Handelle56976d2020-07-08 15:34:3794 MutexLock lock(&lock_);
Danil Chapovalovd44e3412022-09-16 15:26:1095 next_run_time_ =
96 std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis()) + delay);
Sebastian Janssonfc8279d2020-01-16 10:45:5997}
98
99void SimulatedThread::Stop() {
100 Thread::Quit();
101}
102
103SimulatedMainThread::SimulatedMainThread(
104 sim_time_impl::SimulatedTimeControllerImpl* handler)
105 : SimulatedThread(handler, "main", nullptr), current_setter_(this) {}
106
107SimulatedMainThread::~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