Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 1 | /* |
| 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öm | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 14 | #include <utility> |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 15 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 16 | #include "absl/functional/any_invocable.h" |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 17 | #include "api/location.h" |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 18 | #include "api/units/time_delta.h" |
Mirko Bonadei | d4002a7 | 2019-11-12 19:11:48 | [diff] [blame] | 19 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 17:41:39 | [diff] [blame] | 20 | #include "rtc_base/thread_annotations.h" |
Danil Chapovalov | d00405f | 2019-02-25 14:06:13 | [diff] [blame] | 21 | |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 22 | namespace 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 Bonadei | d4002a7 | 2019-11-12 19:11:48 | [diff] [blame] | 28 | class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 29 | public: |
Henrik Boström | 27e8a09 | 2022-01-24 16:12:35 | [diff] [blame] | 30 | 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 Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 40 | // 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 Chapovalov | 6cdb67f | 2021-01-18 16:02:55 | [diff] [blame] | 43 | // Responsible for deallocation. Deallocation may happen synchronously during |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 44 | // 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 Chapovalov | 6cdb67f | 2021-01-18 16:02:55 | [diff] [blame] | 49 | // Should be called on the same task queue or thread that this task queue |
| 50 | // was created on. |
Danil Chapovalov | d00405f | 2019-02-25 14:06:13 | [diff] [blame] | 51 | virtual void Delete() = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 52 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 53 | // Schedules a `task` to execute. Tasks are executed in FIFO order. |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 54 | // When a TaskQueue is deleted, pending tasks will not be executed but they |
Markus Handell | 82da932 | 2022-12-16 14:50:24 | [diff] [blame] | 55 | // 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 Chapovalov | 6cdb67f | 2021-01-18 16:02:55 | [diff] [blame] | 65 | // May be called on any thread or task queue, including this task queue. |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 66 | void PostTask(absl::AnyInvocable<void() &&> task, |
Markus Handell | 9c69c46 | 2023-11-10 18:18:19 | [diff] [blame] | 67 | const Location& location = Location::Current()) { |
| 68 | PostTaskImpl(std::move(task), PostTaskTraits{}, location); |
| 69 | } |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 70 | |
Henrik Boström | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 71 | // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever |
| 72 | // possible. |
| 73 | // |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 74 | // 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öm | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 81 | // |
| 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 Chapovalov | 6cdb67f | 2021-01-18 16:02:55 | [diff] [blame] | 93 | // May be called on any thread or task queue, including this task queue. |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 94 | void PostDelayedTask(absl::AnyInvocable<void() &&> task, |
| 95 | TimeDelta delay, |
| 96 | const Location& location = Location::Current()) { |
Markus Handell | a84368c | 2023-09-12 20:25:05 | [diff] [blame] | 97 | PostDelayedTaskImpl(std::move(task), delay, PostDelayedTaskTraits{}, |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 98 | location); |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 99 | } |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 100 | |
Henrik Boström | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 101 | // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever |
| 102 | // possible. |
| 103 | // |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 104 | // 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öm | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 108 | // |
| 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 Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 117 | void PostDelayedHighPrecisionTask( |
| 118 | absl::AnyInvocable<void() &&> task, |
| 119 | TimeDelta delay, |
| 120 | const Location& location = Location::Current()) { |
Markus Handell | a84368c | 2023-09-12 20:25:05 | [diff] [blame] | 121 | PostDelayedTaskTraits traits; |
| 122 | traits.high_precision = true; |
| 123 | PostDelayedTaskImpl(std::move(task), delay, traits, location); |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 124 | } |
Henrik Boström | cf9899c | 2022-01-20 08:46:16 | [diff] [blame] | 125 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 126 | // As specified by `precision`, calls either PostDelayedTask() or |
Henrik Boström | 27e8a09 | 2022-01-24 16:12:35 | [diff] [blame] | 127 | // PostDelayedHighPrecisionTask(). |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 128 | void PostDelayedTaskWithPrecision( |
| 129 | DelayPrecision precision, |
| 130 | absl::AnyInvocable<void() &&> task, |
| 131 | TimeDelta delay, |
| 132 | const Location& location = Location::Current()) { |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 133 | switch (precision) { |
| 134 | case DelayPrecision::kLow: |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 135 | PostDelayedTask(std::move(task), delay, location); |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 136 | break; |
| 137 | case DelayPrecision::kHigh: |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 138 | PostDelayedHighPrecisionTask(std::move(task), delay, location); |
Danil Chapovalov | 8feb6fd | 2022-07-05 09:01:27 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Danil Chapovalov | d00405f | 2019-02-25 14:06:13 | [diff] [blame] | 143 | // Returns the task queue that is running the current thread. |
| 144 | // Returns nullptr if this thread is not associated with any task queue. |
Danil Chapovalov | 6cdb67f | 2021-01-18 16:02:55 | [diff] [blame] | 145 | // May be called on any thread or task queue, including this task queue. |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 146 | static TaskQueueBase* Current(); |
| 147 | bool IsCurrent() const { return Current() == this; } |
| 148 | |
| 149 | protected: |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 150 | // 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 Shrubsole | 5b8dc1d | 2022-05-19 10:59:04 | [diff] [blame] | 162 | class RTC_EXPORT CurrentTaskQueueSetter { |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 163 | 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 Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 173 | // Subclasses should implement this method to support the behavior defined in |
| 174 | // the PostTask and PostTaskTraits docs above. |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 175 | virtual void PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 176 | const PostTaskTraits& traits, |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 177 | const Location& location) = 0; |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 178 | |
| 179 | // Subclasses should implement this method to support the behavior defined in |
| 180 | // the PostDelayedTask/PostHighPrecisionDelayedTask and PostDelayedTaskTraits |
| 181 | // docs above. |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 182 | virtual void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 183 | TimeDelta delay, |
| 184 | const PostDelayedTaskTraits& traits, |
Markus Handell | ae61aca | 2023-03-01 12:05:08 | [diff] [blame] | 185 | const Location& location) = 0; |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 186 | |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 187 | // Users of the TaskQueue should call Delete instead of directly deleting |
| 188 | // this object. |
Danil Chapovalov | d00405f | 2019-02-25 14:06:13 | [diff] [blame] | 189 | virtual ~TaskQueueBase() = default; |
Danil Chapovalov | 348b08a | 2019-01-17 12:07:25 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | struct 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_ |