blob: bbaafd75bdcea05044501e441e4a4e23714de28f [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#ifndef TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_
11#define TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_
12
13#include <memory>
14
15#include "test/time_controller/simulated_time_controller.h"
16
17namespace webrtc {
18
19class SimulatedThread : public rtc::Thread,
20 public sim_time_impl::SimulatedSequenceRunner {
21 public:
22 class CurrentThreadSetter : CurrentTaskQueueSetter {
23 public:
24 explicit CurrentThreadSetter(Thread* thread)
25 : CurrentTaskQueueSetter(thread),
26 manager_(rtc::ThreadManager::Instance()),
27 previous_(manager_->CurrentThread()) {
28 manager_->ChangeCurrentThreadForTest(thread);
29 }
30 ~CurrentThreadSetter() { manager_->ChangeCurrentThreadForTest(previous_); }
31
32 private:
33 rtc::ThreadManager* const manager_;
34 rtc::Thread* const previous_;
35 };
36 SimulatedThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
37 absl::string_view name,
38 std::unique_ptr<rtc::SocketServer> socket_server);
39 ~SimulatedThread() override;
40
41 void RunReady(Timestamp at_time) override;
42
43 Timestamp GetNextRunTime() const override {
44 rtc::CritScope lock(&lock_);
45 return next_run_time_;
46 }
47
48 TaskQueueBase* GetAsTaskQueue() override { return this; }
49
50 // Thread interface
51 void Send(const rtc::Location& posted_from,
52 rtc::MessageHandler* phandler,
53 uint32_t id,
54 rtc::MessageData* pdata) override;
55 void Post(const rtc::Location& posted_from,
56 rtc::MessageHandler* phandler,
57 uint32_t id,
58 rtc::MessageData* pdata,
59 bool time_sensitive) override;
60 void PostDelayed(const rtc::Location& posted_from,
61 int delay_ms,
62 rtc::MessageHandler* phandler,
63 uint32_t id,
64 rtc::MessageData* pdata) override;
65 void PostAt(const rtc::Location& posted_from,
66 int64_t target_time_ms,
67 rtc::MessageHandler* phandler,
68 uint32_t id,
69 rtc::MessageData* pdata) override;
70
71 void Stop() override;
72
73 private:
74 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
75 // Using char* to be debugger friendly.
76 char* name_;
77 rtc::CriticalSection lock_;
78 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
79};
80
81class SimulatedMainThread : public SimulatedThread {
82 public:
83 explicit SimulatedMainThread(
84 sim_time_impl::SimulatedTimeControllerImpl* handler);
85 ~SimulatedMainThread();
86
87 private:
88 CurrentThreadSetter current_setter_;
89};
90} // namespace webrtc
91#endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_