| 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" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/pending_task.h" | 13 #include "base/pending_task.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 18 #include "content/renderer/scheduler/task_queue_selector.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 namespace trace_event { | 21 namespace trace_event { |
| 21 class ConvertableToTraceFormat; | 22 class ConvertableToTraceFormat; |
| 22 class TracedValue; | 23 class TracedValue; |
| 23 } | 24 } |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace cc { | 27 namespace cc { |
| 27 class TestNowSource; | 28 class TestNowSource; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 38 // sub queues: | 39 // sub queues: |
| 39 // | 40 // |
| 40 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | 41 // 1. Incoming task queue. Tasks that are posted get immediately appended here. |
| 41 // When a task is appended into an empty incoming queue, the task manager | 42 // When a task is appended into an empty incoming queue, the task manager |
| 42 // work function (DoWork) is scheduled to run on the main task runner. | 43 // work function (DoWork) is scheduled to run on the main task runner. |
| 43 // | 44 // |
| 44 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | 45 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from |
| 45 // the incoming task queue (if any) are moved here. The work queues are | 46 // the incoming task queue (if any) are moved here. The work queues are |
| 46 // registered with the selector as input to the scheduling decision. | 47 // registered with the selector as input to the scheduling decision. |
| 47 // | 48 // |
| 48 class CONTENT_EXPORT TaskQueueManager { | 49 class CONTENT_EXPORT TaskQueueManager |
| 50 : public TaskQueueSelector::TaskQueueSelectorObserver { |
| 49 public: | 51 public: |
| 50 // Keep TaskQueue::PumpPolicyToString in sync with this enum. | 52 // Keep TaskQueue::PumpPolicyToString in sync with this enum. |
| 51 enum class PumpPolicy { | 53 enum class PumpPolicy { |
| 52 // Tasks posted to an incoming queue with an AUTO pump policy will be | 54 // Tasks posted to an incoming queue with an AUTO pump policy will be |
| 53 // automatically scheduled for execution or transferred to the work queue | 55 // automatically scheduled for execution or transferred to the work queue |
| 54 // automatically. | 56 // automatically. |
| 55 AUTO, | 57 AUTO, |
| 56 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy | 58 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy |
| 57 // will be scheduled for execution or transferred to the work queue | 59 // will be scheduled for execution or transferred to the work queue |
| 58 // automatically but only after another queue has executed a task. | 60 // automatically but only after another queue has executed a task. |
| 59 AFTER_WAKEUP, | 61 AFTER_WAKEUP, |
| 60 // Tasks posted to an incoming queue with a MANUAL will not be | 62 // Tasks posted to an incoming queue with a MANUAL will not be |
| 61 // automatically scheduled for execution or transferred to the work queue. | 63 // automatically scheduled for execution or transferred to the work queue. |
| 62 // Instead, the selector should call PumpQueue() when necessary to bring | 64 // Instead, the selector should call PumpQueue() when necessary to bring |
| 63 // in new tasks for execution. | 65 // in new tasks for execution. |
| 64 MANUAL | 66 MANUAL |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 // Create a task queue manager with |task_queue_count| task queues. | 69 // Create a task queue manager with |task_queue_count| task queues. |
| 68 // |main_task_runner| identifies the thread on which where the tasks are | 70 // |main_task_runner| identifies the thread on which where the tasks are |
| 69 // eventually run. |selector| is used to choose which task queue to service. | 71 // eventually run. |selector| is used to choose which task queue to service. |
| 70 // It should outlive this class. | 72 // It should outlive this class. |
| 71 TaskQueueManager(size_t task_queue_count, | 73 TaskQueueManager(size_t task_queue_count, |
| 72 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 74 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 73 TaskQueueSelector* selector); | 75 TaskQueueSelector* selector); |
| 74 ~TaskQueueManager(); | 76 ~TaskQueueManager() override; |
| 75 | 77 |
| 76 // Returns the task runner which targets the queue selected by |queue_index|. | 78 // Returns the task runner which targets the queue selected by |queue_index|. |
| 77 scoped_refptr<base::SingleThreadTaskRunner> TaskRunnerForQueue( | 79 scoped_refptr<base::SingleThreadTaskRunner> TaskRunnerForQueue( |
| 78 size_t queue_index) const; | 80 size_t queue_index) const; |
| 79 | 81 |
| 80 // Sets the pump policy for the |queue_index| to |pump_policy|. By | 82 // Sets the pump policy for the |queue_index| to |pump_policy|. By |
| 81 // default queues are created with AUTO_PUMP_POLICY. | 83 // default queues are created with AUTO_PUMP_POLICY. |
| 82 void SetPumpPolicy(size_t queue_index, PumpPolicy pump_policy); | 84 void SetPumpPolicy(size_t queue_index, PumpPolicy pump_policy); |
| 83 | 85 |
| 84 // Reloads new tasks from the incoming queue for |queue_index| into the work | 86 // Reloads new tasks from the incoming queue for |queue_index| into the work |
| (...skipping 21 matching lines...) Expand all Loading... |
| 106 // tasks posted to the main loop. The batch size is 1 by default. | 108 // tasks posted to the main loop. The batch size is 1 by default. |
| 107 void SetWorkBatchSize(int work_batch_size); | 109 void SetWorkBatchSize(int work_batch_size); |
| 108 | 110 |
| 109 // These functions can only be called on the same thread that the task queue | 111 // These functions can only be called on the same thread that the task queue |
| 110 // manager executes its tasks on. | 112 // manager executes its tasks on. |
| 111 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 113 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 112 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 114 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 113 | 115 |
| 114 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); | 116 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); |
| 115 | 117 |
| 118 // TaskQueueSelectorObserver implementation |
| 119 void OnTaskQueueEnabled() override; |
| 120 |
| 116 private: | 121 private: |
| 117 friend class internal::TaskQueue; | 122 friend class internal::TaskQueue; |
| 118 | 123 |
| 119 // Called by the task queue to register a new pending task and allocate a | 124 // Called by the task queue to register a new pending task and allocate a |
| 120 // sequence number for it. | 125 // sequence number for it. |
| 121 void DidQueueTask(base::PendingTask* pending_task); | 126 void DidQueueTask(base::PendingTask* pending_task); |
| 122 | 127 |
| 123 // Post a task to call DoWork() on the main task runner. Only one pending | 128 // Post a task to call DoWork() on the main task runner. Only one pending |
| 124 // DoWork is allowed from the main thread, to prevent an explosion of pending | 129 // DoWork is allowed from the main thread, to prevent an explosion of pending |
| 125 // DoWorks. | 130 // DoWorks. |
| 126 void MaybePostDoWorkOnMainRunner(); | 131 void MaybePostDoWorkOnMainRunner(); |
| 127 | 132 |
| 128 // Use the selector to choose a pending task and run it. | 133 // Use the selector to choose a pending task and run it. |
| 129 void DoWork(bool posted_from_main_thread); | 134 void DoWork(bool posted_from_main_thread); |
| 130 | 135 |
| 136 // Delayed Tasks with run_times <= |now| are enqueued onto their work queues. |
| 131 // Reloads any empty work queues which have automatic pumping enabled and | 137 // Reloads any empty work queues which have automatic pumping enabled and |
| 132 // which are eligible to be auto pumped based on the |previous_task| which was | 138 // which are eligible to be auto pumped based on the |previous_task| which was |
| 133 // run. Call with an empty |previous_task| if no task was just run. Returns | 139 // run. Call with an empty |previous_task| if no task was just run. Returns |
| 134 // true if any work queue has tasks after doing this. | 140 // true if any work queue has tasks after doing this. |
| 135 // |next_pending_delayed_task| should be the time of the next known delayed | 141 // |next_pending_delayed_task| should be the time of the next known delayed |
| 136 // task. It is updated if any task is found which should run earlier. | 142 // task. It is updated if any task is found which should run earlier. |
| 137 bool UpdateWorkQueues(base::TimeTicks* next_pending_delayed_task, | 143 bool UpdateWorkQueues(base::TimeTicks now, |
| 138 const base::PendingTask* previous_task); | 144 const base::PendingTask* previous_task); |
| 139 | 145 |
| 140 // Chooses the next work queue to service. Returns true if |out_queue_index| | 146 // Chooses the next work queue to service. Returns true if |out_queue_index| |
| 141 // indicates the queue from which the next task should be run, false to | 147 // indicates the queue from which the next task should be run, false to |
| 142 // avoid running any tasks. | 148 // avoid running any tasks. |
| 143 bool SelectWorkQueueToService(size_t* out_queue_index); | 149 bool SelectWorkQueueToService(size_t* out_queue_index); |
| 144 | 150 |
| 145 // Runs a single nestable task from the work queue designated by | 151 // Runs a single nestable task from the work queue designated by |
| 146 // |queue_index|. If |has_previous_task| is true, |previous_task| should | 152 // |queue_index|. If |has_previous_task| is true, |previous_task| should |
| 147 // contain the previous task in this work batch. Non-nestable task are | 153 // contain the previous task in this work batch. Non-nestable task are |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 ObserverList<base::MessageLoop::TaskObserver> task_observers_; | 191 ObserverList<base::MessageLoop::TaskObserver> task_observers_; |
| 186 | 192 |
| 187 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | 193 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 188 | 194 |
| 189 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | 195 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 190 }; | 196 }; |
| 191 | 197 |
| 192 } // namespace content | 198 } // namespace content |
| 193 | 199 |
| 194 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | 200 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| OLD | NEW |