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

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: 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 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 bool PostDelayedTaskAssertZeroDelay(
60 const tracked_objects::Location& from_here,
61 const Closure& task,
62 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.
63
64 const bool tasks_are_slow_;
65
66 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner);
67 };
68
69 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow)
70 : tasks_are_slow_(tasks_are_slow) {
71 }
72
73 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
74 }
75
76 bool WorkerPoolTaskRunner::PostDelayedTask(
77 const tracked_objects::Location& from_here,
78 const Closure& task,
79 int64 delay_ms) {
80 return PostDelayedTaskAssertZeroDelay(from_here, task, delay_ms);
81 }
82
83 bool WorkerPoolTaskRunner::PostDelayedTask(
84 const tracked_objects::Location& from_here,
85 const Closure& task,
86 TimeDelta delay) {
87 return PostDelayedTask(from_here, task, delay.InMillisecondsRoundedUp());
88 }
89
90 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
91 return WorkerPool::RunsTasksOnCurrentThread();
92 }
93
94 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
95 const tracked_objects::Location& from_here,
96 const Closure& task,
97 int64 delay_ms) {
98 DCHECK_EQ(delay_ms, 0)
99 << "WorkerPoolTaskRunner does not support non-zero delays";
100 return WorkerPool::PostTask(from_here, task, tasks_are_slow_);
101 }
102
103 struct TaskRunnerHolder {
104 TaskRunnerHolder() {
105 taskrunners_[0] = new WorkerPoolTaskRunner(false);
106 taskrunners_[1] = new WorkerPoolTaskRunner(true);
107 }
108 scoped_refptr<TaskRunner> taskrunners_[2];
109 };
110
111 base::LazyInstance<TaskRunnerHolder>::Leaky
112 g_taskrunners = LAZY_INSTANCE_INITIALIZER;
113
30 } // namespace 114 } // namespace
31 115
32 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here, 116 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here,
33 const Closure& task, 117 const Closure& task,
34 const Closure& reply, 118 const Closure& reply,
35 bool task_is_slow) { 119 bool task_is_slow) {
36 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply( 120 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply(
37 from_here, task, reply); 121 from_here, task, reply);
38 } 122 }
39 123
124 // static
125 const scoped_refptr<TaskRunner>&
126 WorkerPool::GetTaskRunner(bool tasks_are_slow) {
127 return g_taskrunners.Get().taskrunners_[tasks_are_slow];
128 }
129
40 } // namespace base 130 } // 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