simulated_thread.h 2.5 KB

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