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 15 #include "rtc_base/synchronization/mutex.h" 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: 23 using CurrentThreadSetter = CurrentThreadSetter; 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 GetNextRunTime()31 Timestamp GetNextRunTime() const override { 32 MutexLock lock(&lock_); 33 return next_run_time_; 34 } 35 GetAsTaskQueue()36 TaskQueueBase* GetAsTaskQueue() override { return this; } 37 38 // Thread interface 39 void Send(const rtc::Location& posted_from, 40 rtc::MessageHandler* phandler, 41 uint32_t id, 42 rtc::MessageData* pdata) override; 43 void Post(const rtc::Location& posted_from, 44 rtc::MessageHandler* phandler, 45 uint32_t id, 46 rtc::MessageData* pdata, 47 bool time_sensitive) override; 48 void PostDelayed(const rtc::Location& posted_from, 49 int delay_ms, 50 rtc::MessageHandler* phandler, 51 uint32_t id, 52 rtc::MessageData* pdata) override; 53 void PostAt(const rtc::Location& posted_from, 54 int64_t target_time_ms, 55 rtc::MessageHandler* phandler, 56 uint32_t id, 57 rtc::MessageData* pdata) override; 58 59 void Stop() override; 60 61 private: 62 sim_time_impl::SimulatedTimeControllerImpl* const handler_; 63 // Using char* to be debugger friendly. 64 char* name_; 65 mutable Mutex lock_; 66 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); 67 }; 68 69 class SimulatedMainThread : public SimulatedThread { 70 public: 71 explicit SimulatedMainThread( 72 sim_time_impl::SimulatedTimeControllerImpl* handler); 73 ~SimulatedMainThread(); 74 75 private: 76 CurrentThreadSetter current_setter_; 77 }; 78 } // namespace webrtc 79 #endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_ 80