xref: /aosp_15_r20/external/webrtc/test/time_controller/simulated_task_queue.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
18 #include "absl/functional/any_invocable.h"
19 #include "api/units/time_delta.h"
20 #include "rtc_base/synchronization/mutex.h"
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 
GetNextRunTime()35   Timestamp GetNextRunTime() const override {
36     MutexLock lock(&lock_);
37     return next_run_time_;
38   }
GetAsTaskQueue()39   TaskQueueBase* GetAsTaskQueue() override { return this; }
40 
41   // TaskQueueBase interface
42   void Delete() override;
43   void PostTask(absl::AnyInvocable<void() &&> task) override;
44   void PostDelayedTask(absl::AnyInvocable<void() &&> task,
45                        TimeDelta delay) override;
46   void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
47                                     TimeDelta delay) override;
48 
49  private:
50   sim_time_impl::SimulatedTimeControllerImpl* const handler_;
51   // Using char* to be debugger friendly.
52   char* name_;
53 
54   mutable Mutex lock_;
55 
56   std::deque<absl::AnyInvocable<void() &&>> ready_tasks_ RTC_GUARDED_BY(lock_);
57   std::map<Timestamp, std::vector<absl::AnyInvocable<void() &&>>> delayed_tasks_
58       RTC_GUARDED_BY(lock_);
59 
60   Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
61 };
62 
63 }  // namespace webrtc
64 
65 #endif  // TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_
66