blob: 9272e28b565780d4134d58b61050eb201426bfee [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
Markus Handelle56976d2020-07-08 17:34:37 +020015#include "rtc_base/synchronization/mutex.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010016#include "test/time_controller/simulated_time_controller.h"
17
18namespace webrtc {
19
20class SimulatedThread : public rtc::Thread,
21 public sim_time_impl::SimulatedSequenceRunner {
22 public:
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010023 using CurrentThreadSetter = CurrentThreadSetter;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010024 SimulatedThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
25 absl::string_view name,
26 std::unique_ptr<rtc::SocketServer> socket_server);
27 ~SimulatedThread() override;
28
29 void RunReady(Timestamp at_time) override;
30
31 Timestamp GetNextRunTime() const override {
Markus Handelle56976d2020-07-08 17:34:37 +020032 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010033 return next_run_time_;
34 }
35
36 TaskQueueBase* GetAsTaskQueue() override { return this; }
37
38 // Thread interface
Danil Chapovalov7c323ad2022-09-08 13:13:53 +020039 void BlockingCall(rtc::FunctionView<void()> functor) override;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010040 void Post(const rtc::Location& posted_from,
41 rtc::MessageHandler* phandler,
42 uint32_t id,
43 rtc::MessageData* pdata,
44 bool time_sensitive) override;
45 void PostDelayed(const rtc::Location& posted_from,
46 int delay_ms,
47 rtc::MessageHandler* phandler,
48 uint32_t id,
49 rtc::MessageData* pdata) override;
50 void PostAt(const rtc::Location& posted_from,
51 int64_t target_time_ms,
52 rtc::MessageHandler* phandler,
53 uint32_t id,
54 rtc::MessageData* pdata) override;
55
56 void Stop() override;
57
58 private:
59 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
60 // Using char* to be debugger friendly.
61 char* name_;
Markus Handelle56976d2020-07-08 17:34:37 +020062 mutable Mutex lock_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010063 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
64};
65
66class SimulatedMainThread : public SimulatedThread {
67 public:
68 explicit SimulatedMainThread(
69 sim_time_impl::SimulatedTimeControllerImpl* handler);
70 ~SimulatedMainThread();
71
72 private:
73 CurrentThreadSetter current_setter_;
74};
75} // namespace webrtc
76#endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_