Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [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_TASK_QUEUE_H_ |
| 11 | #define TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_ |
| 12 | |
| 13 | #include <deque> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <vector> |
| 17 | |
Danil Chapovalov | 9c125c6 | 2022-07-07 18:29:30 | [diff] [blame] | 18 | #include "absl/functional/any_invocable.h" |
| 19 | #include "api/units/time_delta.h" |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 20 | #include "rtc_base/synchronization/mutex.h" |
Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [diff] [blame] | 21 | #include "test/time_controller/simulated_time_controller.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | class SimulatedTaskQueue : public TaskQueueBase, |
| 26 | public sim_time_impl::SimulatedSequenceRunner { |
| 27 | public: |
| 28 | SimulatedTaskQueue(sim_time_impl::SimulatedTimeControllerImpl* handler, |
| 29 | absl::string_view name); |
| 30 | |
| 31 | ~SimulatedTaskQueue(); |
| 32 | |
| 33 | void RunReady(Timestamp at_time) override; |
| 34 | |
| 35 | Timestamp GetNextRunTime() const override { |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 36 | MutexLock lock(&lock_); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [diff] [blame] | 37 | return next_run_time_; |
| 38 | } |
| 39 | TaskQueueBase* GetAsTaskQueue() override { return this; } |
| 40 | |
| 41 | // TaskQueueBase interface |
| 42 | void Delete() override; |
Markus Handell | a1ceae2 | 2023-03-01 09:13:28 | [diff] [blame] | 43 | void PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 44 | const PostTaskTraits& traits, |
| 45 | const Location& location) override; |
| 46 | void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 47 | TimeDelta delay, |
| 48 | const PostDelayedTaskTraits& traits, |
| 49 | const Location& location) override; |
Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | sim_time_impl::SimulatedTimeControllerImpl* const handler_; |
| 53 | // Using char* to be debugger friendly. |
| 54 | char* name_; |
| 55 | |
Markus Handell | e56976d | 2020-07-08 15:34:37 | [diff] [blame] | 56 | mutable Mutex lock_; |
Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [diff] [blame] | 57 | |
Danil Chapovalov | 9c125c6 | 2022-07-07 18:29:30 | [diff] [blame] | 58 | std::deque<absl::AnyInvocable<void() &&>> ready_tasks_ RTC_GUARDED_BY(lock_); |
| 59 | std::map<Timestamp, std::vector<absl::AnyInvocable<void() &&>>> delayed_tasks_ |
Sebastian Jansson | 53cd9e2 | 2020-01-13 09:33:19 | [diff] [blame] | 60 | RTC_GUARDED_BY(lock_); |
| 61 | |
| 62 | Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); |
| 63 | }; |
| 64 | |
| 65 | } // namespace webrtc |
| 66 | |
| 67 | #endif // TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_ |