OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <unordered_set> | 10 #include <unordered_set> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 worker_pool_ = SchedulerWorkerPoolImpl::Create( | 65 worker_pool_ = SchedulerWorkerPoolImpl::Create( |
66 "TestWorkerPoolWithFileIO", ThreadPriority::NORMAL, | 66 "TestWorkerPoolWithFileIO", ThreadPriority::NORMAL, |
67 kNumWorkersInWorkerPool, IORestriction::ALLOWED, | 67 kNumWorkersInWorkerPool, IORestriction::ALLOWED, |
68 Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, | 68 Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, |
69 Unretained(this)), | 69 Unretained(this)), |
70 &task_tracker_, &delayed_task_manager_); | 70 &task_tracker_, &delayed_task_manager_); |
71 ASSERT_TRUE(worker_pool_); | 71 ASSERT_TRUE(worker_pool_); |
72 } | 72 } |
73 | 73 |
74 void TearDown() override { | 74 void TearDown() override { |
75 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 75 worker_pool_->WaitForAllWorkersIdleForTesting(); |
76 worker_pool_->JoinForTesting(); | 76 worker_pool_->JoinForTesting(); |
77 } | 77 } |
78 | 78 |
79 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; | 79 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; |
80 | 80 |
81 TaskTracker task_tracker_; | 81 TaskTracker task_tracker_; |
82 TestDelayedTaskManager delayed_task_manager_; | 82 TestDelayedTaskManager delayed_task_manager_; |
83 | 83 |
84 private: | 84 private: |
85 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | 85 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
86 // In production code, this callback would be implemented by the | 86 // In production code, this callback would be implemented by the |
87 // TaskScheduler which would first determine which PriorityQueue the | 87 // TaskScheduler which would first determine which PriorityQueue the |
88 // sequence must be re-enqueued. | 88 // sequence must be re-enqueued. |
89 const SequenceSortKey sort_key(sequence->GetSortKey()); | 89 const SequenceSortKey sort_key(sequence->GetSortKey()); |
90 worker_pool_->ReEnqueueSequence(std::move(sequence), sort_key); | 90 worker_pool_->ReEnqueueSequence(std::move(sequence), sort_key); |
91 } | 91 } |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplTest); | 93 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplTest); |
94 }; | 94 }; |
95 | 95 |
96 using PostNestedTask = test::TestTaskFactory::PostNestedTask; | 96 using PostNestedTask = test::TestTaskFactory::PostNestedTask; |
97 | 97 |
98 class ThreadPostingTasks : public SimpleThread { | 98 class ThreadPostingTasks : public SimpleThread { |
99 public: | 99 public: |
100 enum class WaitBeforePostTask { | 100 enum class WaitBeforePostTask { |
101 NO_WAIT, | 101 NO_WAIT, |
102 WAIT_FOR_ALL_THREADS_IDLE, | 102 WAIT_FOR_ALL_THREADS_IDLE, |
fdoray
2016/06/20 15:08:40
WAIT_FOR_ALL_WORKERS_IDLE
robliao
2016/06/20 17:50:09
Done.
| |
103 }; | 103 }; |
104 | 104 |
105 // Constructs a thread that posts tasks to |worker_pool| through an | 105 // Constructs a thread that posts tasks to |worker_pool| through an |
106 // |execution_mode| task runner. If |wait_before_post_task| is | 106 // |execution_mode| task runner. If |wait_before_post_task| is |
107 // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in | 107 // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all workers in |
108 // |worker_pool| are idle before posting a new task. If |post_nested_task| is | 108 // |worker_pool| are idle before posting a new task. If |post_nested_task| is |
109 // YES, each task posted by this thread posts another task when it runs. | 109 // YES, each task posted by this thread posts another task when it runs. |
110 ThreadPostingTasks(SchedulerWorkerPoolImpl* worker_pool, | 110 ThreadPostingTasks(SchedulerWorkerPoolImpl* worker_pool, |
111 ExecutionMode execution_mode, | 111 ExecutionMode execution_mode, |
112 WaitBeforePostTask wait_before_post_task, | 112 WaitBeforePostTask wait_before_post_task, |
113 PostNestedTask post_nested_task) | 113 PostNestedTask post_nested_task) |
114 : SimpleThread("ThreadPostingTasks"), | 114 : SimpleThread("ThreadPostingTasks"), |
115 worker_pool_(worker_pool), | 115 worker_pool_(worker_pool), |
116 wait_before_post_task_(wait_before_post_task), | 116 wait_before_post_task_(wait_before_post_task), |
117 post_nested_task_(post_nested_task), | 117 post_nested_task_(post_nested_task), |
118 factory_(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), | 118 factory_(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), |
119 execution_mode), | 119 execution_mode), |
120 execution_mode) { | 120 execution_mode) { |
121 DCHECK(worker_pool_); | 121 DCHECK(worker_pool_); |
122 } | 122 } |
123 | 123 |
124 const test::TestTaskFactory* factory() const { return &factory_; } | 124 const test::TestTaskFactory* factory() const { return &factory_; } |
125 | 125 |
126 private: | 126 private: |
127 void Run() override { | 127 void Run() override { |
128 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | 128 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
129 | 129 |
130 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | 130 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
131 if (wait_before_post_task_ == | 131 if (wait_before_post_task_ == |
132 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { | 132 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { |
133 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 133 worker_pool_->WaitForAllWorkersIdleForTesting(); |
134 } | 134 } |
135 EXPECT_TRUE(factory_.PostTask(post_nested_task_, Closure())); | 135 EXPECT_TRUE(factory_.PostTask(post_nested_task_, Closure())); |
136 } | 136 } |
137 } | 137 } |
138 | 138 |
139 SchedulerWorkerPoolImpl* const worker_pool_; | 139 SchedulerWorkerPoolImpl* const worker_pool_; |
140 const scoped_refptr<TaskRunner> task_runner_; | 140 const scoped_refptr<TaskRunner> task_runner_; |
141 const WaitBeforePostTask wait_before_post_task_; | 141 const WaitBeforePostTask wait_before_post_task_; |
142 const PostNestedTask post_nested_task_; | 142 const PostNestedTask post_nested_task_; |
143 test::TestTaskFactory factory_; | 143 test::TestTaskFactory factory_; |
(...skipping 18 matching lines...) Expand all Loading... | |
162 PostNestedTask::NO))); | 162 PostNestedTask::NO))); |
163 threads_posting_tasks.back()->Start(); | 163 threads_posting_tasks.back()->Start(); |
164 } | 164 } |
165 | 165 |
166 // Wait for all tasks to run. | 166 // Wait for all tasks to run. |
167 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 167 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
168 thread_posting_tasks->Join(); | 168 thread_posting_tasks->Join(); |
169 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 169 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
170 } | 170 } |
171 | 171 |
172 // Wait until all worker threads are idle to be sure that no task accesses | 172 // Wait until all workers are idle to be sure that no task accesses |
173 // its TestTaskFactory after |thread_posting_tasks| is destroyed. | 173 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
174 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 174 worker_pool_->WaitForAllWorkersIdleForTesting(); |
175 } | 175 } |
176 | 176 |
177 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasksWaitAllThreadsIdle) { | 177 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasksWaitAllThreadsIdle) { |
fdoray
2016/06/20 15:08:40
WaitAllWorkersIdle
robliao
2016/06/20 17:50:09
Done.
| |
178 // Create threads to post tasks. To verify that worker threads can sleep and | 178 // Create threads to post tasks. To verify that workers can sleep and be woken |
179 // be woken up when new tasks are posted, wait for all threads to become idle | 179 // up when new tasks are posted, wait for all threads to become idle before |
fdoray
2016/06/20 15:08:40
for all *workers* to become
robliao
2016/06/20 17:50:09
Done.
| |
180 // before posting a new task. | 180 // posting a new task. |
181 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 181 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
182 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 182 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
183 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 183 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
184 worker_pool_.get(), GetParam(), | 184 worker_pool_.get(), GetParam(), |
185 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); | 185 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); |
186 threads_posting_tasks.back()->Start(); | 186 threads_posting_tasks.back()->Start(); |
187 } | 187 } |
188 | 188 |
189 // Wait for all tasks to run. | 189 // Wait for all tasks to run. |
190 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 190 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
191 thread_posting_tasks->Join(); | 191 thread_posting_tasks->Join(); |
192 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 192 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
193 } | 193 } |
194 | 194 |
195 // Wait until all worker threads are idle to be sure that no task accesses | 195 // Wait until all workers are idle to be sure that no task accesses its |
196 // its TestTaskFactory after |thread_posting_tasks| is destroyed. | 196 // TestTaskFactory after |thread_posting_tasks| is destroyed. |
197 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 197 worker_pool_->WaitForAllWorkersIdleForTesting(); |
198 } | 198 } |
199 | 199 |
200 TEST_P(TaskSchedulerWorkerPoolImplTest, NestedPostTasks) { | 200 TEST_P(TaskSchedulerWorkerPoolImplTest, NestedPostTasks) { |
201 // Create threads to post tasks. Each task posted by these threads will post | 201 // Create threads to post tasks. Each task posted by these threads will post |
202 // another task when it runs. | 202 // another task when it runs. |
203 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 203 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
204 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 204 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
205 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 205 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
206 worker_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, | 206 worker_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
207 PostNestedTask::YES))); | 207 PostNestedTask::YES))); |
208 threads_posting_tasks.back()->Start(); | 208 threads_posting_tasks.back()->Start(); |
209 } | 209 } |
210 | 210 |
211 // Wait for all tasks to run. | 211 // Wait for all tasks to run. |
212 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 212 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
213 thread_posting_tasks->Join(); | 213 thread_posting_tasks->Join(); |
214 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 214 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
215 } | 215 } |
216 | 216 |
217 // Wait until all worker threads are idle to be sure that no task accesses | 217 // Wait until all workers are idle to be sure that no task accesses its |
218 // its TestTaskFactory after |thread_posting_tasks| is destroyed. | 218 // TestTaskFactory after |thread_posting_tasks| is destroyed. |
219 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 219 worker_pool_->WaitForAllWorkersIdleForTesting(); |
220 } | 220 } |
221 | 221 |
222 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasksWithOneAvailableThread) { | 222 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTasksWithOneAvailableThread) { |
fdoray
2016/06/20 15:08:40
OneAvailableWorker
robliao
2016/06/20 17:50:09
Done.
| |
223 // Post blocking tasks to keep all threads busy except one until |event| is | 223 // Post blocking tasks to keep all threads busy except one until |event| is |
fdoray
2016/06/20 15:08:40
all *workers* busy
robliao
2016/06/20 17:50:09
Done.
| |
224 // signaled. Use different factories so that tasks are added to different | 224 // signaled. Use different factories so that tasks are added to different |
225 // sequences and can run simultaneously when the execution mode is SEQUENCED. | 225 // sequences and can run simultaneously when the execution mode is SEQUENCED. |
226 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 226 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
227 WaitableEvent::InitialState::NOT_SIGNALED); | 227 WaitableEvent::InitialState::NOT_SIGNALED); |
228 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; | 228 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; |
229 for (size_t i = 0; i < (kNumWorkersInWorkerPool - 1); ++i) { | 229 for (size_t i = 0; i < (kNumWorkersInWorkerPool - 1); ++i) { |
230 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( | 230 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( |
231 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 231 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
232 GetParam()))); | 232 GetParam()))); |
233 EXPECT_TRUE(blocked_task_factories.back()->PostTask( | 233 EXPECT_TRUE(blocked_task_factories.back()->PostTask( |
234 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); | 234 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); |
235 blocked_task_factories.back()->WaitForAllTasksToRun(); | 235 blocked_task_factories.back()->WaitForAllTasksToRun(); |
236 } | 236 } |
237 | 237 |
238 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 238 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
239 // that only one thread in |worker_pool_| isn't busy. | 239 // that only one thread in |worker_pool_| isn't busy. |
fdoray
2016/06/20 15:08:40
one *worker*
robliao
2016/06/20 17:50:09
Done.
| |
240 test::TestTaskFactory short_task_factory( | 240 test::TestTaskFactory short_task_factory( |
241 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 241 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
242 GetParam()); | 242 GetParam()); |
243 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 243 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
244 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, Closure())); | 244 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, Closure())); |
245 short_task_factory.WaitForAllTasksToRun(); | 245 short_task_factory.WaitForAllTasksToRun(); |
246 | 246 |
247 // Release tasks waiting on |event|. | 247 // Release tasks waiting on |event|. |
248 event.Signal(); | 248 event.Signal(); |
249 | 249 |
250 // Wait until all worker threads are idle to be sure that no task accesses | 250 // Wait until all workers are idle to be sure that no task accesses |
251 // its TestTaskFactory after it is destroyed. | 251 // its TestTaskFactory after it is destroyed. |
252 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 252 worker_pool_->WaitForAllWorkersIdleForTesting(); |
253 } | 253 } |
254 | 254 |
255 TEST_P(TaskSchedulerWorkerPoolImplTest, Saturate) { | 255 TEST_P(TaskSchedulerWorkerPoolImplTest, Saturate) { |
256 // Verify that it is possible to have |kNumWorkersInWorkerPool| | 256 // Verify that it is possible to have |kNumWorkersInWorkerPool| |
257 // tasks/sequences running simultaneously. Use different factories so that the | 257 // tasks/sequences running simultaneously. Use different factories so that the |
258 // blocking tasks are added to different sequences and can run simultaneously | 258 // blocking tasks are added to different sequences and can run simultaneously |
259 // when the execution mode is SEQUENCED. | 259 // when the execution mode is SEQUENCED. |
260 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 260 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
261 WaitableEvent::InitialState::NOT_SIGNALED); | 261 WaitableEvent::InitialState::NOT_SIGNALED); |
262 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; | 262 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; |
263 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { | 263 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { |
264 factories.push_back(WrapUnique(new test::TestTaskFactory( | 264 factories.push_back(WrapUnique(new test::TestTaskFactory( |
265 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 265 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
266 GetParam()))); | 266 GetParam()))); |
267 EXPECT_TRUE(factories.back()->PostTask( | 267 EXPECT_TRUE(factories.back()->PostTask( |
268 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); | 268 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); |
269 factories.back()->WaitForAllTasksToRun(); | 269 factories.back()->WaitForAllTasksToRun(); |
270 } | 270 } |
271 | 271 |
272 // Release tasks waiting on |event|. | 272 // Release tasks waiting on |event|. |
273 event.Signal(); | 273 event.Signal(); |
274 | 274 |
275 // Wait until all worker threads are idle to be sure that no task accesses | 275 // Wait until all workers are idle to be sure that no task accesses |
276 // its TestTaskFactory after it is destroyed. | 276 // its TestTaskFactory after it is destroyed. |
277 worker_pool_->WaitForAllWorkerWorkersIdleForTesting(); | 277 worker_pool_->WaitForAllWorkersIdleForTesting(); |
278 } | 278 } |
279 | 279 |
280 // Verify that a Task can't be posted after shutdown. | 280 // Verify that a Task can't be posted after shutdown. |
281 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { | 281 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { |
282 auto task_runner = | 282 auto task_runner = |
283 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); | 283 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); |
284 task_tracker_.Shutdown(); | 284 task_tracker_.Shutdown(); |
285 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); | 285 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); |
286 } | 286 } |
287 | 287 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 | 379 |
380 INSTANTIATE_TEST_CASE_P(IOAllowed, | 380 INSTANTIATE_TEST_CASE_P(IOAllowed, |
381 TaskSchedulerWorkerPoolImplIORestrictionTest, | 381 TaskSchedulerWorkerPoolImplIORestrictionTest, |
382 ::testing::Values(IORestriction::ALLOWED)); | 382 ::testing::Values(IORestriction::ALLOWED)); |
383 INSTANTIATE_TEST_CASE_P(IODisallowed, | 383 INSTANTIATE_TEST_CASE_P(IODisallowed, |
384 TaskSchedulerWorkerPoolImplIORestrictionTest, | 384 TaskSchedulerWorkerPoolImplIORestrictionTest, |
385 ::testing::Values(IORestriction::DISALLOWED)); | 385 ::testing::Values(IORestriction::DISALLOWED)); |
386 | 386 |
387 } // namespace internal | 387 } // namespace internal |
388 } // namespace base | 388 } // namespace base |
OLD | NEW |