OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // This file contains the implementation of TaskQueue for Mac and iOS. | 11 // This file contains the implementation of TaskQueue for Mac and iOS. |
12 // The implementation uses Grand Central Dispatch queues (GCD) to | 12 // The implementation uses Grand Central Dispatch queues (GCD) to |
13 // do the actual task queuing. | 13 // do the actual task queuing. |
14 | 14 |
15 #include "webrtc/base/task_queue.h" | 15 #include "webrtc/base/task_queue.h" |
16 | 16 |
17 #include <string.h> | 17 #include <string.h> |
18 | 18 |
19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/task_queue_posix.h" | 21 #include "webrtc/base/task_queue_posix.h" |
22 | 22 |
23 namespace rtc { | 23 namespace rtc { |
| 24 namespace { |
| 25 |
| 26 using Priority = TaskQueue::Priority; |
| 27 |
| 28 int 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 |
24 using internal::GetQueuePtrTls; | 40 using internal::GetQueuePtrTls; |
25 using internal::AutoSetCurrentQueuePtr; | 41 using internal::AutoSetCurrentQueuePtr; |
26 | 42 |
27 struct TaskQueue::QueueContext { | 43 struct TaskQueue::QueueContext { |
28 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} | 44 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} |
29 | 45 |
30 static void SetNotActive(void* context) { | 46 static void SetNotActive(void* context) { |
31 QueueContext* qc = static_cast<QueueContext*>(context); | 47 QueueContext* qc = static_cast<QueueContext*>(context); |
32 qc->is_active = false; | 48 qc->is_active = false; |
33 } | 49 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // Post the reply task. This hands the work over to the parent struct. | 103 // Post the reply task. This hands the work over to the parent struct. |
88 // This task will eventually delete |this|. | 104 // This task will eventually delete |this|. |
89 dispatch_async_f(rc->reply_queue_, rc, &TaskContext::RunTask); | 105 dispatch_async_f(rc->reply_queue_, rc, &TaskContext::RunTask); |
90 } | 106 } |
91 | 107 |
92 QueueContext* const first_queue_ctx; | 108 QueueContext* const first_queue_ctx; |
93 std::unique_ptr<QueuedTask> first_task; | 109 std::unique_ptr<QueuedTask> first_task; |
94 dispatch_queue_t reply_queue_; | 110 dispatch_queue_t reply_queue_; |
95 }; | 111 }; |
96 | 112 |
97 TaskQueue::TaskQueue(const char* queue_name) | 113 TaskQueue::TaskQueue(const char* queue_name, Priority priority /*= NORMAL*/) |
98 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), | 114 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), |
99 context_(new QueueContext(this)) { | 115 context_(new QueueContext(this)) { |
100 RTC_DCHECK(queue_name); | 116 RTC_DCHECK(queue_name); |
101 RTC_CHECK(queue_); | 117 RTC_CHECK(queue_); |
102 dispatch_set_context(queue_, context_); | 118 dispatch_set_context(queue_, context_); |
103 // Assign a finalizer that will delete the context when the last reference | 119 // 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 | 120 // to the queue is released. This may run after the TaskQueue object has |
105 // been deleted. | 121 // been deleted. |
106 dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); | 122 dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); |
| 123 |
| 124 dispatch_set_target_queue( |
| 125 queue_, dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0)); |
107 } | 126 } |
108 | 127 |
109 TaskQueue::~TaskQueue() { | 128 TaskQueue::~TaskQueue() { |
110 RTC_DCHECK(!IsCurrent()); | 129 RTC_DCHECK(!IsCurrent()); |
111 // Implementation/behavioral note: | 130 // Implementation/behavioral note: |
112 // Dispatch queues are reference counted via calls to dispatch_retain and | 131 // Dispatch queues are reference counted via calls to dispatch_retain and |
113 // dispatch_release. Pending blocks submitted to a queue also hold a | 132 // 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 | 133 // 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. | 134 // queue have been released, the queue will be deallocated by the system. |
116 // This is why we check the context before running tasks. | 135 // This is why we check the context before running tasks. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 context_, std::move(task), reply_queue->context_, std::move(reply)); | 177 context_, std::move(task), reply_queue->context_, std::move(reply)); |
159 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); | 178 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); |
160 } | 179 } |
161 | 180 |
162 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | 181 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
163 std::unique_ptr<QueuedTask> reply) { | 182 std::unique_ptr<QueuedTask> reply) { |
164 return PostTaskAndReply(std::move(task), std::move(reply), Current()); | 183 return PostTaskAndReply(std::move(task), std::move(reply), Current()); |
165 } | 184 } |
166 | 185 |
167 } // namespace rtc | 186 } // namespace rtc |
OLD | NEW |