OLD | NEW |
1 // Copyright (c) 2012 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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/debug/leak_annotations.h" | 11 #include "base/debug/leak_annotations.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
14 #include "base/threading/post_task_and_reply_impl.h" | 14 #include "base/threading/post_task_and_reply_impl.h" |
15 #include "base/tracked_objects.h" | 15 #include "base/tracked_objects.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl { | 21 class PostTaskAndReplyWorkerPool : public internal::PostTaskAndReplyImpl { |
22 public: | 22 public: |
23 explicit PostTaskAndReplyWorkerPool(bool task_is_slow) | 23 explicit PostTaskAndReplyWorkerPool(bool task_is_slow) |
24 : task_is_slow_(task_is_slow) { | 24 : task_is_slow_(task_is_slow) { |
25 } | 25 } |
26 ~PostTaskAndReplyWorkerPool() override = default; | 26 ~PostTaskAndReplyWorkerPool() override = default; |
27 | 27 |
28 private: | 28 private: |
29 bool PostTask(const tracked_objects::Location& from_here, | 29 bool PostTask(const tracked_objects::Location& from_here, |
30 const Closure& task) override { | 30 Closure task) override { |
31 return WorkerPool::PostTask(from_here, task, task_is_slow_); | 31 return WorkerPool::PostTask(from_here, std::move(task), task_is_slow_); |
32 } | 32 } |
33 | 33 |
34 bool task_is_slow_; | 34 bool task_is_slow_; |
35 }; | 35 }; |
36 | 36 |
37 // WorkerPoolTaskRunner --------------------------------------------- | 37 // WorkerPoolTaskRunner --------------------------------------------- |
38 // A TaskRunner which posts tasks to a WorkerPool with a | 38 // A TaskRunner which posts tasks to a WorkerPool with a |
39 // fixed ShutdownBehavior. | 39 // fixed ShutdownBehavior. |
40 // | 40 // |
41 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). | 41 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). |
42 class WorkerPoolTaskRunner : public TaskRunner { | 42 class WorkerPoolTaskRunner : public TaskRunner { |
43 public: | 43 public: |
44 explicit WorkerPoolTaskRunner(bool tasks_are_slow); | 44 explicit WorkerPoolTaskRunner(bool tasks_are_slow); |
45 | 45 |
46 // TaskRunner implementation | 46 // TaskRunner implementation |
47 bool PostDelayedTask(const tracked_objects::Location& from_here, | 47 bool PostDelayedTask(const tracked_objects::Location& from_here, |
48 const Closure& task, | 48 Closure task, |
49 TimeDelta delay) override; | 49 TimeDelta delay) override; |
50 bool RunsTasksOnCurrentThread() const override; | 50 bool RunsTasksOnCurrentThread() const override; |
51 | 51 |
52 private: | 52 private: |
53 ~WorkerPoolTaskRunner() override; | 53 ~WorkerPoolTaskRunner() override; |
54 | 54 |
55 // Helper function for posting a delayed task. Asserts that the delay is | 55 // Helper function for posting a delayed task. Asserts that the delay is |
56 // zero because non-zero delays are not supported. | 56 // zero because non-zero delays are not supported. |
57 bool PostDelayedTaskAssertZeroDelay( | 57 bool PostDelayedTaskAssertZeroDelay( |
58 const tracked_objects::Location& from_here, | 58 const tracked_objects::Location& from_here, |
59 const Closure& task, | 59 Closure task, |
60 base::TimeDelta delay); | 60 base::TimeDelta delay); |
61 | 61 |
62 const bool tasks_are_slow_; | 62 const bool tasks_are_slow_; |
63 | 63 |
64 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner); | 64 DISALLOW_COPY_AND_ASSIGN(WorkerPoolTaskRunner); |
65 }; | 65 }; |
66 | 66 |
67 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow) | 67 WorkerPoolTaskRunner::WorkerPoolTaskRunner(bool tasks_are_slow) |
68 : tasks_are_slow_(tasks_are_slow) { | 68 : tasks_are_slow_(tasks_are_slow) { |
69 } | 69 } |
70 | 70 |
71 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() { | 71 WorkerPoolTaskRunner::~WorkerPoolTaskRunner() { |
72 } | 72 } |
73 | 73 |
74 bool WorkerPoolTaskRunner::PostDelayedTask( | 74 bool WorkerPoolTaskRunner::PostDelayedTask( |
75 const tracked_objects::Location& from_here, | 75 const tracked_objects::Location& from_here, |
76 const Closure& task, | 76 Closure task, |
77 TimeDelta delay) { | 77 TimeDelta delay) { |
78 return PostDelayedTaskAssertZeroDelay(from_here, task, delay); | 78 return PostDelayedTaskAssertZeroDelay(from_here, std::move(task), delay); |
79 } | 79 } |
80 | 80 |
81 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { | 81 bool WorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { |
82 return WorkerPool::RunsTasksOnCurrentThread(); | 82 return WorkerPool::RunsTasksOnCurrentThread(); |
83 } | 83 } |
84 | 84 |
85 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay( | 85 bool WorkerPoolTaskRunner::PostDelayedTaskAssertZeroDelay( |
86 const tracked_objects::Location& from_here, | 86 const tracked_objects::Location& from_here, |
87 const Closure& task, | 87 Closure task, |
88 base::TimeDelta delay) { | 88 base::TimeDelta delay) { |
89 DCHECK_EQ(delay.InMillisecondsRoundedUp(), 0) | 89 DCHECK_EQ(delay.InMillisecondsRoundedUp(), 0) |
90 << "WorkerPoolTaskRunner does not support non-zero delays"; | 90 << "WorkerPoolTaskRunner does not support non-zero delays"; |
91 return WorkerPool::PostTask(from_here, task, tasks_are_slow_); | 91 return WorkerPool::PostTask(from_here, std::move(task), tasks_are_slow_); |
92 } | 92 } |
93 | 93 |
94 struct TaskRunnerHolder { | 94 struct TaskRunnerHolder { |
95 TaskRunnerHolder() { | 95 TaskRunnerHolder() { |
96 taskrunners_[0] = new WorkerPoolTaskRunner(false); | 96 taskrunners_[0] = new WorkerPoolTaskRunner(false); |
97 taskrunners_[1] = new WorkerPoolTaskRunner(true); | 97 taskrunners_[1] = new WorkerPoolTaskRunner(true); |
98 } | 98 } |
99 scoped_refptr<TaskRunner> taskrunners_[2]; | 99 scoped_refptr<TaskRunner> taskrunners_[2]; |
100 }; | 100 }; |
101 | 101 |
(...skipping 14 matching lines...) Expand all Loading... |
116 } | 116 } |
117 | 117 |
118 // static | 118 // static |
119 const scoped_refptr<TaskRunner>& | 119 const scoped_refptr<TaskRunner>& |
120 WorkerPool::GetTaskRunner(bool tasks_are_slow) { | 120 WorkerPool::GetTaskRunner(bool tasks_are_slow) { |
121 static auto* task_runner_holder = new TaskRunnerHolder(); | 121 static auto* task_runner_holder = new TaskRunnerHolder(); |
122 return task_runner_holder->taskrunners_[tasks_are_slow]; | 122 return task_runner_holder->taskrunners_[tasks_are_slow]; |
123 } | 123 } |
124 | 124 |
125 } // namespace base | 125 } // namespace base |
OLD | NEW |