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/task_scheduler_impl.h" | 5 #include "base/task_scheduler/task_scheduler_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 params.push_back(TraitsExecutionModePair( | 168 params.push_back(TraitsExecutionModePair( |
| 169 TaskTraits().WithPriority(priority), execution_mode)); | 169 TaskTraits().WithPriority(priority), execution_mode)); |
| 170 params.push_back(TraitsExecutionModePair( | 170 params.push_back(TraitsExecutionModePair( |
| 171 TaskTraits().WithPriority(priority).MayBlock(), execution_mode)); | 171 TaskTraits().WithPriority(priority).MayBlock(), execution_mode)); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 return params; | 175 return params; |
| 176 } | 176 } |
| 177 | 177 |
| 178 enum WorkerPoolType { | |
| 179 BACKGROUND_WORKER_POOL = 0, | |
| 180 BACKGROUND_BLOCKING_WORKER_POOL, | |
| 181 FOREGROUND_WORKER_POOL, | |
| 182 FOREGROUND_BLOCKING_WORKER_POOL, | |
| 183 }; | |
| 184 | |
| 185 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) { | |
| 186 if (traits.may_block()) { | |
| 187 return traits.priority() == TaskPriority::BACKGROUND | |
| 188 ? BACKGROUND_BLOCKING_WORKER_POOL | |
| 189 : FOREGROUND_BLOCKING_WORKER_POOL; | |
| 190 } | |
| 191 return traits.priority() == TaskPriority::BACKGROUND ? BACKGROUND_WORKER_POOL | |
| 192 : FOREGROUND_WORKER_POOL; | |
| 193 } | |
| 194 | |
| 195 class TaskSchedulerImplTest | 178 class TaskSchedulerImplTest |
| 196 : public testing::TestWithParam<TraitsExecutionModePair> { | 179 : public testing::TestWithParam<TraitsExecutionModePair> { |
| 197 protected: | 180 protected: |
| 198 TaskSchedulerImplTest() = default; | 181 TaskSchedulerImplTest() = default; |
| 199 | 182 |
| 200 void SetUp() override { | 183 void SetUp() override { |
| 201 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; | 184 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 202 | 185 |
| 203 std::vector<SchedulerWorkerPoolParams> params_vector; | 186 constexpr TimeDelta kSuggestedReclaimTime = TimeDelta::FromSeconds(30); |
| 187 constexpr int kMaxNumBackgroundThreads = 1; | |
| 188 constexpr int kMaxNumBackgroundBlockingThreads = 3; | |
| 189 constexpr int kMaxNumForegroundThreads = 4; | |
| 190 constexpr int kMaxNumForegroundBlockingThreads = 12; | |
| 204 | 191 |
| 205 ASSERT_EQ(BACKGROUND_WORKER_POOL, params_vector.size()); | 192 scheduler_ = TaskSchedulerImpl::Create( |
| 206 params_vector.emplace_back("Background", ThreadPriority::BACKGROUND, | 193 "Test", {{StandbyThreadPolicy::LAZY, kMaxNumBackgroundThreads, |
| 207 StandbyThreadPolicy::LAZY, 1U, TimeDelta::Max()); | 194 kSuggestedReclaimTime}, |
| 195 {StandbyThreadPolicy::LAZY, kMaxNumBackgroundBlockingThreads, | |
| 196 kSuggestedReclaimTime}, | |
| 197 {StandbyThreadPolicy::LAZY, kMaxNumForegroundThreads, | |
| 198 kSuggestedReclaimTime}, | |
| 199 {StandbyThreadPolicy::LAZY, kMaxNumForegroundBlockingThreads, | |
| 200 kSuggestedReclaimTime}}); | |
| 208 | 201 |
| 209 ASSERT_EQ(BACKGROUND_BLOCKING_WORKER_POOL, params_vector.size()); | |
| 210 params_vector.emplace_back("BackgroundBlocking", ThreadPriority::BACKGROUND, | |
| 211 StandbyThreadPolicy::LAZY, 3U, TimeDelta::Max()); | |
| 212 | |
| 213 ASSERT_EQ(FOREGROUND_WORKER_POOL, params_vector.size()); | |
| 214 params_vector.emplace_back("Foreground", ThreadPriority::NORMAL, | |
| 215 StandbyThreadPolicy::LAZY, 4U, TimeDelta::Max()); | |
| 216 | |
| 217 ASSERT_EQ(FOREGROUND_BLOCKING_WORKER_POOL, params_vector.size()); | |
| 218 params_vector.emplace_back("ForegroundBlocking", ThreadPriority::NORMAL, | |
| 219 StandbyThreadPolicy::LAZY, 12U, | |
| 220 TimeDelta::Max()); | |
| 221 | |
| 222 scheduler_ = TaskSchedulerImpl::Create(params_vector, | |
| 223 Bind(&GetThreadPoolIndexForTraits)); | |
| 224 ASSERT_TRUE(scheduler_); | 202 ASSERT_TRUE(scheduler_); |
| 225 } | 203 } |
| 226 | 204 |
| 227 void TearDown() override { scheduler_->JoinForTesting(); } | 205 void TearDown() override { scheduler_->JoinForTesting(); } |
| 228 | 206 |
| 229 std::unique_ptr<TaskSchedulerImpl> scheduler_; | 207 std::unique_ptr<TaskSchedulerImpl> scheduler_; |
| 230 | 208 |
| 231 private: | 209 private: |
| 232 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); | 210 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest); |
| 233 }; | 211 }; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with | 269 // TaskTraits and ExecutionModes. Verifies that each Task runs on a thread with |
| 292 // the expected priority and I/O restrictions and respects the characteristics | 270 // the expected priority and I/O restrictions and respects the characteristics |
| 293 // of its ExecutionMode. | 271 // of its ExecutionMode. |
| 294 TEST_F(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { | 272 TEST_F(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) { |
| 295 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; | 273 std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks; |
| 296 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { | 274 for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) { |
| 297 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( | 275 threads_posting_tasks.push_back(WrapUnique(new ThreadPostingTasks( |
| 298 scheduler_.get(), traits_execution_mode_pair.traits, | 276 scheduler_.get(), traits_execution_mode_pair.traits, |
| 299 traits_execution_mode_pair.execution_mode))); | 277 traits_execution_mode_pair.execution_mode))); |
| 300 threads_posting_tasks.back()->Start(); | 278 threads_posting_tasks.back()->Start(); |
| 279 threads_posting_tasks.back()->WaitForAllTasksToRun(); | |
| 280 threads_posting_tasks.back()->Join(); | |
| 301 } | 281 } |
| 302 | 282 /* |
| 303 for (const auto& thread : threads_posting_tasks) { | 283 for (const auto& thread : threads_posting_tasks) { |
| 304 thread->WaitForAllTasksToRun(); | 284 //thread->WaitForAllTasksToRun(); |
| 305 thread->Join(); | 285 //thread->Join(); |
| 306 } | 286 }*/ |
|
gab
2017/04/12 18:46:24
Fix temp code?
fdoray
2017/04/12 20:00:23
Done.
| |
| 307 } | 287 } |
| 308 | 288 |
| 309 TEST_F(TaskSchedulerImplTest, GetMaxConcurrentTasksWithTraitsDeprecated) { | 289 TEST_F(TaskSchedulerImplTest, GetMaxConcurrentTasksWithTraitsDeprecated) { |
| 310 EXPECT_EQ(1, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 290 EXPECT_EQ(1, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
| 311 TaskTraits().WithPriority(TaskPriority::BACKGROUND))); | 291 TaskTraits().WithPriority(TaskPriority::BACKGROUND))); |
| 312 EXPECT_EQ( | 292 EXPECT_EQ( |
| 313 3, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 293 3, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
| 314 TaskTraits().WithPriority(TaskPriority::BACKGROUND).MayBlock())); | 294 TaskTraits().WithPriority(TaskPriority::BACKGROUND).MayBlock())); |
| 315 EXPECT_EQ(4, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 295 EXPECT_EQ(4, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
| 316 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE))); | 296 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE))); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 } | 369 } |
| 390 task_ran->Signal(); | 370 task_ran->Signal(); |
| 391 }, | 371 }, |
| 392 com_sta_task_runner, Unretained(&task_ran))); | 372 com_sta_task_runner, Unretained(&task_ran))); |
| 393 task_ran.Wait(); | 373 task_ran.Wait(); |
| 394 } | 374 } |
| 395 #endif // defined(OS_WIN) | 375 #endif // defined(OS_WIN) |
| 396 | 376 |
| 397 } // namespace internal | 377 } // namespace internal |
| 398 } // namespace base | 378 } // namespace base |
| OLD | NEW |