Chromium Code Reviews| Index: content/renderer/raster_worker_pool.cc |
| diff --git a/content/renderer/raster_worker_pool.cc b/content/renderer/raster_worker_pool.cc |
| index 0924c0a5118b24e3a97717b8e842d3045fe55286..73ec4ef60210dddf63bce35c1680311806c40070 100644 |
| --- a/content/renderer/raster_worker_pool.cc |
| +++ b/content/renderer/raster_worker_pool.cc |
| @@ -21,19 +21,22 @@ namespace { |
| // categories. |
| class RasterWorkerPoolThread : public base::SimpleThread { |
| public: |
| - explicit RasterWorkerPoolThread(const std::string& name_prefix, |
| - const Options& options, |
| - RasterWorkerPool* pool, |
| - std::vector<cc::TaskCategory> categories) |
| + RasterWorkerPoolThread(const std::string& name_prefix, |
| + const Options& options, |
| + RasterWorkerPool* pool, |
| + std::vector<cc::TaskCategory> categories, |
| + base::ConditionVariable* has_ready_to_run_tasks_cv) |
| : SimpleThread(name_prefix, options), |
| pool_(pool), |
| - categories_(categories) {} |
| + categories_(categories), |
|
Eric Willigers
2016/03/07 09:42:47
Perhaps
categories_(std::move(categories)),
|
| + has_ready_to_run_tasks_cv_(has_ready_to_run_tasks_cv) {} |
| - void Run() override { pool_->Run(categories_); } |
| + void Run() override { pool_->Run(categories_, has_ready_to_run_tasks_cv_); } |
| private: |
| RasterWorkerPool* const pool_; |
| const std::vector<cc::TaskCategory> categories_; |
| + base::ConditionVariable* const has_ready_to_run_tasks_cv_; |
| }; |
| } // namespace |
| @@ -116,7 +119,8 @@ class RasterWorkerPool::RasterWorkerPoolSequencedTaskRunner |
| RasterWorkerPool::RasterWorkerPool() |
| : namespace_token_(GetNamespaceToken()), |
| - has_ready_to_run_tasks_cv_(&lock_), |
| + has_ready_to_run_foreground_tasks_cv_(&lock_), |
| + has_ready_to_run_background_tasks_cv_(&lock_), |
| has_namespaces_with_finished_running_tasks_cv_(&lock_), |
| shutdown_(false) {} |
| @@ -124,31 +128,35 @@ void RasterWorkerPool::Start( |
| int num_threads, |
| const base::SimpleThread::Options& thread_options) { |
| DCHECK(threads_.empty()); |
| - while (threads_.size() < static_cast<size_t>(num_threads)) { |
| - // Determine the categories that each thread can run. |
| - std::vector<cc::TaskCategory> task_categories; |
| - // The first thread can run nonconcurrent tasks. |
| - if (threads_.size() == 0) { |
| - task_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND); |
| - } |
| - |
| - // All threads can run foreground tasks. |
| - task_categories.push_back(cc::TASK_CATEGORY_FOREGROUND); |
| - |
| - // The last thread can run background tasks. |
| - if (threads_.size() == (static_cast<size_t>(num_threads) - 1)) { |
| - task_categories.push_back(cc::TASK_CATEGORY_BACKGROUND); |
| - } |
| + // Start |num_threads| threads for foreground work, including nonconcurrent |
| + // foreground work. |
| + std::vector<cc::TaskCategory> foreground_categories; |
| + foreground_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND); |
| + foreground_categories.push_back(cc::TASK_CATEGORY_FOREGROUND); |
| + for (int i = 0; i < num_threads; i++) { |
| scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread( |
| base::StringPrintf("CompositorTileWorker%u", |
| static_cast<unsigned>(threads_.size() + 1)) |
| .c_str(), |
| - thread_options, this, task_categories)); |
| + thread_options, this, foreground_categories, |
| + &has_ready_to_run_foreground_tasks_cv_)); |
| thread->Start(); |
| threads_.push_back(std::move(thread)); |
| } |
| + |
| + // Start a single thread for background work. |
| + std::vector<cc::TaskCategory> background_categories; |
| + background_categories.push_back(cc::TASK_CATEGORY_BACKGROUND); |
| + scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread( |
| + base::StringPrintf("CompositorTileWorker%u", |
| + static_cast<unsigned>(threads_.size() + 1)) |
| + .c_str(), |
| + thread_options, this, background_categories, |
| + &has_ready_to_run_background_tasks_cv_)); |
| + thread->Start(); |
| + threads_.push_back(std::move(thread)); |
| } |
| void RasterWorkerPool::Shutdown() { |
| @@ -165,7 +173,8 @@ void RasterWorkerPool::Shutdown() { |
| shutdown_ = true; |
| // Wake up all workers so they exit. |
| - has_ready_to_run_tasks_cv_.Broadcast(); |
| + has_ready_to_run_foreground_tasks_cv_.Broadcast(); |
| + has_ready_to_run_background_tasks_cv_.Broadcast(); |
| } |
| while (!threads_.empty()) { |
| threads_.back()->Join(); |
| @@ -211,17 +220,22 @@ bool RasterWorkerPool::RunsTasksOnCurrentThread() const { |
| return true; |
| } |
| -void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories) { |
| +void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories, |
| + base::ConditionVariable* has_ready_to_run_tasks_cv) { |
| base::AutoLock lock(lock_); |
| while (true) { |
| if (!RunTaskWithLockAcquired(categories)) { |
| + // We are no longer running tasks, which may allow another category to |
| + // start running. Signal other worker threads. |
| + SignalHasReadyToRunTasksWithLockAcquired(); |
| + |
| // Exit when shutdown is set and no more tasks are pending. |
| if (shutdown_) |
| break; |
| // Wait for more tasks. |
| - has_ready_to_run_tasks_cv_.Wait(); |
| + has_ready_to_run_tasks_cv->Wait(); |
| continue; |
| } |
| } |
| @@ -266,9 +280,8 @@ void RasterWorkerPool::ScheduleTasksWithLockAcquired(cc::NamespaceToken token, |
| work_queue_.ScheduleTasks(token, graph); |
| - // If there is more work available, wake up the other worker threads. |
| - if (work_queue_.HasReadyToRunTasks()) |
| - has_ready_to_run_tasks_cv_.Broadcast(); |
| + // There may be more work available, so wake up another worker thread. |
| + SignalHasReadyToRunTasksWithLockAcquired(); |
| } |
| void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) { |
| @@ -288,6 +301,10 @@ void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) { |
| while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace)) |
| has_namespaces_with_finished_running_tasks_cv_.Wait(); |
| + |
| + // There may be other namespaces that have finished running tasks, so wake |
| + // up another origin thread. |
| + has_namespaces_with_finished_running_tasks_cv_.Signal(); |
| } |
| } |
| @@ -313,7 +330,7 @@ void RasterWorkerPool::CollectCompletedTasksWithLockAcquired( |
| bool RasterWorkerPool::RunTaskWithLockAcquired( |
| const std::vector<cc::TaskCategory>& categories) { |
| for (const auto& category : categories) { |
| - if (work_queue_.HasReadyToRunTasksForCategory(category)) { |
| + if (ShouldRunTaskForCategoryWithLockAcquired(category)) { |
| RunTaskInCategoryWithLockAcquired(category); |
| return true; |
| } |
| @@ -330,6 +347,9 @@ void RasterWorkerPool::RunTaskInCategoryWithLockAcquired( |
| auto prioritized_task = work_queue_.GetNextTaskToRun(category); |
| cc::Task* task = prioritized_task.task; |
| + // There may be more work available, so wake up another worker thread. |
| + SignalHasReadyToRunTasksWithLockAcquired(); |
| + |
| // Call WillRun() before releasing |lock_| and running task. |
| task->WillRun(); |
| @@ -344,14 +364,57 @@ void RasterWorkerPool::RunTaskInCategoryWithLockAcquired( |
| work_queue_.CompleteTask(prioritized_task); |
| - // We may have just dequeued more tasks, wake up the other worker threads. |
| - if (work_queue_.HasReadyToRunTasks()) |
| - has_ready_to_run_tasks_cv_.Broadcast(); |
| - |
| // If namespace has finished running all tasks, wake up origin threads. |
| if (work_queue_.HasFinishedRunningTasksInNamespace( |
| prioritized_task.task_namespace)) |
| - has_namespaces_with_finished_running_tasks_cv_.Broadcast(); |
| + has_namespaces_with_finished_running_tasks_cv_.Signal(); |
| +} |
| + |
| +bool RasterWorkerPool::ShouldRunTaskForCategoryWithLockAcquired( |
| + cc::TaskCategory category) { |
| + lock_.AssertAcquired(); |
| + |
| + if (!work_queue_.HasReadyToRunTasksForCategory(category)) |
| + return false; |
| + |
| + if (category == cc::TASK_CATEGORY_BACKGROUND) { |
| + // Only run background tasks if there are no foreground tasks running or |
| + // ready to run. |
| + size_t num_running_foreground_tasks = |
| + work_queue_.NumRunningTasksForCategory( |
| + cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) + |
| + work_queue_.NumRunningTasksForCategory(cc::TASK_CATEGORY_FOREGROUND); |
| + bool has_ready_to_run_foreground_tasks = |
| + work_queue_.HasReadyToRunTasksForCategory( |
| + cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) || |
| + work_queue_.HasReadyToRunTasksForCategory(cc::TASK_CATEGORY_FOREGROUND); |
| + |
| + if (num_running_foreground_tasks > 0 || has_ready_to_run_foreground_tasks) |
| + return false; |
| + } |
| + |
| + // Enforce that only one nonconcurrent task runs at a time. |
| + if (category == cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND && |
| + work_queue_.NumRunningTasksForCategory( |
| + cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) > 0) { |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void RasterWorkerPool::SignalHasReadyToRunTasksWithLockAcquired() { |
| + lock_.AssertAcquired(); |
| + |
| + if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_FOREGROUND) || |
| + ShouldRunTaskForCategoryWithLockAcquired( |
| + cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) { |
| + has_ready_to_run_foreground_tasks_cv_.Signal(); |
| + } |
| + |
| + if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) { |
| + has_ready_to_run_background_tasks_cv_.Signal(); |
| + } |
| } |
| RasterWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure) |