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 "rtc_base/synchronization/mutex.h"
19 #include "test/time_controller/simulated_time_controller.h"
20 
21 namespace webrtc {
22 
23 class SimulatedTaskQueue : public TaskQueueBase,
24                            public sim_time_impl::SimulatedSequenceRunner {
25  public:
26   SimulatedTaskQueue(sim_time_impl::SimulatedTimeControllerImpl* handler,
27                      absl::string_view name);
28 
29   ~SimulatedTaskQueue();
30 
31   void RunReady(Timestamp at_time) override;
32 
GetNextRunTime()33   Timestamp GetNextRunTime() const override {
34     MutexLock lock(&lock_);
35     return next_run_time_;
36   }
GetAsTaskQueue()37   TaskQueueBase* GetAsTaskQueue() override { return this; }
38 
39   // TaskQueueBase interface
40   void Delete() override;
41   void PostTask(std::unique_ptr<QueuedTask> task) override;
42   void PostDelayedTask(std::unique_ptr<QueuedTask> task,
43                        uint32_t milliseconds) override;
44 
45  private:
46   sim_time_impl::SimulatedTimeControllerImpl* const handler_;
47   // Using char* to be debugger friendly.
48   char* name_;
49 
50   mutable Mutex lock_;
51 
52   std::deque<std::unique_ptr<QueuedTask>> ready_tasks_ RTC_GUARDED_BY(lock_);
53   std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_
54       RTC_GUARDED_BY(lock_);
55 
56   Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
57 };
58 
59 }  // namespace webrtc
60 
61 #endif  // TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_
62