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