blob: 4094dd5192b58ca1662e9aac09f3401f0ecff755 [file] [log] [blame]
tommic06b1332016-05-14 11:31:40 -07001/*
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
kjellandere96c45b2017-06-30 10:45:21 -070015#include "webrtc/rtc_base/task_queue.h"
tommic06b1332016-05-14 11:31:40 -070016
17#include <string.h>
18
kjellandere96c45b2017-06-30 10:45:21 -070019#include "webrtc/rtc_base/checks.h"
20#include "webrtc/rtc_base/logging.h"
21#include "webrtc/rtc_base/task_queue_posix.h"
tommic06b1332016-05-14 11:31:40 -070022
23namespace rtc {
tommic9bb7912017-02-24 10:42:14 -080024namespace {
25
26using Priority = TaskQueue::Priority;
27
28int TaskQueuePriorityToGCD(Priority priority) {
29 switch (priority) {
30 case Priority::NORMAL:
31 return DISPATCH_QUEUE_PRIORITY_DEFAULT;
32 case Priority::HIGH:
33 return DISPATCH_QUEUE_PRIORITY_HIGH;
34 case Priority::LOW:
35 return DISPATCH_QUEUE_PRIORITY_LOW;
36 }
37}
38}
39
tommic06b1332016-05-14 11:31:40 -070040using internal::GetQueuePtrTls;
41using internal::AutoSetCurrentQueuePtr;
42
43struct TaskQueue::QueueContext {
44 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {}
45
46 static void SetNotActive(void* context) {
47 QueueContext* qc = static_cast<QueueContext*>(context);
48 qc->is_active = false;
49 }
50
51 static void DeleteContext(void* context) {
52 QueueContext* qc = static_cast<QueueContext*>(context);
53 delete qc;
54 }
55
56 TaskQueue* const queue;
57 bool is_active;
58};
59
60struct TaskQueue::TaskContext {
61 TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task)
62 : queue_ctx(queue_ctx), task(std::move(task)) {}
63 virtual ~TaskContext() {}
64
65 static void RunTask(void* context) {
66 std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(context));
67 if (tc->queue_ctx->is_active) {
68 AutoSetCurrentQueuePtr set_current(tc->queue_ctx->queue);
69 if (!tc->task->Run())
70 tc->task.release();
71 }
72 }
73
74 QueueContext* const queue_ctx;
75 std::unique_ptr<QueuedTask> task;
76};
77
78// Special case context for holding two tasks, a |first_task| + the task
79// that's owned by the parent struct, TaskContext, that then becomes the
80// second (i.e. 'reply') task.
81struct TaskQueue::PostTaskAndReplyContext : public TaskQueue::TaskContext {
82 explicit PostTaskAndReplyContext(QueueContext* first_queue_ctx,
83 std::unique_ptr<QueuedTask> first_task,
84 QueueContext* second_queue_ctx,
85 std::unique_ptr<QueuedTask> second_task)
86 : TaskContext(second_queue_ctx, std::move(second_task)),
87 first_queue_ctx(first_queue_ctx),
tommi8c80c6e2017-02-23 00:34:52 -080088 first_task(std::move(first_task)),
89 reply_queue_(second_queue_ctx->queue->queue_) {
tommic06b1332016-05-14 11:31:40 -070090 // Retain the reply queue for as long as this object lives.
91 // If we don't, we may have memory leaks and/or failures.
tommi8c80c6e2017-02-23 00:34:52 -080092 dispatch_retain(reply_queue_);
tommic06b1332016-05-14 11:31:40 -070093 }
tommi8c80c6e2017-02-23 00:34:52 -080094 ~PostTaskAndReplyContext() override { dispatch_release(reply_queue_); }
tommic06b1332016-05-14 11:31:40 -070095
96 static void RunTask(void* context) {
97 auto* rc = static_cast<PostTaskAndReplyContext*>(context);
98 if (rc->first_queue_ctx->is_active) {
99 AutoSetCurrentQueuePtr set_current(rc->first_queue_ctx->queue);
100 if (!rc->first_task->Run())
101 rc->first_task.release();
102 }
103 // Post the reply task. This hands the work over to the parent struct.
104 // This task will eventually delete |this|.
tommi8c80c6e2017-02-23 00:34:52 -0800105 dispatch_async_f(rc->reply_queue_, rc, &TaskContext::RunTask);
tommic06b1332016-05-14 11:31:40 -0700106 }
107
108 QueueContext* const first_queue_ctx;
109 std::unique_ptr<QueuedTask> first_task;
tommi8c80c6e2017-02-23 00:34:52 -0800110 dispatch_queue_t reply_queue_;
tommic06b1332016-05-14 11:31:40 -0700111};
112
tommic9bb7912017-02-24 10:42:14 -0800113TaskQueue::TaskQueue(const char* queue_name, Priority priority /*= NORMAL*/)
tommic06b1332016-05-14 11:31:40 -0700114 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)),
115 context_(new QueueContext(this)) {
116 RTC_DCHECK(queue_name);
117 RTC_CHECK(queue_);
118 dispatch_set_context(queue_, context_);
119 // Assign a finalizer that will delete the context when the last reference
120 // to the queue is released. This may run after the TaskQueue object has
121 // been deleted.
122 dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext);
tommic9bb7912017-02-24 10:42:14 -0800123
124 dispatch_set_target_queue(
125 queue_, dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0));
tommic06b1332016-05-14 11:31:40 -0700126}
127
128TaskQueue::~TaskQueue() {
129 RTC_DCHECK(!IsCurrent());
130 // Implementation/behavioral note:
131 // Dispatch queues are reference counted via calls to dispatch_retain and
132 // dispatch_release. Pending blocks submitted to a queue also hold a
133 // reference to the queue until they have finished. Once all references to a
134 // queue have been released, the queue will be deallocated by the system.
135 // This is why we check the context before running tasks.
136
137 // Use dispatch_sync to set the context to null to guarantee that there's not
138 // a race between checking the context and using it from a task.
139 dispatch_sync_f(queue_, context_, &QueueContext::SetNotActive);
140 dispatch_release(queue_);
141}
142
143// static
144TaskQueue* TaskQueue::Current() {
145 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls()));
146}
147
tommic06b1332016-05-14 11:31:40 -0700148bool TaskQueue::IsCurrent() const {
149 RTC_DCHECK(queue_);
150 return this == Current();
151}
152
153void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
154 auto* context = new TaskContext(context_, std::move(task));
155 dispatch_async_f(queue_, context, &TaskContext::RunTask);
156}
157
158void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
159 uint32_t milliseconds) {
160 auto* context = new TaskContext(context_, std::move(task));
161 dispatch_after_f(
162 dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_,
163 context, &TaskContext::RunTask);
164}
165
166void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
167 std::unique_ptr<QueuedTask> reply,
168 TaskQueue* reply_queue) {
169 auto* context = new PostTaskAndReplyContext(
170 context_, std::move(task), reply_queue->context_, std::move(reply));
171 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask);
172}
173
174void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
175 std::unique_ptr<QueuedTask> reply) {
176 return PostTaskAndReply(std::move(task), std::move(reply), Current());
177}
178
179} // namespace rtc