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" | 18 #include "build/build_config.h" |
19 | 19 |
20 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 20 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
21 #include "base/task_scheduler/task_tracker_posix.h" | 21 #include "base/task_scheduler/task_tracker_posix.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace base { | 24 namespace base { |
25 namespace internal { | 25 namespace internal { |
26 | 26 |
| 27 namespace { |
| 28 |
| 29 enum EnvironmentType { |
| 30 BACKGROUND = 0, |
| 31 BACKGROUND_BLOCKING, |
| 32 FOREGROUND, |
| 33 FOREGROUND_BLOCKING, |
| 34 ENVIRONMENT_COUNT // Always last. |
| 35 }; |
| 36 |
| 37 // Order must match the EnvironmentType enum. |
| 38 constexpr struct { |
| 39 // The threads and histograms of this environment will be labeled with |
| 40 // the task scheduler name concatenated to this. |
| 41 const char* name_suffix; |
| 42 |
| 43 // Preferred priority for threads in this environment; the actual thread |
| 44 // priority depends on shutdown state and platform capabilities. |
| 45 ThreadPriority priority_hint; |
| 46 } kEnvironmentParams[] = { |
| 47 {"Background", base::ThreadPriority::BACKGROUND}, |
| 48 {"BackgroundBlocking", base::ThreadPriority::BACKGROUND}, |
| 49 {"Foreground", base::ThreadPriority::NORMAL}, |
| 50 {"ForegroundBlocking", base::ThreadPriority::NORMAL}, |
| 51 }; |
| 52 |
| 53 size_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { |
| 54 const bool is_background = |
| 55 traits.priority() == base::TaskPriority::BACKGROUND; |
| 56 if (traits.may_block() || traits.with_base_sync_primitives()) |
| 57 return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; |
| 58 return is_background ? BACKGROUND : FOREGROUND; |
| 59 } |
| 60 |
| 61 void AddAugmentedSchedulerWorkerPoolParamsToVector( |
| 62 EnvironmentType environment_type, |
| 63 const std::string& task_scheduler_name, |
| 64 const SchedulerWorkerPoolParams& params, |
| 65 std::vector<SchedulerWorkerPoolParams>* |
| 66 scheduler_worker_pool_params_vector) { |
| 67 DCHECK_EQ(static_cast<size_t>(environment_type), |
| 68 scheduler_worker_pool_params_vector->size()); |
| 69 scheduler_worker_pool_params_vector->emplace_back( |
| 70 task_scheduler_name + kEnvironmentParams[environment_type].name_suffix, |
| 71 kEnvironmentParams[environment_type].priority_hint, |
| 72 params.standby_thread_policy(), params.max_threads(), |
| 73 params.suggested_reclaim_time(), params.backward_compatibility()); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
27 // static | 78 // static |
28 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( | 79 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
29 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | 80 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, |
30 const WorkerPoolIndexForTraitsCallback& | 81 const WorkerPoolIndexForTraitsCallback& |
31 worker_pool_index_for_traits_callback) { | 82 worker_pool_index_for_traits_callback) { |
32 std::unique_ptr<TaskSchedulerImpl> scheduler( | 83 std::unique_ptr<TaskSchedulerImpl> scheduler( |
33 new TaskSchedulerImpl(worker_pool_index_for_traits_callback)); | 84 new TaskSchedulerImpl(worker_pool_index_for_traits_callback)); |
34 scheduler->Initialize(worker_pool_params_vector); | 85 scheduler->Initialize(worker_pool_params_vector); |
35 return scheduler; | 86 return scheduler; |
36 } | 87 } |
37 | 88 |
| 89 // static |
| 90 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
| 91 const std::string& name, |
| 92 const TaskScheduler::InitParams& init_params) { |
| 93 // Create a vector of SchedulerWorkerPoolParams using names and priority hints |
| 94 // derived from |kEnvironmentParams| and other params from |init_params|. |
| 95 std::vector<SchedulerWorkerPoolParams> worker_pool_params_vector; |
| 96 AddAugmentedSchedulerWorkerPoolParamsToVector( |
| 97 BACKGROUND, name, init_params.background_worker_pool_params, |
| 98 &worker_pool_params_vector); |
| 99 AddAugmentedSchedulerWorkerPoolParamsToVector( |
| 100 BACKGROUND_BLOCKING, name, |
| 101 init_params.background_blocking_worker_pool_params, |
| 102 &worker_pool_params_vector); |
| 103 AddAugmentedSchedulerWorkerPoolParamsToVector( |
| 104 FOREGROUND, name, init_params.foreground_worker_pool_params, |
| 105 &worker_pool_params_vector); |
| 106 AddAugmentedSchedulerWorkerPoolParamsToVector( |
| 107 FOREGROUND_BLOCKING, name, |
| 108 init_params.foreground_blocking_worker_pool_params, |
| 109 &worker_pool_params_vector); |
| 110 DCHECK_EQ(static_cast<size_t>(ENVIRONMENT_COUNT), |
| 111 worker_pool_params_vector.size()); |
| 112 |
| 113 return Create(worker_pool_params_vector, Bind(&GetEnvironmentIndexForTraits)); |
| 114 } |
| 115 |
38 TaskSchedulerImpl::~TaskSchedulerImpl() { | 116 TaskSchedulerImpl::~TaskSchedulerImpl() { |
39 #if DCHECK_IS_ON() | 117 #if DCHECK_IS_ON() |
40 DCHECK(join_for_testing_returned_.IsSet()); | 118 DCHECK(join_for_testing_returned_.IsSet()); |
41 #endif | 119 #endif |
42 } | 120 } |
43 | 121 |
44 void TaskSchedulerImpl::PostDelayedTaskWithTraits( | 122 void TaskSchedulerImpl::PostDelayedTaskWithTraits( |
45 const tracked_objects::Location& from_here, | 123 const tracked_objects::Location& from_here, |
46 const TaskTraits& traits, | 124 const TaskTraits& traits, |
47 const Closure& task, | 125 const Closure& task, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // in |sequence|. | 269 // in |sequence|. |
192 const TaskTraits traits = | 270 const TaskTraits traits = |
193 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); | 271 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); |
194 | 272 |
195 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | 273 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), |
196 sort_key); | 274 sort_key); |
197 } | 275 } |
198 | 276 |
199 } // namespace internal | 277 } // namespace internal |
200 } // namespace base | 278 } // namespace base |
OLD | NEW |