| 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/rtc_base/task_queue.h" | 15 #include "webrtc/rtc_base/task_queue.h" |
| 16 | 16 |
| 17 #include <string.h> | 17 #include <string.h> |
| 18 | 18 |
| 19 #include <atomic> |
| 20 |
| 19 #include "webrtc/rtc_base/checks.h" | 21 #include "webrtc/rtc_base/checks.h" |
| 20 #include "webrtc/rtc_base/logging.h" | 22 #include "webrtc/rtc_base/logging.h" |
| 21 #include "webrtc/rtc_base/task_queue_posix.h" | 23 #include "webrtc/rtc_base/task_queue_posix.h" |
| 22 | 24 |
| 23 namespace rtc { | 25 namespace rtc { |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 using Priority = TaskQueue::Priority; | 28 using Priority = TaskQueue::Priority; |
| 27 | 29 |
| 28 int TaskQueuePriorityToGCD(Priority priority) { | 30 int TaskQueuePriorityToGCD(Priority priority) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 static void SetNotActive(void* context) { | 48 static void SetNotActive(void* context) { |
| 47 QueueContext* qc = static_cast<QueueContext*>(context); | 49 QueueContext* qc = static_cast<QueueContext*>(context); |
| 48 qc->is_active = false; | 50 qc->is_active = false; |
| 49 } | 51 } |
| 50 | 52 |
| 51 static void DeleteContext(void* context) { | 53 static void DeleteContext(void* context) { |
| 52 QueueContext* qc = static_cast<QueueContext*>(context); | 54 QueueContext* qc = static_cast<QueueContext*>(context); |
| 53 delete qc; | 55 delete qc; |
| 54 } | 56 } |
| 55 | 57 |
| 58 // By posting this function synchronously, one can wait until the queue |
| 59 // has been emptied. |
| 60 static void VoidFunction(void* context) {} // TODO(eladalon): !!! Rename? |
| 61 |
| 56 TaskQueue* const queue; | 62 TaskQueue* const queue; |
| 57 bool is_active; | 63 std::atomic<bool> is_active; |
| 58 }; | 64 }; |
| 59 | 65 |
| 60 struct TaskQueue::TaskContext { | 66 struct TaskQueue::TaskContext { |
| 61 TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) | 67 TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) |
| 62 : queue_ctx(queue_ctx), task(std::move(task)) {} | 68 : queue_ctx(queue_ctx), task(std::move(task)) {} |
| 63 virtual ~TaskContext() {} | 69 virtual ~TaskContext() {} |
| 64 | 70 |
| 65 static void RunTask(void* context) { | 71 static void RunTask(void* context) { |
| 66 std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(context)); | 72 std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(context)); |
| 67 if (tc->queue_ctx->is_active) { | 73 if (tc->queue_ctx->is_active) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 133 |
| 128 TaskQueue::~TaskQueue() { | 134 TaskQueue::~TaskQueue() { |
| 129 RTC_DCHECK(!IsCurrent()); | 135 RTC_DCHECK(!IsCurrent()); |
| 130 // Implementation/behavioral note: | 136 // Implementation/behavioral note: |
| 131 // Dispatch queues are reference counted via calls to dispatch_retain and | 137 // Dispatch queues are reference counted via calls to dispatch_retain and |
| 132 // dispatch_release. Pending blocks submitted to a queue also hold a | 138 // dispatch_release. Pending blocks submitted to a queue also hold a |
| 133 // reference to the queue until they have finished. Once all references to a | 139 // reference to the queue until they have finished. Once all references to a |
| 134 // queue have been released, the queue will be deallocated by the system. | 140 // queue have been released, the queue will be deallocated by the system. |
| 135 // This is why we check the context before running tasks. | 141 // This is why we check the context before running tasks. |
| 136 | 142 |
| 137 // Use dispatch_sync to set the context to null to guarantee that there's not | 143 // Prevent pending tasks from running. |
| 138 // a race between checking the context and using it from a task. | 144 QueueContext::SetNotActive(context_); |
| 139 dispatch_sync_f(queue_, context_, &QueueContext::SetNotActive); | 145 |
| 146 // Wait until all previous tasks' destructors have been invoked. |
| 147 dispatch_sync_f(queue_, context_, &QueueContext::VoidFunction); |
| 148 |
| 149 // TODO(eladalon): !!! It would be good to also RTC_DCHECK here to make sure |
| 150 // no additional tasks were posted in the meantime. Posting tasks during |
| 151 // tear-down would be an error of poster. Additionally, the tasks' destructors |
| 152 // would not get called. |
| 153 |
| 140 dispatch_release(queue_); | 154 dispatch_release(queue_); |
| 141 } | 155 } |
| 142 | 156 |
| 143 // static | 157 // static |
| 144 TaskQueue* TaskQueue::Current() { | 158 TaskQueue* TaskQueue::Current() { |
| 145 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); | 159 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); |
| 146 } | 160 } |
| 147 | 161 |
| 148 // static | 162 // static |
| 149 bool TaskQueue::IsCurrent(const char* queue_name) { | 163 bool TaskQueue::IsCurrent(const char* queue_name) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 177 context_, std::move(task), reply_queue->context_, std::move(reply)); | 191 context_, std::move(task), reply_queue->context_, std::move(reply)); |
| 178 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); | 192 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); |
| 179 } | 193 } |
| 180 | 194 |
| 181 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | 195 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 182 std::unique_ptr<QueuedTask> reply) { | 196 std::unique_ptr<QueuedTask> reply) { |
| 183 return PostTaskAndReply(std::move(task), std::move(reply), Current()); | 197 return PostTaskAndReply(std::move(task), std::move(reply), Current()); |
| 184 } | 198 } |
| 185 | 199 |
| 186 } // namespace rtc | 200 } // namespace rtc |
| OLD | NEW |