| 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/pending_task.h" | 12 #include "base/pending_task.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 namespace debug { | 19 namespace debug { |
| 20 class ConvertableToTraceFormat; | 20 class ConvertableToTraceFormat; |
| 21 class TracedValue; | 21 class TracedValue; |
| 22 } | 22 } |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace cc { | |
| 26 class TestNowSource; | |
| 27 } | |
| 28 | |
| 29 namespace content { | 25 namespace content { |
| 30 namespace internal { | 26 namespace internal { |
| 31 class TaskQueue; | 27 class TaskQueue; |
| 32 } | 28 } |
| 33 class TaskQueueSelector; | 29 class TaskQueueSelector; |
| 34 | 30 |
| 35 // The task queue manager provides N task queues and a selector interface for | 31 // The task queue manager provides N task queues and a selector interface for |
| 36 // choosing which task queue to service next. Each task queue consists of two | 32 // choosing which task queue to service next. Each task queue consists of two |
| 37 // sub queues: | 33 // sub queues: |
| 38 // | 34 // |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 73 |
| 78 // Returns true if there no tasks in either the work or incoming task queue | 74 // Returns true if there no tasks in either the work or incoming task queue |
| 79 // identified by |queue_index|. Note that this function involves taking a | 75 // identified by |queue_index|. Note that this function involves taking a |
| 80 // lock, so calling it has some overhead. | 76 // lock, so calling it has some overhead. |
| 81 bool IsQueueEmpty(size_t queue_index) const; | 77 bool IsQueueEmpty(size_t queue_index) const; |
| 82 | 78 |
| 83 // Set the name |queue_index| for tracing purposes. |name| must be a pointer | 79 // Set the name |queue_index| for tracing purposes. |name| must be a pointer |
| 84 // to a static string. | 80 // to a static string. |
| 85 void SetQueueName(size_t queue_index, const char* name); | 81 void SetQueueName(size_t queue_index, const char* name); |
| 86 | 82 |
| 87 // Set the number of tasks executed in a single invocation of the task queue | |
| 88 // manager. Increasing the batch size can reduce the overhead of yielding | |
| 89 // back to the main message loop -- at the cost of potentially delaying other | |
| 90 // tasks posted to the main loop. The batch size is 1 by default. | |
| 91 void SetWorkBatchSize(int work_batch_size); | |
| 92 | |
| 93 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); | |
| 94 | |
| 95 private: | 83 private: |
| 96 friend class internal::TaskQueue; | 84 friend class internal::TaskQueue; |
| 97 | 85 |
| 98 // Called by the task queue to register a new pending task and allocate a | 86 // Called by the task queue to register a new pending task and allocate a |
| 99 // sequence number for it. | 87 // sequence number for it. |
| 100 void DidQueueTask(base::PendingTask* pending_task); | 88 void DidQueueTask(base::PendingTask* pending_task); |
| 101 | 89 |
| 102 // Post a task to call DoWork() on the main task runner. Only one pending | 90 // Post a task to call DoWork() on the main task runner. Only one pending |
| 103 // DoWork is allowed from the main thread, to prevent an explosion of pending | 91 // DoWork is allowed from the main thread, to prevent an explosion of pending |
| 104 // DoWorks. | 92 // DoWorks. |
| 105 void MaybePostDoWorkOnMainRunner(); | 93 void MaybePostDoWorkOnMainRunner(); |
| 106 | 94 |
| 107 // Use the selector to choose a pending task and run it. | 95 // Use the selector to choose a pending task and run it. |
| 108 void DoWork(bool posted_from_main_thread); | 96 void DoWork(bool posted_from_main_thread); |
| 109 | 97 |
| 110 // Reloads any empty work queues which have automatic pumping enabled. | 98 // Reloads any empty work queues which have automatic pumping enabled. |
| 111 // Returns true if any work queue has tasks after doing this. | 99 // Returns true if any work queue has tasks after doing this. |
| 112 // |next_pending_delayed_task| should be the time of the next known delayed | 100 bool UpdateWorkQueues(); |
| 113 // task. It is updated if any task is found which should run earlier. | |
| 114 bool UpdateWorkQueues(base::TimeTicks* next_pending_delayed_task); | |
| 115 | 101 |
| 116 // Chooses the next work queue to service. Returns true if |out_queue_index| | 102 // Chooses the next work queue to service. Returns true if |out_queue_index| |
| 117 // indicates the queue from which the next task should be run, false to | 103 // indicates the queue from which the next task should be run, false to |
| 118 // avoid running any tasks. | 104 // avoid running any tasks. |
| 119 bool SelectWorkQueueToService(size_t* out_queue_index); | 105 bool SelectWorkQueueToService(size_t* out_queue_index); |
| 120 | 106 |
| 121 // Runs a single nestable task from the work queue designated by | 107 // Runs a single nestable task from the work queue designated by |
| 122 // |queue_index|. Non-nestable task are reposted on the run loop. | 108 // |queue_index|. Non-nestable task are reposted on the run loop. |
| 123 // The queue must not be empty. | 109 // The queue must not be empty. |
| 124 void ProcessTaskFromWorkQueue(size_t queue_index); | 110 void ProcessTaskFromWorkQueue(size_t queue_index); |
| 125 | 111 |
| 126 bool RunsTasksOnCurrentThread() const; | 112 bool RunsTasksOnCurrentThread() const; |
| 127 bool PostDelayedTask(const tracked_objects::Location& from_here, | 113 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 128 const base::Closure& task, | 114 const base::Closure& task, |
| 129 base::TimeDelta delay); | 115 base::TimeDelta delay); |
| 130 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 116 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 131 const base::Closure& task, | 117 const base::Closure& task, |
| 132 base::TimeDelta delay); | 118 base::TimeDelta delay); |
| 133 internal::TaskQueue* Queue(size_t queue_index) const; | 119 internal::TaskQueue* Queue(size_t queue_index) const; |
| 134 | 120 |
| 135 base::TimeTicks Now() const; | |
| 136 | |
| 137 scoped_refptr<base::debug::ConvertableToTraceFormat> | 121 scoped_refptr<base::debug::ConvertableToTraceFormat> |
| 138 AsValueWithSelectorResult(bool should_run, size_t selected_queue) const; | 122 AsValueWithSelectorResult(bool should_run, size_t selected_queue) const; |
| 139 | 123 |
| 140 std::vector<scoped_refptr<internal::TaskQueue>> queues_; | 124 std::vector<scoped_refptr<internal::TaskQueue>> queues_; |
| 141 base::AtomicSequenceNumber task_sequence_num_; | 125 base::AtomicSequenceNumber task_sequence_num_; |
| 142 base::debug::TaskAnnotator task_annotator_; | 126 base::debug::TaskAnnotator task_annotator_; |
| 143 | 127 |
| 144 base::ThreadChecker main_thread_checker_; | 128 base::ThreadChecker main_thread_checker_; |
| 145 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 129 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 146 TaskQueueSelector* selector_; | 130 TaskQueueSelector* selector_; |
| 147 | 131 |
| 148 base::WeakPtr<TaskQueueManager> task_queue_manager_weak_ptr_; | 132 base::WeakPtr<TaskQueueManager> task_queue_manager_weak_ptr_; |
| 149 | 133 |
| 150 // The pending_dowork_count_ is only tracked on the main thread since that's | 134 // The pending_dowork_count_ is only tracked on the main thread since that's |
| 151 // where re-entrant problems happen. | 135 // where re-entrant problems happen. |
| 152 int pending_dowork_count_; | 136 int pending_dowork_count_; |
| 153 | 137 |
| 154 int work_batch_size_; | |
| 155 | |
| 156 scoped_refptr<cc::TestNowSource> time_source_; | |
| 157 | |
| 158 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | 138 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 159 | 139 |
| 160 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | 140 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 161 }; | 141 }; |
| 162 | 142 |
| 163 } // namespace content | 143 } // namespace content |
| 164 | 144 |
| 165 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | 145 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| OLD | NEW |