Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 19 | #include "rtc_base/synchronization/mutex.h" |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 20 | #include "test/time_controller/simulated_time_controller.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 24 | class SimulatedProcessThread : public ProcessThread, |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 25 | public sim_time_impl::SimulatedSequenceRunner { |
| 26 | public: |
| 27 | SimulatedProcessThread(sim_time_impl::SimulatedTimeControllerImpl* handler, |
| 28 | absl::string_view name); |
| 29 | virtual ~SimulatedProcessThread(); |
| 30 | void RunReady(Timestamp at_time) override; |
| 31 | |
| 32 | Timestamp GetNextRunTime() const override { |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 33 | MutexLock lock(&lock_); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 34 | return next_run_time_; |
| 35 | } |
| 36 | |
| 37 | TaskQueueBase* GetAsTaskQueue() override { return this; } |
| 38 | |
| 39 | // ProcessThread interface |
| 40 | void Start() override; |
| 41 | void Stop() override; |
| 42 | void WakeUp(Module* module) override; |
| 43 | void RegisterModule(Module* module, const rtc::Location& from) override; |
| 44 | void DeRegisterModule(Module* module) override; |
| 45 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 46 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 47 | uint32_t milliseconds) override; |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 48 | |
| 49 | private: |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 50 | void Delete() override { |
| 51 | // ProcessThread shouldn't be deleted as a TaskQueue. |
| 52 | RTC_NOTREACHED(); |
| 53 | } |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 54 | Timestamp GetNextTime(Module* module, Timestamp at_time); |
| 55 | |
| 56 | sim_time_impl::SimulatedTimeControllerImpl* const handler_; |
| 57 | // Using char* to be debugger friendly. |
| 58 | char* name_; |
Markus Handell | e56976d | 2020-07-08 17:34:37 +0200 | [diff] [blame] | 59 | mutable Mutex lock_; |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 60 | Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); |
| 61 | |
| 62 | std::deque<std::unique_ptr<QueuedTask>> queue_; |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 63 | std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_ |
| 64 | RTC_GUARDED_BY(lock_); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 65 | |
| 66 | bool process_thread_running_ RTC_GUARDED_BY(lock_) = false; |
| 67 | std::vector<Module*> stopped_modules_ RTC_GUARDED_BY(lock_); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 68 | std::map<Timestamp, std::list<Module*>> delayed_modules_ |
| 69 | RTC_GUARDED_BY(lock_); |
| 70 | }; |
| 71 | } // namespace webrtc |
| 72 | |
| 73 | #endif // TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_ |