blob: df422dcdc6a0e3561633442631eacafb02429a16 [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
16#include "api/task_queue/queued_task.h"
Mirko Bonadeid4002a72019-11-12 20:11:48 +010017#include "rtc_base/system/rtc_export.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010018#include "rtc_base/thread_annotations.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010019
Danil Chapovalov348b08a2019-01-17 13:07:25 +010020namespace 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 Bonadeid4002a72019-11-12 20:11:48 +010026class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010027 public:
28 // Starts destruction of the task queue.
29 // On return ensures no task are running and no new tasks are able to start
30 // on the task queue.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010031 // Responsible for deallocation. Deallocation may happen synchronously during
Danil Chapovalov348b08a2019-01-17 13:07:25 +010032 // Delete or asynchronously after Delete returns.
33 // Code not running on the TaskQueue should not make any assumption when
34 // TaskQueue is deallocated and thus should not call any methods after Delete.
35 // Code running on the TaskQueue should not call Delete, but can assume
36 // TaskQueue still exists and may call other methods, e.g. PostTask.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010037 // Should be called on the same task queue or thread that this task queue
38 // was created on.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010039 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010040
41 // Schedules a task to execute. Tasks are executed in FIFO order.
Artem Titovcfea2182021-08-10 01:22:31 +020042 // If `task->Run()` returns true, task is deleted on the task queue
Danil Chapovalov348b08a2019-01-17 13:07:25 +010043 // before next QueuedTask starts executing.
44 // When a TaskQueue is deleted, pending tasks will not be executed but they
45 // will be deleted. The deletion of tasks may happen synchronously on the
46 // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
47 // This may vary from one implementation to the next so assumptions about
48 // lifetimes of pending tasks should not be made.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010049 // May be called on any thread or task queue, including this task queue.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010050 virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010051
Henrik Boströmcf9899c2022-01-20 09:46:16 +010052 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
53 // possible.
54 //
Danil Chapovalov348b08a2019-01-17 13:07:25 +010055 // Schedules a task to execute a specified number of milliseconds from when
Henrik Boströmcf9899c2022-01-20 09:46:16 +010056 // the call is made, using "low" precision. All scheduling is affected by
57 // OS-specific leeway and current workloads which means that in terms of
58 // precision there are no hard guarantees, but in addition to the OS induced
59 // leeway, "low" precision adds up to a 17 ms additional leeway. The purpose
60 // of this leeway is to achieve more efficient CPU scheduling and reduce Idle
61 // Wake Up frequency.
62 //
63 // The task may execute with [-1, 17 + OS induced leeway) ms additional delay.
64 //
65 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
66 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
67 // precision timers may be used but there are cases, such as when running on
68 // battery, when the timer precision can be as poor as 15 ms.
69 //
70 // "Low" precision is not implemented everywhere yet. Where not yet
71 // implemented, PostDelayedTask() has "high" precision. See
72 // https://crbug.com/webrtc/13583 for more information.
73 //
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +010074 // May be called on any thread or task queue, including this task queue.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010075 virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
76 uint32_t milliseconds) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010077
Henrik Boströmcf9899c2022-01-20 09:46:16 +010078 // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
79 // possible.
80 //
81 // Schedules a task to execute a specified number of milliseconds from when
82 // the call is made, using "high" precision. All scheduling is affected by
83 // OS-specific leeway and current workloads which means that in terms of
84 // precision there are no hard guarantees.
85 //
86 // The task may execute with [-1, OS induced leeway] ms additional delay.
87 //
88 // Avoid making assumptions about the precision of the OS scheduler. On macOS,
89 // the OS induced leeway may be 10% of sleep interval. On Windows, 1 ms
90 // precision timers may be used but there are cases, such as when running on
91 // battery, when the timer precision can be as poor as 15 ms.
92 //
93 // May be called on any thread or task queue, including this task queue.
94 virtual void PostDelayedHighPrecisionTask(std::unique_ptr<QueuedTask> task,
95 uint32_t milliseconds) {
96 // Remove default implementation when dependencies have implemented this
97 // method.
98 PostDelayedTask(std::move(task), milliseconds);
99 }
100
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100101 // Returns the task queue that is running the current thread.
102 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov6cdb67f2021-01-18 17:02:55 +0100103 // May be called on any thread or task queue, including this task queue.
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100104 static TaskQueueBase* Current();
105 bool IsCurrent() const { return Current() == this; }
106
107 protected:
108 class CurrentTaskQueueSetter {
109 public:
110 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
111 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
112 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
113 ~CurrentTaskQueueSetter();
114
115 private:
116 TaskQueueBase* const previous_;
117 };
118
119 // Users of the TaskQueue should call Delete instead of directly deleting
120 // this object.
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100121 virtual ~TaskQueueBase() = default;
Danil Chapovalov348b08a2019-01-17 13:07:25 +0100122};
123
124struct TaskQueueDeleter {
125 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
126};
127
128} // namespace webrtc
129
130#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_