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

Side by Side 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: kill declaration too 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/threading/worker_pool.h ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/worker_pool.h" 5 #include "base/threading/worker_pool.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/lazy_instance.h"
10 #include "base/task_runner.h"
9 #include "base/threading/post_task_and_reply_impl.h" 11 #include "base/threading/post_task_and_reply_impl.h"
10 #include "base/tracked_objects.h" 12 #include "base/tracked_objects.h"
11 13
12 namespace base { 14 namespace base {
13 15
14 namespace { 16 namespace {
15 17
16 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl { 18 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl {
17 public: 19 public:
18 PostTaskAndReplyWorkerPool(bool task_is_slow) : task_is_slow_(task_is_slow) { 20 PostTaskAndReplyWorkerPool(bool task_is_slow) : task_is_slow_(task_is_slow) {
19 } 21 }
20 22
21 private: 23 private:
22 virtual bool PostTask(const tracked_objects::Location& from_here, 24 virtual bool PostTask(const tracked_objects::Location& from_here,
23 const Closure& task) OVERRIDE { 25 const Closure& task) OVERRIDE {
24 return WorkerPool::PostTask(from_here, task, task_is_slow_); 26 return WorkerPool::PostTask(from_here, task, task_is_slow_);
25 } 27 }
26 28
27 bool task_is_slow_; 29 bool task_is_slow_;
28 }; 30 };
29 31
32 // WorkerPoolTaskRunner ---------------------------------------------
33 // A TaskRunner which posts tasks to a WorkerPool with a
34 // fixed ShutdownBehavior.
35 //
36 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
37 class WorkerPoolTaskRunner : public TaskRunner {
38 public:
39 WorkerPoolTaskRunner(bool tasks_are_slow);
40
41 // TaskRunner implementation
42 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
43 const Closure& task,
44 int64 delay_ms) OVERRIDE;
45 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
46 const Closure& task,
47 TimeDelta delay) OVERRIDE;
48 virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
49
50 private:
51 virtual ~WorkerPoolTaskRunner();
52
53 // Helper function for posting a delayed task. Asserts that the delay is
54 // zero because non-zero delays are not supported.
55 bool PostDelayedTaskAssertZeroDelay(
56 const tracked_objects::Location& from_here,
57 const Closure& task,
58 int64 delay_ms);
59
60 const bool tasks_are_slow_;
61
62 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner);
63 };
64
65 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow)
66 : tasks_are_slow_(tasks_are_slow) {
67 }
68
69 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
70 }
71
72 bool WorkerPoolTaskRunner::PostDelayedTask(
73 const tracked_objects::Location& from_here,
74 const Closure& task,
75 int64 delay_ms) {
76 return PostDelayedTaskAssertZeroDelay(from_here, task, delay_ms);
77 }
78
79 bool WorkerPoolTaskRunner::PostDelayedTask(
80 const tracked_objects::Location& from_here,
81 const Closure& task,
82 TimeDelta delay) {
83 return PostDelayedTask(from_here, task, delay.InMillisecondsRoundedUp());
84 }
85
86 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
87 return WorkerPool::RunsTasksOnCurrentThread();
88 }
89
90 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
91 const tracked_objects::Location& from_here,
92 const Closure& task,
93 int64 delay_ms) {
94 DCHECK_EQ(delay_ms, 0)
95 << "WorkerPoolTaskRunner does not support non-zero delays";
96 return WorkerPool::PostTask(from_here, task, tasks_are_slow_);
97 }
98
99 struct TaskRunnerHolder {
100 TaskRunnerHolder() {
101 taskrunners_[0] = new WorkerPoolTaskRunner(false);
102 taskrunners_[1] = new WorkerPoolTaskRunner(true);
103 }
104 scoped_refptr<TaskRunner> taskrunners_[2];
105 };
106
107 base::LazyInstance<TaskRunnerHolder>::Leaky
108 g_taskrunners = LAZY_INSTANCE_INITIALIZER;
109
30 } // namespace 110 } // namespace
31 111
32 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here, 112 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here,
33 const Closure& task, 113 const Closure& task,
34 const Closure& reply, 114 const Closure& reply,
35 bool task_is_slow) { 115 bool task_is_slow) {
36 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply( 116 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply(
37 from_here, task, reply); 117 from_here, task, reply);
38 } 118 }
39 119
120 // static
121 const scoped_refptr<TaskRunner>&
122 WorkerPool::GetTaskRunner(bool tasks_are_slow) {
123 return g_taskrunners.Get().taskrunners_[tasks_are_slow];
124 }
125
40 } // namespace base 126 } // namespace base
OLDNEW
« 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