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 |
| 47 // Offset of the SchedulerWorkerPoolParams corresponding to this environement |
| 48 // in TaskScheduler::InitParams. Enables creation of SchedulerWorkerPools from |
| 49 // TaskScheduler::InitParams and |kEnvironmentParams| in a for loop. |
| 50 size_t offset; |
| 51 } kEnvironmentParams[] = { |
| 52 {"Background", base::ThreadPriority::BACKGROUND, |
| 53 offsetof(TaskScheduler::InitParams, background_worker_pool_params)}, |
| 54 {"BackgroundBlocking", base::ThreadPriority::BACKGROUND, |
| 55 offsetof(TaskScheduler::InitParams, |
| 56 background_blocking_worker_pool_params)}, |
| 57 {"Foreground", base::ThreadPriority::NORMAL, |
| 58 offsetof(TaskScheduler::InitParams, foreground_worker_pool_params)}, |
| 59 {"ForegroundBlocking", base::ThreadPriority::NORMAL, |
| 60 offsetof(TaskScheduler::InitParams, |
| 61 foreground_blocking_worker_pool_params)}, |
| 62 }; |
| 63 |
| 64 size_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { |
| 65 const bool is_background = |
| 66 traits.priority() == base::TaskPriority::BACKGROUND; |
| 67 if (traits.may_block() || traits.with_base_sync_primitives()) |
| 68 return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; |
| 69 return is_background ? BACKGROUND : FOREGROUND; |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
27 // static | 74 // static |
28 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( | 75 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
29 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | 76 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, |
30 const WorkerPoolIndexForTraitsCallback& | 77 const WorkerPoolIndexForTraitsCallback& |
31 worker_pool_index_for_traits_callback) { | 78 worker_pool_index_for_traits_callback) { |
32 std::unique_ptr<TaskSchedulerImpl> scheduler( | 79 std::unique_ptr<TaskSchedulerImpl> scheduler( |
33 new TaskSchedulerImpl(worker_pool_index_for_traits_callback)); | 80 new TaskSchedulerImpl(worker_pool_index_for_traits_callback)); |
34 scheduler->Initialize(worker_pool_params_vector); | 81 scheduler->Initialize(worker_pool_params_vector); |
35 return scheduler; | 82 return scheduler; |
36 } | 83 } |
37 | 84 |
| 85 // static |
| 86 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
| 87 const std::string& name, |
| 88 const TaskScheduler::InitParams& init_params) { |
| 89 // Create a vector of SchedulerWorkerPoolParams using names and priority hints |
| 90 // derived from |kEnvironmentParams| and other params from |init_params|. |
| 91 std::vector<SchedulerWorkerPoolParams> worker_pool_params_vector; |
| 92 for (const auto& environment_params : kEnvironmentParams) { |
| 93 const SchedulerWorkerPoolParams& worker_pool_params_in = |
| 94 *reinterpret_cast<const SchedulerWorkerPoolParams*>( |
| 95 reinterpret_cast<uintptr_t>(&init_params) + |
| 96 environment_params.offset); |
| 97 worker_pool_params_vector.emplace_back( |
| 98 name + environment_params.name_suffix, environment_params.priority_hint, |
| 99 worker_pool_params_in.standby_thread_policy(), |
| 100 worker_pool_params_in.max_threads(), |
| 101 worker_pool_params_in.suggested_reclaim_time(), |
| 102 worker_pool_params_in.backward_compatibility()); |
| 103 } |
| 104 DCHECK_EQ(static_cast<size_t>(ENVIRONMENT_COUNT), |
| 105 worker_pool_params_vector.size()); |
| 106 |
| 107 return Create(worker_pool_params_vector, Bind(&GetEnvironmentIndexForTraits)); |
| 108 } |
| 109 |
38 TaskSchedulerImpl::~TaskSchedulerImpl() { | 110 TaskSchedulerImpl::~TaskSchedulerImpl() { |
39 #if DCHECK_IS_ON() | 111 #if DCHECK_IS_ON() |
40 DCHECK(join_for_testing_returned_.IsSet()); | 112 DCHECK(join_for_testing_returned_.IsSet()); |
41 #endif | 113 #endif |
42 } | 114 } |
43 | 115 |
44 void TaskSchedulerImpl::PostDelayedTaskWithTraits( | 116 void TaskSchedulerImpl::PostDelayedTaskWithTraits( |
45 const tracked_objects::Location& from_here, | 117 const tracked_objects::Location& from_here, |
46 const TaskTraits& traits, | 118 const TaskTraits& traits, |
47 const Closure& task, | 119 const Closure& task, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // in |sequence|. | 263 // in |sequence|. |
192 const TaskTraits traits = | 264 const TaskTraits traits = |
193 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); | 265 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); |
194 | 266 |
195 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | 267 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), |
196 sort_key); | 268 sort_key); |
197 } | 269 } |
198 | 270 |
199 } // namespace internal | 271 } // namespace internal |
200 } // namespace base | 272 } // namespace base |
OLD | NEW |