blob: 6ca46327b21613118f292d83287f1ff24e9c85bb [file] [log] [blame]
Sebastian Jansson53cd9e22020-01-13 09:33:191/*
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 Chapovalov9c125c62022-07-07 18:29:3018#include "absl/functional/any_invocable.h"
19#include "api/units/time_delta.h"
Markus Handelle56976d2020-07-08 15:34:3720#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson53cd9e22020-01-13 09:33:1921#include "test/time_controller/simulated_time_controller.h"
22
23namespace webrtc {
24
25class 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 Handelle56976d2020-07-08 15:34:3736 MutexLock lock(&lock_);
Sebastian Jansson53cd9e22020-01-13 09:33:1937 return next_run_time_;
38 }
39 TaskQueueBase* GetAsTaskQueue() override { return this; }
40
41 // TaskQueueBase interface
42 void Delete() override;
Markus Handella1ceae22023-03-01 09:13:2843 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 Jansson53cd9e22020-01-13 09:33:1950
51 private:
52 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
53 // Using char* to be debugger friendly.
54 char* name_;
55
Markus Handelle56976d2020-07-08 15:34:3756 mutable Mutex lock_;
Sebastian Jansson53cd9e22020-01-13 09:33:1957
Danil Chapovalov9c125c62022-07-07 18:29:3058 std::deque<absl::AnyInvocable<void() &&>> ready_tasks_ RTC_GUARDED_BY(lock_);
59 std::map<Timestamp, std::vector<absl::AnyInvocable<void() &&>>> delayed_tasks_
Sebastian Jansson53cd9e22020-01-13 09:33:1960 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_