tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 11 | // This file contains the implementation of TaskQueue for Mac and iOS. |
| 12 | // The implementation uses Grand Central Dispatch queues (GCD) to |
| 13 | // do the actual task queuing. |
| 14 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 15 | #include "rtc_base/task_queue_gcd.h" |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 16 | |
| 17 | #include <string.h> |
| 18 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 19 | #include <dispatch/dispatch.h> |
| 20 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
| 22 | #include "absl/strings/string_view.h" |
| 23 | #include "api/task_queue/queued_task.h" |
| 24 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/logging.h" |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 27 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 28 | namespace webrtc { |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 31 | int TaskQueuePriorityToGCD(TaskQueueFactory::Priority priority) { |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 32 | switch (priority) { |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 33 | case TaskQueueFactory::Priority::NORMAL: |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 34 | return DISPATCH_QUEUE_PRIORITY_DEFAULT; |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 35 | case TaskQueueFactory::Priority::HIGH: |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 36 | return DISPATCH_QUEUE_PRIORITY_HIGH; |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 37 | case TaskQueueFactory::Priority::LOW: |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 38 | return DISPATCH_QUEUE_PRIORITY_LOW; |
| 39 | } |
| 40 | } |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 41 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 42 | class TaskQueueGcd : public TaskQueueBase { |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 43 | public: |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 44 | TaskQueueGcd(absl::string_view queue_name, int gcd_priority); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 45 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 46 | void Delete() override; |
| 47 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
| 48 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 49 | uint32_t milliseconds) override; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 50 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 51 | private: |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 52 | struct TaskContext { |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 53 | TaskContext(TaskQueueGcd* queue, std::unique_ptr<QueuedTask> task) |
| 54 | : queue(queue), task(std::move(task)) {} |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 55 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 56 | TaskQueueGcd* const queue; |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 57 | std::unique_ptr<QueuedTask> task; |
| 58 | }; |
| 59 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 60 | ~TaskQueueGcd() override; |
| 61 | static void RunTask(void* task_context); |
| 62 | static void SetNotActive(void* task_queue); |
| 63 | static void DeleteQueue(void* task_queue); |
| 64 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 65 | dispatch_queue_t queue_; |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 66 | bool is_active_; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 69 | TaskQueueGcd::TaskQueueGcd(absl::string_view queue_name, int gcd_priority) |
| 70 | : queue_(dispatch_queue_create(std::string(queue_name).c_str(), |
| 71 | DISPATCH_QUEUE_SERIAL)), |
| 72 | is_active_(true) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 73 | RTC_CHECK(queue_); |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 74 | dispatch_set_context(queue_, this); |
| 75 | // Assign a finalizer that will delete the queue when the last reference |
| 76 | // is released. This may run after the TaskQueue::Delete. |
| 77 | dispatch_set_finalizer_f(queue_, &DeleteQueue); |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 78 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 79 | dispatch_set_target_queue(queue_, dispatch_get_global_queue(gcd_priority, 0)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 82 | TaskQueueGcd::~TaskQueueGcd() = default; |
| 83 | |
| 84 | void TaskQueueGcd::Delete() { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 85 | RTC_DCHECK(!IsCurrent()); |
| 86 | // Implementation/behavioral note: |
| 87 | // Dispatch queues are reference counted via calls to dispatch_retain and |
| 88 | // dispatch_release. Pending blocks submitted to a queue also hold a |
| 89 | // reference to the queue until they have finished. Once all references to a |
| 90 | // queue have been released, the queue will be deallocated by the system. |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 91 | // This is why we check the is_active_ before running tasks. |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 92 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 93 | // Use dispatch_sync to set the is_active_ to guarantee that there's not a |
| 94 | // race with checking it from a task. |
| 95 | dispatch_sync_f(queue_, this, &SetNotActive); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 96 | dispatch_release(queue_); |
| 97 | } |
| 98 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 99 | void TaskQueueGcd::PostTask(std::unique_ptr<QueuedTask> task) { |
| 100 | auto* context = new TaskContext(this, std::move(task)); |
| 101 | dispatch_async_f(queue_, context, &RunTask); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 104 | void TaskQueueGcd::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 105 | uint32_t milliseconds) { |
| 106 | auto* context = new TaskContext(this, std::move(task)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 107 | dispatch_after_f( |
| 108 | dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_, |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 109 | context, &RunTask); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 110 | } |
| 111 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 112 | // static |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 113 | void TaskQueueGcd::RunTask(void* task_context) { |
| 114 | std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(task_context)); |
| 115 | if (!tc->queue->is_active_) |
| 116 | return; |
| 117 | |
| 118 | CurrentTaskQueueSetter set_current(tc->queue); |
| 119 | auto* task = tc->task.release(); |
| 120 | if (task->Run()) { |
| 121 | // Delete the task before CurrentTaskQueueSetter clears state that this code |
| 122 | // is running on the task queue. |
| 123 | delete task; |
| 124 | } |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 127 | // static |
| 128 | void TaskQueueGcd::SetNotActive(void* task_queue) { |
| 129 | static_cast<TaskQueueGcd*>(task_queue)->is_active_ = false; |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 132 | // static |
| 133 | void TaskQueueGcd::DeleteQueue(void* task_queue) { |
| 134 | delete static_cast<TaskQueueGcd*>(task_queue); |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 137 | class TaskQueueGcdFactory final : public TaskQueueFactory { |
| 138 | public: |
| 139 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 140 | absl::string_view name, |
| 141 | Priority priority) const override { |
| 142 | return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>( |
| 143 | new TaskQueueGcd(name, TaskQueuePriorityToGCD(priority))); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | } // namespace |
| 148 | |
| 149 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory() { |
| 150 | return absl::make_unique<TaskQueueGcdFactory>(); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 153 | } // namespace webrtc |