Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 1 | /* |
| 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 Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 15 | #include "rtc_base/synchronization/mutex.h" |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 16 | #include "test/time_controller/simulated_time_controller.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class SimulatedThread : public rtc::Thread, |
| 21 | public sim_time_impl::SimulatedSequenceRunner { |
| 22 | public: |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 23 | using CurrentThreadSetter = CurrentThreadSetter; |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 24 | 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 Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 32 | MutexLock lock(&lock_); |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 33 | return next_run_time_; |
| 34 | } |
| 35 | |
| 36 | TaskQueueBase* GetAsTaskQueue() override { return this; } |
| 37 | |
| 38 | // Thread interface |
Danil Chapovalov | 7c323ad | 2022-09-08 13:13:53 +0200 | [diff] [blame] | 39 | void BlockingCall(rtc::FunctionView<void()> functor) override; |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 40 | 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 Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 62 | mutable Mutex lock_; |
Sebastian Jansson | fc8279d | 2020-01-16 11:45:59 +0100 | [diff] [blame] | 63 | Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); |
| 64 | }; |
| 65 | |
| 66 | class 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_ |