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" | |
10 #include "base/task_scheduler/delayed_task_manager.h" | 9 #include "base/task_scheduler/delayed_task_manager.h" |
11 #include "base/task_scheduler/environment_config.h" | 10 #include "base/task_scheduler/environment_config.h" |
12 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 11 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
13 #include "base/task_scheduler/sequence.h" | 12 #include "base/task_scheduler/sequence.h" |
14 #include "base/task_scheduler/sequence_sort_key.h" | 13 #include "base/task_scheduler/sequence_sort_key.h" |
15 #include "base/task_scheduler/task.h" | 14 #include "base/task_scheduler/task.h" |
16 #include "base/task_scheduler/task_tracker.h" | 15 #include "base/task_scheduler/task_tracker.h" |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 TaskSchedulerImpl::TaskSchedulerImpl(StringPiece name) | 20 TaskSchedulerImpl::TaskSchedulerImpl( |
| 21 StringPiece name, |
| 22 std::unique_ptr<TaskTrackerImpl> task_tracker) |
22 : name_(name), | 23 : name_(name), |
23 service_thread_("TaskSchedulerServiceThread"), | 24 service_thread_("TaskSchedulerServiceThread"), |
24 single_thread_task_runner_manager_(&task_tracker_, | 25 task_tracker_(std::move(task_tracker)), |
| 26 single_thread_task_runner_manager_(task_tracker_.get(), |
25 &delayed_task_manager_) { | 27 &delayed_task_manager_) { |
26 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, | 28 static_assert(arraysize(worker_pools_) == ENVIRONMENT_COUNT, |
27 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); | 29 "The size of |worker_pools_| must match ENVIRONMENT_COUNT."); |
28 static_assert( | 30 static_assert( |
29 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, | 31 arraysize(kEnvironmentParams) == ENVIRONMENT_COUNT, |
30 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); | 32 "The size of |kEnvironmentParams| must match ENVIRONMENT_COUNT."); |
31 | 33 |
32 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; | 34 for (int environment_type = 0; environment_type < ENVIRONMENT_COUNT; |
33 ++environment_type) { | 35 ++environment_type) { |
34 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( | 36 worker_pools_[environment_type] = MakeUnique<SchedulerWorkerPoolImpl>( |
35 name_ + kEnvironmentParams[environment_type].name_suffix, | 37 name_ + kEnvironmentParams[environment_type].name_suffix, |
36 kEnvironmentParams[environment_type].priority_hint, &task_tracker_, | 38 kEnvironmentParams[environment_type].priority_hint, task_tracker_.get(), |
37 &delayed_task_manager_); | 39 &delayed_task_manager_); |
38 } | 40 } |
39 } | 41 } |
40 | 42 |
41 TaskSchedulerImpl::~TaskSchedulerImpl() { | 43 TaskSchedulerImpl::~TaskSchedulerImpl() { |
42 #if DCHECK_IS_ON() | 44 #if DCHECK_IS_ON() |
43 DCHECK(join_for_testing_returned_.IsSet()); | 45 DCHECK(join_for_testing_returned_.IsSet()); |
44 #endif | 46 #endif |
45 } | 47 } |
46 | 48 |
47 void TaskSchedulerImpl::Start(const TaskScheduler::InitParams& init_params) { | 49 void TaskSchedulerImpl::Start(const TaskScheduler::InitParams& init_params) { |
48 // Start the service thread. On platforms that support it (POSIX except NaCL | 50 // Start the service thread. On platforms that support it (POSIX except NaCL |
49 // SFI), the service thread runs a MessageLoopForIO which is used to support | 51 // SFI), the service thread runs a MessageLoopForIO which is used to support |
50 // FileDescriptorWatcher in the scope in which tasks run. | 52 // FileDescriptorWatcher in the scope in which tasks run. |
51 Thread::Options service_thread_options; | 53 Thread::Options service_thread_options; |
52 service_thread_options.message_loop_type = | 54 service_thread_options.message_loop_type = |
53 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 55 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
54 MessageLoop::TYPE_IO; | 56 MessageLoop::TYPE_IO; |
55 #else | 57 #else |
56 MessageLoop::TYPE_DEFAULT; | 58 MessageLoop::TYPE_DEFAULT; |
57 #endif | 59 #endif |
58 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; | 60 service_thread_options.timer_slack = TIMER_SLACK_MAXIMUM; |
59 CHECK(service_thread_.StartWithOptions(service_thread_options)); | 61 CHECK(service_thread_.StartWithOptions(service_thread_options)); |
60 | 62 |
61 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) | 63 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
62 // Needs to happen after starting the service thread to get its | 64 // Needs to happen after starting the service thread to get its |
63 // message_loop(). | 65 // message_loop(). |
64 task_tracker_.set_watch_file_descriptor_message_loop( | 66 task_tracker_->set_watch_file_descriptor_message_loop( |
65 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); | 67 static_cast<MessageLoopForIO*>(service_thread_.message_loop())); |
66 | 68 |
67 #if DCHECK_IS_ON() | 69 #if DCHECK_IS_ON() |
68 task_tracker_.set_service_thread_handle(service_thread_.GetThreadHandle()); | 70 task_tracker_->set_service_thread_handle(service_thread_.GetThreadHandle()); |
69 #endif // DCHECK_IS_ON() | 71 #endif // DCHECK_IS_ON() |
70 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) | 72 #endif // defined(OS_POSIX) && !defined(OS_NACL_SFI) |
71 | 73 |
72 // Needs to happen after starting the service thread to get its task_runner(). | 74 // Needs to happen after starting the service thread to get its task_runner(). |
73 delayed_task_manager_.Start(service_thread_.task_runner()); | 75 delayed_task_manager_.Start(service_thread_.task_runner()); |
74 | 76 |
75 single_thread_task_runner_manager_.Start(); | 77 single_thread_task_runner_manager_.Start(); |
76 | 78 |
77 worker_pools_[BACKGROUND]->Start(init_params.background_worker_pool_params); | 79 worker_pools_[BACKGROUND]->Start(init_params.background_worker_pool_params); |
78 worker_pools_[BACKGROUND_BLOCKING]->Start( | 80 worker_pools_[BACKGROUND_BLOCKING]->Start( |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 return histograms; | 139 return histograms; |
138 } | 140 } |
139 | 141 |
140 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( | 142 int TaskSchedulerImpl::GetMaxConcurrentTasksWithTraitsDeprecated( |
141 const TaskTraits& traits) const { | 143 const TaskTraits& traits) const { |
142 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); | 144 return GetWorkerPoolForTraits(traits)->GetMaxConcurrentTasksDeprecated(); |
143 } | 145 } |
144 | 146 |
145 void TaskSchedulerImpl::Shutdown() { | 147 void TaskSchedulerImpl::Shutdown() { |
146 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | 148 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. |
147 task_tracker_.Shutdown(); | 149 task_tracker_->Shutdown(); |
148 } | 150 } |
149 | 151 |
150 void TaskSchedulerImpl::FlushForTesting() { | 152 void TaskSchedulerImpl::FlushForTesting() { |
151 task_tracker_.Flush(); | 153 task_tracker_->Flush(); |
152 } | 154 } |
153 | 155 |
154 void TaskSchedulerImpl::JoinForTesting() { | 156 void TaskSchedulerImpl::JoinForTesting() { |
155 #if DCHECK_IS_ON() | 157 #if DCHECK_IS_ON() |
156 DCHECK(!join_for_testing_returned_.IsSet()); | 158 DCHECK(!join_for_testing_returned_.IsSet()); |
157 #endif | 159 #endif |
158 single_thread_task_runner_manager_.JoinForTesting(); | 160 single_thread_task_runner_manager_.JoinForTesting(); |
159 for (const auto& worker_pool : worker_pools_) | 161 for (const auto& worker_pool : worker_pools_) |
160 worker_pool->DisallowWorkerDetachmentForTesting(); | 162 worker_pool->DisallowWorkerDetachmentForTesting(); |
161 for (const auto& worker_pool : worker_pools_) | 163 for (const auto& worker_pool : worker_pools_) |
162 worker_pool->JoinForTesting(); | 164 worker_pool->JoinForTesting(); |
163 service_thread_.Stop(); | 165 service_thread_.Stop(); |
164 #if DCHECK_IS_ON() | 166 #if DCHECK_IS_ON() |
165 join_for_testing_returned_.Set(); | 167 join_for_testing_returned_.Set(); |
166 #endif | 168 #endif |
167 } | 169 } |
168 | 170 |
169 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( | 171 SchedulerWorkerPoolImpl* TaskSchedulerImpl::GetWorkerPoolForTraits( |
170 const TaskTraits& traits) const { | 172 const TaskTraits& traits) const { |
171 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); | 173 return worker_pools_[GetEnvironmentIndexForTraits(traits)].get(); |
172 } | 174 } |
173 | 175 |
174 } // namespace internal | 176 } // namespace internal |
175 } // namespace base | 177 } // namespace base |
OLD | NEW |