blob: 2c7d649fc9598723994189ee39f82b26e89c1111 [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
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
23namespace rtc {
24using internal::GetQueuePtrTls;
25using internal::AutoSetCurrentQueuePtr;
26
27struct 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
44struct 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.
65struct 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),
72 first_task(std::move(first_task)) {
73 // Retain the reply queue for as long as this object lives.
74 // If we don't, we may have memory leaks and/or failures.
75 dispatch_retain(first_queue_ctx->queue->queue_);
76 }
77 ~PostTaskAndReplyContext() override {
78 dispatch_release(first_queue_ctx->queue->queue_);
79 }
80
81 static void RunTask(void* context) {
82 auto* rc = static_cast<PostTaskAndReplyContext*>(context);
83 if (rc->first_queue_ctx->is_active) {
84 AutoSetCurrentQueuePtr set_current(rc->first_queue_ctx->queue);
85 if (!rc->first_task->Run())
86 rc->first_task.release();
87 }
88 // Post the reply task. This hands the work over to the parent struct.
89 // This task will eventually delete |this|.
90 dispatch_async_f(rc->queue_ctx->queue->queue_, rc, &TaskContext::RunTask);
91 }
92
93 QueueContext* const first_queue_ctx;
94 std::unique_ptr<QueuedTask> first_task;
95};
96
97TaskQueue::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
109TaskQueue::~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
125TaskQueue* TaskQueue::Current() {
126 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls()));
127}
128
129// static
130bool 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
136bool TaskQueue::IsCurrent() const {
137 RTC_DCHECK(queue_);
138 return this == Current();
139}
140
141void 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
146void 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
154void 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
162void 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