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 | |
| 15 | #include "webrtc/base/task_queue.h" |
| 16 | |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include "webrtc/base/checks.h" |
| 20 | #include "webrtc/base/logging.h" |
| 21 | #include "webrtc/base/task_queue_posix.h" |
| 22 | |
| 23 | namespace rtc { |
| 24 | using internal::GetQueuePtrTls; |
| 25 | using internal::AutoSetCurrentQueuePtr; |
| 26 | |
| 27 | struct TaskQueue::QueueContext { |
| 28 | explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} |
| 29 | |
| 30 | static void SetNotActive(void* context) { |
| 31 | QueueContext* qc = static_cast<QueueContext*>(context); |
| 32 | qc->is_active = false; |
| 33 | } |
| 34 | |
| 35 | static void DeleteContext(void* context) { |
| 36 | QueueContext* qc = static_cast<QueueContext*>(context); |
| 37 | delete qc; |
| 38 | } |
| 39 | |
| 40 | TaskQueue* const queue; |
| 41 | bool is_active; |
| 42 | }; |
| 43 | |
| 44 | struct TaskQueue::TaskContext { |
| 45 | TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) |
| 46 | : queue_ctx(queue_ctx), task(std::move(task)) {} |
| 47 | virtual ~TaskContext() {} |
| 48 | |
| 49 | static void RunTask(void* context) { |
| 50 | std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(context)); |
| 51 | if (tc->queue_ctx->is_active) { |
| 52 | AutoSetCurrentQueuePtr set_current(tc->queue_ctx->queue); |
| 53 | if (!tc->task->Run()) |
| 54 | tc->task.release(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | QueueContext* const queue_ctx; |
| 59 | std::unique_ptr<QueuedTask> task; |
| 60 | }; |
| 61 | |
| 62 | // Special case context for holding two tasks, a |first_task| + the task |
| 63 | // that's owned by the parent struct, TaskContext, that then becomes the |
| 64 | // second (i.e. 'reply') task. |
| 65 | struct TaskQueue::PostTaskAndReplyContext : public TaskQueue::TaskContext { |
| 66 | explicit PostTaskAndReplyContext(QueueContext* first_queue_ctx, |
| 67 | std::unique_ptr<QueuedTask> first_task, |
| 68 | QueueContext* second_queue_ctx, |
| 69 | std::unique_ptr<QueuedTask> second_task) |
| 70 | : TaskContext(second_queue_ctx, std::move(second_task)), |
| 71 | first_queue_ctx(first_queue_ctx), |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame^] | 72 | first_task(std::move(first_task)), |
| 73 | reply_queue_(second_queue_ctx->queue->queue_) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 74 | // Retain the reply queue for as long as this object lives. |
| 75 | // If we don't, we may have memory leaks and/or failures. |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame^] | 76 | dispatch_retain(reply_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 77 | } |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame^] | 78 | ~PostTaskAndReplyContext() override { dispatch_release(reply_queue_); } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 79 | |
| 80 | static void RunTask(void* context) { |
| 81 | auto* rc = static_cast<PostTaskAndReplyContext*>(context); |
| 82 | if (rc->first_queue_ctx->is_active) { |
| 83 | AutoSetCurrentQueuePtr set_current(rc->first_queue_ctx->queue); |
| 84 | if (!rc->first_task->Run()) |
| 85 | rc->first_task.release(); |
| 86 | } |
| 87 | // Post the reply task. This hands the work over to the parent struct. |
| 88 | // This task will eventually delete |this|. |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame^] | 89 | dispatch_async_f(rc->reply_queue_, rc, &TaskContext::RunTask); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | QueueContext* const first_queue_ctx; |
| 93 | std::unique_ptr<QueuedTask> first_task; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame^] | 94 | dispatch_queue_t reply_queue_; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | TaskQueue::TaskQueue(const char* queue_name) |
| 98 | : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), |
| 99 | context_(new QueueContext(this)) { |
| 100 | RTC_DCHECK(queue_name); |
| 101 | RTC_CHECK(queue_); |
| 102 | dispatch_set_context(queue_, context_); |
| 103 | // Assign a finalizer that will delete the context when the last reference |
| 104 | // to the queue is released. This may run after the TaskQueue object has |
| 105 | // been deleted. |
| 106 | dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); |
| 107 | } |
| 108 | |
| 109 | TaskQueue::~TaskQueue() { |
| 110 | RTC_DCHECK(!IsCurrent()); |
| 111 | // Implementation/behavioral note: |
| 112 | // Dispatch queues are reference counted via calls to dispatch_retain and |
| 113 | // dispatch_release. Pending blocks submitted to a queue also hold a |
| 114 | // reference to the queue until they have finished. Once all references to a |
| 115 | // queue have been released, the queue will be deallocated by the system. |
| 116 | // This is why we check the context before running tasks. |
| 117 | |
| 118 | // Use dispatch_sync to set the context to null to guarantee that there's not |
| 119 | // a race between checking the context and using it from a task. |
| 120 | dispatch_sync_f(queue_, context_, &QueueContext::SetNotActive); |
| 121 | dispatch_release(queue_); |
| 122 | } |
| 123 | |
| 124 | // static |
| 125 | TaskQueue* TaskQueue::Current() { |
| 126 | return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); |
| 127 | } |
| 128 | |
| 129 | // static |
| 130 | bool TaskQueue::IsCurrent(const char* queue_name) { |
| 131 | TaskQueue* current = Current(); |
| 132 | return current && |
| 133 | strcmp(queue_name, dispatch_queue_get_label(current->queue_)) == 0; |
| 134 | } |
| 135 | |
| 136 | bool TaskQueue::IsCurrent() const { |
| 137 | RTC_DCHECK(queue_); |
| 138 | return this == Current(); |
| 139 | } |
| 140 | |
| 141 | void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { |
| 142 | auto* context = new TaskContext(context_, std::move(task)); |
| 143 | dispatch_async_f(queue_, context, &TaskContext::RunTask); |
| 144 | } |
| 145 | |
| 146 | void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 147 | uint32_t milliseconds) { |
| 148 | auto* context = new TaskContext(context_, std::move(task)); |
| 149 | dispatch_after_f( |
| 150 | dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_, |
| 151 | context, &TaskContext::RunTask); |
| 152 | } |
| 153 | |
| 154 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 155 | std::unique_ptr<QueuedTask> reply, |
| 156 | TaskQueue* reply_queue) { |
| 157 | auto* context = new PostTaskAndReplyContext( |
| 158 | context_, std::move(task), reply_queue->context_, std::move(reply)); |
| 159 | dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); |
| 160 | } |
| 161 | |
| 162 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 163 | std::unique_ptr<QueuedTask> reply) { |
| 164 | return PostTaskAndReply(std::move(task), std::move(reply), Current()); |
| 165 | } |
| 166 | |
| 167 | } // namespace rtc |