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 CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ |
6 #define CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | 6 #define CONTENT_RENDERER_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" |
(...skipping 16 matching lines...) Expand all Loading... |
27 class TracedValue; | 27 class TracedValue; |
28 } // namespace trace_event | 28 } // namespace trace_event |
29 } // namespace base | 29 } // namespace base |
30 | 30 |
31 namespace scheduler { | 31 namespace scheduler { |
32 namespace internal { | 32 namespace internal { |
33 class LazyNow; | 33 class LazyNow; |
34 class TaskQueueImpl; | 34 class TaskQueueImpl; |
35 } // namespace internal | 35 } // namespace internal |
36 | 36 |
37 class NestableSingleThreadTaskRunner; | 37 class TaskQueueManagerDelegate; |
38 | 38 |
39 // The task queue manager provides N task queues and a selector interface for | 39 // The task queue manager provides N task queues and a selector interface for |
40 // choosing which task queue to service next. Each task queue consists of two | 40 // choosing which task queue to service next. Each task queue consists of two |
41 // sub queues: | 41 // sub queues: |
42 // | 42 // |
43 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | 43 // 1. Incoming task queue. Tasks that are posted get immediately appended here. |
44 // When a task is appended into an empty incoming queue, the task manager | 44 // When a task is appended into an empty incoming queue, the task manager |
45 // work function (DoWork) is scheduled to run on the main task runner. | 45 // work function (DoWork) is scheduled to run on the main task runner. |
46 // | 46 // |
47 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | 47 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from |
48 // the incoming task queue (if any) are moved here. The work queues are | 48 // the incoming task queue (if any) are moved here. The work queues are |
49 // registered with the selector as input to the scheduling decision. | 49 // registered with the selector as input to the scheduling decision. |
50 // | 50 // |
51 class SCHEDULER_EXPORT TaskQueueManager | 51 class SCHEDULER_EXPORT TaskQueueManager |
52 : public internal::TaskQueueSelector::Observer { | 52 : public internal::TaskQueueSelector::Observer { |
53 public: | 53 public: |
54 // Create a task queue manager where |main_task_runner| identifies the thread | 54 // Create a task queue manager where |delegate| identifies the thread |
55 // on which where the tasks are eventually run. Category strings must have | 55 // on which where the tasks are eventually run. Category strings must have |
56 // application lifetime (statics or literals). They may not include " chars. | 56 // application lifetime (statics or literals). They may not include " chars. |
57 TaskQueueManager( | 57 TaskQueueManager(scoped_refptr<TaskQueueManagerDelegate> delegate, |
58 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, | 58 const char* tracing_category, |
59 const char* tracing_category, | 59 const char* disabled_by_default_tracing_category, |
60 const char* disabled_by_default_tracing_category, | 60 const char* disabled_by_default_verbose_tracing_category); |
61 const char* disabled_by_default_verbose_tracing_category); | |
62 ~TaskQueueManager() override; | 61 ~TaskQueueManager() override; |
63 | 62 |
64 // Returns the time of the next pending delayed task in any queue. Ignores | 63 // Returns the time of the next pending delayed task in any queue. Ignores |
65 // any delayed tasks whose delay has expired. Returns a null TimeTicks object | 64 // any delayed tasks whose delay has expired. Returns a null TimeTicks object |
66 // if no tasks are pending. NOTE this is somewhat expensive since every queue | 65 // if no tasks are pending. NOTE this is somewhat expensive since every queue |
67 // will get locked. | 66 // will get locked. |
68 base::TimeTicks NextPendingDelayedTaskRunTime(); | 67 base::TimeTicks NextPendingDelayedTaskRunTime(); |
69 | 68 |
70 // Set the number of tasks executed in a single invocation of the task queue | 69 // Set the number of tasks executed in a single invocation of the task queue |
71 // manager. Increasing the batch size can reduce the overhead of yielding | 70 // manager. Increasing the batch size can reduce the overhead of yielding |
72 // back to the main message loop -- at the cost of potentially delaying other | 71 // back to the main message loop -- at the cost of potentially delaying other |
73 // tasks posted to the main loop. The batch size is 1 by default. | 72 // tasks posted to the main loop. The batch size is 1 by default. |
74 void SetWorkBatchSize(int work_batch_size); | 73 void SetWorkBatchSize(int work_batch_size); |
75 | 74 |
76 // These functions can only be called on the same thread that the task queue | 75 // These functions can only be called on the same thread that the task queue |
77 // manager executes its tasks on. | 76 // manager executes its tasks on. |
78 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 77 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
79 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 78 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
80 | 79 |
81 void SetTimeSourceForTesting(scoped_ptr<base::TickClock> time_source); | |
82 | |
83 // Returns true if any task from a monitored task queue was was run since the | 80 // Returns true if any task from a monitored task queue was was run since the |
84 // last call to GetAndClearSystemIsQuiescentBit. | 81 // last call to GetAndClearSystemIsQuiescentBit. |
85 bool GetAndClearSystemIsQuiescentBit(); | 82 bool GetAndClearSystemIsQuiescentBit(); |
86 | 83 |
87 // Creates a task queue with the given |spec|. Must be called on the thread | 84 // Creates a task queue with the given |spec|. Must be called on the thread |
88 // this class was created on. | 85 // this class was created on. |
89 scoped_refptr<internal::TaskQueueImpl> NewTaskQueue( | 86 scoped_refptr<internal::TaskQueueImpl> NewTaskQueue( |
90 const TaskQueue::Spec& spec); | 87 const TaskQueue::Spec& spec); |
91 | 88 |
92 class SCHEDULER_EXPORT Observer { | 89 class SCHEDULER_EXPORT Observer { |
93 public: | 90 public: |
94 virtual ~Observer() {} | 91 virtual ~Observer() {} |
95 | 92 |
96 // Called when |queue| is unregistered. | 93 // Called when |queue| is unregistered. |
97 virtual void OnUnregisterTaskQueue( | 94 virtual void OnUnregisterTaskQueue( |
98 const scoped_refptr<internal::TaskQueueImpl>& queue) = 0; | 95 const scoped_refptr<internal::TaskQueueImpl>& queue) = 0; |
99 }; | 96 }; |
100 | 97 |
101 // Called once to set the Observer. This function is called on the main | 98 // Called once to set the Observer. This function is called on the main |
102 // thread. If |observer| is null, then no callbacks will occur. | 99 // thread. If |observer| is null, then no callbacks will occur. |
103 // Note |observer| is expected to outlive the SchedulerHelper. | 100 // Note |observer| is expected to outlive the SchedulerHelper. |
104 void SetObserver(Observer* observer); | 101 void SetObserver(Observer* observer); |
105 | 102 |
| 103 // Returns the TickClock used by the TaskQueueManager. |
| 104 base::TickClock* tick_clock() const; |
| 105 |
106 private: | 106 private: |
107 friend class internal::LazyNow; | 107 friend class internal::LazyNow; |
108 friend class internal::TaskQueueImpl; | 108 friend class internal::TaskQueueImpl; |
109 friend class TaskQueueManagerTest; | 109 friend class TaskQueueManagerTest; |
110 | 110 |
111 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { | 111 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { |
112 private: | 112 private: |
113 friend class base::RefCounted<DeletionSentinel>; | 113 friend class base::RefCounted<DeletionSentinel>; |
114 ~DeletionSentinel() {} | 114 ~DeletionSentinel() {} |
115 }; | 115 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 enum class ProcessTaskResult { | 153 enum class ProcessTaskResult { |
154 DEFERRED, | 154 DEFERRED, |
155 EXECUTED, | 155 EXECUTED, |
156 TASK_QUEUE_MANAGER_DELETED | 156 TASK_QUEUE_MANAGER_DELETED |
157 }; | 157 }; |
158 ProcessTaskResult ProcessTaskFromWorkQueue( | 158 ProcessTaskResult ProcessTaskFromWorkQueue( |
159 internal::TaskQueueImpl* queue, | 159 internal::TaskQueueImpl* queue, |
160 internal::TaskQueueImpl::Task* out_previous_task); | 160 internal::TaskQueueImpl::Task* out_previous_task); |
161 | 161 |
162 bool RunsTasksOnCurrentThread() const; | 162 bool RunsTasksOnCurrentThread() const; |
163 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
164 const base::Closure& task, | |
165 base::TimeDelta delay); | |
166 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 163 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
167 const base::Closure& task, | 164 const base::Closure& task, |
168 base::TimeDelta delay); | 165 base::TimeDelta delay); |
169 | 166 |
170 base::TimeTicks Now() const; | |
171 | |
172 int GetNextSequenceNumber(); | 167 int GetNextSequenceNumber(); |
173 | 168 |
174 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 169 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
175 AsValueWithSelectorResult(bool should_run, | 170 AsValueWithSelectorResult(bool should_run, |
176 internal::TaskQueueImpl* selected_queue) const; | 171 internal::TaskQueueImpl* selected_queue) const; |
177 | 172 |
178 // Causes DoWork to start calling UpdateWorkQueue for |queue|. Can be called | 173 // Causes DoWork to start calling UpdateWorkQueue for |queue|. Can be called |
179 // from any thread. | 174 // from any thread. |
180 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | 175 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
181 | 176 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 216 |
222 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> | 217 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> |
223 DelayedWakeupMultimap; | 218 DelayedWakeupMultimap; |
224 | 219 |
225 DelayedWakeupMultimap delayed_wakeup_multimap_; | 220 DelayedWakeupMultimap delayed_wakeup_multimap_; |
226 | 221 |
227 base::AtomicSequenceNumber task_sequence_num_; | 222 base::AtomicSequenceNumber task_sequence_num_; |
228 base::debug::TaskAnnotator task_annotator_; | 223 base::debug::TaskAnnotator task_annotator_; |
229 | 224 |
230 base::ThreadChecker main_thread_checker_; | 225 base::ThreadChecker main_thread_checker_; |
231 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner_; | 226 scoped_refptr<TaskQueueManagerDelegate> delegate_; |
232 internal::TaskQueueSelector selector_; | 227 internal::TaskQueueSelector selector_; |
233 | 228 |
234 base::Closure do_work_from_main_thread_closure_; | 229 base::Closure do_work_from_main_thread_closure_; |
235 base::Closure do_work_from_other_thread_closure_; | 230 base::Closure do_work_from_other_thread_closure_; |
236 base::Closure delayed_queue_wakeup_closure_; | 231 base::Closure delayed_queue_wakeup_closure_; |
237 | 232 |
238 bool task_was_run_on_quiescence_monitored_queue_; | 233 bool task_was_run_on_quiescence_monitored_queue_; |
239 | 234 |
240 // The pending_dowork_count_ is only tracked on the main thread since that's | 235 // The pending_dowork_count_ is only tracked on the main thread since that's |
241 // where re-entrant problems happen. | 236 // where re-entrant problems happen. |
242 int pending_dowork_count_; | 237 int pending_dowork_count_; |
243 | 238 |
244 int work_batch_size_; | 239 int work_batch_size_; |
245 | 240 |
246 scoped_ptr<base::TickClock> time_source_; | |
247 | |
248 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; | 241 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; |
249 | 242 |
250 const char* tracing_category_; | 243 const char* tracing_category_; |
251 const char* disabled_by_default_tracing_category_; | 244 const char* disabled_by_default_tracing_category_; |
252 const char* disabled_by_default_verbose_tracing_category_; | 245 const char* disabled_by_default_verbose_tracing_category_; |
253 | 246 |
254 Observer* observer_; // NOT OWNED | 247 Observer* observer_; // NOT OWNED |
255 scoped_refptr<DeletionSentinel> deletion_sentinel_; | 248 scoped_refptr<DeletionSentinel> deletion_sentinel_; |
256 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | 249 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
257 | 250 |
258 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | 251 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
259 }; | 252 }; |
260 | 253 |
261 } // namespace scheduler | 254 } // namespace scheduler |
262 | 255 |
263 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ | 256 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_MANAGER_H_ |
OLD | NEW |