| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
| 11 #include "base/threading/simple_thread.h" | 11 #include "base/threading/simple_thread.h" |
| 12 #include "cc/rendering_stats.h" | |
| 13 | 12 |
| 14 #if defined(OS_ANDROID) | 13 #if defined(OS_ANDROID) |
| 15 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) | 14 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) |
| 16 #include <sys/resource.h> | 15 #include <sys/resource.h> |
| 17 #endif | 16 #endif |
| 18 | 17 |
| 19 namespace cc { | 18 namespace cc { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 class WorkerPoolTaskImpl : public internal::WorkerPoolTask { | 22 class WorkerPoolTaskImpl : public internal::WorkerPoolTask { |
| 24 public: | 23 public: |
| 25 WorkerPoolTaskImpl(const WorkerPool::Callback& task, | 24 WorkerPoolTaskImpl(const WorkerPool::Callback& task, |
| 26 const base::Closure& reply) | 25 const base::Closure& reply) |
| 27 : internal::WorkerPoolTask(reply), | 26 : internal::WorkerPoolTask(reply), |
| 28 task_(task) {} | 27 task_(task) {} |
| 29 | 28 |
| 30 virtual bool IsCheap() OVERRIDE { return false; } | 29 virtual bool IsCheap() OVERRIDE { return false; } |
| 31 | 30 |
| 32 virtual void WillRunOnThread(unsigned thread_index) OVERRIDE {} | 31 virtual void WillRunOnThread(unsigned thread_index) OVERRIDE {} |
| 33 | 32 |
| 34 virtual void Run(RenderingStats* rendering_stats) OVERRIDE { | 33 virtual void Run() OVERRIDE { |
| 35 task_.Run(rendering_stats); | 34 task_.Run(); |
| 36 } | 35 } |
| 37 | 36 |
| 38 private: | 37 private: |
| 39 WorkerPool::Callback task_; | 38 WorkerPool::Callback task_; |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 } // namespace | 41 } // namespace |
| 43 | 42 |
| 44 namespace internal { | 43 namespace internal { |
| 45 | 44 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 61 class WorkerPool::Inner : public base::DelegateSimpleThread::Delegate { | 60 class WorkerPool::Inner : public base::DelegateSimpleThread::Delegate { |
| 62 public: | 61 public: |
| 63 Inner(WorkerPool* worker_pool, | 62 Inner(WorkerPool* worker_pool, |
| 64 size_t num_threads, | 63 size_t num_threads, |
| 65 const std::string& thread_name_prefix, | 64 const std::string& thread_name_prefix, |
| 66 bool need_on_task_completed_callback); | 65 bool need_on_task_completed_callback); |
| 67 ~Inner(); | 66 ~Inner(); |
| 68 | 67 |
| 69 void Shutdown(); | 68 void Shutdown(); |
| 70 | 69 |
| 71 void SetRecordRenderingStats(bool record_rendering_stats); | |
| 72 | |
| 73 void GetRenderingStats(RenderingStats* stats); | |
| 74 | |
| 75 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 70 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); |
| 76 | 71 |
| 77 // Appends all completed tasks to worker pool's completed tasks queue | 72 // Appends all completed tasks to worker pool's completed tasks queue |
| 78 // and returns true if idle. | 73 // and returns true if idle. |
| 79 bool CollectCompletedTasks(); | 74 bool CollectCompletedTasks(); |
| 80 | 75 |
| 81 // Runs cheap tasks on caller thread until |time_limit| is reached | 76 // Runs cheap tasks on caller thread until |time_limit| is reached |
| 82 // and returns true if idle. | 77 // and returns true if idle. |
| 83 bool RunCheapTasksUntilTimeLimit(base::TimeTicks time_limit); | 78 bool RunCheapTasksUntilTimeLimit(base::TimeTicks time_limit); |
| 84 | 79 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 unsigned running_task_count_; | 134 unsigned running_task_count_; |
| 140 | 135 |
| 141 // Set during shutdown. Tells workers to exit when no more tasks | 136 // Set during shutdown. Tells workers to exit when no more tasks |
| 142 // are pending. | 137 // are pending. |
| 143 bool shutdown_; | 138 bool shutdown_; |
| 144 | 139 |
| 145 typedef ScopedPtrDeque<internal::WorkerPoolTask> TaskDeque; | 140 typedef ScopedPtrDeque<internal::WorkerPoolTask> TaskDeque; |
| 146 TaskDeque pending_tasks_; | 141 TaskDeque pending_tasks_; |
| 147 TaskDeque completed_tasks_; | 142 TaskDeque completed_tasks_; |
| 148 | 143 |
| 149 scoped_ptr<RenderingStats> rendering_stats_; | |
| 150 | |
| 151 ScopedPtrDeque<base::DelegateSimpleThread> workers_; | 144 ScopedPtrDeque<base::DelegateSimpleThread> workers_; |
| 152 | 145 |
| 153 DISALLOW_COPY_AND_ASSIGN(Inner); | 146 DISALLOW_COPY_AND_ASSIGN(Inner); |
| 154 }; | 147 }; |
| 155 | 148 |
| 156 WorkerPool::Inner::Inner(WorkerPool* worker_pool, | 149 WorkerPool::Inner::Inner(WorkerPool* worker_pool, |
| 157 size_t num_threads, | 150 size_t num_threads, |
| 158 const std::string& thread_name_prefix, | 151 const std::string& thread_name_prefix, |
| 159 bool need_on_task_completed_callback) | 152 bool need_on_task_completed_callback) |
| 160 : worker_pool_on_origin_thread_(worker_pool), | 153 : worker_pool_on_origin_thread_(worker_pool), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 // to exit as each will wake up another worker before exiting. | 203 // to exit as each will wake up another worker before exiting. |
| 211 has_pending_tasks_cv_.Signal(); | 204 has_pending_tasks_cv_.Signal(); |
| 212 } | 205 } |
| 213 | 206 |
| 214 while (workers_.size()) { | 207 while (workers_.size()) { |
| 215 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front(); | 208 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front(); |
| 216 worker->Join(); | 209 worker->Join(); |
| 217 } | 210 } |
| 218 } | 211 } |
| 219 | 212 |
| 220 void WorkerPool::Inner::SetRecordRenderingStats(bool record_rendering_stats) { | |
| 221 base::AutoLock lock(lock_); | |
| 222 | |
| 223 if (record_rendering_stats) | |
| 224 rendering_stats_.reset(new RenderingStats); | |
| 225 else | |
| 226 rendering_stats_.reset(); | |
| 227 } | |
| 228 | |
| 229 void WorkerPool::Inner::GetRenderingStats(RenderingStats* stats) { | |
| 230 base::AutoLock lock(lock_); | |
| 231 | |
| 232 if (rendering_stats_) | |
| 233 stats->Add(*rendering_stats_); | |
| 234 } | |
| 235 | |
| 236 void WorkerPool::Inner::PostTask(scoped_ptr<internal::WorkerPoolTask> task) { | 213 void WorkerPool::Inner::PostTask(scoped_ptr<internal::WorkerPoolTask> task) { |
| 237 base::AutoLock lock(lock_); | 214 base::AutoLock lock(lock_); |
| 238 | 215 |
| 239 pending_tasks_.push_back(task.Pass()); | 216 pending_tasks_.push_back(task.Pass()); |
| 240 | 217 |
| 241 // There is more work available, so wake up worker thread. | 218 // There is more work available, so wake up worker thread. |
| 242 has_pending_tasks_cv_.Signal(); | 219 has_pending_tasks_cv_.Signal(); |
| 243 } | 220 } |
| 244 | 221 |
| 245 bool WorkerPool::Inner::CollectCompletedTasks() { | 222 bool WorkerPool::Inner::CollectCompletedTasks() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 267 | 244 |
| 268 if (!task) { | 245 if (!task) { |
| 269 // Schedule an idle callback if requested and not pending. | 246 // Schedule an idle callback if requested and not pending. |
| 270 if (!running_task_count_ && pending_tasks_.empty()) | 247 if (!running_task_count_ && pending_tasks_.empty()) |
| 271 ScheduleOnIdleWithLockAcquired(); | 248 ScheduleOnIdleWithLockAcquired(); |
| 272 | 249 |
| 273 // Exit when no more cheap tasks are pending. | 250 // Exit when no more cheap tasks are pending. |
| 274 break; | 251 break; |
| 275 } | 252 } |
| 276 | 253 |
| 277 scoped_ptr<RenderingStats> rendering_stats; | |
| 278 // Collect rendering stats if |rendering_stats_| is set. | |
| 279 if (rendering_stats_) | |
| 280 rendering_stats = make_scoped_ptr(new RenderingStats); | |
| 281 | |
| 282 // Increment |running_task_count_| before starting to run task. | 254 // Increment |running_task_count_| before starting to run task. |
| 283 running_task_count_++; | 255 running_task_count_++; |
| 284 | 256 |
| 285 { | 257 { |
| 286 base::AutoUnlock unlock(lock_); | 258 base::AutoUnlock unlock(lock_); |
| 287 | 259 |
| 288 task->Run(rendering_stats.get()); | 260 task->Run(); |
| 289 | 261 |
| 290 // Append tasks directly to worker pool's completed tasks queue. | 262 // Append tasks directly to worker pool's completed tasks queue. |
| 291 worker_pool_on_origin_thread_->completed_tasks_.push_back(task.Pass()); | 263 worker_pool_on_origin_thread_->completed_tasks_.push_back(task.Pass()); |
| 292 if (need_on_task_completed_callback_) | 264 if (need_on_task_completed_callback_) |
| 293 worker_pool_on_origin_thread_->OnTaskCompleted(); | 265 worker_pool_on_origin_thread_->OnTaskCompleted(); |
| 294 } | 266 } |
| 295 | 267 |
| 296 // Add rendering stat results to |rendering_stats_|. | |
| 297 if (rendering_stats && rendering_stats_) | |
| 298 rendering_stats_->Add(*rendering_stats); | |
| 299 | |
| 300 // Decrement |running_task_count_| now that we are done running task. | 268 // Decrement |running_task_count_| now that we are done running task. |
| 301 running_task_count_--; | 269 running_task_count_--; |
| 302 } | 270 } |
| 303 | 271 |
| 304 // Append any other completed tasks before releasing lock. | 272 // Append any other completed tasks before releasing lock. |
| 305 return AppendCompletedTasksWithLockAcquired( | 273 return AppendCompletedTasksWithLockAcquired( |
| 306 &worker_pool_on_origin_thread_->completed_tasks_); | 274 &worker_pool_on_origin_thread_->completed_tasks_); |
| 307 } | 275 } |
| 308 | 276 |
| 309 bool WorkerPool::Inner::AppendCompletedTasksWithLockAcquired( | 277 bool WorkerPool::Inner::AppendCompletedTasksWithLockAcquired( |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 ScheduleOnIdleWithLockAcquired(); | 358 ScheduleOnIdleWithLockAcquired(); |
| 391 | 359 |
| 392 // Wait for new pending tasks. | 360 // Wait for new pending tasks. |
| 393 has_pending_tasks_cv_.Wait(); | 361 has_pending_tasks_cv_.Wait(); |
| 394 continue; | 362 continue; |
| 395 } | 363 } |
| 396 | 364 |
| 397 // Get next task. | 365 // Get next task. |
| 398 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); | 366 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); |
| 399 | 367 |
| 400 scoped_ptr<RenderingStats> rendering_stats; | |
| 401 // Collect rendering stats if |rendering_stats_| is set. | |
| 402 if (rendering_stats_) | |
| 403 rendering_stats = make_scoped_ptr(new RenderingStats); | |
| 404 | |
| 405 // Increment |running_task_count_| before starting to run task. | 368 // Increment |running_task_count_| before starting to run task. |
| 406 running_task_count_++; | 369 running_task_count_++; |
| 407 | 370 |
| 408 // There may be more work available, so wake up another | 371 // There may be more work available, so wake up another |
| 409 // worker thread. | 372 // worker thread. |
| 410 has_pending_tasks_cv_.Signal(); | 373 has_pending_tasks_cv_.Signal(); |
| 411 | 374 |
| 412 { | 375 { |
| 413 base::AutoUnlock unlock(lock_); | 376 base::AutoUnlock unlock(lock_); |
| 414 | 377 |
| 415 task->WillRunOnThread(thread_index); | 378 task->WillRunOnThread(thread_index); |
| 416 task->Run(rendering_stats.get()); | 379 task->Run(); |
| 417 } | 380 } |
| 418 | 381 |
| 419 completed_tasks_.push_back(task.Pass()); | 382 completed_tasks_.push_back(task.Pass()); |
| 420 | 383 |
| 421 // Add rendering stat results to |rendering_stats_|. | |
| 422 if (rendering_stats && rendering_stats_) | |
| 423 rendering_stats_->Add(*rendering_stats); | |
| 424 | |
| 425 // Decrement |running_task_count_| now that we are done running task. | 384 // Decrement |running_task_count_| now that we are done running task. |
| 426 running_task_count_--; | 385 running_task_count_--; |
| 427 | 386 |
| 428 // Schedule a task completed callback if requested and not pending. | 387 // Schedule a task completed callback if requested and not pending. |
| 429 ScheduleOnTaskCompletedWithLockAcquired(); | 388 ScheduleOnTaskCompletedWithLockAcquired(); |
| 430 } | 389 } |
| 431 | 390 |
| 432 // We noticed we should exit. Wake up the next worker so it knows it should | 391 // We noticed we should exit. Wake up the next worker so it knows it should |
| 433 // exit as well (because the Shutdown() code only signals once). | 392 // exit as well (because the Shutdown() code only signals once). |
| 434 has_pending_tasks_cv_.Signal(); | 393 has_pending_tasks_cv_.Signal(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 task, | 437 task, |
| 479 reply)).PassAs<internal::WorkerPoolTask>()); | 438 reply)).PassAs<internal::WorkerPoolTask>()); |
| 480 } | 439 } |
| 481 | 440 |
| 482 void WorkerPool::SetRunCheapTasksTimeLimit( | 441 void WorkerPool::SetRunCheapTasksTimeLimit( |
| 483 base::TimeTicks run_cheap_tasks_time_limit) { | 442 base::TimeTicks run_cheap_tasks_time_limit) { |
| 484 run_cheap_tasks_time_limit_ = run_cheap_tasks_time_limit; | 443 run_cheap_tasks_time_limit_ = run_cheap_tasks_time_limit; |
| 485 ScheduleRunCheapTasks(); | 444 ScheduleRunCheapTasks(); |
| 486 } | 445 } |
| 487 | 446 |
| 488 void WorkerPool::SetRecordRenderingStats(bool record_rendering_stats) { | |
| 489 inner_->SetRecordRenderingStats(record_rendering_stats); | |
| 490 } | |
| 491 | |
| 492 void WorkerPool::GetRenderingStats(RenderingStats* stats) { | |
| 493 inner_->GetRenderingStats(stats); | |
| 494 } | |
| 495 | |
| 496 void WorkerPool::OnIdle() { | 447 void WorkerPool::OnIdle() { |
| 497 TRACE_EVENT0("cc", "WorkerPool::OnIdle"); | 448 TRACE_EVENT0("cc", "WorkerPool::OnIdle"); |
| 498 | 449 |
| 499 DispatchCompletionCallbacks(); | 450 DispatchCompletionCallbacks(); |
| 500 } | 451 } |
| 501 | 452 |
| 502 void WorkerPool::OnTaskCompleted() { | 453 void WorkerPool::OnTaskCompleted() { |
| 503 TRACE_EVENT0("cc", "WorkerPool::OnTaskCompleted"); | 454 TRACE_EVENT0("cc", "WorkerPool::OnTaskCompleted"); |
| 504 | 455 |
| 505 DispatchCompletionCallbacks(); | 456 DispatchCompletionCallbacks(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks check time"); | 549 TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks check time"); |
| 599 CancelCheckForCompletedTasks(); | 550 CancelCheckForCompletedTasks(); |
| 600 DispatchCompletionCallbacks(); | 551 DispatchCompletionCallbacks(); |
| 601 // Schedule another check for completed tasks if not idle. | 552 // Schedule another check for completed tasks if not idle. |
| 602 if (!is_idle) | 553 if (!is_idle) |
| 603 ScheduleCheckForCompletedTasks(); | 554 ScheduleCheckForCompletedTasks(); |
| 604 } | 555 } |
| 605 } | 556 } |
| 606 | 557 |
| 607 } // namespace cc | 558 } // namespace cc |
| OLD | NEW |