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