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> |
| 14 | |
| 15 | #include "api/task_queue/queued_task.h" |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 16 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 17 | #include "rtc_base/thread_annotations.h" |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 18 | |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | // Asynchronously executes tasks in a way that guarantees that they're executed |
| 22 | // in FIFO order and that tasks never overlap. Tasks may always execute on the |
| 23 | // same worker thread and they may not. To DCHECK that tasks are executing on a |
| 24 | // known task queue, use IsCurrent(). |
Mirko Bonadei | d4002a7 | 2019-11-12 20:11:48 +0100 | [diff] [blame] | 25 | class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 26 | public: |
| 27 | // Starts destruction of the task queue. |
| 28 | // On return ensures no task are running and no new tasks are able to start |
| 29 | // on the task queue. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 30 | // Responsible for deallocation. Deallocation may happen synchronously during |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 31 | // Delete or asynchronously after Delete returns. |
| 32 | // Code not running on the TaskQueue should not make any assumption when |
| 33 | // TaskQueue is deallocated and thus should not call any methods after Delete. |
| 34 | // Code running on the TaskQueue should not call Delete, but can assume |
| 35 | // TaskQueue still exists and may call other methods, e.g. PostTask. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 36 | // Should be called on the same task queue or thread that this task queue |
| 37 | // was created on. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 38 | virtual void Delete() = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 39 | |
| 40 | // Schedules a task to execute. Tasks are executed in FIFO order. |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 41 | // If `task->Run()` returns true, task is deleted on the task queue |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 42 | // before next QueuedTask starts executing. |
| 43 | // When a TaskQueue is deleted, pending tasks will not be executed but they |
| 44 | // will be deleted. The deletion of tasks may happen synchronously on the |
| 45 | // TaskQueue or it may happen asynchronously after TaskQueue is deleted. |
| 46 | // This may vary from one implementation to the next so assumptions about |
| 47 | // lifetimes of pending tasks should not be made. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 48 | // 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] | 49 | virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 50 | |
| 51 | // Schedules a task to execute a specified number of milliseconds from when |
| 52 | // the call is made. The precision should be considered as "best effort" |
| 53 | // and in some cases, such as on Windows when all high precision timers have |
| 54 | // been used up, can be off by as much as 15 millseconds. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 55 | // 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] | 56 | virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 57 | uint32_t milliseconds) = 0; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 58 | |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 59 | // Returns the task queue that is running the current thread. |
| 60 | // Returns nullptr if this thread is not associated with any task queue. |
Danil Chapovalov | 6cdb67f | 2021-01-18 17:02:55 +0100 | [diff] [blame] | 61 | // 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] | 62 | static TaskQueueBase* Current(); |
| 63 | bool IsCurrent() const { return Current() == this; } |
| 64 | |
| 65 | protected: |
| 66 | class CurrentTaskQueueSetter { |
| 67 | public: |
| 68 | explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue); |
| 69 | CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete; |
| 70 | CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete; |
| 71 | ~CurrentTaskQueueSetter(); |
| 72 | |
| 73 | private: |
| 74 | TaskQueueBase* const previous_; |
| 75 | }; |
| 76 | |
| 77 | // Users of the TaskQueue should call Delete instead of directly deleting |
| 78 | // this object. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 79 | virtual ~TaskQueueBase() = default; |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct TaskQueueDeleter { |
| 83 | void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); } |
| 84 | }; |
| 85 | |
| 86 | } // namespace webrtc |
| 87 | |
| 88 | #endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_ |