OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | |
6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | |
7 | |
8 #include "base/atomic_sequence_num.h" | |
9 #include "base/debug/task_annotator.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/pending_task.h" | |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/threading/thread_checker.h" | |
17 #include "content/child/scheduler/task_queue_selector.h" | |
18 #include "content/child/scheduler/time_source.h" | |
19 #include "content/common/content_export.h" | |
20 | |
21 namespace base { | |
22 namespace trace_event { | |
23 class ConvertableToTraceFormat; | |
24 class TracedValue; | |
25 } | |
26 } | |
27 | |
28 namespace content { | |
29 namespace internal { | |
30 class LazyNow; | |
31 class TaskQueue; | |
32 } | |
33 class NestableSingleThreadTaskRunner; | |
34 class TaskQueueSelector; | |
35 class TimeSource; | |
36 | |
37 // The task queue manager provides N task queues and a selector interface for | |
38 // choosing which task queue to service next. Each task queue consists of two | |
39 // sub queues: | |
40 // | |
41 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | |
42 // When a task is appended into an empty incoming queue, the task manager | |
43 // work function (DoWork) is scheduled to run on the main task runner. | |
44 // | |
45 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | |
46 // the incoming task queue (if any) are moved here. The work queues are | |
47 // registered with the selector as input to the scheduling decision. | |
48 // | |
49 class CONTENT_EXPORT TaskQueueManager | |
50 : public TaskQueueSelector::Observer { | |
51 public: | |
52 // Keep TaskQueue::PumpPolicyToString in sync with this enum. | |
53 enum class PumpPolicy { | |
54 // Tasks posted to an incoming queue with an AUTO pump policy will be | |
55 // automatically scheduled for execution or transferred to the work queue | |
56 // automatically. | |
57 AUTO, | |
58 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy | |
59 // will be scheduled for execution or transferred to the work queue | |
60 // automatically but only after another queue has executed a task. | |
61 AFTER_WAKEUP, | |
62 // Tasks posted to an incoming queue with a MANUAL will not be | |
63 // automatically scheduled for execution or transferred to the work queue. | |
64 // Instead, the selector should call PumpQueue() when necessary to bring | |
65 // in new tasks for execution. | |
66 MANUAL | |
67 }; | |
68 | |
69 // Create a task queue manager with |task_queue_count| task queues. | |
70 // |main_task_runner| identifies the thread on which where the tasks are | |
71 // eventually run. |selector| is used to choose which task queue to service. | |
72 // It should outlive this class. Category strings must have application | |
73 // lifetime (statics or literals). They may not include " chars. | |
74 TaskQueueManager( | |
75 size_t task_queue_count, | |
76 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, | |
77 TaskQueueSelector* selector, | |
78 const char* disabled_by_default_tracing_category); | |
79 ~TaskQueueManager() override; | |
80 | |
81 // Returns the task runner which targets the queue selected by |queue_index|. | |
82 scoped_refptr<base::SingleThreadTaskRunner> TaskRunnerForQueue( | |
83 size_t queue_index) const; | |
84 | |
85 // Sets the pump policy for the |queue_index| to |pump_policy|. By | |
86 // default queues are created with AUTO_PUMP_POLICY. | |
87 void SetPumpPolicy(size_t queue_index, PumpPolicy pump_policy); | |
88 | |
89 // Reloads new tasks from the incoming queue for |queue_index| into the work | |
90 // queue, regardless of whether the work queue is empty or not. After this, | |
91 // this function ensures that the tasks in the work queue, if any, are | |
92 // scheduled for execution. | |
93 // | |
94 // This function only needs to be called if automatic pumping is disabled | |
95 // for |queue_index|. See |SetQueueAutoPumpPolicy|. By default automatic | |
96 // pumping is enabled for all queues. | |
97 void PumpQueue(size_t queue_index); | |
98 | |
99 // Returns true if there no tasks in either the work or incoming task queue | |
100 // identified by |queue_index|. Note that this function involves taking a | |
101 // lock, so calling it has some overhead. | |
102 bool IsQueueEmpty(size_t queue_index) const; | |
103 | |
104 // Returns the time of the next pending delayed task in any queue. Ignores | |
105 // any delayed tasks whose delay has expired. Returns a null TimeTicks object | |
106 // if no tasks are pending. NOTE this is somewhat expensive since every queue | |
107 // will get locked. | |
108 base::TimeTicks NextPendingDelayedTaskRunTime(); | |
109 | |
110 // Set the name |queue_index| for tracing purposes. |name| must be a pointer | |
111 // to a static string. | |
112 void SetQueueName(size_t queue_index, const char* name); | |
113 | |
114 // Set the number of tasks executed in a single invocation of the task queue | |
115 // manager. Increasing the batch size can reduce the overhead of yielding | |
116 // back to the main message loop -- at the cost of potentially delaying other | |
117 // tasks posted to the main loop. The batch size is 1 by default. | |
118 void SetWorkBatchSize(int work_batch_size); | |
119 | |
120 // These functions can only be called on the same thread that the task queue | |
121 // manager executes its tasks on. | |
122 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
123 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
124 | |
125 void SetTimeSourceForTesting(scoped_ptr<TimeSource> time_source); | |
126 | |
127 // Returns a bitmap where a bit is set iff a task on the corresponding queue | |
128 // was run since the last call to GetAndClearTaskWasRunOnQueueBitmap. | |
129 uint64 GetAndClearTaskWasRunOnQueueBitmap(); | |
130 | |
131 private: | |
132 // TaskQueueSelector::Observer implementation: | |
133 void OnTaskQueueEnabled() override; | |
134 | |
135 friend class internal::LazyNow; | |
136 friend class internal::TaskQueue; | |
137 | |
138 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { | |
139 private: | |
140 friend class base::RefCounted<DeletionSentinel>; | |
141 ~DeletionSentinel() {} | |
142 }; | |
143 | |
144 // Called by the task queue to register a new pending task and allocate a | |
145 // sequence number for it. | |
146 void DidQueueTask(base::PendingTask* pending_task); | |
147 | |
148 // Post a task to call DoWork() on the main task runner. Only one pending | |
149 // DoWork is allowed from the main thread, to prevent an explosion of pending | |
150 // DoWorks. | |
151 void MaybePostDoWorkOnMainRunner(); | |
152 | |
153 // Use the selector to choose a pending task and run it. | |
154 void DoWork(bool posted_from_main_thread); | |
155 | |
156 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue. | |
157 // Reloads any empty work queues which have automatic pumping enabled and | |
158 // which are eligible to be auto pumped based on the |previous_task| which was | |
159 // run. Call with an empty |previous_task| if no task was just run. Returns | |
160 // true if any work queue has tasks after doing this. | |
161 // |next_pending_delayed_task| should be the time of the next known delayed | |
162 // task. It is updated if any task is found which should run earlier. | |
163 bool UpdateWorkQueues(const base::PendingTask* previous_task); | |
164 | |
165 // Chooses the next work queue to service. Returns true if |out_queue_index| | |
166 // indicates the queue from which the next task should be run, false to | |
167 // avoid running any tasks. | |
168 bool SelectWorkQueueToService(size_t* out_queue_index); | |
169 | |
170 // Runs a single nestable task from the work queue designated by | |
171 // |queue_index|. If |has_previous_task| is true, |previous_task| should | |
172 // contain the previous task in this work batch. Non-nestable task are | |
173 // reposted on the run loop. The queue must not be empty. | |
174 // Returns true if the TaskQueueManager got deleted, and false otherwise. | |
175 bool ProcessTaskFromWorkQueue(size_t queue_index, | |
176 bool has_previous_task, | |
177 base::PendingTask* previous_task); | |
178 | |
179 bool RunsTasksOnCurrentThread() const; | |
180 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
181 const base::Closure& task, | |
182 base::TimeDelta delay); | |
183 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
184 const base::Closure& task, | |
185 base::TimeDelta delay); | |
186 internal::TaskQueue* Queue(size_t queue_index) const; | |
187 | |
188 base::TimeTicks Now() const; | |
189 | |
190 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | |
191 AsValueWithSelectorResult(bool should_run, size_t selected_queue) const; | |
192 | |
193 std::vector<scoped_refptr<internal::TaskQueue>> queues_; | |
194 base::AtomicSequenceNumber task_sequence_num_; | |
195 base::debug::TaskAnnotator task_annotator_; | |
196 | |
197 base::ThreadChecker main_thread_checker_; | |
198 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner_; | |
199 TaskQueueSelector* selector_; | |
200 | |
201 base::Closure do_work_from_main_thread_closure_; | |
202 base::Closure do_work_from_other_thread_closure_; | |
203 | |
204 uint64 task_was_run_bitmap_; | |
205 | |
206 // The pending_dowork_count_ is only tracked on the main thread since that's | |
207 // where re-entrant problems happen. | |
208 int pending_dowork_count_; | |
209 | |
210 int work_batch_size_; | |
211 | |
212 scoped_ptr<TimeSource> time_source_; | |
213 | |
214 ObserverList<base::MessageLoop::TaskObserver> task_observers_; | |
215 | |
216 const char* disabled_by_default_tracing_category_; | |
217 | |
218 scoped_refptr<DeletionSentinel> deletion_sentinel_; | |
219 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | |
220 | |
221 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | |
222 }; | |
223 | |
224 } // namespace content | |
225 | |
226 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | |
OLD | NEW |