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