| 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/memory/ptr_util.h" | |
| 10 #include "base/task_scheduler/delayed_task_manager.h" | 9 #include "base/task_scheduler/delayed_task_manager.h" |
| 11 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 10 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 12 #include "base/task_scheduler/sequence.h" | 11 #include "base/task_scheduler/sequence.h" |
| 13 #include "base/task_scheduler/sequence_sort_key.h" | 12 #include "base/task_scheduler/sequence_sort_key.h" |
| 14 #include "base/task_scheduler/task.h" | 13 #include "base/task_scheduler/task.h" |
| 15 #include "base/task_scheduler/task_tracker.h" | 14 #include "base/task_scheduler/task_tracker.h" |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 namespace internal { | 17 namespace internal { |
| 19 | 18 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 46 size_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { | 45 size_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { |
| 47 const bool is_background = | 46 const bool is_background = |
| 48 traits.priority() == base::TaskPriority::BACKGROUND; | 47 traits.priority() == base::TaskPriority::BACKGROUND; |
| 49 if (traits.may_block() || traits.with_base_sync_primitives()) | 48 if (traits.may_block() || traits.with_base_sync_primitives()) |
| 50 return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; | 49 return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; |
| 51 return is_background ? BACKGROUND : FOREGROUND; | 50 return is_background ? BACKGROUND : FOREGROUND; |
| 52 } | 51 } |
| 53 | 52 |
| 54 } // namespace | 53 } // namespace |
| 55 | 54 |
| 56 TaskSchedulerImpl::TaskSchedulerImpl(StringPiece name) | 55 TaskSchedulerImpl::TaskSchedulerImpl( |
| 56 StringPiece name, |
| 57 std::unique_ptr<TaskTrackerImpl> task_tracker) |
| 57 : name_(name), | 58 : name_(name), |
| 58 service_thread_("TaskSchedulerServiceThread"), | 59 service_thread_("TaskSchedulerServiceThread"), |
| 59 single_thread_task_runner_manager_(&task_tracker_, | 60 task_tracker_(std::move(task_tracker)), |
| 61 single_thread_task_runner_manager_(task_tracker_.get(), |
| 60 &delayed_task_manager_) { | 62 &delayed_task_manager_) { |
| 61 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, | 63 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, |
| 62 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); | 64 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); |
| 63 static_assert( | 65 static_assert( |
| 64 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, | 66 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, |
| 65 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); | 67 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); |
| 66 | 68 |
| 67 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; | 69 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; |
| 68 ++environment_type) { | 70 ++environment_type) { |
| 69 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( | 71 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( |
| 70 name_ + kEnvironmentParams[environment_type].name_suffix, | 72 name_ + kEnvironmentParams[environment_type].name_suffix, |
| 71 kEnvironmentParams[environment_type].priority_hint, &task_tracker_, | 73 kEnvironmentParams[environment_type].priority_hint, task_tracker_.get(), |
| 72 &delayed_task_manager_); | 74 &delayed_task_manager_); |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 | 77 |
| 76 TaskSchedulerImpl::~TaskSchedulerImpl() { | 78 TaskSchedulerImpl::~TaskSchedulerImpl() { |
| 77 #if DCHECK_IS_ON() | 79 #if DCHECK_IS_ON() |
| 78 DCHECK(join_for_testing_returned_.IsSet()); | 80 DCHECK(join_for_testing_returned_.IsSet()); |
| 79 #endif | 81 #endif |
| 80 } | 82 } |
| 81 | 83 |
| 82 void TaskSchedulerImpl::Start(const TaskScheduler::InitParams& init_params) { | 84 void TaskSchedulerImpl::Start(const TaskScheduler::InitParams& init_params) { |
| 83 // Start the service thread. On platforms that support it (POSIX except NaCL | 85 // Start the service thread. On platforms that support it (POSIX except NaCL |
| 84 // SFI), the service thread runs a MessageLoopForIO which is used to support | 86 // SFI), the service thread runs a MessageLoopForIO which is used to support |
| 85 // FileDescriptorWatcher in the scope in which tasks run. | 87 // FileDescriptorWatcher in the scope in which tasks run. |
| 86 Thread::Options service_thread_options; | 88 Thread::Options service_thread_options; |
| 87 service_thread_options.message_loop_type = | 89 service_thread_options.message_loop_type = |
| 88 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 90 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 89 MessageLoop::TYPE_IO; | 91 MessageLoop::TYPE_IO; |
| 90 #else | 92 #else |
| 91 MessageLoop::TYPE_DEFAULT; | 93 MessageLoop::TYPE_DEFAULT; |
| 92 #endif | 94 #endif |
| 93 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; | 95 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; |
| 94 CHECK(service_thread_.StartWithOptions(service_thread_options)); | 96 CHECK(service_thread_.StartWithOptions(service_thread_options)); |
| 95 | 97 |
| 96 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 98 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 97 // Needs to happen after starting the service thread to get its | 99 // Needs to happen after starting the service thread to get its |
| 98 // message_loop(). | 100 // message_loop(). |
| 99 task_tracker_.set_watch_file_descriptor_message_loop( | 101 task_tracker_->set_watch_file_descriptor_message_loop( |
| 100 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); | 102 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); |
| 101 | 103 |
| 102 #if DCHECK_IS_ON() | 104 #if DCHECK_IS_ON() |
| 103 task_tracker_.set_service_thread_handle(service_thread_.GetThreadHandle()); | 105 task_tracker_->set_service_thread_handle(service_thread_.GetThreadHandle()); |
| 104 #endif // DCHECK_IS_ON() | 106 #endif // DCHECK_IS_ON() |
| 105 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) | 107 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 106 | 108 |
| 107 // Needs to happen after starting the service thread to get its task_runner(). | 109 // Needs to happen after starting the service thread to get its task_runner(). |
| 108 delayed_task_manager_.Start(service_thread_.task_runner()); | 110 delayed_task_manager_.Start(service_thread_.task_runner()); |
| 109 | 111 |
| 110 single_thread_task_runner_manager_.Start(); | 112 single_thread_task_runner_manager_.Start(); |
| 111 | 113 |
| 112 worker_pools_[BACKGROUND]->Start(init_params.background_worker_pool_params); | 114 worker_pools_[BACKGROUND]->Start(init_params.background_worker_pool_params); |
| 113 worker_pools_[BACKGROUND_BLOCKING]->Start( | 115 worker_pools_[BACKGROUND_BLOCKING]->Start( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 return histograms; | 174 return histograms; |
| 173 } | 175 } |
| 174 | 176 |
| 175 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( | 177 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( |
| 176 const TaskTraits& traits) const { | 178 const TaskTraits& traits) const { |
| 177 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); | 179 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); |
| 178 } | 180 } |
| 179 | 181 |
| 180 void TaskSchedulerImpl::Shutdown() { | 182 void TaskSchedulerImpl::Shutdown() { |
| 181 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | 183 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. |
| 182 task_tracker_.Shutdown(); | 184 task_tracker_->Shutdown(); |
| 183 } | 185 } |
| 184 | 186 |
| 185 void TaskSchedulerImpl::FlushForTesting() { | 187 void TaskSchedulerImpl::FlushForTesting() { |
| 186 task_tracker_.Flush(); | 188 task_tracker_->Flush(); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void TaskSchedulerImpl::JoinForTesting() { | 191 void TaskSchedulerImpl::JoinForTesting() { |
| 190 #if DCHECK_IS_ON() | 192 #if DCHECK_IS_ON() |
| 191 DCHECK(!join_for_testing_returned_.IsSet()); | 193 DCHECK(!join_for_testing_returned_.IsSet()); |
| 192 #endif | 194 #endif |
| 193 single_thread_task_runner_manager_.JoinForTesting(); | 195 single_thread_task_runner_manager_.JoinForTesting(); |
| 194 for (const auto& worker_pool : worker_pools_) | 196 for (const auto& worker_pool : worker_pools_) |
| 195 worker_pool->DisallowWorkerDetachmentForTesting(); | 197 worker_pool->DisallowWorkerDetachmentForTesting(); |
| 196 for (const auto& worker_pool : worker_pools_) | 198 for (const auto& worker_pool : worker_pools_) |
| 197 worker_pool->JoinForTesting(); | 199 worker_pool->JoinForTesting(); |
| 198 service_thread_.Stop(); | 200 service_thread_.Stop(); |
| 199 #if DCHECK_IS_ON() | 201 #if DCHECK_IS_ON() |
| 200 join_for_testing_returned_.Set(); | 202 join_for_testing_returned_.Set(); |
| 201 #endif | 203 #endif |
| 202 } | 204 } |
| 203 | 205 |
| 204 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( | 206 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( |
| 205 const TaskTraits& traits) const { | 207 const TaskTraits& traits) const { |
| 206 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); | 208 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); |
| 207 } | 209 } |
| 208 | 210 |
| 209 } // namespace internal | 211 } // namespace internal |
| 210 } // namespace base | 212 } // namespace base |
| OLD | NEW |