blob: 758f90989e87a0d3e3792f43e00ddce636a4a8bc [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"
24#include "rtc_base/critical_section.h"
25#include "rtc_base/fake_clock.h"
26#include "rtc_base/platform_thread_types.h"
27#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,
55 Priority priority) const override;
56
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.
60 void YieldExecution() override;
61 // Create process thread with the name |thread_name|.
62 std::unique_ptr<ProcessThread> CreateProcessThread(const char* thread_name);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010063 // Create thread using provided |socket_server|.
64 std::unique_ptr<rtc::Thread> CreateThread(
65 const std::string& name,
66 std::unique_ptr<rtc::SocketServer> socket_server);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010067
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010068 // Runs all runners in |runners_| that has tasks or modules ready for
69 // execution.
70 void RunReadyRunners();
71 // Return |current_time_|.
72 Timestamp CurrentTime() const;
73 // Return min of runner->GetNextRunTime() for runner in |runners_|.
74 Timestamp NextRunTime() const;
75 // Set |current_time_| to |target_time|.
76 void AdvanceTime(Timestamp target_time);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010077 // Adds |runner| to |runners_|.
78 void Register(SimulatedSequenceRunner* runner);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010079 // Removes |runner| from |runners_|.
80 void Unregister(SimulatedSequenceRunner* runner);
81
Sebastian Jansson274cc7f2020-01-17 13:58:54 +010082 // Indicates that |yielding_from| is not ready to run.
83 void StartYield(TaskQueueBase* yielding_from);
84 // Indicates that processing can be continued on |yielding_from|.
85 void StopYield(TaskQueueBase* yielding_from);
86
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010087 private:
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010088 const rtc::PlatformThreadId thread_id_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010089 const std::unique_ptr<rtc::Thread> dummy_thread_ = rtc::Thread::Create();
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010090 rtc::CriticalSection time_lock_;
91 Timestamp current_time_ RTC_GUARDED_BY(time_lock_);
92 rtc::CriticalSection lock_;
Sebastian Jansson7b6add32019-03-29 10:34:26 +010093 std::vector<SimulatedSequenceRunner*> runners_ RTC_GUARDED_BY(lock_);
Sebastian Jansson76540812019-04-11 17:48:30 +020094 // Used in RunReadyRunners() to keep track of ready runners that are to be
95 // processed in a round robin fashion. the reason it's a member is so that
96 // runners can removed from here by Unregister().
97 std::list<SimulatedSequenceRunner*> ready_runners_ RTC_GUARDED_BY(lock_);
98
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010099 // Runners on which YieldExecution has been called.
100 std::unordered_set<TaskQueueBase*> yielded_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100101};
102} // namespace sim_time_impl
103
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100104// Used to satisfy sequence checkers for non task queue sequences.
105class TokenTaskQueue : public TaskQueueBase {
106 public:
107 // Promoted to public
108 using CurrentTaskQueueSetter = TaskQueueBase::CurrentTaskQueueSetter;
109
110 void Delete() override { RTC_NOTREACHED(); }
111 void PostTask(std::unique_ptr<QueuedTask> /*task*/) override {
112 RTC_NOTREACHED();
113 }
114 void PostDelayedTask(std::unique_ptr<QueuedTask> /*task*/,
115 uint32_t /*milliseconds*/) override {
116 RTC_NOTREACHED();
117 }
118};
119
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100120// TimeController implementation using completely simulated time. Task queues
121// and process threads created by this controller will run delayed activities
Markus Handell486cc552019-12-03 14:37:28 +0100122// when AdvanceTime() is called. Overrides the global clock backing
123// rtc::TimeMillis() and rtc::TimeMicros(). Note that this is not thread safe
124// since it modifies global state.
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100125class GlobalSimulatedTimeController : public TimeController {
126 public:
127 explicit GlobalSimulatedTimeController(Timestamp start_time);
128 ~GlobalSimulatedTimeController() override;
129
130 Clock* GetClock() override;
131 TaskQueueFactory* GetTaskQueueFactory() override;
132 std::unique_ptr<ProcessThread> CreateProcessThread(
133 const char* thread_name) override;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100134 std::unique_ptr<rtc::Thread> CreateThread(
135 const std::string& name,
136 std::unique_ptr<rtc::SocketServer> socket_server) override;
137 rtc::Thread* GetMainThread() override;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +0100138
Markus Handell486cc552019-12-03 14:37:28 +0100139 void AdvanceTime(TimeDelta duration) override;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100140
141 private:
Sebastian Janssond624c392019-04-17 10:36:03 +0200142 rtc::ScopedBaseFakeClock global_clock_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100143 // Provides simulated CurrentNtpInMilliseconds()
144 SimulatedClock sim_clock_;
145 sim_time_impl::SimulatedTimeControllerImpl impl_;
Sebastian Jansson340af972019-12-04 10:07:48 +0100146 rtc::ScopedYieldPolicy yield_policy_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +0100147 std::unique_ptr<rtc::Thread> main_thread_;
Sebastian Jansson0d617cc2019-03-22 15:22:16 +0100148};
149} // namespace webrtc
150
151#endif // TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_