blob: 3c55f15ddef52739f4cc38045d79ae2589b400a3 [file] [log] [blame]
Sebastian Jansson53cd9e22020-01-13 10:33:19 +01001/*
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 20:29:30 +020018#include "absl/functional/any_invocable.h"
19#include "api/units/time_delta.h"
Markus Handelle56976d2020-07-08 17:34:37 +020020#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010021#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 17:34:37 +020036 MutexLock lock(&lock_);
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010037 return next_run_time_;
38 }
39 TaskQueueBase* GetAsTaskQueue() override { return this; }
40
41 // TaskQueueBase interface
42 void Delete() override;
Danil Chapovalov9c125c62022-07-07 20:29:30 +020043 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;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010048
49 private:
50 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
51 // Using char* to be debugger friendly.
52 char* name_;
53
Markus Handelle56976d2020-07-08 17:34:37 +020054 mutable Mutex lock_;
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010055
Danil Chapovalov9c125c62022-07-07 20:29:30 +020056 std::deque<absl::AnyInvocable<void() &&>> ready_tasks_ RTC_GUARDED_BY(lock_);
57 std::map<Timestamp, std::vector<absl::AnyInvocable<void() &&>>> delayed_tasks_
Sebastian Jansson53cd9e22020-01-13 10:33:19 +010058 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_