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

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: comment fix 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
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/task_runner.h"
9 #include "base/threading/post_task_and_reply_impl.h" 10 #include "base/threading/post_task_and_reply_impl.h"
10 #include "base/tracked_objects.h" 11 #include "base/tracked_objects.h"
11 12
12 namespace base { 13 namespace base {
13 14
14 namespace { 15 namespace {
15 16
16 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl { 17 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl {
17 public: 18 public:
18 PostTaskAndReplyWorkerPool(bool task_is_slow) : task_is_slow_(task_is_slow) { 19 PostTaskAndReplyWorkerPool(bool task_is_slow) : task_is_slow_(task_is_slow) {
19 } 20 }
20 21
21 private: 22 private:
22 virtual bool PostTask(const tracked_objects::Location& from_here, 23 virtual bool PostTask(const tracked_objects::Location& from_here,
23 const Closure& task) OVERRIDE { 24 const Closure& task) OVERRIDE {
24 return WorkerPool::PostTask(from_here, task, task_is_slow_); 25 return WorkerPool::PostTask(from_here, task, task_is_slow_);
25 } 26 }
26 27
27 bool task_is_slow_; 28 bool task_is_slow_;
28 }; 29 };
29 30
31 // WorkerPoolTaskRunner ---------------------------------------------
32 // A TaskRunner which posts tasks to a WorkerPool with a
33 // fixed ShutdownBehavior.
34 //
35 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
36 class WorkerPoolTaskRunner : public TaskRunner {
37 public:
38 WorkerPoolTaskRunner(bool tasks_are_slow);
39
40 // TaskRunner implementation
41 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
42 const Closure& task,
43 int64 delay_ms) OVERRIDE;
44 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
45 const Closure& task,
46 TimeDelta delay) OVERRIDE;
47 virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
48
49 private:
50 virtual ~WorkerPoolTaskRunner();
51
52 // Helper function for posting a delayed task. Asserts that the delay is
53 // zero because non-zero delays are not supported.
54 bool PostDelayedTaskAssertZeroDelay(
willchan no longer on Chromium 2012/04/24 23:41:50 Can you kill off one of these? You can do the conv
mattm 2012/04/25 00:57:35 Done.
55 const tracked_objects::Location& from_here,
56 const Closure& task,
57 int64 delay_ms);
58 bool PostDelayedTaskAssertZeroDelay(
59 const tracked_objects::Location& from_here,
60 const Closure& task,
61 TimeDelta delay);
62
63 const bool tasks_are_slow_;
64
65 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner);
66 };
67
68 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow)
69 : tasks_are_slow_(tasks_are_slow) {
70 }
71
72 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
73 }
74
75 bool WorkerPoolTaskRunner::PostDelayedTask(
76 const tracked_objects::Location& from_here,
77 const Closure& task,
78 int64 delay_ms) {
79 return PostDelayedTaskAssertZeroDelay(from_here, task, delay_ms);
80 }
81
82 bool WorkerPoolTaskRunner::PostDelayedTask(
83 const tracked_objects::Location& from_here,
84 const Closure& task,
85 TimeDelta delay) {
86 return PostDelayedTaskAssertZeroDelay(from_here, task, delay);
87 }
88
89 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
90 return WorkerPool::RunsTasksOnCurrentThread();
91 }
92
93 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
94 const tracked_objects::Location& from_here,
95 const Closure& task,
96 int64 delay_ms) {
97 DCHECK_EQ(delay_ms, 0)
98 << "WorkerPoolTaskRunner does not support non-zero delays";
99 return WorkerPool::PostTask(from_here, task, tasks_are_slow_);
100 }
101
102 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
103 const tracked_objects::Location& from_here,
104 const Closure& task,
105 TimeDelta delay) {
106 return PostDelayedTaskAssertZeroDelay(from_here,
107 task,
108 delay.InMillisecondsRoundedUp());
109 }
110
30 } // namespace 111 } // namespace
31 112
32 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here, 113 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location& from_here,
33 const Closure& task, 114 const Closure& task,
34 const Closure& reply, 115 const Closure& reply,
35 bool task_is_slow) { 116 bool task_is_slow) {
36 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply( 117 return PostTaskAndReplyWorkerPool(task_is_slow).PostTaskAndReply(
37 from_here, task, reply); 118 from_here, task, reply);
38 } 119 }
39 120
121 // static
122 scoped_refptr<TaskRunner> WorkerPool::GetTaskRunner(bool tasks_are_slow) {
123 return new WorkerPoolTaskRunner(tasks_are_slow);
124 }
125
40 } // namespace base 126 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698