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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/task_scheduler/delayed_task_manager.h" | 12 #include "base/task_scheduler/delayed_task_manager.h" |
| 13 #include "base/task_scheduler/scheduler_single_thread_task_runner_manager.h" | 13 #include "base/task_scheduler/scheduler_single_thread_task_runner_manager.h" |
| 14 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 14 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 15 #include "base/task_scheduler/sequence_sort_key.h" | 15 #include "base/task_scheduler/sequence_sort_key.h" |
| 16 #include "base/task_scheduler/task.h" | 16 #include "base/task_scheduler/task.h" |
| 17 #include "base/task_scheduler/task_tracker.h" | 17 #include "base/task_scheduler/task_tracker.h" |
| 18 #include "build/build_config.h" | |
| 19 | |
| 20 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | |
| 21 #include "base/task_scheduler/task_tracker_posix.h" | |
| 22 #endif | |
| 23 | 18 |
| 24 namespace base { | 19 namespace base { |
| 25 namespace internal { | 20 namespace internal { |
| 26 | 21 |
| 27 namespace { | 22 namespace { |
| 28 | 23 |
| 29 enum EnvironmentType { | 24 enum EnvironmentType { |
| 30 BACKGROUND = 0, | 25 BACKGROUND = 0, |
| 31 BACKGROUND_BLOCKING, | 26 BACKGROUND_BLOCKING, |
| 32 FOREGROUND, | 27 FOREGROUND, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 return histograms; | 122 return histograms; |
| 128 } | 123 } |
| 129 | 124 |
| 130 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( | 125 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( |
| 131 const TaskTraits& traits) const { | 126 const TaskTraits& traits) const { |
| 132 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); | 127 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); |
| 133 } | 128 } |
| 134 | 129 |
| 135 void TaskSchedulerImpl::Shutdown() { | 130 void TaskSchedulerImpl::Shutdown() { |
| 136 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | 131 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. |
| 137 DCHECK(task_tracker_); | 132 task_tracker_.Shutdown(); |
| 138 task_tracker_->Shutdown(); | |
| 139 } | 133 } |
| 140 | 134 |
| 141 void TaskSchedulerImpl::FlushForTesting() { | 135 void TaskSchedulerImpl::FlushForTesting() { |
| 142 DCHECK(task_tracker_); | 136 task_tracker_.Flush(); |
| 143 task_tracker_->Flush(); | |
| 144 } | 137 } |
| 145 | 138 |
| 146 void TaskSchedulerImpl::JoinForTesting() { | 139 void TaskSchedulerImpl::JoinForTesting() { |
| 147 #if DCHECK_IS_ON() | 140 #if DCHECK_IS_ON() |
| 148 DCHECK(!join_for_testing_returned_.IsSet()); | 141 DCHECK(!join_for_testing_returned_.IsSet()); |
| 149 #endif | 142 #endif |
| 150 single_thread_task_runner_manager_->JoinForTesting(); | 143 single_thread_task_runner_manager_->JoinForTesting(); |
| 151 for (const auto& worker_pool : worker_pools_) | 144 for (const auto& worker_pool : worker_pools_) |
| 152 worker_pool->DisallowWorkerDetachmentForTesting(); | 145 worker_pool->DisallowWorkerDetachmentForTesting(); |
| 153 for (const auto& worker_pool : worker_pools_) | 146 for (const auto& worker_pool : worker_pools_) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 169 Thread::Options service_thread_options; | 162 Thread::Options service_thread_options; |
| 170 service_thread_options.message_loop_type = | 163 service_thread_options.message_loop_type = |
| 171 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 164 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 172 MessageLoop::TYPE_IO; | 165 MessageLoop::TYPE_IO; |
| 173 #else | 166 #else |
| 174 MessageLoop::TYPE_DEFAULT; | 167 MessageLoop::TYPE_DEFAULT; |
| 175 #endif | 168 #endif |
| 176 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; | 169 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; |
| 177 CHECK(service_thread_.StartWithOptions(service_thread_options)); | 170 CHECK(service_thread_.StartWithOptions(service_thread_options)); |
| 178 | 171 |
| 179 // Instantiate TaskTracker. Needs to happen after starting the service thread | |
| 180 // to get its message_loop(). | |
| 181 task_tracker_ = | |
| 182 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 172 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 183 base::MakeUnique<TaskTrackerPosix>( | 173 // Needs to happen after starting the service thread to get its |
| 184 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); | 174 // message_loop(). |
| 185 #else | 175 task_tracker_.Start( |
| 186 base::MakeUnique<TaskTracker>(); | 176 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); |
| 187 #endif | 177 #endif |
|
gab
2017/04/12 19:17:34
Hmmm, not starting non-POSIX TaskTracker too??
fdoray
2017/04/13 12:29:08
Renamed Start() -> set_watch_file_descriptor_messa
| |
| 188 | 178 |
| 189 // Start DelayedTaskManager. Needs to happen after starting the service thread | 179 // Needs to happen after starting the service thread to get its task_runner(). |
| 190 // to get its task_runner(). | |
| 191 delayed_task_manager_.Start(service_thread_.task_runner()); | 180 delayed_task_manager_.Start(service_thread_.task_runner()); |
| 192 | 181 |
| 193 single_thread_task_runner_manager_ = | 182 single_thread_task_runner_manager_ = |
| 194 MakeUnique<SchedulerSingleThreadTaskRunnerManager>( | 183 MakeUnique<SchedulerSingleThreadTaskRunnerManager>( |
| 195 task_tracker_.get(), &delayed_task_manager_); | 184 &task_tracker_, &delayed_task_manager_); |
| 196 single_thread_task_runner_manager_->Start(); | 185 single_thread_task_runner_manager_->Start(); |
| 197 | 186 |
| 198 // Callback invoked by workers to re-enqueue a sequence in the appropriate | 187 // Callback invoked by workers to re-enqueue a sequence in the appropriate |
| 199 // PriorityQueue. | 188 // PriorityQueue. |
| 200 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback | 189 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback |
| 201 re_enqueue_sequence_callback = | 190 re_enqueue_sequence_callback = |
| 202 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); | 191 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); |
| 203 | 192 |
| 204 // Order must match the EnvironmentType enum. | 193 // Order must match the EnvironmentType enum. |
| 205 const SchedulerWorkerPoolParams* worker_pool_params[] = { | 194 const SchedulerWorkerPoolParams* worker_pool_params[] = { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 219 | 208 |
| 220 // Start worker pools. | 209 // Start worker pools. |
| 221 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; | 210 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; |
| 222 ++environment_type) { | 211 ++environment_type) { |
| 223 // Passing pointers to objects owned by |this| to the constructor of | 212 // Passing pointers to objects owned by |this| to the constructor of |
| 224 // SchedulerWorkerPoolImpl is safe because a TaskSchedulerImpl can't be | 213 // SchedulerWorkerPoolImpl is safe because a TaskSchedulerImpl can't be |
| 225 // deleted before all its worker pools have been joined. | 214 // deleted before all its worker pools have been joined. |
| 226 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( | 215 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( |
| 227 name_ + kEnvironmentParams[environment_type].name_suffix, | 216 name_ + kEnvironmentParams[environment_type].name_suffix, |
| 228 kEnvironmentParams[environment_type].priority_hint, | 217 kEnvironmentParams[environment_type].priority_hint, |
| 229 re_enqueue_sequence_callback, task_tracker_.get(), | 218 re_enqueue_sequence_callback, &task_tracker_, &delayed_task_manager_); |
| 230 &delayed_task_manager_); | |
| 231 worker_pools_[environment_type]->Start( | 219 worker_pools_[environment_type]->Start( |
| 232 *worker_pool_params[environment_type]); | 220 *worker_pool_params[environment_type]); |
| 233 } | 221 } |
| 234 } | 222 } |
| 235 | 223 |
| 236 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( | 224 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( |
| 237 const TaskTraits& traits) const { | 225 const TaskTraits& traits) const { |
| 238 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); | 226 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); |
| 239 } | 227 } |
| 240 | 228 |
| 241 void TaskSchedulerImpl::ReEnqueueSequenceCallback( | 229 void TaskSchedulerImpl::ReEnqueueSequenceCallback( |
| 242 scoped_refptr<Sequence> sequence) { | 230 scoped_refptr<Sequence> sequence) { |
| 243 DCHECK(sequence); | 231 DCHECK(sequence); |
| 244 | 232 |
| 245 const SequenceSortKey sort_key = sequence->GetSortKey(); | 233 const SequenceSortKey sort_key = sequence->GetSortKey(); |
| 246 | 234 |
| 247 // The next task in |sequence| should run in a worker pool suited for its | 235 // The next task in |sequence| should run in a worker pool suited for its |
| 248 // traits, except for the priority which is adjusted to the highest priority | 236 // traits, except for the priority which is adjusted to the highest priority |
| 249 // in |sequence|. | 237 // in |sequence|. |
| 250 const TaskTraits traits = | 238 const TaskTraits traits = |
| 251 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); | 239 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); |
| 252 | 240 |
| 253 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | 241 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), |
| 254 sort_key); | 242 sort_key); |
| 255 } | 243 } |
| 256 | 244 |
| 257 } // namespace internal | 245 } // namespace internal |
| 258 } // namespace base | 246 } // namespace base |
| OLD | NEW |