blob: b870fa353d9bfca8a7b7aa4b554937b49e3760fe [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:
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010063 const rtc::PlatformThreadId thread_id_;
64 rtc::ThreadChecker thread_checker_;
65 rtc::CriticalSection time_lock_;
66 Timestamp current_time_ RTC_GUARDED_BY(time_lock_);
67 rtc::CriticalSection lock_;
Sebastian Jansson7b6add32019-03-29 10:34:26 +010068 std::vector<SimulatedSequenceRunner*> runners_ RTC_GUARDED_BY(lock_);
Sebastian Jansson76540812019-04-11 17:48:30 +020069 // Used in RunReadyRunners() to keep track of ready runners that are to be
70 // processed in a round robin fashion. the reason it's a member is so that
71 // runners can removed from here by Unregister().
72 std::list<SimulatedSequenceRunner*> ready_runners_ RTC_GUARDED_BY(lock_);
73
Sebastian Jansson0d617cc2019-03-22 15:22:16 +010074 // Task queues on which YieldExecution has been called.
75 std::unordered_set<TaskQueueBase*> yielded_ RTC_GUARDED_BY(thread_checker_);
76};
77} // namespace sim_time_impl
78
79// TimeController implementation using completely simulated time. Task queues
80// and process threads created by this controller will run delayed activities
81// when Sleep() is called. Overrides the global clock backing rtc::TimeMillis()
82// and rtc::TimeMicros(). Note that this is not thread safe since it modifies
83// global state.
84class GlobalSimulatedTimeController : public TimeController {
85 public:
86 explicit GlobalSimulatedTimeController(Timestamp start_time);
87 ~GlobalSimulatedTimeController() override;
88
89 Clock* GetClock() override;
90 TaskQueueFactory* GetTaskQueueFactory() override;
91 std::unique_ptr<ProcessThread> CreateProcessThread(
92 const char* thread_name) override;
93 void Sleep(TimeDelta duration) override;
94 void InvokeWithControlledYield(std::function<void()> closure) override;
95
96 private:
97 rtc::ScopedFakeClock global_clock_;
98 // Provides simulated CurrentNtpInMilliseconds()
99 SimulatedClock sim_clock_;
100 sim_time_impl::SimulatedTimeControllerImpl impl_;
101};
102} // namespace webrtc
103
104#endif // TEST_TIME_CONTROLLER_SIMULATED_TIME_CONTROLLER_H_