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

Unified Diff: base/threading/worker_pool.cc

Issue 10220001: Add TaskRunner wrapper for WorkerPool. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use leaky LazyInstance for TaskRunners Created 8 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 | « base/threading/worker_pool.h ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/worker_pool.cc
diff --git a/base/threading/worker_pool.cc b/base/threading/worker_pool.cc
index 56194d2d605d017d816239472a18673a59bc23b3..12967b330352160dc80543e8bd27294e67b8a344 100644
--- a/base/threading/worker_pool.cc
+++ b/base/threading/worker_pool.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// 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.
@@ -6,6 +6,8 @@
#include "base/bind.h"
#include "base/compiler_specific.h"
+#include "base/lazy_instance.h"
+#include "base/task_runner.h"
#include "base/threading/post_task_and_reply_impl.h"
#include "base/tracked_objects.h"
@@ -27,6 +29,88 @@ class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl {
bool task_is_slow_;
};
+// WorkerPoolTaskRunner ---------------------------------------------
+// A TaskRunner which posts tasks to a WorkerPool with a
+// fixed ShutdownBehavior.
+//
+// Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
+class WorkerPoolTaskRunner : public TaskRunner {
+ public:
+ WorkerPoolTaskRunner(bool tasks_are_slow);
+
+ // TaskRunner implementation
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ const Closure& task,
+ int64 delay_ms) OVERRIDE;
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) OVERRIDE;
+ virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
+
+ private:
+ virtual ~WorkerPoolTaskRunner();
+
+ // Helper function for posting a delayed task. Asserts that the delay is
+ // zero because non-zero delays are not supported.
+ bool PostDelayedTaskAssertZeroDelay(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ int64 delay_ms);
+ bool PostDelayedTaskAssertZeroDelay(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay);
willchan no longer on Chromium 2012/04/25 01:01:08 You killed a definition, but not the declaration.
mattm 2012/04/25 01:21:25 oops, done.
+
+ const bool tasks_are_slow_;
+
+ DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner);
+};
+
+WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow)
+ : tasks_are_slow_(tasks_are_slow) {
+}
+
+WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
+}
+
+bool WorkerPoolTaskRunner::PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ int64 delay_ms) {
+ return PostDelayedTaskAssertZeroDelay(from_here, task, delay_ms);
+}
+
+bool WorkerPoolTaskRunner::PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) {
+ return PostDelayedTask(from_here, task, delay.InMillisecondsRoundedUp());
+}
+
+bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
+ return WorkerPool::RunsTasksOnCurrentThread();
+}
+
+bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ int64 delay_ms) {
+ DCHECK_EQ(delay_ms, 0)
+ << "WorkerPoolTaskRunner does not support non-zero delays";
+ return WorkerPool::PostTask(from_here, task, tasks_are_slow_);
+}
+
+struct TaskRunnerHolder {
+ TaskRunnerHolder() {
+ taskrunners_[0] = new WorkerPoolTaskRunner(false);
+ taskrunners_[1] = new WorkerPoolTaskRunner(true);
+ }
+ scoped_refptr<TaskRunner> taskrunners_[2];
+};
+
+base::LazyInstance<TaskRunnerHolder>::Leaky
+ g_taskrunners = LAZY_INSTANCE_INITIALIZER;
+
} // namespace
bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here,
@@ -37,4 +121,10 @@ bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here,
from_here, task, reply);
}
+// static
+const scoped_refptr<TaskRunner>&
+WorkerPool::GetTaskRunner(bool tasks_are_slow) {
+ return g_taskrunners.Get().taskrunners_[tasks_are_slow];
+}
+
} // namespace base
« no previous file with comments | « base/threading/worker_pool.h ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698