| 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/base/worker_pool.h" | 5 #include "cc/base/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/debug/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 virtual ~Inner(); | 68 virtual ~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, bool signal_workers); | 72 void PostTask(scoped_ptr<internal::WorkerPoolTask> task, bool signal_workers); |
| 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 bool signal_workers) { | 216 bool signal_workers) { |
| 241 base::AutoLock lock(lock_); | 217 base::AutoLock lock(lock_); |
| 242 | 218 |
| 243 pending_tasks_.push_back(task.Pass()); | 219 pending_tasks_.push_back(task.Pass()); |
| 244 | 220 |
| 245 // There is more work available, so wake up worker thread. | 221 // There is more work available, so wake up worker thread. |
| 246 if (signal_workers) | 222 if (signal_workers) |
| 247 has_pending_tasks_cv_.Signal(); | 223 has_pending_tasks_cv_.Signal(); |
| 248 } | 224 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 272 | 248 |
| 273 if (!task) { | 249 if (!task) { |
| 274 // Schedule an idle callback if requested and not pending. | 250 // Schedule an idle callback if requested and not pending. |
| 275 if (!running_task_count_ && pending_tasks_.empty()) | 251 if (!running_task_count_ && pending_tasks_.empty()) |
| 276 ScheduleOnIdleWithLockAcquired(); | 252 ScheduleOnIdleWithLockAcquired(); |
| 277 | 253 |
| 278 // Exit when no more cheap tasks are pending. | 254 // Exit when no more cheap tasks are pending. |
| 279 break; | 255 break; |
| 280 } | 256 } |
| 281 | 257 |
| 282 scoped_ptr<RenderingStats> rendering_stats; | |
| 283 // Collect rendering stats if |rendering_stats_| is set. | |
| 284 if (rendering_stats_) | |
| 285 rendering_stats = make_scoped_ptr(new RenderingStats); | |
| 286 | |
| 287 // Increment |running_task_count_| before starting to run task. | 258 // Increment |running_task_count_| before starting to run task. |
| 288 running_task_count_++; | 259 running_task_count_++; |
| 289 | 260 |
| 290 { | 261 { |
| 291 base::AutoUnlock unlock(lock_); | 262 base::AutoUnlock unlock(lock_); |
| 292 | 263 |
| 293 task->Run(rendering_stats.get()); | 264 task->Run(); |
| 294 | 265 |
| 295 // Append tasks directly to worker pool's completed tasks queue. | 266 // Append tasks directly to worker pool's completed tasks queue. |
| 296 worker_pool_on_origin_thread_->completed_tasks_.push_back(task.Pass()); | 267 worker_pool_on_origin_thread_->completed_tasks_.push_back(task.Pass()); |
| 297 if (need_on_task_completed_callback_) | 268 if (need_on_task_completed_callback_) |
| 298 worker_pool_on_origin_thread_->OnTaskCompleted(); | 269 worker_pool_on_origin_thread_->OnTaskCompleted(); |
| 299 } | 270 } |
| 300 | 271 |
| 301 // Add rendering stat results to |rendering_stats_|. | |
| 302 if (rendering_stats && rendering_stats_) | |
| 303 rendering_stats_->Add(*rendering_stats); | |
| 304 | |
| 305 // Decrement |running_task_count_| now that we are done running task. | 272 // Decrement |running_task_count_| now that we are done running task. |
| 306 running_task_count_--; | 273 running_task_count_--; |
| 307 } | 274 } |
| 308 | 275 |
| 309 if (!pending_tasks_.empty()) | 276 if (!pending_tasks_.empty()) |
| 310 has_pending_tasks_cv_.Signal(); | 277 has_pending_tasks_cv_.Signal(); |
| 311 | 278 |
| 312 // Append any other completed tasks before releasing lock. | 279 // Append any other completed tasks before releasing lock. |
| 313 return AppendCompletedTasksWithLockAcquired( | 280 return AppendCompletedTasksWithLockAcquired( |
| 314 &worker_pool_on_origin_thread_->completed_tasks_); | 281 &worker_pool_on_origin_thread_->completed_tasks_); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 ScheduleOnIdleWithLockAcquired(); | 365 ScheduleOnIdleWithLockAcquired(); |
| 399 | 366 |
| 400 // Wait for new pending tasks. | 367 // Wait for new pending tasks. |
| 401 has_pending_tasks_cv_.Wait(); | 368 has_pending_tasks_cv_.Wait(); |
| 402 continue; | 369 continue; |
| 403 } | 370 } |
| 404 | 371 |
| 405 // Get next task. | 372 // Get next task. |
| 406 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); | 373 scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front(); |
| 407 | 374 |
| 408 scoped_ptr<RenderingStats> rendering_stats; | |
| 409 // Collect rendering stats if |rendering_stats_| is set. | |
| 410 if (rendering_stats_) | |
| 411 rendering_stats = make_scoped_ptr(new RenderingStats); | |
| 412 | |
| 413 // Increment |running_task_count_| before starting to run task. | 375 // Increment |running_task_count_| before starting to run task. |
| 414 running_task_count_++; | 376 running_task_count_++; |
| 415 | 377 |
| 416 // There may be more work available, so wake up another | 378 // There may be more work available, so wake up another |
| 417 // worker thread. | 379 // worker thread. |
| 418 has_pending_tasks_cv_.Signal(); | 380 has_pending_tasks_cv_.Signal(); |
| 419 | 381 |
| 420 { | 382 { |
| 421 base::AutoUnlock unlock(lock_); | 383 base::AutoUnlock unlock(lock_); |
| 422 | 384 |
| 423 task->RunOnThread(rendering_stats.get(), thread_index); | 385 task->RunOnThread(thread_index); |
| 424 } | 386 } |
| 425 | 387 |
| 426 completed_tasks_.push_back(task.Pass()); | 388 completed_tasks_.push_back(task.Pass()); |
| 427 | 389 |
| 428 // Add rendering stat results to |rendering_stats_|. | |
| 429 if (rendering_stats && rendering_stats_) | |
| 430 rendering_stats_->Add(*rendering_stats); | |
| 431 | |
| 432 // Decrement |running_task_count_| now that we are done running task. | 390 // Decrement |running_task_count_| now that we are done running task. |
| 433 running_task_count_--; | 391 running_task_count_--; |
| 434 | 392 |
| 435 // Schedule a task completed callback if requested and not pending. | 393 // Schedule a task completed callback if requested and not pending. |
| 436 ScheduleOnTaskCompletedWithLockAcquired(); | 394 ScheduleOnTaskCompletedWithLockAcquired(); |
| 437 } | 395 } |
| 438 | 396 |
| 439 // We noticed we should exit. Wake up the next worker so it knows it should | 397 // We noticed we should exit. Wake up the next worker so it knows it should |
| 440 // exit as well (because the Shutdown() code only signals once). | 398 // exit as well (because the Shutdown() code only signals once). |
| 441 has_pending_tasks_cv_.Signal(); | 399 has_pending_tasks_cv_.Signal(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 task, | 443 task, |
| 486 reply)).PassAs<internal::WorkerPoolTask>()); | 444 reply)).PassAs<internal::WorkerPoolTask>()); |
| 487 } | 445 } |
| 488 | 446 |
| 489 void WorkerPool::SetRunCheapTasksTimeLimit( | 447 void WorkerPool::SetRunCheapTasksTimeLimit( |
| 490 base::TimeTicks run_cheap_tasks_time_limit) { | 448 base::TimeTicks run_cheap_tasks_time_limit) { |
| 491 run_cheap_tasks_time_limit_ = run_cheap_tasks_time_limit; | 449 run_cheap_tasks_time_limit_ = run_cheap_tasks_time_limit; |
| 492 ScheduleRunCheapTasks(); | 450 ScheduleRunCheapTasks(); |
| 493 } | 451 } |
| 494 | 452 |
| 495 void WorkerPool::SetRecordRenderingStats(bool record_rendering_stats) { | |
| 496 inner_->SetRecordRenderingStats(record_rendering_stats); | |
| 497 } | |
| 498 | |
| 499 void WorkerPool::GetRenderingStats(RenderingStats* stats) { | |
| 500 inner_->GetRenderingStats(stats); | |
| 501 } | |
| 502 | |
| 503 void WorkerPool::OnIdle() { | 453 void WorkerPool::OnIdle() { |
| 504 TRACE_EVENT0("cc", "WorkerPool::OnIdle"); | 454 TRACE_EVENT0("cc", "WorkerPool::OnIdle"); |
| 505 | 455 |
| 506 DispatchCompletionCallbacks(); | 456 DispatchCompletionCallbacks(); |
| 507 } | 457 } |
| 508 | 458 |
| 509 void WorkerPool::OnTaskCompleted() { | 459 void WorkerPool::OnTaskCompleted() { |
| 510 TRACE_EVENT0("cc", "WorkerPool::OnTaskCompleted"); | 460 TRACE_EVENT0("cc", "WorkerPool::OnTaskCompleted"); |
| 511 | 461 |
| 512 DispatchCompletionCallbacks(); | 462 DispatchCompletionCallbacks(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks check time"); | 561 TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks check time"); |
| 612 CancelCheckForCompletedTasks(); | 562 CancelCheckForCompletedTasks(); |
| 613 DispatchCompletionCallbacks(); | 563 DispatchCompletionCallbacks(); |
| 614 // Schedule another check for completed tasks if not idle. | 564 // Schedule another check for completed tasks if not idle. |
| 615 if (!is_idle) | 565 if (!is_idle) |
| 616 ScheduleCheckForCompletedTasks(); | 566 ScheduleCheckForCompletedTasks(); |
| 617 } | 567 } |
| 618 } | 568 } |
| 619 | 569 |
| 620 } // namespace cc | 570 } // namespace cc |
| OLD | NEW |