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/scheduler_service_thread.h" | 12 #include "base/task_scheduler/scheduler_service_thread.h" |
13 #include "base/task_scheduler/scheduler_thread_pool_impl.h" | 13 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
14 #include "base/task_scheduler/sequence_sort_key.h" | 14 #include "base/task_scheduler/sequence_sort_key.h" |
15 #include "base/task_scheduler/task.h" | 15 #include "base/task_scheduler/task.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 namespace internal { | 19 namespace internal { |
20 | 20 |
21 // static | 21 // static |
22 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create() { | 22 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create() { |
23 std::unique_ptr<TaskSchedulerImpl> scheduler(new TaskSchedulerImpl); | 23 std::unique_ptr<TaskSchedulerImpl> scheduler(new TaskSchedulerImpl); |
24 scheduler->Initialize(); | 24 scheduler->Initialize(); |
25 return scheduler; | 25 return scheduler; |
26 } | 26 } |
27 | 27 |
28 TaskSchedulerImpl::~TaskSchedulerImpl() { | 28 TaskSchedulerImpl::~TaskSchedulerImpl() { |
29 #if DCHECK_IS_ON() | 29 #if DCHECK_IS_ON() |
30 DCHECK(join_for_testing_returned_.IsSignaled()); | 30 DCHECK(join_for_testing_returned_.IsSignaled()); |
31 #endif | 31 #endif |
32 } | 32 } |
33 | 33 |
34 void TaskSchedulerImpl::PostTaskWithTraits( | 34 void TaskSchedulerImpl::PostTaskWithTraits( |
35 const tracked_objects::Location& from_here, | 35 const tracked_objects::Location& from_here, |
36 const TaskTraits& traits, | 36 const TaskTraits& traits, |
37 const Closure& task) { | 37 const Closure& task) { |
38 // Post |task| as part of a one-off single-task Sequence. | 38 // Post |task| as part of a one-off single-task Sequence. |
39 GetThreadPoolForTraits(traits)->PostTaskWithSequence( | 39 GetWorkerPoolForTraits(traits)->PostTaskWithSequence( |
40 WrapUnique(new Task(from_here, task, traits, TimeDelta())), | 40 WrapUnique(new Task(from_here, task, traits, TimeDelta())), |
41 make_scoped_refptr(new Sequence), nullptr); | 41 make_scoped_refptr(new Sequence), nullptr); |
42 } | 42 } |
43 | 43 |
44 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits( | 44 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits( |
45 const TaskTraits& traits, | 45 const TaskTraits& traits, |
46 ExecutionMode execution_mode) { | 46 ExecutionMode execution_mode) { |
47 return GetThreadPoolForTraits(traits)->CreateTaskRunnerWithTraits( | 47 return GetWorkerPoolForTraits(traits)->CreateTaskRunnerWithTraits( |
48 traits, execution_mode); | 48 traits, execution_mode); |
49 } | 49 } |
50 | 50 |
51 void TaskSchedulerImpl::Shutdown() { | 51 void TaskSchedulerImpl::Shutdown() { |
52 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. | 52 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. |
53 task_tracker_.Shutdown(); | 53 task_tracker_.Shutdown(); |
54 } | 54 } |
55 | 55 |
56 void TaskSchedulerImpl::JoinForTesting() { | 56 void TaskSchedulerImpl::JoinForTesting() { |
57 #if DCHECK_IS_ON() | 57 #if DCHECK_IS_ON() |
58 DCHECK(!join_for_testing_returned_.IsSignaled()); | 58 DCHECK(!join_for_testing_returned_.IsSignaled()); |
59 #endif | 59 #endif |
60 background_thread_pool_->JoinForTesting(); | 60 background_worker_pool_->JoinForTesting(); |
61 background_file_io_thread_pool_->JoinForTesting(); | 61 background_file_io_worker_pool_->JoinForTesting(); |
62 normal_thread_pool_->JoinForTesting(); | 62 normal_worker_pool_->JoinForTesting(); |
63 normal_file_io_thread_pool_->JoinForTesting(); | 63 normal_file_io_worker_pool_->JoinForTesting(); |
64 service_thread_->JoinForTesting(); | 64 service_thread_->JoinForTesting(); |
65 #if DCHECK_IS_ON() | 65 #if DCHECK_IS_ON() |
66 join_for_testing_returned_.Signal(); | 66 join_for_testing_returned_.Signal(); |
67 #endif | 67 #endif |
68 } | 68 } |
69 | 69 |
70 TaskSchedulerImpl::TaskSchedulerImpl() | 70 TaskSchedulerImpl::TaskSchedulerImpl() |
71 : delayed_task_manager_( | 71 : delayed_task_manager_( |
72 Bind(&TaskSchedulerImpl::OnDelayedRunTimeUpdated, Unretained(this))) | 72 Bind(&TaskSchedulerImpl::OnDelayedRunTimeUpdated, Unretained(this))) |
73 #if DCHECK_IS_ON() | 73 #if DCHECK_IS_ON() |
74 , | 74 , |
75 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, | 75 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, |
76 WaitableEvent::InitialState::NOT_SIGNALED) | 76 WaitableEvent::InitialState::NOT_SIGNALED) |
77 #endif | 77 #endif |
78 { | 78 { |
79 } | 79 } |
80 | 80 |
81 void TaskSchedulerImpl::Initialize() { | 81 void TaskSchedulerImpl::Initialize() { |
82 using IORestriction = SchedulerThreadPoolImpl::IORestriction; | 82 using IORestriction = SchedulerWorkerPoolImpl::IORestriction; |
83 | 83 |
84 const SchedulerThreadPoolImpl::ReEnqueueSequenceCallback | 84 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback |
85 re_enqueue_sequence_callback = | 85 re_enqueue_sequence_callback = |
86 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); | 86 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); |
87 | 87 |
88 // TODO(fdoray): Derive the number of threads per pool from hardware | 88 // TODO(fdoray): Derive the number of threads per pool from hardware |
89 // characteristics rather than using hard-coded constants. | 89 // characteristics rather than using hard-coded constants. |
90 | 90 |
91 // Passing pointers to objects owned by |this| to | 91 // Passing pointers to objects owned by |this| to |
92 // SchedulerThreadPoolImpl::Create() is safe because a TaskSchedulerImpl can't | 92 // SchedulerWorkerPoolImpl::Create() is safe because a TaskSchedulerImpl can't |
93 // be deleted before all its thread pools have been joined. | 93 // be deleted before all its worker pools have been joined. |
94 background_thread_pool_ = SchedulerThreadPoolImpl::Create( | 94 background_worker_pool_ = SchedulerWorkerPoolImpl::Create( |
95 "TaskSchedulerBackground", ThreadPriority::BACKGROUND, 1U, | 95 "TaskSchedulerBackground", ThreadPriority::BACKGROUND, 1U, |
96 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_, | 96 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_, |
97 &delayed_task_manager_); | 97 &delayed_task_manager_); |
98 CHECK(background_thread_pool_); | 98 CHECK(background_worker_pool_); |
99 | 99 |
100 background_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create( | 100 background_file_io_worker_pool_ = SchedulerWorkerPoolImpl::Create( |
101 "TaskSchedulerBackgroundFileIO", ThreadPriority::BACKGROUND, 1U, | 101 "TaskSchedulerBackgroundFileIO", ThreadPriority::BACKGROUND, 1U, |
102 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_, | 102 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_, |
103 &delayed_task_manager_); | 103 &delayed_task_manager_); |
104 CHECK(background_file_io_thread_pool_); | 104 CHECK(background_file_io_worker_pool_); |
105 | 105 |
106 normal_thread_pool_ = SchedulerThreadPoolImpl::Create( | 106 normal_worker_pool_ = SchedulerWorkerPoolImpl::Create( |
107 "TaskSchedulerForeground", ThreadPriority::NORMAL, 4U, | 107 "TaskSchedulerForeground", ThreadPriority::NORMAL, 4U, |
108 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_, | 108 IORestriction::DISALLOWED, re_enqueue_sequence_callback, &task_tracker_, |
109 &delayed_task_manager_); | 109 &delayed_task_manager_); |
110 CHECK(normal_thread_pool_); | 110 CHECK(normal_worker_pool_); |
111 | 111 |
112 normal_file_io_thread_pool_ = SchedulerThreadPoolImpl::Create( | 112 normal_file_io_worker_pool_ = SchedulerWorkerPoolImpl::Create( |
113 "TaskSchedulerForegroundFileIO", ThreadPriority::NORMAL, 12U, | 113 "TaskSchedulerForegroundFileIO", ThreadPriority::NORMAL, 12U, |
114 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_, | 114 IORestriction::ALLOWED, re_enqueue_sequence_callback, &task_tracker_, |
115 &delayed_task_manager_); | 115 &delayed_task_manager_); |
116 CHECK(normal_file_io_thread_pool_); | 116 CHECK(normal_file_io_worker_pool_); |
117 | 117 |
118 service_thread_ = SchedulerServiceThread::Create(&task_tracker_, | 118 service_thread_ = SchedulerServiceThread::Create(&task_tracker_, |
119 &delayed_task_manager_); | 119 &delayed_task_manager_); |
120 CHECK(service_thread_); | 120 CHECK(service_thread_); |
121 } | 121 } |
122 | 122 |
123 SchedulerThreadPool* TaskSchedulerImpl::GetThreadPoolForTraits( | 123 SchedulerWorkerPool* TaskSchedulerImpl::GetWorkerPoolForTraits( |
124 const TaskTraits& traits) { | 124 const TaskTraits& traits) { |
125 if (traits.with_file_io()) { | 125 if (traits.with_file_io()) { |
126 if (traits.priority() == TaskPriority::BACKGROUND) | 126 if (traits.priority() == TaskPriority::BACKGROUND) |
127 return background_file_io_thread_pool_.get(); | 127 return background_file_io_worker_pool_.get(); |
128 return normal_file_io_thread_pool_.get(); | 128 return normal_file_io_worker_pool_.get(); |
129 } | 129 } |
130 | 130 |
131 if (traits.priority() == TaskPriority::BACKGROUND) | 131 if (traits.priority() == TaskPriority::BACKGROUND) |
132 return background_thread_pool_.get(); | 132 return background_worker_pool_.get(); |
133 return normal_thread_pool_.get(); | 133 return normal_worker_pool_.get(); |
134 } | 134 } |
135 | 135 |
136 void TaskSchedulerImpl::ReEnqueueSequenceCallback( | 136 void TaskSchedulerImpl::ReEnqueueSequenceCallback( |
137 scoped_refptr<Sequence> sequence) { | 137 scoped_refptr<Sequence> sequence) { |
138 DCHECK(sequence); | 138 DCHECK(sequence); |
139 | 139 |
140 const SequenceSortKey sort_key = sequence->GetSortKey(); | 140 const SequenceSortKey sort_key = sequence->GetSortKey(); |
141 TaskTraits traits(sequence->PeekTask()->traits); | 141 TaskTraits traits(sequence->PeekTask()->traits); |
142 | 142 |
143 // Update the priority of |traits| so that the next task in |sequence| runs | 143 // Update the priority of |traits| so that the next task in |sequence| runs |
144 // with the highest priority in |sequence| as opposed to the next task's | 144 // with the highest priority in |sequence| as opposed to the next task's |
145 // specific priority. | 145 // specific priority. |
146 traits.WithPriority(sort_key.priority()); | 146 traits.WithPriority(sort_key.priority()); |
147 | 147 |
148 GetThreadPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), | 148 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), |
149 sort_key); | 149 sort_key); |
150 } | 150 } |
151 | 151 |
152 void TaskSchedulerImpl::OnDelayedRunTimeUpdated() { | 152 void TaskSchedulerImpl::OnDelayedRunTimeUpdated() { |
153 service_thread_->WakeUp(); | 153 service_thread_->WakeUp(); |
154 } | 154 } |
155 | 155 |
156 } // namespace internal | 156 } // namespace internal |
157 } // namespace base | 157 } // namespace base |
OLD | NEW |