| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 EXPECT_EQ(supports_background_priority && | 61 EXPECT_EQ(supports_background_priority && |
| 62 traits.priority() == TaskPriority::BACKGROUND | 62 traits.priority() == TaskPriority::BACKGROUND |
| 63 ? ThreadPriority::BACKGROUND | 63 ? ThreadPriority::BACKGROUND |
| 64 : ThreadPriority::NORMAL, | 64 : ThreadPriority::NORMAL, |
| 65 PlatformThread::GetCurrentThreadPriority()); | 65 PlatformThread::GetCurrentThreadPriority()); |
| 66 | 66 |
| 67 #if DCHECK_IS_ON() | 67 #if DCHECK_IS_ON() |
| 68 // The #if above is required because GetIOAllowed() always returns true when | 68 // The #if above is required because GetIOAllowed() always returns true when |
| 69 // !DCHECK_IS_ON(), even when |traits| don't allow file I/O. | 69 // !DCHECK_IS_ON(), even when |traits| don't allow file I/O. |
| 70 EXPECT_EQ(traits.with_file_io(), GetIOAllowed()); | 70 EXPECT_EQ(traits.may_block(), GetIOAllowed()); |
| 71 #endif | 71 #endif |
| 72 | 72 |
| 73 // Verify that the thread the task is running on is named as expected. | 73 // Verify that the thread the task is running on is named as expected. |
| 74 const std::string current_thread_name(PlatformThread::GetName()); | 74 const std::string current_thread_name(PlatformThread::GetName()); |
| 75 EXPECT_NE(std::string::npos, current_thread_name.find("TaskScheduler")); | 75 EXPECT_NE(std::string::npos, current_thread_name.find("TaskScheduler")); |
| 76 EXPECT_NE(std::string::npos, | 76 EXPECT_NE(std::string::npos, |
| 77 current_thread_name.find( | 77 current_thread_name.find( |
| 78 traits.priority() == TaskPriority::BACKGROUND ? "Background" | 78 traits.priority() == TaskPriority::BACKGROUND ? "Background" |
| 79 : "Foreground")); | 79 : "Foreground")); |
| 80 EXPECT_EQ(traits.with_file_io(), | 80 EXPECT_EQ(traits.may_block(), |
| 81 current_thread_name.find("FileIO") != std::string::npos); | 81 current_thread_name.find("Blocking") != std::string::npos); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void VerifyTaskEnvironementAndSignalEvent(const TaskTraits& traits, | 84 void VerifyTaskEnvironementAndSignalEvent(const TaskTraits& traits, |
| 85 WaitableEvent* event) { | 85 WaitableEvent* event) { |
| 86 DCHECK(event); | 86 DCHECK(event); |
| 87 VerifyTaskEnvironement(traits); | 87 VerifyTaskEnvironement(traits); |
| 88 event->Signal(); | 88 event->Signal(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraitsAndExecutionMode( | 91 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraitsAndExecutionMode( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 const TaskTraits traits_; | 134 const TaskTraits traits_; |
| 135 test::TestTaskFactory factory_; | 135 test::TestTaskFactory factory_; |
| 136 | 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); | 137 DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks); |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 // Returns a vector with a TraitsExecutionModePair for each valid | 140 // Returns a vector with a TraitsExecutionModePair for each valid |
| 141 // combination of {ExecutionMode, TaskPriority, WithFileIO()}. | 141 // combination of {ExecutionMode, TaskPriority, MayBlock()}. |
| 142 std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() { | 142 std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() { |
| 143 std::vector<TraitsExecutionModePair> params; | 143 std::vector<TraitsExecutionModePair> params; |
| 144 | 144 |
| 145 const test::ExecutionMode execution_modes[] = { | 145 const test::ExecutionMode execution_modes[] = { |
| 146 test::ExecutionMode::PARALLEL, test::ExecutionMode::SEQUENCED, | 146 test::ExecutionMode::PARALLEL, test::ExecutionMode::SEQUENCED, |
| 147 test::ExecutionMode::SINGLE_THREADED}; | 147 test::ExecutionMode::SINGLE_THREADED}; |
| 148 | 148 |
| 149 for (test::ExecutionMode execution_mode : execution_modes) { | 149 for (test::ExecutionMode execution_mode : execution_modes) { |
| 150 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); | 150 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); |
| 151 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); | 151 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); |
| 152 ++priority_index) { | 152 ++priority_index) { |
| 153 const TaskPriority priority = static_cast<TaskPriority>(priority_index); | 153 const TaskPriority priority = static_cast<TaskPriority>(priority_index); |
| 154 params.push_back(TraitsExecutionModePair( | 154 params.push_back(TraitsExecutionModePair( |
| 155 TaskTraits().WithPriority(priority), execution_mode)); | 155 TaskTraits().WithPriority(priority), execution_mode)); |
| 156 params.push_back(TraitsExecutionModePair( | 156 params.push_back(TraitsExecutionModePair( |
| 157 TaskTraits().WithPriority(priority).WithFileIO(), execution_mode)); | 157 TaskTraits().WithPriority(priority).MayBlock(), execution_mode)); |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 return params; | 161 return params; |
| 162 } | 162 } |
| 163 | 163 |
| 164 enum WorkerPoolType { | 164 enum WorkerPoolType { |
| 165 BACKGROUND_WORKER_POOL = 0, | 165 BACKGROUND_WORKER_POOL = 0, |
| 166 BACKGROUND_FILE_IO_WORKER_POOL, | 166 BACKGROUND_BLOCKING_WORKER_POOL, |
| 167 FOREGROUND_WORKER_POOL, | 167 FOREGROUND_WORKER_POOL, |
| 168 FOREGROUND_FILE_IO_WORKER_POOL, | 168 FOREGROUND_BLOCKING_WORKER_POOL, |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) { | 171 size_t GetThreadPoolIndexForTraits(const TaskTraits& traits) { |
| 172 if (traits.with_file_io()) { | 172 if (traits.may_block()) { |
| 173 return traits.priority() == TaskPriority::BACKGROUND | 173 return traits.priority() == TaskPriority::BACKGROUND |
| 174 ? BACKGROUND_FILE_IO_WORKER_POOL | 174 ? BACKGROUND_BLOCKING_WORKER_POOL |
| 175 : FOREGROUND_FILE_IO_WORKER_POOL; | 175 : FOREGROUND_BLOCKING_WORKER_POOL; |
| 176 } | 176 } |
| 177 return traits.priority() == TaskPriority::BACKGROUND ? BACKGROUND_WORKER_POOL | 177 return traits.priority() == TaskPriority::BACKGROUND ? BACKGROUND_WORKER_POOL |
| 178 : FOREGROUND_WORKER_POOL; | 178 : FOREGROUND_WORKER_POOL; |
| 179 } | 179 } |
| 180 | 180 |
| 181 class TaskSchedulerImplTest | 181 class TaskSchedulerImplTest |
| 182 : public testing::TestWithParam<TraitsExecutionModePair> { | 182 : public testing::TestWithParam<TraitsExecutionModePair> { |
| 183 protected: | 183 protected: |
| 184 TaskSchedulerImplTest() = default; | 184 TaskSchedulerImplTest() = default; |
| 185 | 185 |
| 186 void SetUp() override { | 186 void SetUp() override { |
| 187 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; | 187 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 188 | 188 |
| 189 std::vector<SchedulerWorkerPoolParams> params_vector; | 189 std::vector<SchedulerWorkerPoolParams> params_vector; |
| 190 | 190 |
| 191 ASSERT_EQ(BACKGROUND_WORKER_POOL, params_vector.size()); | 191 ASSERT_EQ(BACKGROUND_WORKER_POOL, params_vector.size()); |
| 192 params_vector.emplace_back("Background", ThreadPriority::BACKGROUND, | 192 params_vector.emplace_back("Background", ThreadPriority::BACKGROUND, |
| 193 StandbyThreadPolicy::LAZY, 1U, TimeDelta::Max()); | 193 StandbyThreadPolicy::LAZY, 1U, TimeDelta::Max()); |
| 194 | 194 |
| 195 ASSERT_EQ(BACKGROUND_FILE_IO_WORKER_POOL, params_vector.size()); | 195 ASSERT_EQ(BACKGROUND_BLOCKING_WORKER_POOL, params_vector.size()); |
| 196 params_vector.emplace_back("BackgroundFileIO", ThreadPriority::BACKGROUND, | 196 params_vector.emplace_back("BackgroundBlocking", ThreadPriority::BACKGROUND, |
| 197 StandbyThreadPolicy::LAZY, 3U, TimeDelta::Max()); | 197 StandbyThreadPolicy::LAZY, 3U, TimeDelta::Max()); |
| 198 | 198 |
| 199 ASSERT_EQ(FOREGROUND_WORKER_POOL, params_vector.size()); | 199 ASSERT_EQ(FOREGROUND_WORKER_POOL, params_vector.size()); |
| 200 params_vector.emplace_back("Foreground", ThreadPriority::NORMAL, | 200 params_vector.emplace_back("Foreground", ThreadPriority::NORMAL, |
| 201 StandbyThreadPolicy::LAZY, 4U, TimeDelta::Max()); | 201 StandbyThreadPolicy::LAZY, 4U, TimeDelta::Max()); |
| 202 | 202 |
| 203 ASSERT_EQ(FOREGROUND_FILE_IO_WORKER_POOL, params_vector.size()); | 203 ASSERT_EQ(FOREGROUND_BLOCKING_WORKER_POOL, params_vector.size()); |
| 204 params_vector.emplace_back("ForegroundFileIO", ThreadPriority::NORMAL, | 204 params_vector.emplace_back("ForegroundBlocking", ThreadPriority::NORMAL, |
| 205 StandbyThreadPolicy::LAZY, 12U, | 205 StandbyThreadPolicy::LAZY, 12U, |
| 206 TimeDelta::Max()); | 206 TimeDelta::Max()); |
| 207 | 207 |
| 208 scheduler_ = TaskSchedulerImpl::Create(params_vector, | 208 scheduler_ = TaskSchedulerImpl::Create(params_vector, |
| 209 Bind(&GetThreadPoolIndexForTraits)); | 209 Bind(&GetThreadPoolIndexForTraits)); |
| 210 ASSERT_TRUE(scheduler_); | 210 ASSERT_TRUE(scheduler_); |
| 211 } | 211 } |
| 212 | 212 |
| 213 void TearDown() override { scheduler_->JoinForTesting(); } | 213 void TearDown() override { scheduler_->JoinForTesting(); } |
| 214 | 214 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 thread->WaitForAllTasksToRun(); | 273 thread->WaitForAllTasksToRun(); |
| 274 thread->Join(); | 274 thread->Join(); |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 | 277 |
| 278 // TODO(fdoray): Add tests with Sequences that move around worker pools once | 278 // TODO(fdoray): Add tests with Sequences that move around worker pools once |
| 279 // child TaskRunners are supported. | 279 // child TaskRunners are supported. |
| 280 | 280 |
| 281 } // namespace internal | 281 } // namespace internal |
| 282 } // namespace base | 282 } // namespace base |
| OLD | NEW |