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
3 // found in the LICENSE file.
5 #include "base/threading/worker_pool.h"
8 #include "base/compiler_specific.h"
9 #include "base/debug/leak_annotations.h"
10 #include "base/lazy_instance.h"
11 #include "base/task_runner.h"
12 #include "base/threading/post_task_and_reply_impl.h"
13 #include "base/tracked_objects.h"
19 class PostTaskAndReplyWorkerPool
: public internal::PostTaskAndReplyImpl
{
21 explicit PostTaskAndReplyWorkerPool(bool task_is_slow
)
22 : task_is_slow_(task_is_slow
) {
26 virtual bool PostTask(const tracked_objects::Location
& from_here
,
27 const Closure
& task
) OVERRIDE
{
28 return WorkerPool::PostTask(from_here
, task
, task_is_slow_
);
34 // WorkerPoolTaskRunner ---------------------------------------------
35 // A TaskRunner which posts tasks to a WorkerPool with a
36 // fixed ShutdownBehavior.
38 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
39 class WorkerPoolTaskRunner
: public TaskRunner
{
41 explicit WorkerPoolTaskRunner(bool tasks_are_slow
);
43 // TaskRunner implementation
44 virtual bool PostDelayedTask(const tracked_objects::Location
& from_here
,
46 TimeDelta delay
) OVERRIDE
;
47 virtual bool RunsTasksOnCurrentThread() const OVERRIDE
;
50 virtual ~WorkerPoolTaskRunner();
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(
55 const tracked_objects::Location
& from_here
,
57 base::TimeDelta delay
);
59 const bool tasks_are_slow_
;
61 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner
);
64 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow
)
65 : tasks_are_slow_(tasks_are_slow
) {
68 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
71 bool WorkerPoolTaskRunner::PostDelayedTask(
72 const tracked_objects::Location
& from_here
,
75 return PostDelayedTaskAssertZeroDelay(from_here
, task
, delay
);
78 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
79 return WorkerPool::RunsTasksOnCurrentThread();
82 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
83 const tracked_objects::Location
& from_here
,
85 base::TimeDelta delay
) {
86 DCHECK_EQ(delay
.InMillisecondsRoundedUp(), 0)
87 << "WorkerPoolTaskRunner does not support non-zero delays";
88 return WorkerPool::PostTask(from_here
, task
, tasks_are_slow_
);
91 struct TaskRunnerHolder
{
93 taskrunners_
[0] = new WorkerPoolTaskRunner(false);
94 taskrunners_
[1] = new WorkerPoolTaskRunner(true);
96 scoped_refptr
<TaskRunner
> taskrunners_
[2];
99 base::LazyInstance
<TaskRunnerHolder
>::Leaky
100 g_taskrunners
= LAZY_INSTANCE_INITIALIZER
;
104 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location
& from_here
,
106 const Closure
& reply
,
108 // Do not report PostTaskAndReplyRelay leaks in tests. There's nothing we can
109 // do about them because WorkerPool doesn't have a flushing API.
110 // http://crbug.com/248513
111 // http://crbug.com/290897
112 // Note: this annotation does not cover tasks posted through a TaskRunner.
113 ANNOTATE_SCOPED_MEMORY_LEAK
;
114 return PostTaskAndReplyWorkerPool(task_is_slow
).PostTaskAndReply(
115 from_here
, task
, reply
);
119 const scoped_refptr
<TaskRunner
>&
120 WorkerPool::GetTaskRunner(bool tasks_are_slow
) {
121 return g_taskrunners
.Get().taskrunners_
[tasks_are_slow
];