OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
| 11 #include "base/cancelable_callback.h" |
11 #include "base/debug/task_annotator.h" | 12 #include "base/debug/task_annotator.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/pending_task.h" | 16 #include "base/pending_task.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
18 #include "platform/scheduler/base/enqueue_order.h" | 19 #include "platform/scheduler/base/enqueue_order.h" |
| 20 #include "platform/scheduler/base/moveable_auto_lock.h" |
19 #include "platform/scheduler/base/task_queue_impl.h" | 21 #include "platform/scheduler/base/task_queue_impl.h" |
20 #include "platform/scheduler/base/task_queue_selector.h" | 22 #include "platform/scheduler/base/task_queue_selector.h" |
21 | 23 |
22 namespace base { | 24 namespace base { |
23 namespace trace_event { | 25 namespace trace_event { |
24 class ConvertableToTraceFormat; | 26 class ConvertableToTraceFormat; |
25 } // namespace trace_event | 27 } // namespace trace_event |
26 } // namespace base | 28 } // namespace base |
27 | 29 |
28 namespace blink { | 30 namespace blink { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 DCHECK(main_thread_checker_.CalledOnValidThread()); | 135 DCHECK(main_thread_checker_.CalledOnValidThread()); |
134 return currently_executing_task_queue_; | 136 return currently_executing_task_queue_; |
135 } | 137 } |
136 | 138 |
137 // Return number of pending tasks in task queues. | 139 // Return number of pending tasks in task queues. |
138 size_t GetNumberOfPendingTasks() const; | 140 size_t GetNumberOfPendingTasks() const; |
139 | 141 |
140 // Returns true if there is a task that could be executed immediately. | 142 // Returns true if there is a task that could be executed immediately. |
141 bool HasImmediateWorkForTesting() const; | 143 bool HasImmediateWorkForTesting() const; |
142 | 144 |
| 145 // There is a small overhead to recording task delay histograms. If you don't |
| 146 // need them, you can turn them off. |
| 147 void SetRecordTaskDelayHistograms(bool record_task_delay_histograms); |
| 148 |
143 private: | 149 private: |
144 friend class LazyNow; | 150 friend class LazyNow; |
145 friend class internal::TaskQueueImpl; | 151 friend class internal::TaskQueueImpl; |
146 friend class TaskQueueManagerTest; | 152 friend class TaskQueueManagerTest; |
147 | 153 |
148 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { | 154 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { |
149 private: | 155 private: |
150 friend class base::RefCounted<DeletionSentinel>; | 156 friend class base::RefCounted<DeletionSentinel>; |
151 ~DeletionSentinel() {} | 157 ~DeletionSentinel() {} |
152 }; | 158 }; |
153 | 159 |
154 // Unregisters a TaskQueue previously created by |NewTaskQueue()|. | 160 // Unregisters a TaskQueue previously created by |NewTaskQueue()|. |
155 // NOTE we have to flush the queue from |newly_updatable_| which means as a | |
156 // side effect MoveNewlyUpdatableQueuesIntoUpdatableQueueSet is called by this | |
157 // function. | |
158 void UnregisterTaskQueue(scoped_refptr<internal::TaskQueueImpl> task_queue); | 161 void UnregisterTaskQueue(scoped_refptr<internal::TaskQueueImpl> task_queue); |
159 | 162 |
160 // TaskQueueSelector::Observer implementation: | 163 // TaskQueueSelector::Observer implementation: |
161 void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) override; | 164 void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) override; |
162 void OnTriedToSelectBlockedWorkQueue( | 165 void OnTriedToSelectBlockedWorkQueue( |
163 internal::WorkQueue* work_queue) override; | 166 internal::WorkQueue* work_queue) override; |
164 | 167 |
165 // base::MessageLoop::NestingObserver implementation: | 168 // base::MessageLoop::NestingObserver implementation: |
166 void OnBeginNestedMessageLoop() override; | 169 void OnBeginNestedMessageLoop() override; |
167 | 170 |
168 // Called by the task queue to register a new pending task. | 171 // Called by the task queue to register a new pending task. |
169 void DidQueueTask(const internal::TaskQueueImpl::Task& pending_task); | 172 void DidQueueTask(const internal::TaskQueueImpl::Task& pending_task); |
170 | 173 |
171 // Use the selector to choose a pending task and run it. | 174 // Use the selector to choose a pending task and run it. |
172 void DoWork(base::TimeTicks run_time, bool from_main_thread); | 175 void DoWork(bool delayed); |
| 176 |
| 177 // Post a DoWork continuation if |next_delay| is not empty. |
| 178 void PostDoWorkContinuationLocked(base::Optional<base::TimeDelta> next_delay, |
| 179 LazyNow* lazy_now, |
| 180 MoveableAutoLock&& lock); |
173 | 181 |
174 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue and | 182 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue and |
175 // reloads any empty work queues. | 183 // reloads any empty work queues. |
176 void UpdateWorkQueues(LazyNow lazy_now); | 184 void UpdateWorkQueues(LazyNow* lazy_now); |
177 | 185 |
178 // Chooses the next work queue to service. Returns true if |out_queue| | 186 // Chooses the next work queue to service. Returns true if |out_queue| |
179 // indicates the queue from which the next task should be run, false to | 187 // indicates the queue from which the next task should be run, false to |
180 // avoid running any tasks. | 188 // avoid running any tasks. |
181 bool SelectWorkQueueToService(internal::WorkQueue** out_work_queue); | 189 bool SelectWorkQueueToService(internal::WorkQueue** out_work_queue); |
182 | 190 |
183 // Runs a single nestable task from the |queue|. On exit, |out_task| will | 191 // Runs a single nestable task from the |queue|. On exit, |out_task| will |
184 // contain the task which was executed. Non-nestable task are reposted on the | 192 // contain the task which was executed. Non-nestable task are reposted on the |
185 // run loop. The queue must not be empty. | 193 // run loop. The queue must not be empty. |
186 enum class ProcessTaskResult { | 194 enum class ProcessTaskResult { |
187 DEFERRED, | 195 DEFERRED, |
188 EXECUTED, | 196 EXECUTED, |
189 TASK_QUEUE_MANAGER_DELETED | 197 TASK_QUEUE_MANAGER_DELETED |
190 }; | 198 }; |
191 ProcessTaskResult ProcessTaskFromWorkQueue(internal::WorkQueue* work_queue, | 199 ProcessTaskResult ProcessTaskFromWorkQueue(internal::WorkQueue* work_queue, |
192 LazyNow*); | 200 bool is_nested, |
| 201 LazyNow* lazy_now); |
193 | 202 |
194 bool RunsTasksOnCurrentThread() const; | 203 bool RunsTasksOnCurrentThread() const; |
195 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 204 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
196 const base::Closure& task, | 205 const base::Closure& task, |
197 base::TimeDelta delay); | 206 base::TimeDelta delay); |
198 | 207 |
199 internal::EnqueueOrder GetNextSequenceNumber(); | 208 internal::EnqueueOrder GetNextSequenceNumber(); |
200 | 209 |
201 // Calls MaybeAdvanceTime on all time domains and returns true if one of them | 210 // Calls DelayTillNextTask on all time domains and returns the smallest delay |
202 // was able to advance. | 211 // requested if any. |
203 bool TryAdvanceTimeDomains(); | 212 base::Optional<base::TimeDelta> ComputeDelayTillNextTaskLocked( |
| 213 LazyNow* lazy_now); |
204 | 214 |
205 void MaybeRecordTaskDelayHistograms( | 215 void MaybeRecordTaskDelayHistograms( |
206 const internal::TaskQueueImpl::Task& pending_task, | 216 const internal::TaskQueueImpl::Task& pending_task, |
207 const internal::TaskQueueImpl* queue); | 217 const internal::TaskQueueImpl* queue); |
208 | 218 |
209 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> | 219 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> |
210 AsValueWithSelectorResult(bool should_run, | 220 AsValueWithSelectorResult(bool should_run, |
211 internal::WorkQueue* selected_work_queue) const; | 221 internal::WorkQueue* selected_work_queue) const; |
212 | 222 |
| 223 void MaybeScheduleImmediateWorkLocked( |
| 224 const tracked_objects::Location& from_here, |
| 225 MoveableAutoLock&& lock); |
| 226 |
| 227 // Adds |queue| to |has_incoming_immediate_work_| and if |
| 228 // |ensure_do_work_posted| is true it calls MaybeScheduleImmediateWorkLocked. |
| 229 void OnQueueHasImmediateWork(internal::TaskQueueImpl* queue, |
| 230 bool ensure_do_work_posted); |
| 231 |
| 232 void NotifyQueuesOfIncomingImmediateWorkOnMainThreadLocked(); |
| 233 |
213 std::set<TimeDomain*> time_domains_; | 234 std::set<TimeDomain*> time_domains_; |
214 std::unique_ptr<RealTimeDomain> real_time_domain_; | 235 std::unique_ptr<RealTimeDomain> real_time_domain_; |
215 | 236 |
216 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_; | 237 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_; |
217 | 238 |
218 // We have to be careful when deleting a queue because some of the code uses | 239 // We have to be careful when deleting a queue because some of the code uses |
219 // raw pointers and doesn't expect the rug to be pulled out from underneath. | 240 // raw pointers and doesn't expect the rug to be pulled out from underneath. |
220 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_to_delete_; | 241 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_to_delete_; |
221 | 242 |
222 internal::EnqueueOrderGenerator enqueue_order_generator_; | 243 internal::EnqueueOrderGenerator enqueue_order_generator_; |
223 base::debug::TaskAnnotator task_annotator_; | 244 base::debug::TaskAnnotator task_annotator_; |
224 | 245 |
225 base::ThreadChecker main_thread_checker_; | 246 base::ThreadChecker main_thread_checker_; |
226 scoped_refptr<TaskQueueManagerDelegate> delegate_; | 247 scoped_refptr<TaskQueueManagerDelegate> delegate_; |
227 internal::TaskQueueSelector selector_; | 248 internal::TaskQueueSelector selector_; |
228 | 249 |
229 base::Closure from_main_thread_immediate_do_work_closure_; | 250 base::Closure immediate_do_work_closure_; |
230 base::Closure from_other_thread_immediate_do_work_closure_; | 251 base::Closure delayed_do_work_closure_; |
231 | 252 |
232 bool task_was_run_on_quiescence_monitored_queue_; | 253 bool task_was_run_on_quiescence_monitored_queue_; |
233 | 254 |
234 // To reduce locking overhead we track pending calls to DoWork separately for | 255 struct AnyThread { |
235 // the main thread and other threads. | 256 AnyThread(); |
236 std::set<base::TimeTicks> main_thread_pending_wakeups_; | |
237 | 257 |
238 // Protects |other_thread_pending_wakeup_|. | 258 int do_work_running_count; |
239 mutable base::Lock other_thread_lock_; | 259 int immediate_do_work_posted_count; |
240 bool other_thread_pending_wakeup_; | 260 std::set<internal::TaskQueueImpl*> has_incoming_immediate_work; |
| 261 bool is_nested; // Whether or not the message loop is currently nested. |
| 262 }; |
| 263 |
| 264 mutable base::Lock any_thread_lock_; |
| 265 AnyThread any_thread_; |
| 266 struct AnyThread& any_thread() { |
| 267 any_thread_lock_.AssertAcquired(); |
| 268 return any_thread_; |
| 269 } |
| 270 const struct AnyThread& any_thread() const { |
| 271 any_thread_lock_.AssertAcquired(); |
| 272 return any_thread_; |
| 273 } |
| 274 |
| 275 bool record_task_delay_histograms_; |
| 276 |
| 277 base::TimeTicks next_delayed_do_work_; |
| 278 base::CancelableClosure cancelable_delayed_do_work_closure_; |
241 | 279 |
242 int work_batch_size_; | 280 int work_batch_size_; |
243 size_t task_count_; | 281 size_t task_count_; |
244 | 282 |
245 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; | 283 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; |
246 | 284 |
247 base::ObserverList<TaskTimeObserver> task_time_observers_; | 285 base::ObserverList<TaskTimeObserver> task_time_observers_; |
248 | 286 |
249 const char* tracing_category_; | 287 const char* tracing_category_; |
250 const char* disabled_by_default_tracing_category_; | 288 const char* disabled_by_default_tracing_category_; |
251 const char* disabled_by_default_verbose_tracing_category_; | 289 const char* disabled_by_default_verbose_tracing_category_; |
252 | 290 |
253 internal::TaskQueueImpl* currently_executing_task_queue_; // NOT OWNED | 291 internal::TaskQueueImpl* currently_executing_task_queue_; // NOT OWNED |
254 | 292 |
255 Observer* observer_; // NOT OWNED | 293 Observer* observer_; // NOT OWNED |
256 scoped_refptr<DeletionSentinel> deletion_sentinel_; | 294 scoped_refptr<DeletionSentinel> deletion_sentinel_; |
257 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | 295 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
258 | 296 |
259 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | 297 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
260 }; | 298 }; |
261 | 299 |
262 } // namespace scheduler | 300 } // namespace scheduler |
263 } // namespace blink | 301 } // namespace blink |
264 | 302 |
265 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_
H_ | 303 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_MANAGER_
H_ |
OLD | NEW |