blob: fd3969670a4f2ad12020adf93e6f11ee911affcb [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:
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010022 using CurrentThreadSetter = CurrentThreadSetter;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010023 SimulatedThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
24 absl::string_view name,
25 std::unique_ptr<rtc::SocketServer> socket_server);
26 ~SimulatedThread() override;
27
28 void RunReady(Timestamp at_time) override;
29
30 Timestamp GetNextRunTime() const override {
31 rtc::CritScope lock(&lock_);
32 return next_run_time_;
33 }
34
35 TaskQueueBase* GetAsTaskQueue() override { return this; }
36
37 // Thread interface
38 void Send(const rtc::Location& posted_from,
39 rtc::MessageHandler* phandler,
40 uint32_t id,
41 rtc::MessageData* pdata) override;
42 void Post(const rtc::Location& posted_from,
43 rtc::MessageHandler* phandler,
44 uint32_t id,
45 rtc::MessageData* pdata,
46 bool time_sensitive) override;
47 void PostDelayed(const rtc::Location& posted_from,
48 int delay_ms,
49 rtc::MessageHandler* phandler,
50 uint32_t id,
51 rtc::MessageData* pdata) override;
52 void PostAt(const rtc::Location& posted_from,
53 int64_t target_time_ms,
54 rtc::MessageHandler* phandler,
55 uint32_t id,
56 rtc::MessageData* pdata) override;
57
58 void Stop() override;
59
60 private:
61 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
62 // Using char* to be debugger friendly.
63 char* name_;
64 rtc::CriticalSection lock_;
65 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
66};
67
68class SimulatedMainThread : public SimulatedThread {
69 public:
70 explicit SimulatedMainThread(
71 sim_time_impl::SimulatedTimeControllerImpl* handler);
72 ~SimulatedMainThread();
73
74 private:
75 CurrentThreadSetter current_setter_;
76};
77} // namespace webrtc
78#endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_