blob: 6837643412c5b6e3820a69f9ddf0de296db24cf0 [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
19#include "api/units/timestamp.h"
20#include "modules/include/module.h"
21#include "modules/utility/include/process_thread.h"
22#include "rtc_base/critical_section.h"
23#include "rtc_base/fake_clock.h"
24#include "rtc_base/platform_thread_types.h"
25#include "rtc_base/synchronization/yield_policy.h"
26#include "rtc_base/thread_checker.h"
27#include "test/time_controller/time_controller.h"
28
29namespace webrtc {
30
31namespace sim_time_impl {
32class SimulatedSequenceRunner;
33
34class SimulatedTimeControllerImpl : public TaskQueueFactory,
35 public rtc::YieldInterface {
36 public:
37 explicit SimulatedTimeControllerImpl(Timestamp start_time);
38 ~SimulatedTimeControllerImpl() override;
39
40 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
41 absl::string_view name,
42 Priority priority) const override;
43
44 // Implements the YieldInterface by running ready tasks on all task queues,
45 // except that if this method is called from a task, the task queue running
46 // that task is skipped.
47 void YieldExecution() override;
48 // Create process thread with the name |thread_name|.
49 std::unique_ptr<ProcessThread> CreateProcessThread(const char* thread_name);
50 // Runs all runners in |runners_| that has tasks or modules ready for
51 // execution.
52 void RunReadyRunners();
53 // Return |current_time_|.
54 Timestamp CurrentTime() const;
55 // Return min of runner->GetNextRunTime() for runner in |runners_|.
56 Timestamp NextRunTime() const;
57 // Set |current_time_| to |target_time|.
58 void AdvanceTime(Timestamp target_time);
59 // Removes |runner| from |runners_|.
60 void Unregister(SimulatedSequenceRunner* runner);
61
62 private:
63 // Returns runners in |runners_| that are ready for execution.
64 std::vector<SimulatedSequenceRunner*> GetNextReadyRunner(
65 Timestamp current_time) RTC_RUN_ON(thread_checker_);
66
67 const rtc::PlatformThreadId thread_id_;
68 rtc::ThreadChecker thread_checker_;
69 rtc::CriticalSection time_lock_;
70 Timestamp current_time_ RTC_GUARDED_BY(time_lock_);
71 rtc::CriticalSection lock_;
Sebastian Jansson7b6add32019-03-29 10:34:26 +010072 std::vector<SimulatedSequenceRunner*> runners_ RTC_GUARDED_BY(lock_);
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010073 // Task queues on which YieldExecution has been called.
74 std::unordered_set<TaskQueueBase*> yielded_ RTC_GUARDED_BY(thread_checker_);
75};
76} // namespace sim_time_impl
77
78// TimeController implementation using completely simulated time. Task queues
79// and process threads created by this controller will run delayed activities
80// when Sleep() is called. Overrides the global clock backing rtc::TimeMillis()
81// and rtc::TimeMicros(). Note that this is not thread safe since it modifies
82// global state.
83class GlobalSimulatedTimeController : public TimeController {
84 public:
85 explicit GlobalSimulatedTimeController(Timestamp start_time);
86 ~GlobalSimulatedTimeController() override;
87
88 Clock* GetClock() override;
89 TaskQueueFactory* GetTaskQueueFactory() override;
90 std::unique_ptr<ProcessThread> CreateProcessThread(
91 const char* thread_name) override;
92 void Sleep(TimeDelta duration) override;
93 void InvokeWithControlledYield(std::function<void()> closure) override;
94
95 private:
96 rtc::ScopedFakeClock global_clock_;
97 // Provides simulated CurrentNtpInMilliseconds()
98 SimulatedClock sim_clock_;
99 sim_time_impl::SimulatedTimeControllerImpl impl_;
100};
101} // namespace webrtc
102
103#endif // TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_