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