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> |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 namespace internal { | 30 namespace internal { |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const size_t kNumThreadsInThreadPool = 4; | 33 const size_t kNumThreadsInThreadPool = 4; |
| 34 const size_t kNumThreadsPostingTasks = 4; | 34 const size_t kNumThreadsPostingTasks = 4; |
| 35 const size_t kNumTasksPostedPerThread = 150; | 35 const size_t kNumTasksPostedPerThread = 150; |
| 36 | 36 |
| 37 class TaskSchedulerThreadPoolTest : public testing::Test { | 37 class TaskSchedulerThreadPoolTest |
| 38 : public testing::TestWithParam<ExecutionMode> { | |
| 38 protected: | 39 protected: |
| 39 TaskSchedulerThreadPoolTest() = default; | 40 TaskSchedulerThreadPoolTest() = default; |
| 40 | 41 |
| 41 void SetUp() override { | 42 void SetUp() override { |
| 42 thread_pool_ = SchedulerThreadPool::CreateThreadPool( | 43 thread_pool_ = SchedulerThreadPool::CreateThreadPool( |
| 43 ThreadPriority::NORMAL, kNumThreadsInThreadPool, | 44 ThreadPriority::NORMAL, kNumThreadsInThreadPool, |
| 44 Bind(&TaskSchedulerThreadPoolTest::RanTaskFromSequenceCallback, | 45 Bind(&TaskSchedulerThreadPoolTest::RanTaskFromSequenceCallback, |
| 45 Unretained(this)), | 46 Unretained(this)), |
| 46 &task_tracker_); | 47 &task_tracker_); |
| 47 ASSERT_TRUE(thread_pool_); | 48 ASSERT_TRUE(thread_pool_); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void TearDown() override { | 51 void TearDown() override { |
| 51 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 52 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 52 thread_pool_->JoinForTesting(); | 53 thread_pool_->JoinForTesting(); |
| 53 } | 54 } |
| 54 | 55 |
| 55 std::unique_ptr<SchedulerThreadPool> thread_pool_; | 56 std::unique_ptr<SchedulerThreadPool> thread_pool_; |
| 56 | 57 |
| 57 private: | 58 private: |
| 58 void RanTaskFromSequenceCallback(scoped_refptr<Sequence> sequence) { | 59 void RanTaskFromSequenceCallback(scoped_refptr<Sequence> sequence) { |
| 59 // Reinsert |sequence| in |thread_pool_|'s shared PriorityQueue if it isn't | 60 // Reinsert |sequence| in |thread_pool_|'s shared PriorityQueue if it isn't |
| 60 // empty after popping one of its Tasks. In production code, this callback | 61 // empty after popping one of its Tasks. In production code, this callback |
| 61 // would be implemented by the TaskScheduler which would first determine in | 62 // would be implemented by the TaskScheduler which would first determine in |
| 62 // which PriorityQueue the sequence must be reinserted. | 63 // which PriorityQueue the sequence must be reinserted. |
| 63 const bool sequence_became_empty = sequence->PopTask(); | 64 if (!sequence->PopTask()) { |
| 64 if (!sequence_became_empty) { | |
| 65 const SequenceSortKey sort_key(sequence->GetSortKey()); | 65 const SequenceSortKey sort_key(sequence->GetSortKey()); |
| 66 thread_pool_->InsertSequenceAfterTaskRan(std::move(sequence), sort_key); | 66 thread_pool_->InsertSequenceAfterTaskRan(std::move(sequence), sort_key); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 TaskTracker task_tracker_; | 70 TaskTracker task_tracker_; |
| 71 | 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); | 72 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 class TaskFactory { | 75 class TaskFactory { |
| 76 public: | 76 public: |
| 77 TaskFactory() : cv_(&lock_) {} | 77 // Constructs a TaskFactory that posts tasks with |execution_mode| to |
| 78 // |thread_pool|. | |
| 79 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) | |
| 80 : cv_(&lock_), | |
| 81 task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), | |
| 82 execution_mode)), | |
| 83 execution_mode_(execution_mode) {} | |
| 78 | 84 |
| 79 // Posts a task through |task_runner|. If |post_nested_task| is true, the task | 85 // Posts a task. If |post_nested_task| is true, the task will post a new task |
| 80 // will post a new task through |task_runner| when it runs. If |event| is set, | 86 // when it runs. If |event| is set, the task will block until it is signaled. |
| 81 // the task will block until it is signaled. | 87 void PostTask(bool post_nested_task, WaitableEvent* event) { |
| 82 void PostTask(scoped_refptr<TaskRunner> task_runner, | |
| 83 bool post_nested_task, | |
| 84 WaitableEvent* event) { | |
| 85 AutoLock auto_lock(lock_); | 88 AutoLock auto_lock(lock_); |
| 86 EXPECT_TRUE(task_runner->PostTask( | 89 task_runner_->PostTask( |
|
gab
2016/04/08 16:52:25
Doesn't hurt to keep the EXPECT_TRUE I guess (alth
robliao
2016/04/08 17:11:11
If we're operating on the premise that the return
fdoray
2016/04/11 14:29:53
Put back the EXPECT_TRUE. The impl returns false w
| |
| 87 FROM_HERE, Bind(&TaskFactory::RunTaskCallback, Unretained(this), | 90 FROM_HERE, |
| 88 num_created_tasks_++, task_runner, post_nested_task, | 91 Bind(&TaskFactory::RunTaskCallback, Unretained(this), |
| 89 Unretained(event)))); | 92 num_created_tasks_++, post_nested_task, Unretained(event))); |
| 90 } | 93 } |
| 91 | 94 |
| 92 // Waits for all tasks posted by PostTask() to start running. It is not | 95 // Waits for all tasks posted by PostTask() to start running. It is not |
| 93 // guaranteed that the tasks have completed their execution when this returns. | 96 // guaranteed that the tasks have completed their execution when this returns. |
| 94 void WaitForAllTasksToRun() const { | 97 void WaitForAllTasksToRun() const { |
| 95 AutoLock auto_lock(lock_); | 98 AutoLock auto_lock(lock_); |
| 96 while (run_tasks_.size() < num_created_tasks_) | 99 while (run_tasks_.size() < num_created_tasks_) |
| 97 cv_.Wait(); | 100 cv_.Wait(); |
| 98 } | 101 } |
| 99 | 102 |
| 100 size_t NumRunTasks() const { | 103 size_t NumRunTasks() const { |
| 101 AutoLock auto_lock(lock_); | 104 AutoLock auto_lock(lock_); |
| 102 return run_tasks_.size(); | 105 return run_tasks_.size(); |
| 103 } | 106 } |
| 104 | 107 |
| 108 bool RunsTasksOnCurrentThread() const { | |
| 109 return task_runner_->RunsTasksOnCurrentThread(); | |
| 110 } | |
| 111 | |
| 105 private: | 112 private: |
| 106 void RunTaskCallback(size_t task_index, | 113 void RunTaskCallback(size_t task_index, |
| 107 scoped_refptr<TaskRunner> task_runner, | |
| 108 bool post_nested_task, | 114 bool post_nested_task, |
| 109 WaitableEvent* event) { | 115 WaitableEvent* event) { |
| 110 if (post_nested_task) | 116 if (post_nested_task) |
| 111 PostTask(task_runner, false, nullptr); | 117 PostTask(false, nullptr); |
| 112 | 118 |
| 113 EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread()); | 119 EXPECT_TRUE(RunsTasksOnCurrentThread()); |
| 114 | 120 |
| 115 { | 121 { |
| 116 AutoLock auto_lock(lock_); | 122 AutoLock auto_lock(lock_); |
| 117 | 123 |
| 124 if (execution_mode_ == ExecutionMode::SEQUENCED && | |
| 125 task_index != run_tasks_.size()) { | |
| 126 ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; | |
| 127 } | |
| 128 | |
| 118 if (run_tasks_.find(task_index) != run_tasks_.end()) | 129 if (run_tasks_.find(task_index) != run_tasks_.end()) |
| 119 ADD_FAILURE() << "A task ran more than once."; | 130 ADD_FAILURE() << "A task ran more than once."; |
| 120 run_tasks_.insert(task_index); | 131 run_tasks_.insert(task_index); |
| 121 | 132 |
| 122 cv_.Signal(); | 133 cv_.Signal(); |
| 123 } | 134 } |
| 124 | 135 |
| 125 if (event) | 136 if (event) |
| 126 event->Wait(); | 137 event->Wait(); |
| 127 } | 138 } |
| 128 | 139 |
| 129 // Synchronizes access to all members below. | 140 // Synchronizes access to all members below. |
| 130 mutable Lock lock_; | 141 mutable Lock lock_; |
| 131 | 142 |
| 132 // Condition variable signaled when a task runs. | 143 // Condition variable signaled when a task runs. |
| 133 mutable ConditionVariable cv_; | 144 mutable ConditionVariable cv_; |
| 134 | 145 |
| 146 // Task runner through which this factory posts tasks. | |
| 147 const scoped_refptr<TaskRunner> task_runner_; | |
| 148 | |
| 149 // Execution mode of |task_runner_|. | |
| 150 const ExecutionMode execution_mode_; | |
| 151 | |
| 135 // Number of tasks posted by PostTask(). | 152 // Number of tasks posted by PostTask(). |
| 136 size_t num_created_tasks_ = 0; | 153 size_t num_created_tasks_ = 0; |
| 137 | 154 |
| 138 // Indexes of tasks that ran. | 155 // Indexes of tasks that ran. |
| 139 std::unordered_set<size_t> run_tasks_; | 156 std::unordered_set<size_t> run_tasks_; |
| 140 | 157 |
| 141 DISALLOW_COPY_AND_ASSIGN(TaskFactory); | 158 DISALLOW_COPY_AND_ASSIGN(TaskFactory); |
| 142 }; | 159 }; |
| 143 | 160 |
| 144 class ThreadPostingTasks : public SimpleThread { | 161 class ThreadPostingTasks : public SimpleThread { |
| 145 public: | 162 public: |
| 146 // Constructs a thread that posts tasks to |thread_pool| through an | 163 // Constructs a thread that posts tasks to |thread_pool| through an |
| 147 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the | 164 // |execution_mode| task runner. If |wait_for_all_threads_idle| is true, the |
| 148 // thread wait until all worker threads in |thread_pool| are idle before | 165 // thread wait until all worker threads in |thread_pool| are idle before |
| 149 // posting a new task. If |post_nested_task| is true, each task posted by this | 166 // posting a new task. If |post_nested_task| is true, each task posted by this |
| 150 // thread posts another task when it runs. | 167 // thread posts another task when it runs. |
| 151 ThreadPostingTasks(SchedulerThreadPool* thread_pool, | 168 ThreadPostingTasks(SchedulerThreadPool* thread_pool, |
| 152 ExecutionMode execution_mode, | 169 ExecutionMode execution_mode, |
| 153 bool wait_for_all_threads_idle, | 170 bool wait_for_all_threads_idle, |
| 154 bool post_nested_task) | 171 bool post_nested_task) |
| 155 : SimpleThread("ThreadPostingTasks"), | 172 : SimpleThread("ThreadPostingTasks"), |
| 156 thread_pool_(thread_pool), | 173 thread_pool_(thread_pool), |
| 157 task_runner_(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), | |
| 158 execution_mode)), | |
| 159 wait_for_all_threads_idle_(wait_for_all_threads_idle), | 174 wait_for_all_threads_idle_(wait_for_all_threads_idle), |
| 160 post_nested_task_(post_nested_task) {} | 175 post_nested_task_(post_nested_task), |
| 176 factory_(thread_pool_, execution_mode) {} | |
| 161 | 177 |
| 162 const TaskFactory* factory() const { return &factory_; } | 178 const TaskFactory* factory() const { return &factory_; } |
| 163 | 179 |
| 164 private: | 180 private: |
| 165 void Run() override { | 181 void Run() override { |
| 166 EXPECT_FALSE(task_runner_->RunsTasksOnCurrentThread()); | 182 EXPECT_FALSE(factory_.RunsTasksOnCurrentThread()); |
| 167 | 183 |
| 168 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { | 184 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) { |
| 169 if (wait_for_all_threads_idle_) | 185 if (wait_for_all_threads_idle_) |
| 170 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 186 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 171 factory_.PostTask(task_runner_, post_nested_task_, nullptr); | 187 factory_.PostTask(post_nested_task_, nullptr); |
| 172 } | 188 } |
| 173 } | 189 } |
| 174 | 190 |
| 175 SchedulerThreadPool* const thread_pool_; | 191 SchedulerThreadPool* const thread_pool_; |
| 176 scoped_refptr<TaskRunner> task_runner_; | |
| 177 const bool wait_for_all_threads_idle_; | 192 const bool wait_for_all_threads_idle_; |
| 178 const bool post_nested_task_; | 193 const bool post_nested_task_; |
| 179 TaskFactory factory_; | 194 TaskFactory factory_; |
| 180 | 195 |
| 181 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 196 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 182 }; | 197 }; |
| 183 | 198 |
| 184 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasks) { | 199 TEST_P(TaskSchedulerThreadPoolTest, PostTasks) { |
| 185 // Create threads to post tasks to PARALLEL TaskRunners. | 200 // Create threads to post tasks. |
| 186 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 201 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 187 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { | 202 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
| 188 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 203 threads_posting_tasks.push_back(WrapUnique( |
| 189 thread_pool_.get(), ExecutionMode::PARALLEL, false, false))); | 204 new ThreadPostingTasks(thread_pool_.get(), GetParam(), false, false))); |
| 190 threads_posting_tasks.back()->Start(); | 205 threads_posting_tasks.back()->Start(); |
| 191 } | 206 } |
| 192 | 207 |
| 193 // Wait for all tasks to run. | 208 // Wait for all tasks to run. |
| 194 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 209 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 195 thread_posting_tasks->Join(); | 210 thread_posting_tasks->Join(); |
| 196 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 211 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 197 EXPECT_EQ(kNumTasksPostedPerThread, | 212 EXPECT_EQ(kNumTasksPostedPerThread, |
| 198 thread_posting_tasks->factory()->NumRunTasks()); | 213 thread_posting_tasks->factory()->NumRunTasks()); |
| 199 } | 214 } |
| 200 | 215 |
| 201 // Wait until all worker threads are idle to be sure that no task accesses | 216 // Wait until all worker threads are idle to be sure that no task accesses |
| 202 // its TaskFactory after |thread_posting_tasks| is destroyed. | 217 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 203 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 218 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 204 } | 219 } |
| 205 | 220 |
| 206 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWaitAllThreadsIdle) { | 221 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) { |
| 207 // Create threads to post tasks to PARALLEL TaskRunners. To verify that | 222 // Create threads to post tasks. To verify that worker threads can sleep and |
| 208 // worker threads can sleep and be woken up when new tasks are posted, wait | 223 // be woken up when new tasks are posted, wait for all threads to become idle |
| 209 // for all threads to become idle before posting a new task. | 224 // before posting a new task. |
| 210 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 225 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 211 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { | 226 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
| 212 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 227 threads_posting_tasks.push_back(WrapUnique( |
| 213 thread_pool_.get(), ExecutionMode::PARALLEL, true, false))); | 228 new ThreadPostingTasks(thread_pool_.get(), GetParam(), true, false))); |
| 214 threads_posting_tasks.back()->Start(); | 229 threads_posting_tasks.back()->Start(); |
| 215 } | 230 } |
| 216 | 231 |
| 217 // Wait for all tasks to run. | 232 // Wait for all tasks to run. |
| 218 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 233 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 219 thread_posting_tasks->Join(); | 234 thread_posting_tasks->Join(); |
| 220 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 235 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 221 EXPECT_EQ(kNumTasksPostedPerThread, | 236 EXPECT_EQ(kNumTasksPostedPerThread, |
| 222 thread_posting_tasks->factory()->NumRunTasks()); | 237 thread_posting_tasks->factory()->NumRunTasks()); |
| 223 } | 238 } |
| 224 | 239 |
| 225 // Wait until all worker threads are idle to be sure that no task accesses | 240 // Wait until all worker threads are idle to be sure that no task accesses |
| 226 // its TaskFactory after |thread_posting_tasks| is destroyed. | 241 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 227 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 242 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 228 } | 243 } |
| 229 | 244 |
| 230 TEST_F(TaskSchedulerThreadPoolTest, NestedPostParallelTasks) { | 245 TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
| 231 // Create threads to post tasks to PARALLEL TaskRunners. Each task posted by | 246 // Create threads to post tasks. Each task posted by these threads will post |
| 232 // these threads will post another task when it runs. | 247 // another task when it runs. |
| 233 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 248 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 234 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { | 249 for (size_t j = 0; j < kNumThreadsPostingTasks; ++j) { |
| 235 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 250 threads_posting_tasks.push_back(WrapUnique( |
| 236 thread_pool_.get(), ExecutionMode::PARALLEL, false, true))); | 251 new ThreadPostingTasks(thread_pool_.get(), GetParam(), false, true))); |
| 237 threads_posting_tasks.back()->Start(); | 252 threads_posting_tasks.back()->Start(); |
| 238 } | 253 } |
| 239 | 254 |
| 240 // Wait for all tasks to run. | 255 // Wait for all tasks to run. |
| 241 for (const auto& thread_posting_tasks : threads_posting_tasks) { | 256 for (const auto& thread_posting_tasks : threads_posting_tasks) { |
| 242 thread_posting_tasks->Join(); | 257 thread_posting_tasks->Join(); |
| 243 thread_posting_tasks->factory()->WaitForAllTasksToRun(); | 258 thread_posting_tasks->factory()->WaitForAllTasksToRun(); |
| 244 EXPECT_EQ(2 * kNumTasksPostedPerThread, | 259 EXPECT_EQ(2 * kNumTasksPostedPerThread, |
| 245 thread_posting_tasks->factory()->NumRunTasks()); | 260 thread_posting_tasks->factory()->NumRunTasks()); |
| 246 } | 261 } |
| 247 | 262 |
| 248 // Wait until all worker threads are idle to be sure that no task accesses | 263 // Wait until all worker threads are idle to be sure that no task accesses |
| 249 // its TaskFactory after |thread_posting_tasks| is destroyed. | 264 // its TaskFactory after |thread_posting_tasks| is destroyed. |
| 250 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 265 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 251 } | 266 } |
| 252 | 267 |
| 253 TEST_F(TaskSchedulerThreadPoolTest, PostParallelTasksWithOneAvailableThread) { | 268 TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
| 254 TaskFactory factory; | |
| 255 | |
| 256 // Post tasks to keep all threads busy except one until |event| is signaled. | 269 // Post tasks to keep all threads busy except one until |event| is signaled. |
| 270 // Use different factories so that tasks are added to different sequences when | |
| 271 // the execution mode is SEQUENCED. | |
|
robliao
2016/04/08 17:11:12
Worth mentioning that we do this to avoid sequence
fdoray
2016/04/11 14:29:53
Done.
| |
| 257 WaitableEvent event(true, false); | 272 WaitableEvent event(true, false); |
| 258 auto task_runner = thread_pool_->CreateTaskRunnerWithTraits( | 273 std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; |
| 259 TaskTraits(), ExecutionMode::PARALLEL); | 274 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
| 260 for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) | 275 blocked_task_factories.push_back( |
| 261 factory.PostTask(task_runner, false, &event); | 276 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
| 262 factory.WaitForAllTasksToRun(); | 277 blocked_task_factories.back()->PostTask(false, &event); |
| 278 blocked_task_factories.back()->WaitForAllTasksToRun(); | |
| 279 } | |
| 263 | 280 |
| 264 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact | 281 // Post |kNumTasksPostedPerThread| tasks that should all run despite the fact |
| 265 // that only one thread in |thread_pool_| isn't busy. | 282 // that only one thread in |thread_pool_| isn't busy. |
| 283 TaskFactory short_task_factory(thread_pool_.get(), GetParam()); | |
| 266 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) | 284 for (size_t i = 0; i < kNumTasksPostedPerThread; ++i) |
| 267 factory.PostTask(task_runner, false, nullptr); | 285 short_task_factory.PostTask(false, nullptr); |
| 268 factory.WaitForAllTasksToRun(); | 286 short_task_factory.WaitForAllTasksToRun(); |
| 269 | 287 |
| 270 // Release tasks waiting on |event|. | 288 // Release tasks waiting on |event|. |
| 271 event.Signal(); | 289 event.Signal(); |
| 272 | 290 |
| 273 // Wait until all worker threads are idle to be sure that no task accesses | 291 // Wait until all worker threads are idle to be sure that no task accesses |
| 274 // |factory| after it is destroyed. | 292 // its TaskFactory after it is destroyed. |
| 275 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 293 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 276 } | 294 } |
| 277 | 295 |
| 278 TEST_F(TaskSchedulerThreadPoolTest, Saturate) { | 296 TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
| 279 TaskFactory factory; | |
| 280 | |
| 281 // Verify that it is possible to have |kNumThreadsInThreadPool| tasks running | 297 // Verify that it is possible to have |kNumThreadsInThreadPool| tasks running |
|
gab
2016/04/08 16:52:25
"tasks running" => "tasks/sequences running" ? to
robliao
2016/04/08 17:11:12
Hrm... this is an interesting point.
We are runnin
fdoray
2016/04/11 14:29:53
Done.
| |
| 282 // simultaneously. | 298 // simultaneously. Use different factories so that tasks are added to |
| 299 // different sequences when the execution mode is SEQUENCED. | |
| 283 WaitableEvent event(true, false); | 300 WaitableEvent event(true, false); |
| 284 auto task_runner = thread_pool_->CreateTaskRunnerWithTraits( | 301 std::vector<std::unique_ptr<TaskFactory>> factories; |
| 285 TaskTraits(), ExecutionMode::PARALLEL); | 302 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
| 286 for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) | 303 factories.push_back( |
| 287 factory.PostTask(task_runner, false, &event); | 304 WrapUnique(new TaskFactory(thread_pool_.get(), GetParam()))); |
| 288 factory.WaitForAllTasksToRun(); | 305 factories.back()->PostTask(false, &event); |
| 306 factories.back()->WaitForAllTasksToRun(); | |
| 307 } | |
| 289 | 308 |
| 290 // Release tasks waiting on |event|. | 309 // Release tasks waiting on |event|. |
| 291 event.Signal(); | 310 event.Signal(); |
| 292 | 311 |
| 293 // Wait until all worker threads are idle to be sure that no task accesses | 312 // Wait until all worker threads are idle to be sure that no task accesses |
| 294 // |factory| after it is destroyed. | 313 // its TaskFactory after it is destroyed. |
| 295 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 314 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 296 } | 315 } |
| 297 | 316 |
| 317 INSTANTIATE_TEST_CASE_P(Parallel, | |
| 318 TaskSchedulerThreadPoolTest, | |
| 319 ::testing::Values(ExecutionMode::PARALLEL)); | |
| 320 INSTANTIATE_TEST_CASE_P(Sequenced, | |
| 321 TaskSchedulerThreadPoolTest, | |
| 322 ::testing::Values(ExecutionMode::SEQUENCED)); | |
| 323 | |
| 298 } // namespace | 324 } // namespace |
| 299 } // namespace internal | 325 } // namespace internal |
| 300 } // namespace base | 326 } // namespace base |
| OLD | NEW |