Chromium Code Reviews| 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_thread_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_thread_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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/callback.h" | |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/condition_variable.h" | 19 #include "base/synchronization/condition_variable.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 20 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/task_runner.h" | 22 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/delayed_task_manager.h" | 23 #include "base/task_scheduler/delayed_task_manager.h" |
| 23 #include "base/task_scheduler/sequence.h" | 24 #include "base/task_scheduler/sequence.h" |
| 24 #include "base/task_scheduler/sequence_sort_key.h" | 25 #include "base/task_scheduler/sequence_sort_key.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 | 120 |
| 120 private: | 121 private: |
| 121 void Run() override { | 122 void Run() override { |
| 122 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | 123 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
| 123 | 124 |
| 124 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | 125 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
| 125 if (wait_before_post_task_ == | 126 if (wait_before_post_task_ == |
| 126 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { | 127 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { |
| 127 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 128 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 128 } | 129 } |
| 129 EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr)); | 130 EXPECT_TRUE(factory_.PostTask(post_nested_task_, Closure())); |
| 130 } | 131 } |
| 131 } | 132 } |
| 132 | 133 |
| 133 SchedulerThreadPoolImpl* const thread_pool_; | 134 SchedulerThreadPoolImpl* const thread_pool_; |
| 134 const scoped_refptr<TaskRunner> task_runner_; | 135 const scoped_refptr<TaskRunner> task_runner_; |
| 135 const WaitBeforePostTask wait_before_post_task_; | 136 const WaitBeforePostTask wait_before_post_task_; |
| 136 const PostNestedTask post_nested_task_; | 137 const PostNestedTask post_nested_task_; |
| 137 test::TestTaskFactory factory_; | 138 test::TestTaskFactory factory_; |
| 138 | 139 |
| 139 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 140 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 thread_posting_tasks->Join(); | 213 thread_posting_tasks->Join(); |
| 213 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 214 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 214 } | 215 } |
| 215 | 216 |
| 216 // Wait until all worker threads are idle to be sure that no task accesses | 217 // Wait until all worker threads are idle to be sure that no task accesses |
| 217 // its TestTaskFactory after |thread_posting_tasks| is destroyed. | 218 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 218 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 219 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 219 } | 220 } |
| 220 | 221 |
| 221 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) { | 222 TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) { |
| 222 // Post tasks to keep all threads busy except one until |event| is signaled. | 223 // Post tasks to keep all threads busy except one until |event| is signaled. |
|
gab
2016/04/27 19:15:28
s/Post tasks/Post blocking tasks/ ? (since it's no
fdoray
2016/04/28 18:36:31
Done.
| |
| 223 // Use different factories so that tasks are added to different sequences and | 224 // Use different factories so that tasks are added to different sequences and |
| 224 // can run simultaneously when the execution mode is SEQUENCED. | 225 // can run simultaneously when the execution mode is SEQUENCED. |
| 225 WaitableEvent event(true, false); | 226 WaitableEvent event(true, false); |
| 226 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; | 227 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; |
| 227 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { | 228 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
| 228 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( | 229 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 229 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 230 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 230 GetParam()))); | 231 GetParam()))); |
| 231 EXPECT_TRUE( | 232 EXPECT_TRUE(blocked_task_factories.back()->PostTask( |
| 232 blocked_task_factories.back()->PostTask(PostNestedTask::NO, &event)); | 233 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); |
| 233 blocked_task_factories.back()->WaitForAllTasksToRun(); | 234 blocked_task_factories.back()->WaitForAllTasksToRun(); |
| 234 } | 235 } |
| 235 | 236 |
| 236 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 237 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 237 // that only one thread in |thread_pool_| isn't busy. | 238 // that only one thread in |thread_pool_| isn't busy. |
| 238 test::TestTaskFactory short_task_factory( | 239 test::TestTaskFactory short_task_factory( |
| 239 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 240 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 240 GetParam()); | 241 GetParam()); |
| 241 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 242 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
| 242 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, nullptr)); | 243 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, Closure())); |
| 243 short_task_factory.WaitForAllTasksToRun(); | 244 short_task_factory.WaitForAllTasksToRun(); |
| 244 | 245 |
| 245 // Release tasks waiting on |event|. | 246 // Release tasks waiting on |event|. |
| 246 event.Signal(); | 247 event.Signal(); |
| 247 | 248 |
| 248 // Wait until all worker threads are idle to be sure that no task accesses | 249 // Wait until all worker threads are idle to be sure that no task accesses |
| 249 // its TestTaskFactory after it is destroyed. | 250 // its TestTaskFactory after it is destroyed. |
| 250 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 251 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 251 } | 252 } |
| 252 | 253 |
| 253 TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) { | 254 TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) { |
| 254 // Verify that it is possible to have |kNumThreadsInThreadPool| | 255 // Verify that it is possible to have |kNumThreadsInThreadPool| |
| 255 // tasks/sequences running simultaneously. Use different factories so that | 256 // tasks/sequences running simultaneously. Use different factories so that |
| 256 // tasks are added to different sequences and can run simultaneously when the | 257 // tasks are added to different sequences and can run simultaneously when the |
|
gab
2016/04/27 19:15:28
s/tasks/the blocking tasks/
fdoray
2016/04/28 18:36:31
Done.
| |
| 257 // execution mode is SEQUENCED. | 258 // execution mode is SEQUENCED. |
| 258 WaitableEvent event(true, false); | 259 WaitableEvent event(true, false); |
| 259 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; | 260 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; |
| 260 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { | 261 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
| 261 factories.push_back(WrapUnique(new test::TestTaskFactory( | 262 factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 262 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | 263 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 263 GetParam()))); | 264 GetParam()))); |
| 264 EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event)); | 265 EXPECT_TRUE(factories.back()->PostTask( |
| 266 PostNestedTask::NO, Bind(&WaitableEvent::Wait, Unretained(&event)))); | |
| 265 factories.back()->WaitForAllTasksToRun(); | 267 factories.back()->WaitForAllTasksToRun(); |
| 266 } | 268 } |
| 267 | 269 |
| 268 // Release tasks waiting on |event|. | 270 // Release tasks waiting on |event|. |
| 269 event.Signal(); | 271 event.Signal(); |
| 270 | 272 |
| 271 // Wait until all worker threads are idle to be sure that no task accesses | 273 // Wait until all worker threads are idle to be sure that no task accesses |
| 272 // its TestTaskFactory after it is destroyed. | 274 // its TestTaskFactory after it is destroyed. |
| 273 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 274 } | 276 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 | 312 |
| 311 INSTANTIATE_TEST_CASE_P(Parallel, | 313 INSTANTIATE_TEST_CASE_P(Parallel, |
| 312 TaskSchedulerThreadPoolImplTest, | 314 TaskSchedulerThreadPoolImplTest, |
| 313 ::testing::Values(ExecutionMode::PARALLEL)); | 315 ::testing::Values(ExecutionMode::PARALLEL)); |
| 314 INSTANTIATE_TEST_CASE_P(Sequenced, | 316 INSTANTIATE_TEST_CASE_P(Sequenced, |
| 315 TaskSchedulerThreadPoolImplTest, | 317 TaskSchedulerThreadPoolImplTest, |
| 316 ::testing::Values(ExecutionMode::SEQUENCED)); | 318 ::testing::Values(ExecutionMode::SEQUENCED)); |
| 317 | 319 |
| 318 } // namespace internal | 320 } // namespace internal |
| 319 } // namespace base | 321 } // namespace base |
| OLD | NEW |