blob: 6c6dbfab9da0d19e56c2d68e16aa815d089cdead [file] [log] [blame]
Sebastian Jansson0d617cc2019-03-22 15:22:16 +01001/*
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
Sebastian Jansson7b6add32019-03-29 10:34:26 +010013#include <list>
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010014#include <memory>
15#include <unordered_set>
16#include <utility>
17#include <vector>
18
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010019#include "absl/strings/string_view.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080020#include "api/test/time_controller.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010021#include "api/units/timestamp.h"
22#include "modules/include/module.h"
23#include "modules/utility/include/process_thread.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010024#include "rtc_base/fake_clock.h"
25#include "rtc_base/platform_thread_types.h"
Markus Handelle56976d2020-07-08 17:34:37 +020026#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010027#include "rtc_base/synchronization/yield_policy.h"
28#include "rtc_base/thread_checker.h"
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010029
30namespace webrtc {
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010031namespace sim_time_impl {
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010032class SimulatedSequenceRunner {
33 public:
34 virtual ~SimulatedSequenceRunner() = default;
35 // Provides next run time.
36 virtual Timestamp GetNextRunTime() const = 0;
37 // Runs all ready tasks and modules and updates next run time.
38 virtual void RunReady(Timestamp at_time) = 0;
39
40 // All implementations also implements TaskQueueBase in some form, but if we'd
41 // inherit from it in this interface we'd run into issues with double
42 // inheritance. Therefore we simply allow the implementations to provide a
43 // casted pointer to themself.
44 virtual TaskQueueBase* GetAsTaskQueue() = 0;
45};
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010046
47class SimulatedTimeControllerImpl : public TaskQueueFactory,
48 public rtc::YieldInterface {
49 public:
50 explicit SimulatedTimeControllerImpl(Timestamp start_time);
51 ~SimulatedTimeControllerImpl() override;
52
53 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
54 absl::string_view name,
Markus Handell222598d2020-05-15 18:43:05 +020055 Priority priority) const RTC_LOCKS_EXCLUDED(time_lock_) override;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010056
57 // Implements the YieldInterface by running ready tasks on all task queues,
58 // except that if this method is called from a task, the task queue running
59 // that task is skipped.
Markus Handell222598d2020-05-15 18:43:05 +020060 void YieldExecution() RTC_LOCKS_EXCLUDED(time_lock_, lock_) override;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010061 // Create process thread with the name |thread_name|.
Markus Handell222598d2020-05-15 18:43:05 +020062 std::unique_ptr<ProcessThread> CreateProcessThread(const char* thread_name)
63 RTC_LOCKS_EXCLUDED(time_lock_, lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010064 // Create thread using provided |socket_server|.
65 std::unique_ptr<rtc::Thread> CreateThread(
66 const std::string& name,
Markus Handell222598d2020-05-15 18:43:05 +020067 std::unique_ptr<rtc::SocketServer> socket_server)
68 RTC_LOCKS_EXCLUDED(time_lock_, lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010069
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010070 // Runs all runners in |runners_| that has tasks or modules ready for
71 // execution.
Markus Handell222598d2020-05-15 18:43:05 +020072 void RunReadyRunners() RTC_LOCKS_EXCLUDED(time_lock_, lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010073 // Return |current_time_|.
Markus Handell222598d2020-05-15 18:43:05 +020074 Timestamp CurrentTime() const RTC_LOCKS_EXCLUDED(time_lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010075 // Return min of runner->GetNextRunTime() for runner in |runners_|.
Markus Handell222598d2020-05-15 18:43:05 +020076 Timestamp NextRunTime() const RTC_LOCKS_EXCLUDED(lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010077 // Set |current_time_| to |target_time|.
Markus Handell222598d2020-05-15 18:43:05 +020078 void AdvanceTime(Timestamp target_time) RTC_LOCKS_EXCLUDED(time_lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010079 // Adds |runner| to |runners_|.
Markus Handell222598d2020-05-15 18:43:05 +020080 void Register(SimulatedSequenceRunner* runner) RTC_LOCKS_EXCLUDED(lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010081 // Removes |runner| from |runners_|.
Markus Handell222598d2020-05-15 18:43:05 +020082 void Unregister(SimulatedSequenceRunner* runner) RTC_LOCKS_EXCLUDED(lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010083
Sebastian Jansson274cc7f2020-01-17 13:58:54 +010084 // Indicates that |yielding_from| is not ready to run.
85 void StartYield(TaskQueueBase* yielding_from);
86 // Indicates that processing can be continued on |yielding_from|.
87 void StopYield(TaskQueueBase* yielding_from);
88
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010089 private:
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010090 const rtc::PlatformThreadId thread_id_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010091 const std::unique_ptr<rtc::Thread> dummy_thread_ = rtc::Thread::Create();
Markus Handelle56976d2020-07-08 17:34:37 +020092 mutable Mutex time_lock_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010093 Timestamp current_time_ RTC_GUARDED_BY(time_lock_);
Markus Handelle56976d2020-07-08 17:34:37 +020094 mutable Mutex lock_;
Sebastian Jansson7b6add32019-03-29 10:34:26 +010095 std::vector<SimulatedSequenceRunner*> runners_ RTC_GUARDED_BY(lock_);
Sebastian Jansson76540812019-04-11 17:48:30 +020096 // Used in RunReadyRunners() to keep track of ready runners that are to be
97 // processed in a round robin fashion. the reason it's a member is so that
98 // runners can removed from here by Unregister().
99 std::list<SimulatedSequenceRunner*> ready_runners_ RTC_GUARDED_BY(lock_);
100
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100101 // Runners on which YieldExecution has been called.
102 std::unordered_set<TaskQueueBase*> yielded_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100103};
104} // namespace sim_time_impl
105
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100106// Used to satisfy sequence checkers for non task queue sequences.
107class TokenTaskQueue : public TaskQueueBase {
108 public:
109 // Promoted to public
110 using CurrentTaskQueueSetter = TaskQueueBase::CurrentTaskQueueSetter;
111
112 void Delete() override { RTC_NOTREACHED(); }
113 void PostTask(std::unique_ptr<QueuedTask> /*task*/) override {
114 RTC_NOTREACHED();
115 }
116 void PostDelayedTask(std::unique_ptr<QueuedTask> /*task*/,
117 uint32_t /*milliseconds*/) override {
118 RTC_NOTREACHED();
119 }
120};
121
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100122// TimeController implementation using completely simulated time. Task queues
123// and process threads created by this controller will run delayed activities
Markus Handell486cc552019-12-03 14:37:28 +0100124// when AdvanceTime() is called. Overrides the global clock backing
125// rtc::TimeMillis() and rtc::TimeMicros(). Note that this is not thread safe
126// since it modifies global state.
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100127class GlobalSimulatedTimeController : public TimeController {
128 public:
129 explicit GlobalSimulatedTimeController(Timestamp start_time);
130 ~GlobalSimulatedTimeController() override;
131
132 Clock* GetClock() override;
133 TaskQueueFactory* GetTaskQueueFactory() override;
134 std::unique_ptr<ProcessThread> CreateProcessThread(
135 const char* thread_name) override;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100136 std::unique_ptr<rtc::Thread> CreateThread(
137 const std::string& name,
138 std::unique_ptr<rtc::SocketServer> socket_server) override;
139 rtc::Thread* GetMainThread() override;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100140
Markus Handell486cc552019-12-03 14:37:28 +0100141 void AdvanceTime(TimeDelta duration) override;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100142
143 private:
Sebastian Janssond624c392019-04-17 10:36:03 +0200144 rtc::ScopedBaseFakeClock global_clock_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100145 // Provides simulated CurrentNtpInMilliseconds()
146 SimulatedClock sim_clock_;
147 sim_time_impl::SimulatedTimeControllerImpl impl_;
Sebastian Jansson340af972019-12-04 10:07:48 +0100148 rtc::ScopedYieldPolicy yield_policy_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100149 std::unique_ptr<rtc::Thread> main_thread_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100150};
151} // namespace webrtc
152
153#endif // TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_