simulated_thread.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <memory>
  13. #include "rtc_base/synchronization/mutex.h"
  14. #include "test/time_controller/simulated_time_controller.h"
  15. namespace webrtc {
  16. class SimulatedThread : public rtc::Thread,
  17. public sim_time_impl::SimulatedSequenceRunner {
  18. public:
  19. using CurrentThreadSetter = CurrentThreadSetter;
  20. SimulatedThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
  21. absl::string_view name,
  22. std::unique_ptr<rtc::SocketServer> socket_server);
  23. ~SimulatedThread() override;
  24. void RunReady(Timestamp at_time) override;
  25. Timestamp GetNextRunTime() const override {
  26. MutexLock lock(&lock_);
  27. return next_run_time_;
  28. }
  29. TaskQueueBase* GetAsTaskQueue() override { return this; }
  30. // Thread interface
  31. void Send(const rtc::Location& posted_from,
  32. rtc::MessageHandler* phandler,
  33. uint32_t id,
  34. rtc::MessageData* pdata) override;
  35. void Post(const rtc::Location& posted_from,
  36. rtc::MessageHandler* phandler,
  37. uint32_t id,
  38. rtc::MessageData* pdata,
  39. bool time_sensitive) override;
  40. void PostDelayed(const rtc::Location& posted_from,
  41. int delay_ms,
  42. rtc::MessageHandler* phandler,
  43. uint32_t id,
  44. rtc::MessageData* pdata) override;
  45. void PostAt(const rtc::Location& posted_from,
  46. int64_t target_time_ms,
  47. rtc::MessageHandler* phandler,
  48. uint32_t id,
  49. rtc::MessageData* pdata) override;
  50. void Stop() override;
  51. private:
  52. sim_time_impl::SimulatedTimeControllerImpl* const handler_;
  53. // Using char* to be debugger friendly.
  54. char* name_;
  55. mutable Mutex lock_;
  56. Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
  57. };
  58. class SimulatedMainThread : public SimulatedThread {
  59. public:
  60. explicit SimulatedMainThread(
  61. sim_time_impl::SimulatedTimeControllerImpl* handler);
  62. ~SimulatedMainThread();
  63. private:
  64. CurrentThreadSetter current_setter_;
  65. };
  66. } // namespace webrtc
  67. #endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_