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" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
12 #include "base/task_scheduler/delayed_task_manager.h" | 10 #include "base/task_scheduler/delayed_task_manager.h" |
13 #include "base/task_scheduler/scheduler_single_thread_task_runner_manager.h" | 11 #include "base/task_scheduler/scheduler_single_thread_task_runner_manager.h" |
14 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 12 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
15 #include "base/task_scheduler/sequence_sort_key.h" | 13 #include "base/task_scheduler/sequence_sort_key.h" |
16 #include "base/task_scheduler/task.h" | 14 #include "base/task_scheduler/task.h" |
17 #include "base/task_scheduler/task_tracker.h" | 15 #include "base/task_scheduler/task_tracker.h" |
18 #include "build/build_config.h" | 16 #include "build/build_config.h" |
19 | 17 |
20 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 18 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // Instantiate DelayedTaskManager. Needs to happen after starting the service | 187 // Instantiate DelayedTaskManager. Needs to happen after starting the service |
190 // thread to get its task_runner(). | 188 // thread to get its task_runner(). |
191 delayed_task_manager_ = | 189 delayed_task_manager_ = |
192 base::MakeUnique<DelayedTaskManager>(service_thread_.task_runner()); | 190 base::MakeUnique<DelayedTaskManager>(service_thread_.task_runner()); |
193 | 191 |
194 single_thread_task_runner_manager_ = | 192 single_thread_task_runner_manager_ = |
195 MakeUnique<SchedulerSingleThreadTaskRunnerManager>( | 193 MakeUnique<SchedulerSingleThreadTaskRunnerManager>( |
196 task_tracker_.get(), delayed_task_manager_.get()); | 194 task_tracker_.get(), delayed_task_manager_.get()); |
197 single_thread_task_runner_manager_->Start(); | 195 single_thread_task_runner_manager_->Start(); |
198 | 196 |
199 // Callback invoked by workers to re-enqueue a sequence in the appropriate | |
200 // PriorityQueue. | |
201 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback | |
202 re_enqueue_sequence_callback = | |
203 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); | |
204 | |
205 // Order must match the EnvironmentType enum. | 197 // Order must match the EnvironmentType enum. |
206 const SchedulerWorkerPoolParams* worker_pool_params[] = { | 198 const SchedulerWorkerPoolParams* worker_pool_params[] = { |
207 &init_params.background_worker_pool_params, | 199 &init_params.background_worker_pool_params, |
208 &init_params.background_blocking_worker_pool_params, | 200 &init_params.background_blocking_worker_pool_params, |
209 &init_params.foreground_worker_pool_params, | 201 &init_params.foreground_worker_pool_params, |
210 &init_params.foreground_blocking_worker_pool_params}; | 202 &init_params.foreground_blocking_worker_pool_params}; |
211 | 203 |
212 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, | 204 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, |
213 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); | 205 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); |
214 static_assert( | 206 static_assert( |
215 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, | 207 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, |
216 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); | 208 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); |
217 static_assert( | 209 static_assert( |
218 arraysize(worker_pool_params) == ENVIRONMENT_COUNT, | 210 arraysize(worker_pool_params) == ENVIRONMENT_COUNT, |
219 "The size of |worker_pool_params| must match ENVIRONMENT_COUNT."); | 211 "The size of |worker_pool_params| must match ENVIRONMENT_COUNT."); |
220 | 212 |
221 // Start worker pools. | 213 // Start worker pools. |
222 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; | 214 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; |
223 ++environment_type) { | 215 ++environment_type) { |
224 // Passing pointers to objects owned by |this| to the constructor of | 216 // Passing pointers to objects owned by |this| to the constructor of |
225 // SchedulerWorkerPoolImpl is safe because a TaskSchedulerImpl can't be | 217 // SchedulerWorkerPoolImpl is safe because a TaskSchedulerImpl can't be |
226 // deleted before all its worker pools have been joined. | 218 // deleted before all its worker pools have been joined. |
227 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( | 219 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( |
228 name_ + kEnvironmentParams[environment_type].name_suffix, | 220 name_ + kEnvironmentParams[environment_type].name_suffix, |
229 kEnvironmentParams[environment_type].priority_hint, | 221 kEnvironmentParams[environment_type].priority_hint, task_tracker_.get(), |
230 re_enqueue_sequence_callback, task_tracker_.get(), | |
231 delayed_task_manager_.get()); | 222 delayed_task_manager_.get()); |
232 worker_pools_[environment_type]->Start( | 223 worker_pools_[environment_type]->Start( |
233 *worker_pool_params[environment_type]); | 224 *worker_pool_params[environment_type]); |
234 } | 225 } |
235 } | 226 } |
236 | 227 |
237 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( | 228 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( |
238 const TaskTraits& traits) const { | 229 const TaskTraits& traits) const { |
239 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); | 230 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); |
240 } | 231 } |
241 | 232 |
242 void TaskSchedulerImpl::ReEnqueueSequenceCallback( | |
243 scoped_refptr<Sequence> sequence) { | |
244 DCHECK(sequence); | |
245 | |
246 const SequenceSortKey sort_key = sequence->GetSortKey(); | |
247 | |
248 // The next task in |sequence| should run in a worker pool suited for its | |
249 // traits, except for the priority which is adjusted to the highest priority | |
250 // in |sequence|. | |
251 const TaskTraits traits = | |
252 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); | |
253 | |
254 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | |
255 sort_key); | |
256 } | |
257 | |
258 } // namespace internal | 233 } // namespace internal |
259 } // namespace base | 234 } // namespace base |
OLD | NEW |