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 #include "content/renderer/scheduler/task_queue_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" |
| 9 #include "content/renderer/scheduler/task_queue_selector.h" |
| 10 |
| 11 namespace content { |
| 12 namespace internal { |
| 13 |
| 14 class TaskRunner : public base::SingleThreadTaskRunner { |
| 15 public: |
| 16 TaskRunner(base::WeakPtr<TaskQueueManager> task_queue_manager, |
| 17 size_t queue_index); |
| 18 |
| 19 // base::SingleThreadTaskRunner implementation. |
| 20 virtual bool RunsTasksOnCurrentThread() const override; |
| 21 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 22 const base::Closure& task, |
| 23 base::TimeDelta delay) override; |
| 24 virtual bool PostNonNestableDelayedTask( |
| 25 const tracked_objects::Location& from_here, |
| 26 const base::Closure& task, |
| 27 base::TimeDelta delay) override; |
| 28 |
| 29 private: |
| 30 virtual ~TaskRunner(); |
| 31 |
| 32 base::WeakPtr<TaskQueueManager> task_queue_manager_; |
| 33 const size_t queue_index_; |
| 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(TaskRunner); |
| 36 }; |
| 37 |
| 38 TaskRunner::TaskRunner(base::WeakPtr<TaskQueueManager> task_queue_manager, |
| 39 size_t queue_index) |
| 40 : task_queue_manager_(task_queue_manager), queue_index_(queue_index) { |
| 41 } |
| 42 |
| 43 TaskRunner::~TaskRunner() { |
| 44 } |
| 45 |
| 46 bool TaskRunner::RunsTasksOnCurrentThread() const { |
| 47 if (!task_queue_manager_) |
| 48 return false; |
| 49 return task_queue_manager_->RunsTasksOnCurrentThread(); |
| 50 } |
| 51 |
| 52 bool TaskRunner::PostDelayedTask(const tracked_objects::Location& from_here, |
| 53 const base::Closure& task, |
| 54 base::TimeDelta delay) { |
| 55 if (!task_queue_manager_) |
| 56 return false; |
| 57 return task_queue_manager_->PostDelayedTask( |
| 58 queue_index_, from_here, task, delay); |
| 59 } |
| 60 |
| 61 bool TaskRunner::PostNonNestableDelayedTask( |
| 62 const tracked_objects::Location& from_here, |
| 63 const base::Closure& task, |
| 64 base::TimeDelta delay) { |
| 65 if (!task_queue_manager_) |
| 66 return false; |
| 67 return task_queue_manager_->PostNonNestableDelayedTask( |
| 68 queue_index_, from_here, task, delay); |
| 69 } |
| 70 |
| 71 struct TaskQueue { |
| 72 TaskQueue() : auto_pump(true) {} |
| 73 ~TaskQueue() {} |
| 74 |
| 75 scoped_refptr<TaskRunner> task_runner; |
| 76 |
| 77 base::Lock incoming_queue_lock; |
| 78 base::TaskQueue incoming_queue; |
| 79 |
| 80 bool auto_pump; |
| 81 base::TaskQueue work_queue; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 TaskQueueManager::TaskQueueManager( |
| 89 size_t task_queue_count, |
| 90 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 91 TaskQueueSelector* selector) |
| 92 : main_task_runner_(main_task_runner), |
| 93 selector_(selector), |
| 94 weak_factory_(this) { |
| 95 DCHECK(main_task_runner->RunsTasksOnCurrentThread()); |
| 96 |
| 97 for (size_t i = 0; i < task_queue_count; i++) { |
| 98 scoped_ptr<internal::TaskQueue> queue(new internal::TaskQueue()); |
| 99 queue->task_runner = make_scoped_refptr( |
| 100 new internal::TaskRunner(weak_factory_.GetWeakPtr(), i)); |
| 101 queues_.push_back(queue.release()); |
| 102 } |
| 103 |
| 104 std::vector<const base::TaskQueue*> work_queues; |
| 105 for (const auto& queue: queues_) |
| 106 work_queues.push_back(&queue->work_queue); |
| 107 selector_->RegisterWorkQueues(work_queues); |
| 108 } |
| 109 |
| 110 TaskQueueManager::~TaskQueueManager() { |
| 111 } |
| 112 |
| 113 internal::TaskQueue* TaskQueueManager::Queue(size_t queue_index) const { |
| 114 DCHECK_LT(queue_index, queues_.size()); |
| 115 return queues_[queue_index]; |
| 116 } |
| 117 |
| 118 scoped_refptr<base::SingleThreadTaskRunner> |
| 119 TaskQueueManager::TaskRunnerForQueue(size_t queue_index) const { |
| 120 return Queue(queue_index)->task_runner; |
| 121 } |
| 122 |
| 123 bool TaskQueueManager::IsQueueEmpty(size_t queue_index) { |
| 124 internal::TaskQueue* queue = Queue(queue_index); |
| 125 if (!queue->work_queue.empty()) |
| 126 return false; |
| 127 base::AutoLock lock(queue->incoming_queue_lock); |
| 128 return queue->incoming_queue.empty(); |
| 129 } |
| 130 |
| 131 void TaskQueueManager::EnqueueTask(size_t queue_index, |
| 132 const base::PendingTask& pending_task) { |
| 133 internal::TaskQueue* queue = Queue(queue_index); |
| 134 base::AutoLock lock(queue->incoming_queue_lock); |
| 135 if (queue->auto_pump && queue->incoming_queue.empty()) |
| 136 PostDoWorkOnMainRunner(); |
| 137 queue->incoming_queue.push(pending_task); |
| 138 } |
| 139 |
| 140 void TaskQueueManager::SetAutoPump(size_t queue_index, bool auto_pump) { |
| 141 internal::TaskQueue* queue = Queue(queue_index); |
| 142 base::AutoLock lock(queue->incoming_queue_lock); |
| 143 if (auto_pump) { |
| 144 queue->auto_pump = true; |
| 145 PumpQueueLocked(queue); |
| 146 } else { |
| 147 queue->auto_pump = false; |
| 148 } |
| 149 } |
| 150 |
| 151 void TaskQueueManager::PumpQueueLocked(internal::TaskQueue* queue) { |
| 152 main_thread_checker_.CalledOnValidThread(); |
| 153 queue->incoming_queue_lock.AssertAcquired(); |
| 154 while (!queue->incoming_queue.empty()) { |
| 155 queue->work_queue.push(queue->incoming_queue.front()); |
| 156 queue->incoming_queue.pop(); |
| 157 } |
| 158 if (!queue->work_queue.empty()) |
| 159 PostDoWorkOnMainRunner(); |
| 160 } |
| 161 |
| 162 void TaskQueueManager::PumpQueue(size_t queue_index) { |
| 163 main_thread_checker_.CalledOnValidThread(); |
| 164 internal::TaskQueue* queue = Queue(queue_index); |
| 165 base::AutoLock lock(queue->incoming_queue_lock); |
| 166 PumpQueueLocked(queue); |
| 167 } |
| 168 |
| 169 bool TaskQueueManager::UpdateWorkQueues() { |
| 170 // TODO(skyostil): This is not efficient when the number of queues grows very |
| 171 // large due to the number of locks taken. Consider optimizing when we get |
| 172 // there. |
| 173 main_thread_checker_.CalledOnValidThread(); |
| 174 bool has_work = false; |
| 175 for (auto& queue: queues_) { |
| 176 if (!queue->work_queue.empty()) { |
| 177 has_work = true; |
| 178 continue; |
| 179 } |
| 180 base::AutoLock lock(queue->incoming_queue_lock); |
| 181 if (!queue->auto_pump || queue->incoming_queue.empty()) |
| 182 continue; |
| 183 queue->work_queue.Swap(&queue->incoming_queue); |
| 184 has_work = true; |
| 185 } |
| 186 return has_work; |
| 187 } |
| 188 |
| 189 void TaskQueueManager::PostDoWorkOnMainRunner() { |
| 190 main_task_runner_->PostTask( |
| 191 FROM_HERE, Bind(&TaskQueueManager::DoWork, weak_factory_.GetWeakPtr())); |
| 192 } |
| 193 |
| 194 void TaskQueueManager::DoWork() { |
| 195 main_thread_checker_.CalledOnValidThread(); |
| 196 if (!UpdateWorkQueues()) |
| 197 return; |
| 198 |
| 199 size_t queue_index; |
| 200 if (!selector_->SelectWorkQueueToService(&queue_index)) |
| 201 return; |
| 202 PostDoWorkOnMainRunner(); |
| 203 RunTaskFromWorkQueue(queue_index); |
| 204 } |
| 205 |
| 206 void TaskQueueManager::RunTaskFromWorkQueue(size_t queue_index) { |
| 207 main_thread_checker_.CalledOnValidThread(); |
| 208 internal::TaskQueue* queue = Queue(queue_index); |
| 209 DCHECK(!queue->work_queue.empty()); |
| 210 base::PendingTask pending_task = queue->work_queue.front(); |
| 211 queue->work_queue.pop(); |
| 212 task_annotator_.RunTask( |
| 213 "TaskQueueManager::PostTask", "TaskQueueManager::RunTask", pending_task); |
| 214 } |
| 215 |
| 216 bool TaskQueueManager::RunsTasksOnCurrentThread() const { |
| 217 return main_task_runner_->RunsTasksOnCurrentThread(); |
| 218 } |
| 219 |
| 220 bool TaskQueueManager::PostDelayedTask( |
| 221 size_t queue_index, |
| 222 const tracked_objects::Location& from_here, |
| 223 const base::Closure& task, |
| 224 base::TimeDelta delay) { |
| 225 int sequence_num = task_sequence_num_.GetNext(); |
| 226 |
| 227 base::PendingTask pending_task(from_here, task); |
| 228 pending_task.sequence_num = sequence_num; |
| 229 |
| 230 task_annotator_.DidQueueTask("TaskQueueManager::PostTask", pending_task); |
| 231 if (delay > base::TimeDelta()) { |
| 232 return main_task_runner_->PostDelayedTask( |
| 233 from_here, |
| 234 Bind(&TaskQueueManager::EnqueueTask, |
| 235 weak_factory_.GetWeakPtr(), |
| 236 queue_index, |
| 237 pending_task), |
| 238 delay); |
| 239 } |
| 240 EnqueueTask(queue_index, pending_task); |
| 241 return true; |
| 242 } |
| 243 |
| 244 bool TaskQueueManager::PostNonNestableDelayedTask( |
| 245 size_t queue_index, |
| 246 const tracked_objects::Location& from_here, |
| 247 const base::Closure& task, |
| 248 base::TimeDelta delay) { |
| 249 // Defer non-nestable work to the main task runner. |
| 250 return main_task_runner_->PostNonNestableDelayedTask(from_here, task, delay); |
| 251 } |
| 252 |
| 253 } // namespace content |
OLD | NEW |