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 int TaskQueuePriorityToGCD(TaskQueue::Priority priority) { |
| 26 switch (priority) { |
| 27 case TaskQueue::NORMAL: |
| 28 return DISPATCH_QUEUE_PRIORITY_DEFAULT; |
| 29 case TaskQueue::HIGH: |
| 30 return DISPATCH_QUEUE_PRIORITY_HIGH; |
| 31 case TaskQueue::LOW: |
| 32 return DISPATCH_QUEUE_PRIORITY_LOW; |
| 33 } |
| 34 } |
| 35 } |
| 36 |
24 using internal::GetQueuePtrTls; | 37 using internal::GetQueuePtrTls; |
25 using internal::AutoSetCurrentQueuePtr; | 38 using internal::AutoSetCurrentQueuePtr; |
26 | 39 |
27 struct TaskQueue::QueueContext { | 40 struct TaskQueue::QueueContext { |
28 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} | 41 explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {} |
29 | 42 |
30 static void SetNotActive(void* context) { | 43 static void SetNotActive(void* context) { |
31 QueueContext* qc = static_cast<QueueContext*>(context); | 44 QueueContext* qc = static_cast<QueueContext*>(context); |
32 qc->is_active = false; | 45 qc->is_active = false; |
33 } | 46 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 100 } |
88 // Post the reply task. This hands the work over to the parent struct. | 101 // Post the reply task. This hands the work over to the parent struct. |
89 // This task will eventually delete |this|. | 102 // This task will eventually delete |this|. |
90 dispatch_async_f(rc->queue_ctx->queue->queue_, rc, &TaskContext::RunTask); | 103 dispatch_async_f(rc->queue_ctx->queue->queue_, rc, &TaskContext::RunTask); |
91 } | 104 } |
92 | 105 |
93 QueueContext* const first_queue_ctx; | 106 QueueContext* const first_queue_ctx; |
94 std::unique_ptr<QueuedTask> first_task; | 107 std::unique_ptr<QueuedTask> first_task; |
95 }; | 108 }; |
96 | 109 |
97 TaskQueue::TaskQueue(const char* queue_name) | 110 TaskQueue::TaskQueue(const char* queue_name, Priority priority /*= NORMAL*/) |
98 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), | 111 : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)), |
99 context_(new QueueContext(this)) { | 112 context_(new QueueContext(this)) { |
100 RTC_DCHECK(queue_name); | 113 RTC_DCHECK(queue_name); |
101 RTC_CHECK(queue_); | 114 RTC_CHECK(queue_); |
102 dispatch_set_context(queue_, context_); | 115 dispatch_set_context(queue_, context_); |
103 // Assign a finalizer that will delete the context when the last reference | 116 // 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 | 117 // to the queue is released. This may run after the TaskQueue object has |
105 // been deleted. | 118 // been deleted. |
106 dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); | 119 dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext); |
| 120 |
| 121 dispatch_set_target_queue( |
| 122 queue_, dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0)); |
107 } | 123 } |
108 | 124 |
109 TaskQueue::~TaskQueue() { | 125 TaskQueue::~TaskQueue() { |
110 RTC_DCHECK(!IsCurrent()); | 126 RTC_DCHECK(!IsCurrent()); |
111 // Implementation/behavioral note: | 127 // Implementation/behavioral note: |
112 // Dispatch queues are reference counted via calls to dispatch_retain and | 128 // Dispatch queues are reference counted via calls to dispatch_retain and |
113 // dispatch_release. Pending blocks submitted to a queue also hold a | 129 // 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 | 130 // 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. | 131 // queue have been released, the queue will be deallocated by the system. |
116 // This is why we check the context before running tasks. | 132 // 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)); | 174 context_, std::move(task), reply_queue->context_, std::move(reply)); |
159 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); | 175 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); |
160 } | 176 } |
161 | 177 |
162 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | 178 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
163 std::unique_ptr<QueuedTask> reply) { | 179 std::unique_ptr<QueuedTask> reply) { |
164 return PostTaskAndReply(std::move(task), std::move(reply), Current()); | 180 return PostTaskAndReply(std::move(task), std::move(reply), Current()); |
165 } | 181 } |
166 | 182 |
167 } // namespace rtc | 183 } // namespace rtc |
OLD | NEW |