blob: c131d82c571a421cf4df3109722b90991f55a8fd [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
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010015#include "rtc_base/task_queue_gcd.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
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010021#include "absl/memory/memory.h"
22#include "absl/strings/string_view.h"
23#include "api/task_queue/queued_task.h"
24#include "api/task_queue/task_queue_base.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
26#include "rtc_base/logging.h"
tommic06b1332016-05-14 11:31:40 -070027
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010028namespace webrtc {
tommic9bb7912017-02-24 10:42:14 -080029namespace {
30
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010031int TaskQueuePriorityToGCD(TaskQueueFactory::Priority priority) {
tommic9bb7912017-02-24 10:42:14 -080032 switch (priority) {
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010033 case TaskQueueFactory::Priority::NORMAL:
tommic9bb7912017-02-24 10:42:14 -080034 return DISPATCH_QUEUE_PRIORITY_DEFAULT;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010035 case TaskQueueFactory::Priority::HIGH:
tommic9bb7912017-02-24 10:42:14 -080036 return DISPATCH_QUEUE_PRIORITY_HIGH;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010037 case TaskQueueFactory::Priority::LOW:
tommic9bb7912017-02-24 10:42:14 -080038 return DISPATCH_QUEUE_PRIORITY_LOW;
39 }
40}
tommic9bb7912017-02-24 10:42:14 -080041
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010042class TaskQueueGcd : public TaskQueueBase {
nisse66d07c52017-09-08 05:12:51 -070043 public:
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010044 TaskQueueGcd(absl::string_view queue_name, int gcd_priority);
tommic06b1332016-05-14 11:31:40 -070045
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010046 void Delete() override;
47 void PostTask(std::unique_ptr<QueuedTask> task) override;
48 void PostDelayedTask(std::unique_ptr<QueuedTask> task,
49 uint32_t milliseconds) override;
tommic06b1332016-05-14 11:31:40 -070050
nisse66d07c52017-09-08 05:12:51 -070051 private:
nisse66d07c52017-09-08 05:12:51 -070052 struct TaskContext {
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010053 TaskContext(TaskQueueGcd* queue, std::unique_ptr<QueuedTask> task)
54 : queue(queue), task(std::move(task)) {}
nisse66d07c52017-09-08 05:12:51 -070055
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010056 TaskQueueGcd* const queue;
nisse66d07c52017-09-08 05:12:51 -070057 std::unique_ptr<QueuedTask> task;
58 };
59
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010060 ~TaskQueueGcd() override;
61 static void RunTask(void* task_context);
62 static void SetNotActive(void* task_queue);
63 static void DeleteQueue(void* task_queue);
64
nisse66d07c52017-09-08 05:12:51 -070065 dispatch_queue_t queue_;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010066 bool is_active_;
tommic06b1332016-05-14 11:31:40 -070067};
68
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010069TaskQueueGcd::TaskQueueGcd(absl::string_view queue_name, int gcd_priority)
70 : queue_(dispatch_queue_create(std::string(queue_name).c_str(),
71 DISPATCH_QUEUE_SERIAL)),
72 is_active_(true) {
tommic06b1332016-05-14 11:31:40 -070073 RTC_CHECK(queue_);
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010074 dispatch_set_context(queue_, this);
75 // Assign a finalizer that will delete the queue when the last reference
76 // is released. This may run after the TaskQueue::Delete.
77 dispatch_set_finalizer_f(queue_, &DeleteQueue);
tommic9bb7912017-02-24 10:42:14 -080078
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010079 dispatch_set_target_queue(queue_, dispatch_get_global_queue(gcd_priority, 0));
tommic06b1332016-05-14 11:31:40 -070080}
81
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010082TaskQueueGcd::~TaskQueueGcd() = default;
83
84void TaskQueueGcd::Delete() {
tommic06b1332016-05-14 11:31:40 -070085 RTC_DCHECK(!IsCurrent());
86 // Implementation/behavioral note:
87 // Dispatch queues are reference counted via calls to dispatch_retain and
88 // dispatch_release. Pending blocks submitted to a queue also hold a
89 // reference to the queue until they have finished. Once all references to a
90 // queue have been released, the queue will be deallocated by the system.
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010091 // This is why we check the is_active_ before running tasks.
tommic06b1332016-05-14 11:31:40 -070092
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010093 // Use dispatch_sync to set the is_active_ to guarantee that there's not a
94 // race with checking it from a task.
95 dispatch_sync_f(queue_, this, &SetNotActive);
tommic06b1332016-05-14 11:31:40 -070096 dispatch_release(queue_);
97}
98
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010099void TaskQueueGcd::PostTask(std::unique_ptr<QueuedTask> task) {
100 auto* context = new TaskContext(this, std::move(task));
101 dispatch_async_f(queue_, context, &RunTask);
tommic06b1332016-05-14 11:31:40 -0700102}
103
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100104void TaskQueueGcd::PostDelayedTask(std::unique_ptr<QueuedTask> task,
105 uint32_t milliseconds) {
106 auto* context = new TaskContext(this, std::move(task));
tommic06b1332016-05-14 11:31:40 -0700107 dispatch_after_f(
108 dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_,
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100109 context, &RunTask);
tommic06b1332016-05-14 11:31:40 -0700110}
111
nisse66d07c52017-09-08 05:12:51 -0700112// static
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100113void TaskQueueGcd::RunTask(void* task_context) {
114 std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(task_context));
115 if (!tc->queue->is_active_)
116 return;
117
118 CurrentTaskQueueSetter set_current(tc->queue);
119 auto* task = tc->task.release();
120 if (task->Run()) {
121 // Delete the task before CurrentTaskQueueSetter clears state that this code
122 // is running on the task queue.
123 delete task;
124 }
nisse66d07c52017-09-08 05:12:51 -0700125}
126
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100127// static
128void TaskQueueGcd::SetNotActive(void* task_queue) {
129 static_cast<TaskQueueGcd*>(task_queue)->is_active_ = false;
nisse66d07c52017-09-08 05:12:51 -0700130}
131
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100132// static
133void TaskQueueGcd::DeleteQueue(void* task_queue) {
134 delete static_cast<TaskQueueGcd*>(task_queue);
nisse66d07c52017-09-08 05:12:51 -0700135}
136
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100137class TaskQueueGcdFactory final : public TaskQueueFactory {
138 public:
139 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
140 absl::string_view name,
141 Priority priority) const override {
142 return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>(
143 new TaskQueueGcd(name, TaskQueuePriorityToGCD(priority)));
144 }
145};
146
147} // namespace
148
149std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory() {
150 return absl::make_unique<TaskQueueGcdFactory>();
tommic06b1332016-05-14 11:31:40 -0700151}
152
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100153} // namespace webrtc