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
) {
24 ~PostTaskAndReplyWorkerPool() override
= default;
27 bool PostTask(const tracked_objects::Location
& from_here
,
28 const Closure
& task
) override
{
29 return WorkerPool::PostTask(from_here
, task
, task_is_slow_
);
35 // WorkerPoolTaskRunner ---------------------------------------------
36 // A TaskRunner which posts tasks to a WorkerPool with a
37 // fixed ShutdownBehavior.
39 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
40 class WorkerPoolTaskRunner
: public TaskRunner
{
42 explicit WorkerPoolTaskRunner(bool tasks_are_slow
);
44 // TaskRunner implementation
45 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
47 TimeDelta delay
) override
;
48 bool RunsTasksOnCurrentThread() const override
;
51 ~WorkerPoolTaskRunner() override
;
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
,
58 base::TimeDelta delay
);
60 const bool tasks_are_slow_
;
62 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner
);
65 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow
)
66 : tasks_are_slow_(tasks_are_slow
) {
69 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() {
72 bool WorkerPoolTaskRunner::PostDelayedTask(
73 const tracked_objects::Location
& from_here
,
76 return PostDelayedTaskAssertZeroDelay(from_here
, task
, delay
);
79 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
80 return WorkerPool::RunsTasksOnCurrentThread();
83 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay(
84 const tracked_objects::Location
& from_here
,
86 base::TimeDelta delay
) {
87 DCHECK_EQ(delay
.InMillisecondsRoundedUp(), 0)
88 << "WorkerPoolTaskRunner does not support non-zero delays";
89 return WorkerPool::PostTask(from_here
, task
, tasks_are_slow_
);
92 struct TaskRunnerHolder
{
94 taskrunners_
[0] = new WorkerPoolTaskRunner(false);
95 taskrunners_
[1] = new WorkerPoolTaskRunner(true);
97 scoped_refptr
<TaskRunner
> taskrunners_
[2];
100 base::LazyInstance
<TaskRunnerHolder
>::Leaky
101 g_taskrunners
= LAZY_INSTANCE_INITIALIZER
;
105 bool WorkerPool::PostTaskAndReply(const tracked_objects::Location
& from_here
,
107 const Closure
& reply
,
109 // Do not report PostTaskAndReplyRelay leaks in tests. There's nothing we can
110 // do about them because WorkerPool doesn't have a flushing API.
111 // http://crbug.com/248513
112 // http://crbug.com/290897
113 // Note: this annotation does not cover tasks posted through a TaskRunner.
114 ANNOTATE_SCOPED_MEMORY_LEAK
;
115 return PostTaskAndReplyWorkerPool(task_is_slow
).PostTaskAndReply(
116 from_here
, task
, reply
);
120 const scoped_refptr
<TaskRunner
>&
121 WorkerPool::GetTaskRunner(bool tasks_are_slow
) {
122 return g_taskrunners
.Get().taskrunners_
[tasks_are_slow
];