blob: 6026826d2f5d2fda799143690743e12d5b9f989b [file] [log] [blame]
Sebastian Jansson53cd9e22020-01-13 10:33:19 +01001/*
2 * Copyright (c) 2020 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_PROCESS_THREAD_H_
11#define TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_
12
13#include <deque>
14#include <list>
15#include <map>
16#include <memory>
17#include <vector>
18
19#include "test/time_controller/simulated_time_controller.h"
20
21namespace webrtc {
22
Danil Chapovalov14273de2020-02-27 13:37:43 +010023class SimulatedProcessThread : public ProcessThread,
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010024 public sim_time_impl::SimulatedSequenceRunner {
25 public:
26 SimulatedProcessThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
27 absl::string_view name);
28 virtual ~SimulatedProcessThread();
29 void RunReady(Timestamp at_time) override;
30
31 Timestamp GetNextRunTime() const override {
32 rtc::CritScope lock(&lock_);
33 return next_run_time_;
34 }
35
36 TaskQueueBase* GetAsTaskQueue() override { return this; }
37
38 // ProcessThread interface
39 void Start() override;
40 void Stop() override;
41 void WakeUp(Module* module) override;
42 void RegisterModule(Module* module, const rtc::Location& from) override;
43 void DeRegisterModule(Module* module) override;
44 void PostTask(std::unique_ptr<QueuedTask> task) override;
Danil Chapovalov14273de2020-02-27 13:37:43 +010045 void PostDelayedTask(std::unique_ptr<QueuedTask> task,
46 uint32_t milliseconds) override;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010047
48 private:
Danil Chapovalov14273de2020-02-27 13:37:43 +010049 void Delete() override {
50 // ProcessThread shouldn't be deleted as a TaskQueue.
51 RTC_NOTREACHED();
52 }
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010053 Timestamp GetNextTime(Module* module, Timestamp at_time);
54
55 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
56 // Using char* to be debugger friendly.
57 char* name_;
58 rtc::CriticalSection lock_;
59 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
60
61 std::deque<std::unique_ptr<QueuedTask>> queue_;
Danil Chapovalov14273de2020-02-27 13:37:43 +010062 std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_
63 RTC_GUARDED_BY(lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010064
65 bool process_thread_running_ RTC_GUARDED_BY(lock_) = false;
66 std::vector<Module*> stopped_modules_ RTC_GUARDED_BY(lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010067 std::map<Timestamp, std::list<Module*>> delayed_modules_
68 RTC_GUARDED_BY(lock_);
69};
70} // namespace webrtc
71
72#endif // TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_