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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/task_queue.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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 23 | #include "rtc_base/ref_count.h" |
| 24 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/task_queue_posix.h" |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 26 | |
| 27 | namespace rtc { |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | using Priority = TaskQueue::Priority; |
| 31 | |
| 32 | int TaskQueuePriorityToGCD(Priority priority) { |
| 33 | switch (priority) { |
| 34 | case Priority::NORMAL: |
| 35 | return DISPATCH_QUEUE_PRIORITY_DEFAULT; |
| 36 | case Priority::HIGH: |
| 37 | return DISPATCH_QUEUE_PRIORITY_HIGH; |
| 38 | case Priority::LOW: |
| 39 | return DISPATCH_QUEUE_PRIORITY_LOW; |
| 40 | } |
| 41 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | } // namespace |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 43 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 44 | using internal::GetQueuePtrTls; |
| 45 | using internal::AutoSetCurrentQueuePtr; |
| 46 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 47 | class TaskQueue::Impl : public RefCountInterface { |
| 48 | public: |
| 49 | Impl(const char* queue_name, TaskQueue* task_queue, Priority priority); |
| 50 | ~Impl() override; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 51 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 52 | static TaskQueue* Current(); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 53 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 54 | // Used for DCHECKing the current queue. |
| 55 | bool IsCurrent() const; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 56 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 57 | void PostTask(std::unique_ptr<QueuedTask> task); |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 58 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 59 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 60 | private: |
| 61 | struct QueueContext { |
| 62 | explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} |
| 63 | |
| 64 | static void SetNotActive(void* context) { |
| 65 | QueueContext* qc = static_cast<QueueContext*>(context); |
| 66 | qc->is_active = false; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 67 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 68 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 69 | static void DeleteContext(void* context) { |
| 70 | QueueContext* qc = static_cast<QueueContext*>(context); |
| 71 | delete qc; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 72 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 73 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 74 | TaskQueue* const queue; |
| 75 | bool is_active; |
| 76 | }; |
| 77 | |
| 78 | struct TaskContext { |
| 79 | TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) |
| 80 | : queue_ctx(queue_ctx), task(std::move(task)) {} |
| 81 | virtual ~TaskContext() {} |
| 82 | |
| 83 | static void RunTask(void* context) { |
| 84 | std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(context)); |
| 85 | if (tc->queue_ctx->is_active) { |
| 86 | AutoSetCurrentQueuePtr set_current(tc->queue_ctx->queue); |
| 87 | if (!tc->task->Run()) |
| 88 | tc->task.release(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | QueueContext* const queue_ctx; |
| 93 | std::unique_ptr<QueuedTask> task; |
| 94 | }; |
| 95 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 96 | dispatch_queue_t queue_; |
| 97 | QueueContext* const context_; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 100 | TaskQueue::Impl::Impl(const char* queue_name, |
| 101 | TaskQueue* task_queue, |
| 102 | Priority priority) |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 103 | : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 104 | context_(new QueueContext(task_queue)) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 105 | RTC_DCHECK(queue_name); |
| 106 | RTC_CHECK(queue_); |
| 107 | dispatch_set_context(queue_, context_); |
| 108 | // Assign a finalizer that will delete the context when the last reference |
| 109 | // to the queue is released. This may run after the TaskQueue object has |
| 110 | // been deleted. |
| 111 | dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 112 | |
| 113 | dispatch_set_target_queue( |
| 114 | queue_, dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 115 | } |
| 116 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 117 | TaskQueue::Impl::~Impl() { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 118 | RTC_DCHECK(!IsCurrent()); |
| 119 | // Implementation/behavioral note: |
| 120 | // Dispatch queues are reference counted via calls to dispatch_retain and |
| 121 | // dispatch_release. Pending blocks submitted to a queue also hold a |
| 122 | // reference to the queue until they have finished. Once all references to a |
| 123 | // queue have been released, the queue will be deallocated by the system. |
| 124 | // This is why we check the context before running tasks. |
| 125 | |
| 126 | // Use dispatch_sync to set the context to null to guarantee that there's not |
| 127 | // a race between checking the context and using it from a task. |
| 128 | dispatch_sync_f(queue_, context_, &QueueContext::SetNotActive); |
| 129 | dispatch_release(queue_); |
| 130 | } |
| 131 | |
| 132 | // static |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 133 | TaskQueue* TaskQueue::Impl::Current() { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 134 | return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); |
| 135 | } |
| 136 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 137 | bool TaskQueue::Impl::IsCurrent() const { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 138 | RTC_DCHECK(queue_); |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 139 | const TaskQueue* current = Current(); |
| 140 | return current && this == current->impl_.get(); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 143 | void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 144 | auto* context = new TaskContext(context_, std::move(task)); |
| 145 | dispatch_async_f(queue_, context, &TaskContext::RunTask); |
| 146 | } |
| 147 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 148 | void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 149 | uint32_t milliseconds) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 150 | auto* context = new TaskContext(context_, std::move(task)); |
| 151 | dispatch_after_f( |
| 152 | dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_, |
| 153 | context, &TaskContext::RunTask); |
| 154 | } |
| 155 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 156 | // Boilerplate for the PIMPL pattern. |
| 157 | TaskQueue::TaskQueue(const char* queue_name, Priority priority) |
| 158 | : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) { |
| 159 | } |
| 160 | |
| 161 | TaskQueue::~TaskQueue() {} |
| 162 | |
| 163 | // static |
| 164 | TaskQueue* TaskQueue::Current() { |
| 165 | return TaskQueue::Impl::Current(); |
| 166 | } |
| 167 | |
| 168 | // Used for DCHECKing the current queue. |
| 169 | bool TaskQueue::IsCurrent() const { |
| 170 | return impl_->IsCurrent(); |
| 171 | } |
| 172 | |
| 173 | void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { |
| 174 | return TaskQueue::impl_->PostTask(std::move(task)); |
| 175 | } |
| 176 | |
nisse | 66d07c5 | 2017-09-08 05:12:51 -0700 | [diff] [blame] | 177 | void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 178 | uint32_t milliseconds) { |
| 179 | return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | } // namespace rtc |