blob: 405edab40335eed2d4ca57be28380d08266f173c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/task_queue.h"
tommic06b1332016-05-14 11:31:40 -070016
17#include <string.h>
18
nisse66d07c52017-09-08 05:12:51 -070019#include <dispatch/dispatch.h>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ref_count.h"
24#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/task_queue_posix.h"
tommic06b1332016-05-14 11:31:40 -070026
27namespace rtc {
tommic9bb7912017-02-24 10:42:14 -080028namespace {
29
30using Priority = TaskQueue::Priority;
31
32int 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 Gerey665174f2018-06-19 15:03:05 +020042} // namespace
tommic9bb7912017-02-24 10:42:14 -080043
tommic06b1332016-05-14 11:31:40 -070044using internal::GetQueuePtrTls;
45using internal::AutoSetCurrentQueuePtr;
46
nisse66d07c52017-09-08 05:12:51 -070047class TaskQueue::Impl : public RefCountInterface {
48 public:
49 Impl(const char* queue_name, TaskQueue* task_queue, Priority priority);
50 ~Impl() override;
tommic06b1332016-05-14 11:31:40 -070051
nisse66d07c52017-09-08 05:12:51 -070052 static TaskQueue* Current();
tommic06b1332016-05-14 11:31:40 -070053
nisse66d07c52017-09-08 05:12:51 -070054 // Used for DCHECKing the current queue.
55 bool IsCurrent() const;
tommic06b1332016-05-14 11:31:40 -070056
nisse66d07c52017-09-08 05:12:51 -070057 void PostTask(std::unique_ptr<QueuedTask> task);
nisse66d07c52017-09-08 05:12:51 -070058 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
tommic06b1332016-05-14 11:31:40 -070059
nisse66d07c52017-09-08 05:12:51 -070060 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;
tommic06b1332016-05-14 11:31:40 -070067 }
tommic06b1332016-05-14 11:31:40 -070068
nisse66d07c52017-09-08 05:12:51 -070069 static void DeleteContext(void* context) {
70 QueueContext* qc = static_cast<QueueContext*>(context);
71 delete qc;
tommic06b1332016-05-14 11:31:40 -070072 }
tommic06b1332016-05-14 11:31:40 -070073
nisse66d07c52017-09-08 05:12:51 -070074 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
nisse66d07c52017-09-08 05:12:51 -070096 dispatch_queue_t queue_;
97 QueueContext* const context_;
tommic06b1332016-05-14 11:31:40 -070098};
99
nisse66d07c52017-09-08 05:12:51 -0700100TaskQueue::Impl::Impl(const char* queue_name,
101 TaskQueue* task_queue,
102 Priority priority)
tommic06b1332016-05-14 11:31:40 -0700103 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)),
nisse66d07c52017-09-08 05:12:51 -0700104 context_(new QueueContext(task_queue)) {
tommic06b1332016-05-14 11:31:40 -0700105 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);
tommic9bb7912017-02-24 10:42:14 -0800112
113 dispatch_set_target_queue(
114 queue_, dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0));
tommic06b1332016-05-14 11:31:40 -0700115}
116
nisse66d07c52017-09-08 05:12:51 -0700117TaskQueue::Impl::~Impl() {
tommic06b1332016-05-14 11:31:40 -0700118 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
nisse66d07c52017-09-08 05:12:51 -0700133TaskQueue* TaskQueue::Impl::Current() {
tommic06b1332016-05-14 11:31:40 -0700134 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls()));
135}
136
nisse66d07c52017-09-08 05:12:51 -0700137bool TaskQueue::Impl::IsCurrent() const {
tommic06b1332016-05-14 11:31:40 -0700138 RTC_DCHECK(queue_);
nisse66d07c52017-09-08 05:12:51 -0700139 const TaskQueue* current = Current();
140 return current && this == current->impl_.get();
tommic06b1332016-05-14 11:31:40 -0700141}
142
nisse66d07c52017-09-08 05:12:51 -0700143void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) {
tommic06b1332016-05-14 11:31:40 -0700144 auto* context = new TaskContext(context_, std::move(task));
145 dispatch_async_f(queue_, context, &TaskContext::RunTask);
146}
147
nisse66d07c52017-09-08 05:12:51 -0700148void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task,
149 uint32_t milliseconds) {
tommic06b1332016-05-14 11:31:40 -0700150 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
nisse66d07c52017-09-08 05:12:51 -0700156// Boilerplate for the PIMPL pattern.
157TaskQueue::TaskQueue(const char* queue_name, Priority priority)
158 : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) {
159}
160
161TaskQueue::~TaskQueue() {}
162
163// static
164TaskQueue* TaskQueue::Current() {
165 return TaskQueue::Impl::Current();
166}
167
168// Used for DCHECKing the current queue.
169bool TaskQueue::IsCurrent() const {
170 return impl_->IsCurrent();
171}
172
173void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
174 return TaskQueue::impl_->PostTask(std::move(task));
175}
176
nisse66d07c52017-09-08 05:12:51 -0700177void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
178 uint32_t milliseconds) {
179 return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds);
tommic06b1332016-05-14 11:31:40 -0700180}
181
182} // namespace rtc