blob: 89e9e9e3b0adef6077e2c54f5c0fa027ef425e3f [file] [log] [blame]
Danil Chapovalov348b08a2019-01-17 12:07:251/*
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 08:46:1614#include <utility>
Danil Chapovalov348b08a2019-01-17 12:07:2515
Danil Chapovalov8feb6fd2022-07-05 09:01:2716#include "absl/functional/any_invocable.h"
Markus Handell2a256c82023-02-27 11:41:3917#include "api/location.h"
Danil Chapovalov8feb6fd2022-07-05 09:01:2718#include "api/units/time_delta.h"
Mirko Bonadeid4002a72019-11-12 19:11:4819#include "rtc_base/system/rtc_export.h"
Danil Chapovalov4423c362019-03-06 17:41:3920#include "rtc_base/thread_annotations.h"
Danil Chapovalovd00405f2019-02-25 14:06:1321
Danil Chapovalov348b08a2019-01-17 12:07:2522namespace 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 19:11:4828class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 12:07:2529 public:
Henrik Boström27e8a092022-01-24 16:12:3530 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 12:07:2540 // 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 16:02:5543 // Responsible for deallocation. Deallocation may happen synchronously during
Danil Chapovalov348b08a2019-01-17 12:07:2544 // 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 16:02:5549 // Should be called on the same task queue or thread that this task queue
50 // was created on.
Danil Chapovalovd00405f2019-02-25 14:06:1351 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 12:07:2552
Danil Chapovalov8feb6fd2022-07-05 09:01:2753 // Schedules a `task` to execute. Tasks are executed in FIFO order.
Danil Chapovalov348b08a2019-01-17 12:07:2554 // When a TaskQueue is deleted, pending tasks will not be executed but they
Markus Handell82da9322022-12-16 14:50:2455 // will be deleted.
56 //
57 // As long as tasks are not posted from task destruction, posted tasks are
58 // guaranteed to be destroyed with Current() pointing to the task queue they
59 // were posted to, whether they're executed or not. That means SequenceChecker
60 // works during task destruction, a fact that can be used to guarantee
61 // thread-compatible object deletion happening on a particular task queue
62 // which can simplify class design.
63 // Note that this guarantee does not apply to delayed tasks.
64 //
Danil Chapovalov6cdb67f2021-01-18 16:02:5565 // May be called on any thread or task queue, including this task queue.
Markus Handellae61aca2023-03-01 12:05:0866 void PostTask(absl::AnyInvocable<void() &&> task,
Markus Handell9c69c462023-11-10 18:18:1967 const Location& location = Location::Current()) {
68 PostTaskImpl(std::move(task), PostTaskTraits{}, location);
69 }
Danil Chapovalov348b08a2019-01-17 12:07:2570
Henrik Boströmcf9899c2022-01-20 08:46:1671 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
72 // possible.
73 //
Danil Chapovalov8feb6fd2022-07-05 09:01:2774 // Schedules a `task` to execute a specified `delay` from when the call is
75 // made, using "low" precision. All scheduling is affected by OS-specific
76 // leeway and current workloads which means that in terms of precision there
77 // are no hard guarantees, but in addition to the OS induced leeway, "low"
78 // precision adds up to a 17 ms additional leeway. The purpose of this leeway
79 // is to achieve more efficient CPU scheduling and reduce Idle Wake Up
80 // frequency.
Henrik Boströmcf9899c2022-01-20 08:46:1681 //
82 // The task may execute with [-1, 17 + OS induced leeway) ms additional delay.
83 //
84 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
85 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
86 // precision timers may be used but there are cases, such as when running on
87 // battery, when the timer precision can be as poor as 15 ms.
88 //
89 // "Low" precision is not implemented everywhere yet. Where not yet
90 // implemented, PostDelayedTask() has "high" precision. See
91 // https://crbug.com/webrtc/13583 for more information.
92 //
Danil Chapovalov6cdb67f2021-01-18 16:02:5593 // May be called on any thread or task queue, including this task queue.
Markus Handellae61aca2023-03-01 12:05:0894 void PostDelayedTask(absl::AnyInvocable<void() &&> task,
95 TimeDelta delay,
96 const Location& location = Location::Current()) {
Markus Handella84368c2023-09-12 20:25:0597 PostDelayedTaskImpl(std::move(task), delay, PostDelayedTaskTraits{},
Markus Handellae61aca2023-03-01 12:05:0898 location);
Markus Handell2a256c82023-02-27 11:41:3999 }
Danil Chapovalov348b08a2019-01-17 12:07:25100
Henrik Boströmcf9899c2022-01-20 08:46:16101 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
102 // possible.
103 //
Danil Chapovalov8feb6fd2022-07-05 09:01:27104 // Schedules a `task` to execute a specified `delay` from when the call is
105 // made, using "high" precision. All scheduling is affected by OS-specific
106 // leeway and current workloads which means that in terms of precision there
107 // are no hard guarantees.
Henrik Boströmcf9899c2022-01-20 08:46:16108 //
109 // The task may execute with [-1, OS induced leeway] ms additional delay.
110 //
111 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
112 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
113 // precision timers may be used but there are cases, such as when running on
114 // battery, when the timer precision can be as poor as 15 ms.
115 //
116 // May be called on any thread or task queue, including this task queue.
Markus Handellae61aca2023-03-01 12:05:08117 void PostDelayedHighPrecisionTask(
118 absl::AnyInvocable<void() &&> task,
119 TimeDelta delay,
120 const Location& location = Location::Current()) {
Markus Handella84368c2023-09-12 20:25:05121 PostDelayedTaskTraits traits;
122 traits.high_precision = true;
123 PostDelayedTaskImpl(std::move(task), delay, traits, location);
Markus Handell2a256c82023-02-27 11:41:39124 }
Henrik Boströmcf9899c2022-01-20 08:46:16125
Danil Chapovalov8feb6fd2022-07-05 09:01:27126 // As specified by `precision`, calls either PostDelayedTask() or
Henrik Boström27e8a092022-01-24 16:12:35127 // PostDelayedHighPrecisionTask().
Markus Handellae61aca2023-03-01 12:05:08128 void PostDelayedTaskWithPrecision(
129 DelayPrecision precision,
130 absl::AnyInvocable<void() &&> task,
131 TimeDelta delay,
132 const Location& location = Location::Current()) {
Danil Chapovalov8feb6fd2022-07-05 09:01:27133 switch (precision) {
134 case DelayPrecision::kLow:
Markus Handellae61aca2023-03-01 12:05:08135 PostDelayedTask(std::move(task), delay, location);
Danil Chapovalov8feb6fd2022-07-05 09:01:27136 break;
137 case DelayPrecision::kHigh:
Markus Handellae61aca2023-03-01 12:05:08138 PostDelayedHighPrecisionTask(std::move(task), delay, location);
Danil Chapovalov8feb6fd2022-07-05 09:01:27139 break;
140 }
141 }
142
Danil Chapovalovd00405f2019-02-25 14:06:13143 // Returns the task queue that is running the current thread.
144 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov6cdb67f2021-01-18 16:02:55145 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov348b08a2019-01-17 12:07:25146 static TaskQueueBase* Current();
147 bool IsCurrent() const { return Current() == this; }
148
149 protected:
Markus Handell2a256c82023-02-27 11:41:39150 // This is currently only present here to simplify introduction of future
151 // planned task queue changes.
152 struct PostTaskTraits {};
153
154 struct PostDelayedTaskTraits {
155 // If `high_precision` is false, tasks may execute within up to a 17 ms
156 // leeway in addition to OS timer precision. Otherwise the task should be
157 // limited to OS timer precision. See PostDelayedTask() and
158 // PostDelayedHighPrecisionTask() for more information.
159 bool high_precision = false;
160 };
161
Evan Shrubsole5b8dc1d2022-05-19 10:59:04162 class RTC_EXPORT CurrentTaskQueueSetter {
Danil Chapovalov348b08a2019-01-17 12:07:25163 public:
164 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
165 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
166 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
167 ~CurrentTaskQueueSetter();
168
169 private:
170 TaskQueueBase* const previous_;
171 };
172
Markus Handell2a256c82023-02-27 11:41:39173 // Subclasses should implement this method to support the behavior defined in
174 // the PostTask and PostTaskTraits docs above.
Markus Handell2a256c82023-02-27 11:41:39175 virtual void PostTaskImpl(absl::AnyInvocable<void() &&> task,
176 const PostTaskTraits& traits,
Markus Handellae61aca2023-03-01 12:05:08177 const Location& location) = 0;
Markus Handell2a256c82023-02-27 11:41:39178
179 // Subclasses should implement this method to support the behavior defined in
180 // the PostDelayedTask/PostHighPrecisionDelayedTask and PostDelayedTaskTraits
181 // docs above.
Markus Handell2a256c82023-02-27 11:41:39182 virtual void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
183 TimeDelta delay,
184 const PostDelayedTaskTraits& traits,
Markus Handellae61aca2023-03-01 12:05:08185 const Location& location) = 0;
Markus Handell2a256c82023-02-27 11:41:39186
Danil Chapovalov348b08a2019-01-17 12:07:25187 // Users of the TaskQueue should call Delete instead of directly deleting
188 // this object.
Danil Chapovalovd00405f2019-02-25 14:06:13189 virtual ~TaskQueueBase() = default;
Danil Chapovalov348b08a2019-01-17 12:07:25190};
191
192struct TaskQueueDeleter {
193 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
194};
195
196} // namespace webrtc
197
198#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_