blob: f78600de6028083057a2fc93ceee43b683dbcf57 [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 Chapovalov8feb6fd2022-07-05 11:01:27 +020017#include "api/units/time_delta.h"
Mirko Bonadeid4002a72019-11-12 20:11:48 +010018#include "rtc_base/system/rtc_export.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010019#include "rtc_base/thread_annotations.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010020
Danil Chapovalov348b08a2019-01-17 13:07:25 +010021namespace 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 Bonadeid4002a72019-11-12 20:11:48 +010027class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010028 public:
Henrik Boström27e8a092022-01-24 17:12:35 +010029 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 Chapovalov348b08a2019-01-17 13:07:25 +010039 // 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 Chapovalov6cdb67f2021-01-18 17:02:55 +010042 // Responsible for deallocation. Deallocation may happen synchronously during
Danil Chapovalov348b08a2019-01-17 13:07:25 +010043 // 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 Chapovalov6cdb67f2021-01-18 17:02:55 +010048 // Should be called on the same task queue or thread that this task queue
49 // was created on.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010050 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010051
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020052 // Schedules a `task` to execute. Tasks are executed in FIFO order.
Danil Chapovalov348b08a2019-01-17 13:07:25 +010053 // When a TaskQueue is deleted, pending tasks will not be executed but they
Markus Handell82da9322022-12-16 15:50:24 +010054 // will be deleted.
55 //
56 // As long as tasks are not posted from task destruction, posted tasks are
57 // guaranteed to be destroyed with Current() pointing to the task queue they
58 // were posted to, whether they're executed or not. That means SequenceChecker
59 // works during task destruction, a fact that can be used to guarantee
60 // thread-compatible object deletion happening on a particular task queue
61 // which can simplify class design.
62 // Note that this guarantee does not apply to delayed tasks.
63 //
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010064 // May be called on any thread or task queue, including this task queue.
Danil Chapovalovc0ce4542022-08-09 11:24:52 +020065 virtual void PostTask(absl::AnyInvocable<void() &&> task) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010066
Henrik Boströmcf9899c2022-01-20 09:46:16 +010067 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
68 // possible.
69 //
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020070 // Schedules a `task` to execute a specified `delay` from when the call is
71 // made, using "low" precision. All scheduling is affected by OS-specific
72 // leeway and current workloads which means that in terms of precision there
73 // are no hard guarantees, but in addition to the OS induced leeway, "low"
74 // precision adds up to a 17 ms additional leeway. The purpose of this leeway
75 // is to achieve more efficient CPU scheduling and reduce Idle Wake Up
76 // frequency.
Henrik Boströmcf9899c2022-01-20 09:46:16 +010077 //
78 // The task may execute with [-1, 17 + OS induced leeway) ms additional delay.
79 //
80 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
81 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
82 // precision timers may be used but there are cases, such as when running on
83 // battery, when the timer precision can be as poor as 15 ms.
84 //
85 // "Low" precision is not implemented everywhere yet. Where not yet
86 // implemented, PostDelayedTask() has "high" precision. See
87 // https://crbug.com/webrtc/13583 for more information.
88 //
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010089 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020090 virtual void PostDelayedTask(absl::AnyInvocable<void() &&> task,
Danil Chapovalovc0ce4542022-08-09 11:24:52 +020091 TimeDelta delay) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010092
Henrik Boströmcf9899c2022-01-20 09:46:16 +010093 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
94 // possible.
95 //
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +020096 // Schedules a `task` to execute a specified `delay` from when the call is
97 // made, using "high" precision. All scheduling is affected by OS-specific
98 // leeway and current workloads which means that in terms of precision there
99 // are no hard guarantees.
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100100 //
101 // The task may execute with [-1, OS induced leeway] ms additional delay.
102 //
103 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
104 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
105 // precision timers may be used but there are cases, such as when running on
106 // battery, when the timer precision can be as poor as 15 ms.
107 //
108 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200109 virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
Danil Chapovalovc0ce4542022-08-09 11:24:52 +0200110 TimeDelta delay) = 0;
Henrik Boströmcf9899c2022-01-20 09:46:16 +0100111
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200112 // As specified by `precision`, calls either PostDelayedTask() or
Henrik Boström27e8a092022-01-24 17:12:35 +0100113 // PostDelayedHighPrecisionTask().
114 void PostDelayedTaskWithPrecision(DelayPrecision precision,
Danil Chapovalov8feb6fd2022-07-05 11:01:27 +0200115 absl::AnyInvocable<void() &&> task,
116 TimeDelta delay) {
117 switch (precision) {
118 case DelayPrecision::kLow:
119 PostDelayedTask(std::move(task), delay);
120 break;
121 case DelayPrecision::kHigh:
122 PostDelayedHighPrecisionTask(std::move(task), delay);
123 break;
124 }
125 }
126
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100127 // Returns the task queue that is running the current thread.
128 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +0100129 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100130 static TaskQueueBase* Current();
131 bool IsCurrent() const { return Current() == this; }
132
133 protected:
Evan Shrubsole5b8dc1d2022-05-19 12:59:04 +0200134 class RTC_EXPORT CurrentTaskQueueSetter {
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100135 public:
136 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
137 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
138 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
139 ~CurrentTaskQueueSetter();
140
141 private:
142 TaskQueueBase* const previous_;
143 };
144
145 // Users of the TaskQueue should call Delete instead of directly deleting
146 // this object.
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100147 virtual ~TaskQueueBase() = default;
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100148};
149
150struct TaskQueueDeleter {
151 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
152};
153
154} // namespace webrtc
155
156#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_