blob: 2276f635c53be62707be34f4fe2693ba607b13a9 [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
nisse66d07c52017-09-08 05:12:51 -070017#include <dispatch/dispatch.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020018#include <string.h>
nisse66d07c52017-09-08 05:12:51 -070019
Mirko Bonadei317a1f02019-09-17 17:06:18 +020020#include <memory>
21
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010022#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"
Yura Yaroshevichde863812020-03-12 13:18:01 +030027#include "rtc_base/system/gcd_helpers.h"
tommic06b1332016-05-14 11:31:40 -070028
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010029namespace webrtc {
tommic9bb7912017-02-24 10:42:14 -080030namespace {
31
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010032int TaskQueuePriorityToGCD(TaskQueueFactory::Priority priority) {
tommic9bb7912017-02-24 10:42:14 -080033 switch (priority) {
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010034 case TaskQueueFactory::Priority::NORMAL:
tommic9bb7912017-02-24 10:42:14 -080035 return DISPATCH_QUEUE_PRIORITY_DEFAULT;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010036 case TaskQueueFactory::Priority::HIGH:
tommic9bb7912017-02-24 10:42:14 -080037 return DISPATCH_QUEUE_PRIORITY_HIGH;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010038 case TaskQueueFactory::Priority::LOW:
tommic9bb7912017-02-24 10:42:14 -080039 return DISPATCH_QUEUE_PRIORITY_LOW;
40 }
41}
tommic9bb7912017-02-24 10:42:14 -080042
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010043class TaskQueueGcd : public TaskQueueBase {
nisse66d07c52017-09-08 05:12:51 -070044 public:
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010045 TaskQueueGcd(absl::string_view queue_name, int gcd_priority);
tommic06b1332016-05-14 11:31:40 -070046
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010047 void Delete() override;
48 void PostTask(std::unique_ptr<QueuedTask> task) override;
49 void PostDelayedTask(std::unique_ptr<QueuedTask> task,
50 uint32_t milliseconds) override;
tommic06b1332016-05-14 11:31:40 -070051
nisse66d07c52017-09-08 05:12:51 -070052 private:
nisse66d07c52017-09-08 05:12:51 -070053 struct TaskContext {
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010054 TaskContext(TaskQueueGcd* queue, std::unique_ptr<QueuedTask> task)
55 : queue(queue), task(std::move(task)) {}
nisse66d07c52017-09-08 05:12:51 -070056
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010057 TaskQueueGcd* const queue;
nisse66d07c52017-09-08 05:12:51 -070058 std::unique_ptr<QueuedTask> task;
59 };
60
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010061 ~TaskQueueGcd() override;
62 static void RunTask(void* task_context);
63 static void SetNotActive(void* task_queue);
64 static void DeleteQueue(void* task_queue);
65
nisse66d07c52017-09-08 05:12:51 -070066 dispatch_queue_t queue_;
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010067 bool is_active_;
tommic06b1332016-05-14 11:31:40 -070068};
69
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010070TaskQueueGcd::TaskQueueGcd(absl::string_view queue_name, int gcd_priority)
Yura Yaroshevichde863812020-03-12 13:18:01 +030071 : queue_(RTCDispatchQueueCreateWithTarget(
72 std::string(queue_name).c_str(),
73 DISPATCH_QUEUE_SERIAL,
74 dispatch_get_global_queue(gcd_priority, 0))),
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010075 is_active_(true) {
tommic06b1332016-05-14 11:31:40 -070076 RTC_CHECK(queue_);
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010077 dispatch_set_context(queue_, this);
78 // Assign a finalizer that will delete the queue when the last reference
79 // is released. This may run after the TaskQueue::Delete.
80 dispatch_set_finalizer_f(queue_, &DeleteQueue);
tommic06b1332016-05-14 11:31:40 -070081}
82
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010083TaskQueueGcd::~TaskQueueGcd() = default;
84
85void TaskQueueGcd::Delete() {
tommic06b1332016-05-14 11:31:40 -070086 RTC_DCHECK(!IsCurrent());
87 // Implementation/behavioral note:
88 // Dispatch queues are reference counted via calls to dispatch_retain and
89 // dispatch_release. Pending blocks submitted to a queue also hold a
90 // reference to the queue until they have finished. Once all references to a
91 // queue have been released, the queue will be deallocated by the system.
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010092 // This is why we check the is_active_ before running tasks.
tommic06b1332016-05-14 11:31:40 -070093
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +010094 // Use dispatch_sync to set the is_active_ to guarantee that there's not a
95 // race with checking it from a task.
96 dispatch_sync_f(queue_, this, &SetNotActive);
tommic06b1332016-05-14 11:31:40 -070097 dispatch_release(queue_);
98}
99
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100100void TaskQueueGcd::PostTask(std::unique_ptr<QueuedTask> task) {
101 auto* context = new TaskContext(this, std::move(task));
102 dispatch_async_f(queue_, context, &RunTask);
tommic06b1332016-05-14 11:31:40 -0700103}
104
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100105void TaskQueueGcd::PostDelayedTask(std::unique_ptr<QueuedTask> task,
106 uint32_t milliseconds) {
107 auto* context = new TaskContext(this, std::move(task));
tommic06b1332016-05-14 11:31:40 -0700108 dispatch_after_f(
109 dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_,
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100110 context, &RunTask);
tommic06b1332016-05-14 11:31:40 -0700111}
112
nisse66d07c52017-09-08 05:12:51 -0700113// static
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100114void TaskQueueGcd::RunTask(void* task_context) {
115 std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(task_context));
116 if (!tc->queue->is_active_)
117 return;
118
119 CurrentTaskQueueSetter set_current(tc->queue);
120 auto* task = tc->task.release();
121 if (task->Run()) {
122 // Delete the task before CurrentTaskQueueSetter clears state that this code
123 // is running on the task queue.
124 delete task;
125 }
nisse66d07c52017-09-08 05:12:51 -0700126}
127
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100128// static
129void TaskQueueGcd::SetNotActive(void* task_queue) {
130 static_cast<TaskQueueGcd*>(task_queue)->is_active_ = false;
nisse66d07c52017-09-08 05:12:51 -0700131}
132
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100133// static
134void TaskQueueGcd::DeleteQueue(void* task_queue) {
135 delete static_cast<TaskQueueGcd*>(task_queue);
nisse66d07c52017-09-08 05:12:51 -0700136}
137
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100138class TaskQueueGcdFactory final : public TaskQueueFactory {
139 public:
140 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
141 absl::string_view name,
142 Priority priority) const override {
143 return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>(
144 new TaskQueueGcd(name, TaskQueuePriorityToGCD(priority)));
145 }
146};
147
148} // namespace
149
150std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200151 return std::make_unique<TaskQueueGcdFactory>();
tommic06b1332016-05-14 11:31:40 -0700152}
153
Danil Chapovalov47cf5ea2019-02-19 20:20:16 +0100154} // namespace webrtc