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