Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [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 09:46:16 +0100 | [diff] [blame] | 14 | #include <utility> |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 15 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 16 | #include "absl/functional/any_invocable.h" |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 17 | #include "api/units/time_delta.h" |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 18 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 19 | #include "rtc_base/thread_annotations.h" |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 20 | |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
| 23 | // Asynchronously executes tasks in a way that guarantees that they're executed |
| 24 | // in FIFO order and that tasks never overlap. Tasks may always execute on the |
| 25 | // same worker thread and they may not. To DCHECK that tasks are executing on a |
| 26 | // known task queue, use IsCurrent(). |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 27 | class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 28 | public: |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 29 | enum class DelayPrecision { |
| 30 | // This may include up to a 17 ms leeway in addition to OS timer precision. |
| 31 | // See PostDelayedTask() for more information. |
| 32 | kLow, |
| 33 | // This does not have the additional delay that kLow has, but it is still |
| 34 | // limited by OS timer precision. See PostDelayedHighPrecisionTask() for |
| 35 | // more information. |
| 36 | kHigh, |
| 37 | }; |
| 38 | |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 39 | // Starts destruction of the task queue. |
| 40 | // On return ensures no task are running and no new tasks are able to start |
| 41 | // on the task queue. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 42 | // Responsible for deallocation. Deallocation may happen synchronously during |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 43 | // Delete or asynchronously after Delete returns. |
| 44 | // Code not running on the TaskQueue should not make any assumption when |
| 45 | // TaskQueue is deallocated and thus should not call any methods after Delete. |
| 46 | // Code running on the TaskQueue should not call Delete, but can assume |
| 47 | // TaskQueue still exists and may call other methods, e.g. PostTask. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 48 | // Should be called on the same task queue or thread that this task queue |
| 49 | // was created on. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 50 | virtual void Delete() = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 51 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 52 | // Schedules a `task` to execute. Tasks are executed in FIFO order. |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 53 | // When a TaskQueue is deleted, pending tasks will not be executed but they |
| 54 | // will be deleted. The deletion of tasks may happen synchronously on the |
| 55 | // TaskQueue or it may happen asynchronously after TaskQueue is deleted. |
| 56 | // This may vary from one implementation to the next so assumptions about |
| 57 | // lifetimes of pending tasks should not be made. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 58 | // May be called on any thread or task queue, including this task queue. |
Danil Chapovalov | c0ce454 | 2022-08-09 11:24:52 +0200 | [diff] [blame^] | 59 | virtual void PostTask(absl::AnyInvocable<void() &&> task) = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 60 | |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 61 | // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever |
| 62 | // possible. |
| 63 | // |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 64 | // Schedules a `task` to execute a specified `delay` from when the call is |
| 65 | // made, using "low" precision. All scheduling is affected by OS-specific |
| 66 | // leeway and current workloads which means that in terms of precision there |
| 67 | // are no hard guarantees, but in addition to the OS induced leeway, "low" |
| 68 | // precision adds up to a 17 ms additional leeway. The purpose of this leeway |
| 69 | // is to achieve more efficient CPU scheduling and reduce Idle Wake Up |
| 70 | // frequency. |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 71 | // |
| 72 | // The task may execute with [-1, 17 + OS induced leeway) ms additional delay. |
| 73 | // |
| 74 | // Avoid making assumptions about the precision of the OS scheduler. On macOS, |
| 75 | // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms |
| 76 | // precision timers may be used but there are cases, such as when running on |
| 77 | // battery, when the timer precision can be as poor as 15 ms. |
| 78 | // |
| 79 | // "Low" precision is not implemented everywhere yet. Where not yet |
| 80 | // implemented, PostDelayedTask() has "high" precision. See |
| 81 | // https://crbug.com/webrtc/13583 for more information. |
| 82 | // |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 83 | // May be called on any thread or task queue, including this task queue. |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 84 | virtual void PostDelayedTask(absl::AnyInvocable<void() &&> task, |
Danil Chapovalov | c0ce454 | 2022-08-09 11:24:52 +0200 | [diff] [blame^] | 85 | TimeDelta delay) = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 86 | |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 87 | // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever |
| 88 | // possible. |
| 89 | // |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 90 | // Schedules a `task` to execute a specified `delay` from when the call is |
| 91 | // made, using "high" precision. All scheduling is affected by OS-specific |
| 92 | // leeway and current workloads which means that in terms of precision there |
| 93 | // are no hard guarantees. |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 94 | // |
| 95 | // The task may execute with [-1, OS induced leeway] ms additional delay. |
| 96 | // |
| 97 | // Avoid making assumptions about the precision of the OS scheduler. On macOS, |
| 98 | // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms |
| 99 | // precision timers may be used but there are cases, such as when running on |
| 100 | // battery, when the timer precision can be as poor as 15 ms. |
| 101 | // |
| 102 | // May be called on any thread or task queue, including this task queue. |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 103 | virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task, |
Danil Chapovalov | c0ce454 | 2022-08-09 11:24:52 +0200 | [diff] [blame^] | 104 | TimeDelta delay) = 0; |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 105 | |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 106 | // As specified by `precision`, calls either PostDelayedTask() or |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 107 | // PostDelayedHighPrecisionTask(). |
| 108 | void PostDelayedTaskWithPrecision(DelayPrecision precision, |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 109 | absl::AnyInvocable<void() &&> task, |
| 110 | TimeDelta delay) { |
| 111 | switch (precision) { |
| 112 | case DelayPrecision::kLow: |
| 113 | PostDelayedTask(std::move(task), delay); |
| 114 | break; |
| 115 | case DelayPrecision::kHigh: |
| 116 | PostDelayedHighPrecisionTask(std::move(task), delay); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 121 | // Returns the task queue that is running the current thread. |
| 122 | // Returns nullptr if this thread is not associated with any task queue. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 123 | // May be called on any thread or task queue, including this task queue. |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 124 | static TaskQueueBase* Current(); |
| 125 | bool IsCurrent() const { return Current() == this; } |
| 126 | |
| 127 | protected: |
Evan Shrubsole | 5b8dc1d | 2022-05-19 12:59:04 +0200 | [diff] [blame] | 128 | class RTC_EXPORT CurrentTaskQueueSetter { |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 129 | public: |
| 130 | explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue); |
| 131 | CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete; |
| 132 | CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete; |
| 133 | ~CurrentTaskQueueSetter(); |
| 134 | |
| 135 | private: |
| 136 | TaskQueueBase* const previous_; |
| 137 | }; |
| 138 | |
| 139 | // Users of the TaskQueue should call Delete instead of directly deleting |
| 140 | // this object. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 141 | virtual ~TaskQueueBase() = default; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | struct TaskQueueDeleter { |
| 145 | void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); } |
| 146 | }; |
| 147 | |
| 148 | } // namespace webrtc |
| 149 | |
| 150 | #endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_ |