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