| 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_TASK_QUEUE_MANAGER_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| 6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | 6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| 7 | 7 |
| 8 #include "base/atomic_sequence_num.h" | 8 #include "base/atomic_sequence_num.h" |
| 9 #include "base/debug/task_annotator.h" | 9 #include "base/debug/task_annotator.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class TracedValue; | 22 class TracedValue; |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace cc { | 26 namespace cc { |
| 27 class TestNowSource; | 27 class TestNowSource; |
| 28 } | 28 } |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 namespace internal { | 31 namespace internal { |
| 32 class LazyNow; |
| 32 class TaskQueue; | 33 class TaskQueue; |
| 33 } | 34 } |
| 34 class TaskQueueSelector; | 35 class TaskQueueSelector; |
| 35 class NestableSingleThreadTaskRunner; | 36 class NestableSingleThreadTaskRunner; |
| 36 | 37 |
| 37 // The task queue manager provides N task queues and a selector interface for | 38 // 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 // choosing which task queue to service next. Each task queue consists of two |
| 39 // sub queues: | 40 // sub queues: |
| 40 // | 41 // |
| 41 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | 42 // 1. Incoming task queue. Tasks that are posted get immediately appended here. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // This function only needs to be called if automatic pumping is disabled | 92 // This function only needs to be called if automatic pumping is disabled |
| 92 // for |queue_index|. See |SetQueueAutoPumpPolicy|. By default automatic | 93 // for |queue_index|. See |SetQueueAutoPumpPolicy|. By default automatic |
| 93 // pumping is enabled for all queues. | 94 // pumping is enabled for all queues. |
| 94 void PumpQueue(size_t queue_index); | 95 void PumpQueue(size_t queue_index); |
| 95 | 96 |
| 96 // Returns true if there no tasks in either the work or incoming task queue | 97 // Returns true if there no tasks in either the work or incoming task queue |
| 97 // identified by |queue_index|. Note that this function involves taking a | 98 // identified by |queue_index|. Note that this function involves taking a |
| 98 // lock, so calling it has some overhead. | 99 // lock, so calling it has some overhead. |
| 99 bool IsQueueEmpty(size_t queue_index) const; | 100 bool IsQueueEmpty(size_t queue_index) const; |
| 100 | 101 |
| 101 // Returns the time of the next pending delayed task in any queue. Returns | 102 // Returns the time of the next pending delayed task in any queue. Ignores |
| 102 // a null TimeTicks object if no tasks are pending. | 103 // any delayed tasks whose delay has expired. Returns a null TimeTicks object |
| 104 // if no tasks are pending. NOTE this is somewhat expensive since every queue |
| 105 // will get locked. |
| 103 base::TimeTicks NextPendingDelayedTaskRunTime(); | 106 base::TimeTicks NextPendingDelayedTaskRunTime(); |
| 104 | 107 |
| 105 // Set the name |queue_index| for tracing purposes. |name| must be a pointer | 108 // Set the name |queue_index| for tracing purposes. |name| must be a pointer |
| 106 // to a static string. | 109 // to a static string. |
| 107 void SetQueueName(size_t queue_index, const char* name); | 110 void SetQueueName(size_t queue_index, const char* name); |
| 108 | 111 |
| 109 // Set the number of tasks executed in a single invocation of the task queue | 112 // Set the number of tasks executed in a single invocation of the task queue |
| 110 // manager. Increasing the batch size can reduce the overhead of yielding | 113 // manager. Increasing the batch size can reduce the overhead of yielding |
| 111 // back to the main message loop -- at the cost of potentially delaying other | 114 // back to the main message loop -- at the cost of potentially delaying other |
| 112 // tasks posted to the main loop. The batch size is 1 by default. | 115 // tasks posted to the main loop. The batch size is 1 by default. |
| 113 void SetWorkBatchSize(int work_batch_size); | 116 void SetWorkBatchSize(int work_batch_size); |
| 114 | 117 |
| 115 // These functions can only be called on the same thread that the task queue | 118 // These functions can only be called on the same thread that the task queue |
| 116 // manager executes its tasks on. | 119 // manager executes its tasks on. |
| 117 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 120 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 118 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 121 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 119 | 122 |
| 120 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); | 123 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); |
| 121 | 124 |
| 122 private: | 125 private: |
| 126 friend class internal::LazyNow; |
| 123 friend class internal::TaskQueue; | 127 friend class internal::TaskQueue; |
| 124 | 128 |
| 125 // Called by the task queue to register a new pending task and allocate a | 129 // Called by the task queue to register a new pending task and allocate a |
| 126 // sequence number for it. | 130 // sequence number for it. |
| 127 void DidQueueTask(base::PendingTask* pending_task); | 131 void DidQueueTask(base::PendingTask* pending_task); |
| 128 | 132 |
| 129 // Post a task to call DoWork() on the main task runner. Only one pending | 133 // Post a task to call DoWork() on the main task runner. Only one pending |
| 130 // DoWork is allowed from the main thread, to prevent an explosion of pending | 134 // DoWork is allowed from the main thread, to prevent an explosion of pending |
| 131 // DoWorks. | 135 // DoWorks. |
| 132 void MaybePostDoWorkOnMainRunner(); | 136 void MaybePostDoWorkOnMainRunner(); |
| 133 | 137 |
| 134 // Use the selector to choose a pending task and run it. | 138 // Use the selector to choose a pending task and run it. |
| 135 void DoWork(bool posted_from_main_thread); | 139 void DoWork(bool posted_from_main_thread); |
| 136 | 140 |
| 141 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue. |
| 137 // Reloads any empty work queues which have automatic pumping enabled and | 142 // Reloads any empty work queues which have automatic pumping enabled and |
| 138 // which are eligible to be auto pumped based on the |previous_task| which was | 143 // which are eligible to be auto pumped based on the |previous_task| which was |
| 139 // run. Call with an empty |previous_task| if no task was just run. Returns | 144 // run. Call with an empty |previous_task| if no task was just run. Returns |
| 140 // true if any work queue has tasks after doing this. | 145 // true if any work queue has tasks after doing this. |
| 141 // |next_pending_delayed_task| should be the time of the next known delayed | 146 // |next_pending_delayed_task| should be the time of the next known delayed |
| 142 // task. It is updated if any task is found which should run earlier. | 147 // task. It is updated if any task is found which should run earlier. |
| 143 bool UpdateWorkQueues(base::TimeTicks* next_pending_delayed_task, | 148 bool UpdateWorkQueues(const base::PendingTask* previous_task); |
| 144 const base::PendingTask* previous_task); | |
| 145 | 149 |
| 146 // Chooses the next work queue to service. Returns true if |out_queue_index| | 150 // Chooses the next work queue to service. Returns true if |out_queue_index| |
| 147 // indicates the queue from which the next task should be run, false to | 151 // indicates the queue from which the next task should be run, false to |
| 148 // avoid running any tasks. | 152 // avoid running any tasks. |
| 149 bool SelectWorkQueueToService(size_t* out_queue_index); | 153 bool SelectWorkQueueToService(size_t* out_queue_index); |
| 150 | 154 |
| 151 // Runs a single nestable task from the work queue designated by | 155 // Runs a single nestable task from the work queue designated by |
| 152 // |queue_index|. If |has_previous_task| is true, |previous_task| should | 156 // |queue_index|. If |has_previous_task| is true, |previous_task| should |
| 153 // contain the previous task in this work batch. Non-nestable task are | 157 // contain the previous task in this work batch. Non-nestable task are |
| 154 // reposted on the run loop. The queue must not be empty. | 158 // reposted on the run loop. The queue must not be empty. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 ObserverList<base::MessageLoop::TaskObserver> task_observers_; | 195 ObserverList<base::MessageLoop::TaskObserver> task_observers_; |
| 192 | 196 |
| 193 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | 197 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 194 | 198 |
| 195 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | 199 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 196 }; | 200 }; |
| 197 | 201 |
| 198 } // namespace content | 202 } // namespace content |
| 199 | 203 |
| 200 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | 204 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| OLD | NEW |