Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "cc/worker_pool.h" | 5 #include "cc/worker_pool.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 class WorkerPoolTaskImpl : public internal::WorkerPoolTask { | 22 class WorkerPoolTaskImpl : public internal::WorkerPoolTask { |
| 23 public: | 23 public: |
| 24 WorkerPoolTaskImpl(const WorkerPool::Callback& task, | 24 WorkerPoolTaskImpl(const WorkerPool::Callback& task, |
| 25 const base::Closure& reply) | 25 const base::Closure& reply) |
| 26 : internal::WorkerPoolTask(reply), | 26 : internal::WorkerPoolTask(reply), |
| 27 task_(task) {} | 27 task_(task) {} |
| 28 | 28 |
| 29 virtual void Run(RenderingStats* rendering_stats) OVERRIDE { | 29 virtual void Run(RenderingStats* rendering_stats) OVERRIDE { |
| 30 task_.Run(rendering_stats); | 30 task_.Run(rendering_stats); |
| 31 base::subtle::Release_Store(&completed_, 1); | |
| 31 } | 32 } |
| 32 | 33 |
| 33 private: | 34 private: |
| 34 WorkerPool::Callback task_; | 35 WorkerPool::Callback task_; |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 const char* kWorkerThreadNamePrefix = "Compositor"; | 38 const char* kWorkerThreadNamePrefix = "Compositor"; |
| 38 | 39 |
| 39 // Allow two pending tasks per worker. This keeps resource usage | |
| 40 // low while making sure workers aren't unnecessarily idle. | |
| 41 const int kNumPendingTasksPerWorker = 2; | |
| 42 | |
| 43 } // namespace | 40 } // namespace |
| 44 | 41 |
| 45 namespace internal { | 42 namespace internal { |
| 46 | 43 |
| 47 WorkerPoolTask::WorkerPoolTask(const base::Closure& reply) | 44 WorkerPoolTask::WorkerPoolTask(const base::Closure& reply) |
| 48 : reply_(reply) { | 45 : reply_(reply) { |
| 46 base::subtle::Acquire_Store(&completed_, 0); | |
| 49 } | 47 } |
| 50 | 48 |
| 51 WorkerPoolTask::~WorkerPoolTask() { | 49 WorkerPoolTask::~WorkerPoolTask() { |
| 52 } | 50 } |
| 53 | 51 |
| 52 bool WorkerPoolTask::IsPending() { | |
| 53 return base::subtle::Acquire_Load(&completed_) == 0; | |
| 54 } | |
| 55 | |
| 54 void WorkerPoolTask::Completed() { | 56 void WorkerPoolTask::Completed() { |
| 57 DCHECK_EQ(base::subtle::Acquire_Load(&completed_), 1); | |
| 55 reply_.Run(); | 58 reply_.Run(); |
| 56 } | 59 } |
| 57 | 60 |
| 58 } // namespace internal | 61 } // namespace internal |
| 59 | 62 |
| 60 WorkerPool::Worker::Worker(WorkerPool* worker_pool, const std::string name) | 63 WorkerPool::Worker::Worker(WorkerPool* worker_pool, const std::string name) |
| 61 : base::Thread(name.c_str()), | 64 : base::Thread(name.c_str()), |
| 62 worker_pool_(worker_pool), | 65 worker_pool_(worker_pool), |
| 63 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 64 rendering_stats_(make_scoped_ptr(new RenderingStats)), | 66 rendering_stats_(make_scoped_ptr(new RenderingStats)), |
| 65 record_rendering_stats_(false) { | 67 record_rendering_stats_(false) { |
| 66 Start(); | 68 Start(); |
| 67 DCHECK(IsRunning()); | 69 DCHECK(IsRunning()); |
| 68 } | 70 } |
| 69 | 71 |
| 70 WorkerPool::Worker::~Worker() { | 72 WorkerPool::Worker::~Worker() { |
| 71 DCHECK(!IsRunning()); | 73 DCHECK(!IsRunning()); |
| 72 DCHECK_EQ(pending_tasks_.size(), 0); | 74 DCHECK_EQ(pending_tasks_.size(), 0); |
| 73 } | 75 } |
| 74 | 76 |
| 75 void WorkerPool::Worker::StopAfterCompletingAllPendingTasks() { | 77 void WorkerPool::Worker::StopAfterCompletingAllPendingTasks() { |
| 76 // Signals the thread to exit and returns once all pending tasks have run. | 78 // Signals the thread to exit and returns once all pending tasks have run. |
| 77 Stop(); | 79 Stop(); |
| 78 | 80 |
| 79 // Complete all pending tasks. The Stop() call above guarantees that | 81 // Complete all pending tasks. The Stop() call above guarantees that |
| 80 // all tasks have finished running. | 82 // all tasks have finished running. |
| 81 while (!pending_tasks_.empty()) | 83 while (!pending_tasks_.empty()) |
| 82 OnTaskCompleted(); | 84 OnTaskCompleted(); |
| 83 | |
| 84 // Cancel all pending replies. | |
| 85 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 86 } | 85 } |
| 87 | 86 |
| 88 void WorkerPool::Worker::PostTask(scoped_ptr<internal::WorkerPoolTask> task) { | 87 void WorkerPool::Worker::PostTask(scoped_ptr<internal::WorkerPoolTask> task) { |
| 89 DCHECK_LT(num_pending_tasks(), kNumPendingTasksPerWorker); | |
| 90 | |
| 91 RenderingStats* stats = | 88 RenderingStats* stats = |
| 92 record_rendering_stats_ ? rendering_stats_.get() : NULL; | 89 record_rendering_stats_ ? rendering_stats_.get() : NULL; |
| 93 | 90 |
| 94 message_loop_proxy()->PostTaskAndReply( | 91 worker_pool_->WillPostMoreWork(); |
| 92 | |
| 93 message_loop_proxy()->PostTask( | |
| 95 FROM_HERE, | 94 FROM_HERE, |
| 96 base::Bind(&Worker::RunTask, | 95 base::Bind(&Worker::RunTask, |
| 97 base::Unretained(task.get()), | 96 base::Unretained(task.get()), |
| 98 base::Unretained(stats)), | 97 base::Unretained(worker_pool_), |
| 99 base::Bind(&Worker::OnTaskCompleted, weak_ptr_factory_.GetWeakPtr())); | 98 base::Unretained(stats))); |
| 100 | 99 |
| 101 pending_tasks_.push_back(task.Pass()); | 100 pending_tasks_.push_back(task.Pass()); |
| 102 | 101 |
| 103 worker_pool_->DidNumPendingTasksChange(); | 102 worker_pool_->DidNumPendingTasksChange(); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void WorkerPool::Worker::Init() { | 105 void WorkerPool::Worker::Init() { |
| 107 #if defined(OS_ANDROID) | 106 #if defined(OS_ANDROID) |
| 108 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) | 107 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) |
| 109 int nice_value = 10; // Idle priority. | 108 int nice_value = 10; // Idle priority. |
| 110 setpriority(PRIO_PROCESS, base::PlatformThread::CurrentId(), nice_value); | 109 setpriority(PRIO_PROCESS, base::PlatformThread::CurrentId(), nice_value); |
| 111 #endif | 110 #endif |
| 112 } | 111 } |
| 113 | 112 |
| 114 // static | 113 // static |
| 115 void WorkerPool::Worker::RunTask( | 114 void WorkerPool::Worker::RunTask( |
| 116 internal::WorkerPoolTask* task, RenderingStats* rendering_stats) { | 115 internal::WorkerPoolTask* task, |
| 116 WorkerPool* worker_pool, | |
| 117 RenderingStats* rendering_stats) { | |
| 117 task->Run(rendering_stats); | 118 task->Run(rendering_stats); |
| 119 worker_pool->OnWorkCompleted(); | |
|
nduca
2013/02/13 01:33:18
Use suffix to OnWorkCompletedOnWorkerThread
reveman
2013/02/13 08:12:51
Done.
| |
| 118 } | 120 } |
| 119 | 121 |
| 120 void WorkerPool::Worker::OnTaskCompleted() { | 122 void WorkerPool::Worker::OnTaskCompleted() { |
| 121 CHECK(!pending_tasks_.empty()); | 123 CHECK(!pending_tasks_.empty()); |
| 122 | 124 |
| 123 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); | 125 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); |
| 124 task->Completed(); | 126 task->Completed(); |
| 125 | 127 |
| 126 worker_pool_->DidNumPendingTasksChange(); | 128 worker_pool_->DidNumPendingTasksChange(); |
| 127 } | 129 } |
| 128 | 130 |
| 129 WorkerPool::WorkerPool(size_t num_threads) | 131 void WorkerPool::Worker::CheckForCompletedTasks() { |
| 130 : workers_need_sorting_(false), | 132 while (!pending_tasks_.empty()) { |
| 133 if (pending_tasks_.front()->IsPending()) | |
| 134 return; | |
| 135 | |
| 136 OnTaskCompleted(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 WorkerPool::WorkerPool(WorkerPoolClient* client, size_t num_threads) | |
| 141 : client_(client), | |
| 142 origin_loop_(base::MessageLoopProxy::current()), | |
| 143 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 144 workers_need_sorting_(false), | |
| 131 shutdown_(false) { | 145 shutdown_(false) { |
| 132 const std::string thread_name_prefix = kWorkerThreadNamePrefix; | 146 const std::string thread_name_prefix = kWorkerThreadNamePrefix; |
| 133 while (workers_.size() < num_threads) { | 147 while (workers_.size() < num_threads) { |
| 134 int thread_number = workers_.size() + 1; | 148 int thread_number = workers_.size() + 1; |
| 135 workers_.push_back(new Worker( | 149 workers_.push_back(new Worker( |
| 136 this, | 150 this, |
| 137 thread_name_prefix + StringPrintf("Worker%d", thread_number).c_str())); | 151 thread_name_prefix + StringPrintf("Worker%d", thread_number).c_str())); |
| 138 } | 152 } |
| 153 base::subtle::Acquire_Store(&work_count_, 0); | |
| 139 } | 154 } |
| 140 | 155 |
| 141 WorkerPool::~WorkerPool() { | 156 WorkerPool::~WorkerPool() { |
| 142 Shutdown(); | 157 Shutdown(); |
| 143 STLDeleteElements(&workers_); | 158 STLDeleteElements(&workers_); |
| 159 // Cancel any pending idle callback. | |
| 160 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 161 DCHECK_EQ(base::subtle::Acquire_Load(&work_count_), 0); | |
| 144 } | 162 } |
| 145 | 163 |
| 146 void WorkerPool::Shutdown() { | 164 void WorkerPool::Shutdown() { |
| 147 DCHECK(!shutdown_); | 165 DCHECK(!shutdown_); |
| 148 shutdown_ = true; | 166 shutdown_ = true; |
| 149 | 167 |
| 150 for (WorkerVector::iterator it = workers_.begin(); | 168 for (WorkerVector::iterator it = workers_.begin(); |
| 151 it != workers_.end(); it++) { | 169 it != workers_.end(); it++) { |
| 152 Worker* worker = *it; | 170 Worker* worker = *it; |
| 153 worker->StopAfterCompletingAllPendingTasks(); | 171 worker->StopAfterCompletingAllPendingTasks(); |
| 154 } | 172 } |
| 155 } | 173 } |
| 156 | 174 |
| 157 void WorkerPool::PostTaskAndReply( | 175 void WorkerPool::PostTaskAndReply( |
| 158 const Callback& task, const base::Closure& reply) { | 176 const Callback& task, const base::Closure& reply) { |
| 159 Worker* worker = GetWorkerForNextTask(); | 177 Worker* worker = GetWorkerForNextTask(); |
| 160 | 178 |
| 161 worker->PostTask( | 179 worker->PostTask( |
| 162 make_scoped_ptr(new WorkerPoolTaskImpl( | 180 make_scoped_ptr(new WorkerPoolTaskImpl( |
| 163 task, | 181 task, |
| 164 reply)).PassAs<internal::WorkerPoolTask>()); | 182 reply)).PassAs<internal::WorkerPoolTask>()); |
| 165 } | 183 } |
| 166 | 184 |
| 167 bool WorkerPool::IsBusy() { | 185 void WorkerPool::CheckForCompletedTasks() { |
| 168 Worker* worker = GetWorkerForNextTask(); | 186 for (WorkerVector::iterator it = workers_.begin(); |
| 169 | 187 it != workers_.end(); it++) { |
| 170 return worker->num_pending_tasks() >= kNumPendingTasksPerWorker; | 188 Worker* worker = *it; |
| 189 worker->CheckForCompletedTasks(); | |
| 190 } | |
| 171 } | 191 } |
| 172 | 192 |
| 173 void WorkerPool::SetRecordRenderingStats(bool record_rendering_stats) { | 193 void WorkerPool::SetRecordRenderingStats(bool record_rendering_stats) { |
| 174 for (WorkerVector::iterator it = workers_.begin(); | 194 for (WorkerVector::iterator it = workers_.begin(); |
| 175 it != workers_.end(); ++it) { | 195 it != workers_.end(); ++it) { |
| 176 Worker* worker = *it; | 196 Worker* worker = *it; |
| 177 worker->set_record_rendering_stats(record_rendering_stats); | 197 worker->set_record_rendering_stats(record_rendering_stats); |
| 178 } | 198 } |
| 179 } | 199 } |
| 180 | 200 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 197 worker->rendering_stats()->totalDeferredImageDecodeTime; | 217 worker->rendering_stats()->totalDeferredImageDecodeTime; |
| 198 } | 218 } |
| 199 } | 219 } |
| 200 | 220 |
| 201 WorkerPool::Worker* WorkerPool::GetWorkerForNextTask() { | 221 WorkerPool::Worker* WorkerPool::GetWorkerForNextTask() { |
| 202 CHECK(!shutdown_); | 222 CHECK(!shutdown_); |
| 203 SortWorkersIfNeeded(); | 223 SortWorkersIfNeeded(); |
| 204 return workers_.front(); | 224 return workers_.front(); |
| 205 } | 225 } |
| 206 | 226 |
| 227 void WorkerPool::WillPostMoreWork() { | |
| 228 // Instantiate new idle handler before pool becomes active. | |
| 229 if (base::subtle::Barrier_AtomicIncrement(&work_count_, 1) == 1) { | |
| 230 idle_handler_ = base::Bind(&WorkerPool::OnIdle, | |
|
brianderson
2013/02/13 01:19:57
I do not see where idle_handler_ is reset or set t
reveman
2013/02/13 08:12:51
latest patch initialize this once in the construct
| |
| 231 weak_ptr_factory_.GetWeakPtr()); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void WorkerPool::OnWorkCompleted() { | |
| 236 // Post idle handler task when pool work count reaches 0. | |
| 237 if (base::subtle::Barrier_AtomicIncrement(&work_count_, -1) == 0) { | |
| 238 origin_loop_->PostTask(FROM_HERE, idle_handler_); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 void WorkerPool::OnIdle() { | |
| 243 client_->OnIdle(); | |
| 244 } | |
| 245 | |
| 207 void WorkerPool::DidNumPendingTasksChange() { | 246 void WorkerPool::DidNumPendingTasksChange() { |
| 208 workers_need_sorting_ = true; | 247 workers_need_sorting_ = true; |
| 209 } | 248 } |
| 210 | 249 |
| 211 void WorkerPool::SortWorkersIfNeeded() { | 250 void WorkerPool::SortWorkersIfNeeded() { |
| 212 if (!workers_need_sorting_) | 251 if (!workers_need_sorting_) |
| 213 return; | 252 return; |
| 214 | 253 |
| 215 std::sort(workers_.begin(), workers_.end(), NumPendingTasksComparator()); | 254 std::sort(workers_.begin(), workers_.end(), NumPendingTasksComparator()); |
| 216 workers_need_sorting_ = false; | 255 workers_need_sorting_ = false; |
| 217 } | 256 } |
| 218 | 257 |
| 219 } // namespace cc | 258 } // namespace cc |
| OLD | NEW |