| 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 = test::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 WaitBeforePostTask { |
| 80 NO_WAIT, |
| 81 WAIT_FOR_ALL_THREADS_IDLE, |
| 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_before_post_task| is |
| 163 // thread wait until all worker threads in |thread_pool| are idle before | 86 // WAIT_FOR_ALL_THREADS_IDLE, the thread waits until all worker threads in |
| 164 // posting a new task. If |post_nested_task| is true, each task posted by this | 87 // |thread_pool| are idle before posting a new task. If |post_nested_task| is |
| 165 // thread posts another task when it runs. | 88 // YES, each task posted by this 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 WaitBeforePostTask wait_before_post_task, |
| 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_before_post_task_(wait_before_post_task), |
| 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 test::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_before_post_task_ == |
| 111 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE) { |
| 186 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 112 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 187 factory_.PostTestTask(post_nested_task_, nullptr); | 113 } |
| 114 EXPECT_TRUE(factory_.PostTask(post_nested_task_, nullptr)); |
| 188 } | 115 } |
| 189 } | 116 } |
| 190 | 117 |
| 191 SchedulerThreadPool* const thread_pool_; | 118 SchedulerThreadPool* const thread_pool_; |
| 192 const scoped_refptr<TaskRunner> task_runner_; | 119 const scoped_refptr<TaskRunner> task_runner_; |
| 193 const bool wait_for_all_threads_idle_; | 120 const WaitBeforePostTask wait_before_post_task_; |
| 194 const bool post_nested_task_; | 121 const PostNestedTask post_nested_task_; |
| 195 TaskFactory factory_; | 122 test::TestTaskFactory factory_; |
| 196 | 123 |
| 197 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 124 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 198 }; | 125 }; |
| 199 | 126 |
| 127 using WaitBeforePostTask = ThreadPostingTasks::WaitBeforePostTask; |
| 128 |
| 200 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { | 129 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
| 201 // Create threads to post tasks. | 130 // Create threads to post tasks. |
| 202 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 131 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 203 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 132 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 204 const bool kWaitForAllThreadIdle = false; | 133 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 205 const bool kPostNestedTasks = false; | 134 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
| 206 threads_posting_tasks.push_back(WrapUnique( | 135 PostNestedTask::NO))); |
| 207 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 208 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 209 threads_posting_tasks.back()->Start(); | 136 threads_posting_tasks.back()->Start(); |
| 210 } | 137 } |
| 211 | 138 |
| 212 // Wait for all tasks to run. | 139 // Wait for all tasks to run. |
| 213 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 140 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 214 thread_posting_tasks->Join(); | 141 thread_posting_tasks->Join(); |
| 215 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 142 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 216 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 217 thread_posting_tasks->factory()->NumRunTasks()); | |
| 218 } | 143 } |
| 219 | 144 |
| 220 // Wait until all worker threads are idle to be sure that no task accesses | 145 // Wait until all worker threads are idle to be sure that no task accesses |
| 221 // its TaskFactory after |thread_posting_tasks| is destroyed. | 146 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 222 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 147 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 223 } | 148 } |
| 224 | 149 |
| 225 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { | 150 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
| 226 // Create threads to post tasks. To verify that worker threads can sleep and | 151 // 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 | 152 // be woken up when new tasks are posted, wait for all threads to become idle |
| 228 // before posting a new task. | 153 // before posting a new task. |
| 229 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 154 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 230 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 155 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 231 const bool kWaitForAllThreadIdle = true; | 156 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 232 const bool kPostNestedTasks = false; | 157 thread_pool_.get(), GetParam(), |
| 233 threads_posting_tasks.push_back(WrapUnique( | 158 WaitBeforePostTask::WAIT_FOR_ALL_THREADS_IDLE, PostNestedTask::NO))); |
| 234 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 235 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 236 threads_posting_tasks.back()->Start(); | 159 threads_posting_tasks.back()->Start(); |
| 237 } | 160 } |
| 238 | 161 |
| 239 // Wait for all tasks to run. | 162 // Wait for all tasks to run. |
| 240 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 163 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 241 thread_posting_tasks->Join(); | 164 thread_posting_tasks->Join(); |
| 242 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 165 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 243 EXPECT_EQ(kNumTasksPostedPerThread, | |
| 244 thread_posting_tasks->factory()->NumRunTasks()); | |
| 245 } | 166 } |
| 246 | 167 |
| 247 // Wait until all worker threads are idle to be sure that no task accesses | 168 // Wait until all worker threads are idle to be sure that no task accesses |
| 248 // its TaskFactory after |thread_posting_tasks| is destroyed. | 169 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 249 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 170 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 250 } | 171 } |
| 251 | 172 |
| 252 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { | 173 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
| 253 // Create threads to post tasks. Each task posted by these threads will post | 174 // Create threads to post tasks. Each task posted by these threads will post |
| 254 // another task when it runs. | 175 // another task when it runs. |
| 255 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 176 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 256 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { | 177 for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) { |
| 257 const bool kWaitForAllThreadIdle = false; | 178 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 258 const bool kPostNestedTasks = true; | 179 thread_pool_.get(), GetParam(), WaitBeforePostTask::NO_WAIT, |
| 259 threads_posting_tasks.push_back(WrapUnique( | 180 PostNestedTask::YES))); |
| 260 new ThreadPostingTasks(thread_pool_.get(), GetParam(), | |
| 261 kWaitForAllThreadIdle, kPostNestedTasks))); | |
| 262 threads_posting_tasks.back()->Start(); | 181 threads_posting_tasks.back()->Start(); |
| 263 } | 182 } |
| 264 | 183 |
| 265 // Wait for all tasks to run. | 184 // Wait for all tasks to run. |
| 266 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 185 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 267 thread_posting_tasks->Join(); | 186 thread_posting_tasks->Join(); |
| 268 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 187 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 269 EXPECT_EQ(2 * kNumTasksPostedPerThread, | |
| 270 thread_posting_tasks->factory()->NumRunTasks()); | |
| 271 } | 188 } |
| 272 | 189 |
| 273 // Wait until all worker threads are idle to be sure that no task accesses | 190 // Wait until all worker threads are idle to be sure that no task accesses |
| 274 // its TaskFactory after |thread_posting_tasks| is destroyed. | 191 // its TestTaskFactory after |thread_posting_tasks| is destroyed. |
| 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 192 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 276 } | 193 } |
| 277 | 194 |
| 278 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { | 195 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
| 279 // Post tasks to keep all threads busy except one until |event| is signaled. | 196 // 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 | 197 // Use different factories so that tasks are added to different sequences and |
| 281 // can run simultaneously when the execution mode is SEQUENCED. | 198 // can run simultaneously when the execution mode is SEQUENCED. |
| 282 WaitableEvent event(true, false); | 199 WaitableEvent event(true, false); |
| 283 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; | 200 std::vector<std::unique_ptr<test::TestTaskFactory>> blocked_task_factories; |
| 284 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { | 201 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
| 285 blocked_task_factories.push_back( | 202 blocked_task_factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 286 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 203 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 287 blocked_task_factories.back()->PostTestTask(false, &event); | 204 GetParam()))); |
| 205 EXPECT_TRUE( |
| 206 blocked_task_factories.back()->PostTask(PostNestedTask::NO, &event)); |
| 288 blocked_task_factories.back()->WaitForAllTasksToRun(); | 207 blocked_task_factories.back()->WaitForAllTasksToRun(); |
| 289 } | 208 } |
| 290 | 209 |
| 291 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 210 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 292 // that only one thread in |thread_pool_| isn't busy. | 211 // that only one thread in |thread_pool_| isn't busy. |
| 293 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); | 212 test::TestTaskFactory short_task_factory( |
| 213 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 214 GetParam()); |
| 294 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 215 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
| 295 short_task_factory.PostTestTask(false, nullptr); | 216 EXPECT_TRUE(short_task_factory.PostTask(PostNestedTask::NO, nullptr)); |
| 296 short_task_factory.WaitForAllTasksToRun(); | 217 short_task_factory.WaitForAllTasksToRun(); |
| 297 | 218 |
| 298 // Release tasks waiting on |event|. | 219 // Release tasks waiting on |event|. |
| 299 event.Signal(); | 220 event.Signal(); |
| 300 | 221 |
| 301 // Wait until all worker threads are idle to be sure that no task accesses | 222 // Wait until all worker threads are idle to be sure that no task accesses |
| 302 // its TaskFactory after it is destroyed. | 223 // its TestTaskFactory after it is destroyed. |
| 303 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 224 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 304 } | 225 } |
| 305 | 226 |
| 306 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { | 227 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
| 307 // Verify that it is possible to have |kNumThreadsInThreadPool| | 228 // Verify that it is possible to have |kNumThreadsInThreadPool| |
| 308 // tasks/sequences running simultaneously. Use different factories so that | 229 // tasks/sequences running simultaneously. Use different factories so that |
| 309 // tasks are added to different sequences and can run simultaneously when the | 230 // tasks are added to different sequences and can run simultaneously when the |
| 310 // execution mode is SEQUENCED. | 231 // execution mode is SEQUENCED. |
| 311 WaitableEvent event(true, false); | 232 WaitableEvent event(true, false); |
| 312 std::vector<std::unique_ptr<TaskFactory>> factories; | 233 std::vector<std::unique_ptr<test::TestTaskFactory>> factories; |
| 313 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { | 234 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
| 314 factories.push_back( | 235 factories.push_back(WrapUnique(new test::TestTaskFactory( |
| 315 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); | 236 thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()), |
| 316 factories.back()->PostTestTask(false, &event); | 237 GetParam()))); |
| 238 EXPECT_TRUE(factories.back()->PostTask(PostNestedTask::NO, &event)); |
| 317 factories.back()->WaitForAllTasksToRun(); | 239 factories.back()->WaitForAllTasksToRun(); |
| 318 } | 240 } |
| 319 | 241 |
| 320 // Release tasks waiting on |event|. | 242 // Release tasks waiting on |event|. |
| 321 event.Signal(); | 243 event.Signal(); |
| 322 | 244 |
| 323 // Wait until all worker threads are idle to be sure that no task accesses | 245 // Wait until all worker threads are idle to be sure that no task accesses |
| 324 // its TaskFactory after it is destroyed. | 246 // its TestTaskFactory after it is destroyed. |
| 325 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 247 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 326 } | 248 } |
| 327 | 249 |
| 328 INSTANTIATE_TEST_CASE_P(Parallel, | 250 INSTANTIATE_TEST_CASE_P(Parallel, |
| 329 TaskSchedulerThreadPoolTest, | 251 TaskSchedulerThreadPoolTest, |
| 330 ::testing::Values(ExecutionMode::PARALLEL)); | 252 ::testing::Values(ExecutionMode::PARALLEL)); |
| 331 INSTANTIATE_TEST_CASE_P(Sequenced, | 253 INSTANTIATE_TEST_CASE_P(Sequenced, |
| 332 TaskSchedulerThreadPoolTest, | 254 TaskSchedulerThreadPoolTest, |
| 333 ::testing::Values(ExecutionMode::SEQUENCED)); | 255 ::testing::Values(ExecutionMode::SEQUENCED)); |
| 334 | 256 |
| 335 } // namespace | 257 } // namespace |
| 336 } // namespace internal | 258 } // namespace internal |
| 337 } // namespace base | 259 } // namespace base |
| OLD | NEW |