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