simulated_time_controller.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2019 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_TIME_CONTROLLER_H_
  11. #define TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_
  12. #include <list>
  13. #include <memory>
  14. #include <unordered_set>
  15. #include <utility>
  16. #include <vector>
  17. #include "absl/strings/string_view.h"
  18. #include "api/test/time_controller.h"
  19. #include "api/units/timestamp.h"
  20. #include "modules/include/module.h"
  21. #include "modules/utility/include/process_thread.h"
  22. #include "rtc_base/fake_clock.h"
  23. #include "rtc_base/platform_thread_types.h"
  24. #include "rtc_base/synchronization/mutex.h"
  25. #include "rtc_base/synchronization/yield_policy.h"
  26. #include "rtc_base/thread_checker.h"
  27. namespace webrtc {
  28. namespace sim_time_impl {
  29. class SimulatedSequenceRunner {
  30. public:
  31. virtual ~SimulatedSequenceRunner() = default;
  32. // Provides next run time.
  33. virtual Timestamp GetNextRunTime() const = 0;
  34. // Runs all ready tasks and modules and updates next run time.
  35. virtual void RunReady(Timestamp at_time) = 0;
  36. // All implementations also implements TaskQueueBase in some form, but if we'd
  37. // inherit from it in this interface we'd run into issues with double
  38. // inheritance. Therefore we simply allow the implementations to provide a
  39. // casted pointer to themself.
  40. virtual TaskQueueBase* GetAsTaskQueue() = 0;
  41. };
  42. class SimulatedTimeControllerImpl : public TaskQueueFactory,
  43. public rtc::YieldInterface {
  44. public:
  45. explicit SimulatedTimeControllerImpl(Timestamp start_time);
  46. ~SimulatedTimeControllerImpl() override;
  47. std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
  48. absl::string_view name,
  49. Priority priority) const RTC_LOCKS_EXCLUDED(time_lock_) override;
  50. // Implements the YieldInterface by running ready tasks on all task queues,
  51. // except that if this method is called from a task, the task queue running
  52. // that task is skipped.
  53. void YieldExecution() RTC_LOCKS_EXCLUDED(time_lock_, lock_) override;
  54. // Create process thread with the name |thread_name|.
  55. std::unique_ptr<ProcessThread> CreateProcessThread(const char* thread_name)
  56. RTC_LOCKS_EXCLUDED(time_lock_, lock_);
  57. // Create thread using provided |socket_server|.
  58. std::unique_ptr<rtc::Thread> CreateThread(
  59. const std::string& name,
  60. std::unique_ptr<rtc::SocketServer> socket_server)
  61. RTC_LOCKS_EXCLUDED(time_lock_, lock_);
  62. // Runs all runners in |runners_| that has tasks or modules ready for
  63. // execution.
  64. void RunReadyRunners() RTC_LOCKS_EXCLUDED(time_lock_, lock_);
  65. // Return |current_time_|.
  66. Timestamp CurrentTime() const RTC_LOCKS_EXCLUDED(time_lock_);
  67. // Return min of runner->GetNextRunTime() for runner in |runners_|.
  68. Timestamp NextRunTime() const RTC_LOCKS_EXCLUDED(lock_);
  69. // Set |current_time_| to |target_time|.
  70. void AdvanceTime(Timestamp target_time) RTC_LOCKS_EXCLUDED(time_lock_);
  71. // Adds |runner| to |runners_|.
  72. void Register(SimulatedSequenceRunner* runner) RTC_LOCKS_EXCLUDED(lock_);
  73. // Removes |runner| from |runners_|.
  74. void Unregister(SimulatedSequenceRunner* runner) RTC_LOCKS_EXCLUDED(lock_);
  75. // Indicates that |yielding_from| is not ready to run.
  76. void StartYield(TaskQueueBase* yielding_from);
  77. // Indicates that processing can be continued on |yielding_from|.
  78. void StopYield(TaskQueueBase* yielding_from);
  79. private:
  80. const rtc::PlatformThreadId thread_id_;
  81. const std::unique_ptr<rtc::Thread> dummy_thread_ = rtc::Thread::Create();
  82. mutable Mutex time_lock_;
  83. Timestamp current_time_ RTC_GUARDED_BY(time_lock_);
  84. mutable Mutex lock_;
  85. std::vector<SimulatedSequenceRunner*> runners_ RTC_GUARDED_BY(lock_);
  86. // Used in RunReadyRunners() to keep track of ready runners that are to be
  87. // processed in a round robin fashion. the reason it's a member is so that
  88. // runners can removed from here by Unregister().
  89. std::list<SimulatedSequenceRunner*> ready_runners_ RTC_GUARDED_BY(lock_);
  90. // Runners on which YieldExecution has been called.
  91. std::unordered_set<TaskQueueBase*> yielded_;
  92. };
  93. } // namespace sim_time_impl
  94. // Used to satisfy sequence checkers for non task queue sequences.
  95. class TokenTaskQueue : public TaskQueueBase {
  96. public:
  97. // Promoted to public
  98. using CurrentTaskQueueSetter = TaskQueueBase::CurrentTaskQueueSetter;
  99. void Delete() override { RTC_NOTREACHED(); }
  100. void PostTask(std::unique_ptr<QueuedTask> /*task*/) override {
  101. RTC_NOTREACHED();
  102. }
  103. void PostDelayedTask(std::unique_ptr<QueuedTask> /*task*/,
  104. uint32_t /*milliseconds*/) override {
  105. RTC_NOTREACHED();
  106. }
  107. };
  108. // TimeController implementation using completely simulated time. Task queues
  109. // and process threads created by this controller will run delayed activities
  110. // when AdvanceTime() is called. Overrides the global clock backing
  111. // rtc::TimeMillis() and rtc::TimeMicros(). Note that this is not thread safe
  112. // since it modifies global state.
  113. class GlobalSimulatedTimeController : public TimeController {
  114. public:
  115. explicit GlobalSimulatedTimeController(Timestamp start_time);
  116. ~GlobalSimulatedTimeController() override;
  117. Clock* GetClock() override;
  118. TaskQueueFactory* GetTaskQueueFactory() override;
  119. std::unique_ptr<ProcessThread> CreateProcessThread(
  120. const char* thread_name) override;
  121. std::unique_ptr<rtc::Thread> CreateThread(
  122. const std::string& name,
  123. std::unique_ptr<rtc::SocketServer> socket_server) override;
  124. rtc::Thread* GetMainThread() override;
  125. void AdvanceTime(TimeDelta duration) override;
  126. private:
  127. rtc::ScopedBaseFakeClock global_clock_;
  128. // Provides simulated CurrentNtpInMilliseconds()
  129. SimulatedClock sim_clock_;
  130. sim_time_impl::SimulatedTimeControllerImpl impl_;
  131. rtc::ScopedYieldPolicy yield_policy_;
  132. std::unique_ptr<rtc::Thread> main_thread_;
  133. };
  134. } // namespace webrtc
  135. #endif // TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_