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.h" | 5 #include "base/task_scheduler/scheduler_thread_pool.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/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/delayed_task_manager.h" | 22 #include "base/task_scheduler/delayed_task_manager.h" |
| 23 #include "base/task_scheduler/sequence.h" | 23 #include "base/task_scheduler/sequence.h" |
| 24 #include "base/task_scheduler/sequence_sort_key.h" | 24 #include "base/task_scheduler/sequence_sort_key.h" |
| 25 #include "base/task_scheduler/task_tracker.h" | 25 #include "base/task_scheduler/task_tracker.h" |
| 26 #include "base/task_scheduler/test_task_factory.h" | |
| 26 #include "base/threading/platform_thread.h" | 27 #include "base/threading/platform_thread.h" |
| 27 #include "base/threading/simple_thread.h" | 28 #include "base/threading/simple_thread.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 31 namespace internal { | 32 namespace internal { |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 const size_t kNumThreadsInThreadPool = 4; | 35 const size_t kNumThreadsInThreadPool = 4; |
| 35 const size_t kNumThreadsPostingTasks = 4; | 36 const size_t kNumThreadsPostingTasks = 4; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 64 const SequenceSortKey sort_key(sequence->GetSortKey()); | 65 const SequenceSortKey sort_key(sequence->GetSortKey()); |
| 65 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); | 66 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); |
| 66 } | 67 } |
| 67 | 68 |
| 68 TaskTracker task_tracker_; | 69 TaskTracker task_tracker_; |
| 69 DelayedTaskManager delayed_task_manager_; | 70 DelayedTaskManager delayed_task_manager_; |
| 70 | 71 |
| 71 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); | 72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 class TaskFactory { | 75 using PostNestedTask = TestTaskFactory::PostNestedTask; |
| 75 public: | |
| 76 // Constructs a TaskFactory that posts tasks with |execution_mode| to | |
| 77 // |thread_pool|. | |
| 78 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) | |
| 79 : cv_(&lock_), | |
| 80 task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), | |
| 81 execution_mode)), | |
| 82 execution_mode_(execution_mode) {} | |
| 83 | |
| 84 // Posts a task through |task_runner_|. If |post_nested_task| is true, the | |
| 85 // task will post a new task when it runs. If |event| is set, the task will | |
| 86 // block until it is signaled. | |
| 87 void PostTestTask(bool post_nested_task, WaitableEvent* event) { | |
| 88 AutoLock auto_lock(lock_); | |
| 89 EXPECT_TRUE(task_runner_->PostTask( | |
| 90 FROM_HERE, | |
| 91 Bind(&TaskFactory::RunTaskCallback, Unretained(this), | |
| 92 num_created_tasks_++, post_nested_task, Unretained(event)))); | |
| 93 } | |
| 94 | |
| 95 // Waits for all tasks posted by PostTestTask() to start running. It is not | |
| 96 // guaranteed that the tasks have completed their execution when this returns. | |
| 97 void WaitForAllTasksToRun() const { | |
| 98 AutoLock auto_lock(lock_); | |
| 99 while (ran_tasks_.size() < num_created_tasks_) | |
| 100 cv_.Wait(); | |
| 101 } | |
| 102 | |
| 103 size_t NumRunTasks() const { | |
| 104 AutoLock auto_lock(lock_); | |
| 105 return ran_tasks_.size(); | |
| 106 } | |
| 107 | |
| 108 const TaskRunner* task_runner() const { return task_runner_.get(); } | |
| 109 | |
| 110 private: | |
| 111 void RunTaskCallback(size_t task_index, | |
| 112 bool post_nested_task, | |
| 113 WaitableEvent* event) { | |
| 114 if (post_nested_task) | |
| 115 PostTestTask(false, nullptr); | |
| 116 | |
| 117 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); | |
| 118 | |
| 119 { | |
| 120 AutoLock auto_lock(lock_); | |
| 121 | |
| 122 if (execution_mode_ == ExecutionMode::SEQUENCED && | |
| 123 task_index != ran_tasks_.size()) { | |
| 124 ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; | |
| 125 } | |
| 126 | |
| 127 if (ran_tasks_.find(task_index) != ran_tasks_.end()) | |
| 128 ADD_FAILURE() << "A task ran more than once."; | |
| 129 ran_tasks_.insert(task_index); | |
| 130 | |
| 131 cv_.Signal(); | |
| 132 } | |
| 133 | |
| 134 if (event) | |
| 135 event->Wait(); | |
| 136 } | |
| 137 | |
| 138 // Synchronizes access to all members below. | |
| 139 mutable Lock lock_; | |
| 140 | |
| 141 // Condition variable signaled when a task runs. | |
| 142 mutable ConditionVariable cv_; | |
| 143 | |
| 144 // Task runner through which this factory posts tasks. | |
| 145 const scoped_refptr<TaskRunner> task_runner_; | |
| 146 | |
| 147 // Execution mode of |task_runner_|. | |
| 148 const ExecutionMode execution_mode_; | |
| 149 | |
| 150 // Number of tasks posted by PostTestTask(). | |
| 151 size_t num_created_tasks_ = 0; | |
| 152 | |
| 153 // Indexes of tasks that ran. | |
| 154 std::unordered_set<size_t> ran_tasks_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(TaskFactory); | |
| 157 }; | |
| 158 | 76 |
| 159 class ThreadPostingTasks : public SimpleThread { | 77 class ThreadPostingTasks : public SimpleThread { |
| 160 public: | 78 public: |
| 79 enum class WaitForAllThreadsIdle { | |
| 80 YES, | |
|
robliao
2016/04/21 01:38:17
Maybe this can be
enum class ThreadIdleBehavior {
fdoray
2016/04/22 15:54:03
Done.
| |
| 81 NO, | |
| 82 }; | |
| 83 | |
| 161 // Constructs a thread that posts tasks to |thread_pool| through an | 84 // Constructs a thread that posts tasks to |thread_pool| through an |
| 162 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the | 85 // |execution_mode| task runner. If |wait_for_all_threads_idle| is YES, the |
| 163 // thread wait until all worker threads in |thread_pool| are idle before | 86 // thread waits until all worker threads in |thread_pool| are idle before |
| 164 // posting a new task. If |post_nested_task| is true, each task posted by this | 87 // posting a new task. If |post_nested_task| is YES, each task posted by this |
| 165 // thread posts another task when it runs. | 88 // thread posts another task when it runs. |
| 166 ThreadPostingTasks(SchedulerThreadPool* thread_pool, | 89 ThreadPostingTasks(SchedulerThreadPool* thread_pool, |
| 167 ExecutionMode execution_mode, | 90 ExecutionMode execution_mode, |
| 168 bool wait_for_all_threads_idle, | 91 WaitForAllThreadsIdle wait_for_all_threads_idle, |
| 169 bool post_nested_task) | 92 PostNestedTask post_nested_task) |
| 170 : SimpleThread("ThreadPostingTasks"), | 93 : SimpleThread("ThreadPostingTasks"), |
| 171 thread_pool_(thread_pool), | 94 thread_pool_(thread_pool), |
| 172 wait_for_all_threads_idle_(wait_for_all_threads_idle), | 95 wait_for_all_threads_idle_(wait_for_all_threads_idle), |
| 173 post_nested_task_(post_nested_task), | 96 post_nested_task_(post_nested_task), |
| 174 factory_(thread_pool_, execution_mode) { | 97 factory_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), |
| 98 execution_mode), | |
| 99 execution_mode) { | |
| 175 DCHECK(thread_pool_); | 100 DCHECK(thread_pool_); |
| 176 } | 101 } |
| 177 | 102 |
| 178 const TaskFactory* factory() const { return &factory_; } | 103 const TestTaskFactory* factory() const { return &factory_; } |
| 179 | 104 |
| 180 private: | 105 private: |
| 181 void Run() override { | 106 void Run() override { |
| 182 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); | 107 EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread()); |
| 183 | 108 |
| 184 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | 109 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
| 185 if (wait_for_all_threads_idle_) | 110 if (wait_for_all_threads_idle_ == WaitForAllThreadsIdle::YES) |
| 186 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 111 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 187 factory_.PostTestTask(post_nested_task_, nullptr); | 112 factory_.PostTask(post_nested_task_, nullptr); |
| 188 } | 113 } |
| 189 } | 114 } |
| 190 | 115 |
| 191 SchedulerThreadPool* const thread_pool_; | 116 SchedulerThreadPool* const thread_pool_; |
| 192 const scoped_refptr<TaskRunner> task_runner_; | 117 const scoped_refptr<TaskRunner> task_runner_; |
| 193 const bool wait_for_all_threads_idle_; | 118 const WaitForAllThreadsIdle wait_for_all_threads_idle_; |
| 194 const bool post_nested_task_; | 119 const TestTaskFactory::PostNestedTask post_nested_task_; |
| 195 TaskFactory factory_; | 120 TestTaskFactory factory_; |
| 196 | 121 |
| 197 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 122 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 198 }; | 123 }; |
| 199 | 124 |
| 125 using WaitForAllThreadsIdle = ThreadPostingTasks::WaitForAllThreadsIdle; | |
| 126 | |
| 200 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { | 127 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
| 201 // Create threads to post tasks. | 128 // Create threads to post tasks. |
| 202 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 129 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 203 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 130 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 204 const bool kWaitForAllThreadIdle = false; | |
| 205 const bool kPostNestedTasks = false; | |
| 206 threads_posting_tasks.push_back(WrapUnique( | 131 threads_posting_tasks.push_back(WrapUnique( |
| 207 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | 132 new ThreadPostingTasks(thread_pool_.get(), GetParam(), |
| 208 kWaitForAllThreadIdle, kPostNestedTasks))); | 133 WaitForAllThreadsIdle::NO, PostNestedTask::NO))); |
| 209 threads_posting_tasks.back()->Start(); | 134 threads_posting_tasks.back()->Start(); |
| 210 } | 135 } |
| 211 | 136 |
| 212 // Wait for all tasks to run. | 137 // Wait for all tasks to run. |
| 213 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 138 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 214 thread_posting_tasks->Join(); | 139 thread_posting_tasks->Join(); |
| 215 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 140 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 216 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 217 thread_posting_tasks->factory()->NumRunTasks()); | |
| 218 } | 141 } |
| 219 | 142 |
| 220 // Wait until all worker threads are idle to be sure that no task accesses | 143 // Wait until all worker threads are idle to be sure that no task accesses |
| 221 // its TaskFactory after |thread_posting_tasks| is destroyed. | 144 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 222 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 145 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 223 } | 146 } |
| 224 | 147 |
| 225 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { | 148 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
| 226 // Create threads to post tasks. To verify that worker threads can sleep and | 149 // Create threads to post tasks. To verify that worker threads can sleep and |
| 227 // be woken up when new tasks are posted, wait for all threads to become idle | 150 // be woken up when new tasks are posted, wait for all threads to become idle |
| 228 // before posting a new task. | 151 // before posting a new task. |
| 229 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 152 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 230 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 153 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 231 const bool kWaitForAllThreadIdle = true; | 154 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 232 const bool kPostNestedTasks = false; | 155 thread_pool_.get(), GetParam(), WaitForAllThreadsIdle::YES, |
| 233 threads_posting_tasks.push_back(WrapUnique( | 156 PostNestedTask::NO))); |
| 234 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 235 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 236 threads_posting_tasks.back()->Start(); | 157 threads_posting_tasks.back()->Start(); |
| 237 } | 158 } |
| 238 | 159 |
| 239 // Wait for all tasks to run. | 160 // Wait for all tasks to run. |
| 240 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 161 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 241 thread_posting_tasks->Join(); | 162 thread_posting_tasks->Join(); |
| 242 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 163 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 243 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 244 thread_posting_tasks->factory()->NumRunTasks()); | |
| 245 } | 164 } |
| 246 | 165 |
| 247 // Wait until all worker threads are idle to be sure that no task accesses | 166 // Wait until all worker threads are idle to be sure that no task accesses |
| 248 // its TaskFactory after |thread_posting_tasks| is destroyed. | 167 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 249 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 168 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 250 } | 169 } |
| 251 | 170 |
| 252 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { | 171 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
| 253 // Create threads to post tasks. Each task posted by these threads will post | 172 // Create threads to post tasks. Each task posted by these threads will post |
| 254 // another task when it runs. | 173 // another task when it runs. |
| 255 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 174 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 256 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 175 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 257 const bool kWaitForAllThreadIdle = false; | 176 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 258 const bool kPostNestedTasks = true; | 177 thread_pool_.get(), GetParam(), WaitForAllThreadsIdle::NO, |
| 259 threads_posting_tasks.push_back(WrapUnique( | 178 PostNestedTask::YES))); |
| 260 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 261 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 262 threads_posting_tasks.back()->Start(); | 179 threads_posting_tasks.back()->Start(); |
| 263 } | 180 } |
| 264 | 181 |
| 265 // Wait for all tasks to run. | 182 // Wait for all tasks to run. |
| 266 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 183 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 267 thread_posting_tasks->Join(); | 184 thread_posting_tasks->Join(); |
| 268 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 185 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 269 EXPECT_EQ(2 * kNumTasksPostedPerThread, | |
| 270 thread_posting_tasks->factory()->NumRunTasks()); | |
| 271 } | 186 } |
| 272 | 187 |
| 273 // Wait until all worker threads are idle to be sure that no task accesses | 188 // Wait until all worker threads are idle to be sure that no task accesses |
| 274 // its TaskFactory after |thread_posting_tasks| is destroyed. | 189 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 190 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 276 } | 191 } |
| 277 | 192 |
| 278 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { | 193 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
| 279 // Post tasks to keep all threads busy except one until |event| is signaled. | 194 // Post tasks to keep all threads busy except one until |event| is signaled. |
| 280 // Use different factories so that tasks are added to different sequences and | 195 // Use different factories so that tasks are added to different sequences and |
| 281 // can run simultaneously when the execution mode is SEQUENCED. | 196 // can run simultaneously when the execution mode is SEQUENCED. |
| 282 WaitableEvent event(true, false); | 197 WaitableEvent event(true, false); |
| 283 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; | 198 std::vector<std::unique_ptr<TestTaskFactory>> blocked_task_factories; |
| 284 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { | 199 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
| 285 blocked_task_factories.push_back( | 200 blocked_task_factories.push_back(WrapUnique(new TestTaskFactory( |
| 286 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 201 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 287 blocked_task_factories.back()->PostTestTask(false, &event); | 202 GetParam()))); |
| 203 blocked_task_factories.back()->PostTask(PostNestedTask::NO, &event); | |
| 288 blocked_task_factories.back()->WaitForAllTasksToRun(); | 204 blocked_task_factories.back()->WaitForAllTasksToRun(); |
| 289 } | 205 } |
| 290 | 206 |
| 291 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 207 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 292 // that only one thread in |thread_pool_| isn't busy. | 208 // that only one thread in |thread_pool_| isn't busy. |
| 293 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); | 209 TestTaskFactory short_task_factory( |
| 210 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), | |
| 211 GetParam()); | |
| 294 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 212 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
| 295 short_task_factory.PostTestTask(false, nullptr); | 213 short_task_factory.PostTask(PostNestedTask::NO, nullptr); |
| 296 short_task_factory.WaitForAllTasksToRun(); | 214 short_task_factory.WaitForAllTasksToRun(); |
| 297 | 215 |
| 298 // Release tasks waiting on |event|. | 216 // Release tasks waiting on |event|. |
| 299 event.Signal(); | 217 event.Signal(); |
| 300 | 218 |
| 301 // Wait until all worker threads are idle to be sure that no task accesses | 219 // Wait until all worker threads are idle to be sure that no task accesses |
| 302 // its TaskFactory after it is destroyed. | 220 // its TestTaskFactory after it is destroyed. |
| 303 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 221 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 304 } | 222 } |
| 305 | 223 |
| 306 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { | 224 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
| 307 // Verify that it is possible to have |kNumThreadsInThreadPool| | 225 // Verify that it is possible to have |kNumThreadsInThreadPool| |
| 308 // tasks/sequences running simultaneously. Use different factories so that | 226 // tasks/sequences running simultaneously. Use different factories so that |
| 309 // tasks are added to different sequences and can run simultaneously when the | 227 // tasks are added to different sequences and can run simultaneously when the |
| 310 // execution mode is SEQUENCED. | 228 // execution mode is SEQUENCED. |
| 311 WaitableEvent event(true, false); | 229 WaitableEvent event(true, false); |
| 312 std::vector<std::unique_ptr<TaskFactory>> factories; | 230 std::vector<std::unique_ptr<TestTaskFactory>> factories; |
| 313 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { | 231 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
| 314 factories.push_back( | 232 factories.push_back(WrapUnique(new TestTaskFactory( |
| 315 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 233 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 316 factories.back()->PostTestTask(false, &event); | 234 GetParam()))); |
| 235 factories.back()->PostTask(PostNestedTask::NO, &event); | |
| 317 factories.back()->WaitForAllTasksToRun(); | 236 factories.back()->WaitForAllTasksToRun(); |
| 318 } | 237 } |
| 319 | 238 |
| 320 // Release tasks waiting on |event|. | 239 // Release tasks waiting on |event|. |
| 321 event.Signal(); | 240 event.Signal(); |
| 322 | 241 |
| 323 // Wait until all worker threads are idle to be sure that no task accesses | 242 // Wait until all worker threads are idle to be sure that no task accesses |
| 324 // its TaskFactory after it is destroyed. | 243 // its TestTaskFactory after it is destroyed. |
| 325 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 244 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 326 } | 245 } |
| 327 | 246 |
| 328 INSTANTIATE_TEST_CASE_P(Parallel, | 247 INSTANTIATE_TEST_CASE_P(Parallel, |
| 329 TaskSchedulerThreadPoolTest, | 248 TaskSchedulerThreadPoolTest, |
| 330 ::testing::Values(ExecutionMode::PARALLEL)); | 249 ::testing::Values(ExecutionMode::PARALLEL)); |
| 331 INSTANTIATE_TEST_CASE_P(Sequenced, | 250 INSTANTIATE_TEST_CASE_P(Sequenced, |
| 332 TaskSchedulerThreadPoolTest, | 251 TaskSchedulerThreadPoolTest, |
| 333 ::testing::Values(ExecutionMode::SEQUENCED)); | 252 ::testing::Values(ExecutionMode::SEQUENCED)); |
| 334 | 253 |
| 335 } // namespace | 254 } // namespace |
| 336 } // namespace internal | 255 } // namespace internal |
| 337 } // namespace base | 256 } // namespace base |
| OLD | NEW |