blob: 870637c9b519d29a0f53257b2f7facb84022d5cb [file] [log] [blame]
Danil Chapovalov348b08a2019-01-17 13:07:25 +01001/*
2 * Copyright 2019 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 API_TASK_QUEUE_TASK_QUEUE_BASE_H_
11#define API_TASK_QUEUE_TASK_QUEUE_BASE_H_
12
13#include <memory>
Henrik Boströmcf9899c2022-01-20 09:46:16 +010014#include <utility>
Danil Chapovalov348b08a2019-01-17 13:07:25 +010015
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020016#include "absl/functional/any_invocable.h"
Danil Chapovalov348b08a2019-01-17 13:07:25 +010017#include "api/task_queue/queued_task.h"
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020018#include "api/units/time_delta.h"
Mirko Bonadeid4002a72019-11-12 20:11:48 +010019#include "rtc_base/system/rtc_export.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010020#include "rtc_base/thread_annotations.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010021
Danil Chapovalov348b08a2019-01-17 13:07:25 +010022namespace webrtc {
23
24// Asynchronously executes tasks in a way that guarantees that they're executed
25// in FIFO order and that tasks never overlap. Tasks may always execute on the
26// same worker thread and they may not. To DCHECK that tasks are executing on a
27// known task queue, use IsCurrent().
Mirko Bonadeid4002a72019-11-12 20:11:48 +010028class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010029 public:
Henrik Boström27e8a092022-01-24 17:12:35 +010030 enum class DelayPrecision {
31 // This may include up to a 17 ms leeway in addition to OS timer precision.
32 // See PostDelayedTask() for more information.
33 kLow,
34 // This does not have the additional delay that kLow has, but it is still
35 // limited by OS timer precision. See PostDelayedHighPrecisionTask() for
36 // more information.
37 kHigh,
38 };
39
Danil Chapovalov348b08a2019-01-17 13:07:25 +010040 // Starts destruction of the task queue.
41 // On return ensures no task are running and no new tasks are able to start
42 // on the task queue.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010043 // Responsible for deallocation. Deallocation may happen synchronously during
Danil Chapovalov348b08a2019-01-17 13:07:25 +010044 // Delete or asynchronously after Delete returns.
45 // Code not running on the TaskQueue should not make any assumption when
46 // TaskQueue is deallocated and thus should not call any methods after Delete.
47 // Code running on the TaskQueue should not call Delete, but can assume
48 // TaskQueue still exists and may call other methods, e.g. PostTask.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010049 // Should be called on the same task queue or thread that this task queue
50 // was created on.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010051 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010052
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020053 // Schedules a `task` to execute. Tasks are executed in FIFO order.
Danil Chapovalov348b08a2019-01-17 13:07:25 +010054 // When a TaskQueue is deleted, pending tasks will not be executed but they
55 // will be deleted. The deletion of tasks may happen synchronously on the
56 // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
57 // This may vary from one implementation to the next so assumptions about
58 // lifetimes of pending tasks should not be made.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010059 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020060 // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
61 // derived classes.
62 virtual void PostTask(absl::AnyInvocable<void() &&> task);
63
64 // Deprecated, use PostTask variant above in new code.
65 // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
66 // function above.
67 virtual void PostTask(std::unique_ptr<QueuedTask> task);
Danil Chapovalov348b08a2019-01-17 13:07:25 +010068
Henrik Boströmcf9899c2022-01-20 09:46:16 +010069 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
70 // possible.
71 //
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020072 // Schedules a `task` to execute a specified `delay` from when the call is
73 // made, using "low" precision. All scheduling is affected by OS-specific
74 // leeway and current workloads which means that in terms of precision there
75 // are no hard guarantees, but in addition to the OS induced leeway, "low"
76 // precision adds up to a 17 ms additional leeway. The purpose of this leeway
77 // is to achieve more efficient CPU scheduling and reduce Idle Wake Up
78 // frequency.
Henrik Boströmcf9899c2022-01-20 09:46:16 +010079 //
80 // The task may execute with [-1, 17 + OS induced leeway) ms additional delay.
81 //
82 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
83 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
84 // precision timers may be used but there are cases, such as when running on
85 // battery, when the timer precision can be as poor as 15 ms.
86 //
87 // "Low" precision is not implemented everywhere yet. Where not yet
88 // implemented, PostDelayedTask() has "high" precision. See
89 // https://crbug.com/webrtc/13583 for more information.
90 //
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010091 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020092 // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
93 // derived classes.
94 virtual void PostDelayedTask(absl::AnyInvocable<void() &&> task,
95 TimeDelta delay);
96
97 // Deprecated, use PostDelayedTask variant above in new code.
98 // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
99 // function above.
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100100 virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200101 uint32_t milliseconds);
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100102
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100103 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
104 // possible.
105 //
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200106 // Schedules a `task` to execute a specified `delay` from when the call is
107 // made, using "high" precision. All scheduling is affected by OS-specific
108 // leeway and current workloads which means that in terms of precision there
109 // are no hard guarantees.
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100110 //
111 // The task may execute with [-1, OS induced leeway] ms additional delay.
112 //
113 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
114 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
115 // precision timers may be used but there are cases, such as when running on
116 // battery, when the timer precision can be as poor as 15 ms.
117 //
118 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200119 // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
120 // derived classes.
121 virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
122 TimeDelta delay);
123
124 // Deprecated, use `PostDelayedHighPrecisionTask` variant above in new code.
125 // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
126 // function above.
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100127 virtual void PostDelayedHighPrecisionTask(std::unique_ptr<QueuedTask> task,
128 uint32_t milliseconds) {
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100129 PostDelayedTask(std::move(task), milliseconds);
130 }
131
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200132 // As specified by `precision`, calls either PostDelayedTask() or
Henrik Boström27e8a092022-01-24 17:12:35 +0100133 // PostDelayedHighPrecisionTask().
134 void PostDelayedTaskWithPrecision(DelayPrecision precision,
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200135 absl::AnyInvocable<void() &&> task,
136 TimeDelta delay) {
137 switch (precision) {
138 case DelayPrecision::kLow:
139 PostDelayedTask(std::move(task), delay);
140 break;
141 case DelayPrecision::kHigh:
142 PostDelayedHighPrecisionTask(std::move(task), delay);
143 break;
144 }
145 }
146
147 // Deprecated, use `PostDelayedTaskWithPrecision` variant above in new code.
148 // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
149 // function above.
150 void PostDelayedTaskWithPrecision(DelayPrecision precision,
Henrik Boström27e8a092022-01-24 17:12:35 +0100151 std::unique_ptr<QueuedTask> task,
152 uint32_t milliseconds) {
153 switch (precision) {
154 case DelayPrecision::kLow:
155 PostDelayedTask(std::move(task), milliseconds);
156 break;
157 case DelayPrecision::kHigh:
158 PostDelayedHighPrecisionTask(std::move(task), milliseconds);
159 break;
160 }
161 }
162
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100163 // Returns the task queue that is running the current thread.
164 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +0100165 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100166 static TaskQueueBase* Current();
167 bool IsCurrent() const { return Current() == this; }
168
169 protected:
Evan Shrubsole5b8dc1d2022-05-19 12:59:04 +0200170 class RTC_EXPORT CurrentTaskQueueSetter {
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100171 public:
172 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
173 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
174 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
175 ~CurrentTaskQueueSetter();
176
177 private:
178 TaskQueueBase* const previous_;
179 };
180
181 // Users of the TaskQueue should call Delete instead of directly deleting
182 // this object.
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100183 virtual ~TaskQueueBase() = default;
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100184};
185
186struct TaskQueueDeleter {
187 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
188};
189
190} // namespace webrtc
191
192#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_