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 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 static void SetNotActive(void* context) { | 46 static void SetNotActive(void* context) { |
47 QueueContext* qc = static_cast<QueueContext*>(context); | 47 QueueContext* qc = static_cast<QueueContext*>(context); |
48 qc->is_active = false; | 48 qc->is_active = false; |
49 } | 49 } |
50 | 50 |
51 static void DeleteContext(void* context) { | 51 static void DeleteContext(void* context) { |
52 QueueContext* qc = static_cast<QueueContext*>(context); | 52 QueueContext* qc = static_cast<QueueContext*>(context); |
53 delete qc; | 53 delete qc; |
54 } | 54 } |
55 | 55 |
56 // By posting this function synchronously, one can wait until the queue | |
57 // has been emptied. | |
58 static void VoidFunction(void* context) {} | |
59 | |
56 TaskQueue* const queue; | 60 TaskQueue* const queue; |
57 bool is_active; | 61 bool is_active; |
58 }; | 62 }; |
59 | 63 |
60 struct TaskQueue::TaskContext { | 64 struct TaskQueue::TaskContext { |
61 TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) | 65 TaskContext(QueueContext* queue_ctx, std::unique_ptr<QueuedTask> task) |
62 : queue_ctx(queue_ctx), task(std::move(task)) {} | 66 : queue_ctx(queue_ctx), task(std::move(task)) {} |
63 virtual ~TaskContext() {} | 67 virtual ~TaskContext() {} |
64 | 68 |
65 static void RunTask(void* context) { | 69 static void RunTask(void* context) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 | 131 |
128 TaskQueue::~TaskQueue() { | 132 TaskQueue::~TaskQueue() { |
129 RTC_DCHECK(!IsCurrent()); | 133 RTC_DCHECK(!IsCurrent()); |
130 // Implementation/behavioral note: | 134 // Implementation/behavioral note: |
131 // Dispatch queues are reference counted via calls to dispatch_retain and | 135 // Dispatch queues are reference counted via calls to dispatch_retain and |
132 // dispatch_release. Pending blocks submitted to a queue also hold a | 136 // 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 | 137 // 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. | 138 // queue have been released, the queue will be deallocated by the system. |
135 // This is why we check the context before running tasks. | 139 // This is why we check the context before running tasks. |
136 | 140 |
137 // Use dispatch_sync to set the context to null to guarantee that there's not | 141 // Toggle the context to not-active while the task-queue is suspended, to |
138 // a race between checking the context and using it from a task. | 142 // avoid a race. |
139 dispatch_sync_f(queue_, context_, &QueueContext::SetNotActive); | 143 dispatch_suspend(queue_); |
nisse-webrtc
2017/08/30 09:20:35
The interesting question is what dispatch_suspend
eladalon
2017/08/30 09:41:27
My thinking was that it would be helpful in either
eladalon
2017/08/30 10:21:42
Okay, looks like it prevents the next task from la
| |
144 QueueContext::SetNotActive(context_); | |
145 dispatch_resume(queue_); | |
146 | |
147 // Wait until all previous tasks' destructors have been invoked. | |
148 dispatch_sync_f(queue_, context_, &QueueContext::VoidFunction); | |
149 | |
eladalon
2017/08/30 09:19:36
Might not hurt to add an RTC_DCHECK here to make s
| |
140 dispatch_release(queue_); | 150 dispatch_release(queue_); |
141 } | 151 } |
142 | 152 |
143 // static | 153 // static |
144 TaskQueue* TaskQueue::Current() { | 154 TaskQueue* TaskQueue::Current() { |
145 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); | 155 return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls())); |
146 } | 156 } |
147 | 157 |
148 // static | 158 // static |
149 bool TaskQueue::IsCurrent(const char* queue_name) { | 159 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)); | 187 context_, std::move(task), reply_queue->context_, std::move(reply)); |
178 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); | 188 dispatch_async_f(queue_, context, &PostTaskAndReplyContext::RunTask); |
179 } | 189 } |
180 | 190 |
181 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | 191 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
182 std::unique_ptr<QueuedTask> reply) { | 192 std::unique_ptr<QueuedTask> reply) { |
183 return PostTaskAndReply(std::move(task), std::move(reply), Current()); | 193 return PostTaskAndReply(std::move(task), std::move(reply), Current()); |
184 } | 194 } |
185 | 195 |
186 } // namespace rtc | 196 } // namespace rtc |
OLD | NEW |