summaryrefslogtreecommitdiffstats
path: root/base/mac/libdispatch_task_runner.cc
diff options
context:
space:
mode:
authorrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-07 07:47:15 +0000
committerrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-07 07:47:15 +0000
commit3bb6472100f89924f9fb6a4f794a06539e3bfc35 (patch)
treeec1554f7c4c143f531e03e1a4235ddfc886a7ccd /base/mac/libdispatch_task_runner.cc
parentc9d09c8f002e08550ec4df3f8f854b6721db6b16 (diff)
downloadchromium_src-3bb6472100f89924f9fb6a4f794a06539e3bfc35.zip
chromium_src-3bb6472100f89924f9fb6a4f794a06539e3bfc35.tar.gz
chromium_src-3bb6472100f89924f9fb6a4f794a06539e3bfc35.tar.bz2
Create LibDispatchTaskRunner, a SingleThreadTaskRunner that is backed by a libdispatch queue.
This task runner is meant to be used when an object needs to live on a single thread but needs to both post tasks and receive callbacks via a dispatch queue. BUG=158170 TEST=Covered by new unit test. Review URL: https://chromiumcodereview.appspot.com/11464009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171718 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac/libdispatch_task_runner.cc')
-rw-r--r--base/mac/libdispatch_task_runner.cc62
1 files changed, 62 insertions, 0 deletions
diff --git a/base/mac/libdispatch_task_runner.cc b/base/mac/libdispatch_task_runner.cc
new file mode 100644
index 0000000..12d2822
--- /dev/null
+++ b/base/mac/libdispatch_task_runner.cc
@@ -0,0 +1,62 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/mac/libdispatch_task_runner.h"
+
+#include "base/callback.h"
+
+namespace base {
+namespace mac {
+
+LibDispatchTaskRunner::LibDispatchTaskRunner(const char* name)
+ : queue_(dispatch_queue_create(name, NULL)) {
+}
+
+bool LibDispatchTaskRunner::PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ base::TimeDelta delay) {
+ if (!queue_)
+ return false;
+
+ // The block runtime would implicitly copy the reference, not the object
+ // it's referencing. Copy the closure into block storage so it's available
+ // to run.
+ __block const Closure task_copy = task;
+ void(^run_task)(void) = ^{
+ task_copy.Run();
+ };
+
+ int64 delay_nano =
+ delay.InMicroseconds() * base::Time::kNanosecondsPerMicrosecond;
+ if (delay_nano > 0) {
+ dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay_nano);
+ dispatch_after(time, queue_, run_task);
+ } else {
+ dispatch_async(queue_, run_task);
+ }
+ return true;
+}
+
+bool LibDispatchTaskRunner::RunsTasksOnCurrentThread() const {
+ return queue_ == dispatch_get_current_queue();
+}
+
+bool LibDispatchTaskRunner::PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ base::TimeDelta delay) {
+ return PostDelayedTask(from_here, task, delay);
+}
+
+dispatch_queue_t LibDispatchTaskRunner::GetDispatchQueue() const {
+ return queue_;
+}
+
+LibDispatchTaskRunner::~LibDispatchTaskRunner() {
+ dispatch_release(queue_);
+}
+
+} // namespace mac
+} // namespace base