Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(769)

Unified Diff: webrtc/base/task_queue_gcd.cc

Issue 1803823003: WIP: TaskQueue (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: New task queing primitive: TaskQueue Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/task_queue.h ('k') | webrtc/base/task_queue_libevent.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/task_queue_gcd.cc
diff --git a/webrtc/base/task_queue_gcd.cc b/webrtc/base/task_queue_gcd.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b4235c4fea739dd6eecc18e75ac2be80530e6a1d
--- /dev/null
+++ b/webrtc/base/task_queue_gcd.cc
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2016 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/base/task_queue.h"
+
+#include <string.h>
+
+#include "webrtc/base/checks.h"
+#include "webrtc/base/logging.h"
+#include "webrtc/base/task_queue_posix.h"
+
+namespace rtc {
+using internal::GetQueuePtrTls;
+using internal::AutoSetCurrentQueuePtr;
+
+struct TaskQueue::QueueContext {
+ explicit QueueContext(TaskQueue* q) : queue(q), is_active(true) {}
+
+ static void DeleteContext(void* context) {
+ QueueContext* qc = reinterpret_cast<QueueContext*>(context);
+ delete qc;
+ }
+
+ TaskQueue* const queue;
+ bool is_active;
+};
+
+TaskQueue::TaskQueue(const char* queue_name)
+ : queue_(dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL)),
+ context_(new QueueContext(this)) {
+ RTC_DCHECK(queue_name);
+ queue_ = dispatch_queue_create(queue_name, DISPATCH_QUEUE_SERIAL);
+ RTC_CHECK(queue_);
+ dispatch_set_context(queue_, context_);
+ // Assign a finalizer that will delete the context when the last reference
+ // to the queue is released. This may run after the TaskQueue object has
+ // been deleted.
+ dispatch_set_finalizer_f(queue_, &QueueContext::DeleteContext);
+}
+
+TaskQueue::~TaskQueue() {
+ RTC_DCHECK(!IsCurrent());
+ // Implementation/behavioral note:
+ // Dispatch queues are reference counted via calls to dispatch_retain and
+ // dispatch_release. Pending blocks submitted to a queue also hold a
+ // reference to the queue until they have finished. Once all references to a
+ // queue have been released, the queue will be deallocated by the system.
+ // This is why we check the context before running tasks.
+
+ // Use dispatch_sync to set the context to null to guarantee that there's not
+ // a race between checking the context and using it from a task.
+ dispatch_sync(queue_, ^{
+ context_->is_active = false;
+ });
+ dispatch_release(queue_);
+}
+
+// static
+TaskQueue* TaskQueue::Current() {
+ return static_cast<TaskQueue*>(pthread_getspecific(GetQueuePtrTls()));
+}
+
+// static
+bool TaskQueue::IsCurrent(const char* queue_name) {
+ TaskQueue* current = Current();
+ return current &&
+ strcmp(queue_name, dispatch_queue_get_label(current->queue_)) == 0;
+}
+
+bool TaskQueue::IsCurrent() const {
+ RTC_DCHECK(queue_);
+ return this == Current();
+}
+
+void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
+ QueuedTask* task_ptr = task.release();
+ QueueContext* context = context_;
+ dispatch_async(queue_, ^{
+ QueuedTask* t = task_ptr; // since task_ptr is unassignable.
+ if (context->is_active) {
+ AutoSetCurrentQueuePtr set_current(context->queue);
+ if (!t->Run())
+ t = nullptr;
+ }
+ if (t)
+ delete t;
+ });
+}
+
+void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
+ uint32_t milliseconds) {
+ QueuedTask* task_ptr = task.release();
+ QueueContext* context = context_;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC),
+ queue_, ^{
+ QueuedTask* t = task_ptr;
+ if (context->is_active) {
+ AutoSetCurrentQueuePtr set_current(context->queue);
+ if (!t->Run())
+ t = nullptr;
+ }
+ if (t)
+ delete t;
+ });
+}
+
+void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
+ std::unique_ptr<QueuedTask> reply,
+ TaskQueue* reply_queue) {
+ QueuedTask* task_ptr = task.release();
+ QueuedTask* reply_task_ptr = reply.release();
+ dispatch_queue_t target_queue = reply_queue->queue_;
+ dispatch_retain(target_queue);
+ QueueContext* context = context_;
+ QueueContext* reply_context = reply_queue->context_;
+ dispatch_async(queue_, ^{
+ QueuedTask* t = task_ptr;
+ if (context->is_active) {
+ AutoSetCurrentQueuePtr set_current(context->queue);
+ if (!t->Run())
+ t = nullptr;
+ }
+ if (t)
+ delete t;
+ dispatch_async(target_queue, ^{
+ QueuedTask* r = reply_task_ptr;
+ if (reply_context->is_active) {
+ AutoSetCurrentQueuePtr set_current(reply_context->queue);
+ if (!r->Run())
+ r = nullptr;
+ }
+ if (r)
+ delete r;
+ });
+ dispatch_release(target_queue);
+ });
+}
+
+void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
+ std::unique_ptr<QueuedTask> reply) {
+ return PostTaskAndReply(std::move(task), std::move(reply), Current());
+}
+
+} // namespace rtc
« no previous file with comments | « webrtc/base/task_queue.h ('k') | webrtc/base/task_queue_libevent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698