| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/categorized_worker_pool.h" | 5 #include "content/renderer/categorized_worker_pool.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 14 #include "cc/base/math_util.h" | 14 #include "cc/base/math_util.h" |
| 15 #include "cc/raster/task_category.h" | 15 #include "cc/raster/task_category.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 constexpr int kHeartbeatIntervalInSeconds = 60; // 1 minute |
| 21 |
| 20 // A thread which forwards to CategorizedWorkerPool::Run with the runnable | 22 // A thread which forwards to CategorizedWorkerPool::Run with the runnable |
| 21 // categories. | 23 // categories. |
| 22 class CategorizedWorkerPoolThread : public base::SimpleThread { | 24 class CategorizedWorkerPoolThread : public base::SimpleThread { |
| 23 public: | 25 public: |
| 24 CategorizedWorkerPoolThread( | 26 CategorizedWorkerPoolThread( |
| 25 const std::string& name_prefix, | 27 const std::string& name_prefix, |
| 26 const Options& options, | 28 const Options& options, |
| 27 CategorizedWorkerPool* pool, | 29 CategorizedWorkerPool* pool, |
| 28 std::vector<cc::TaskCategory> categories, | 30 std::vector<cc::TaskCategory> categories, |
| 29 base::ConditionVariable* has_ready_to_run_tasks_cv) | 31 base::ConditionVariable* has_ready_to_run_tasks_cv) |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 if (!RunTaskWithLockAcquired(categories)) { | 234 if (!RunTaskWithLockAcquired(categories)) { |
| 233 // We are no longer running tasks, which may allow another category to | 235 // We are no longer running tasks, which may allow another category to |
| 234 // start running. Signal other worker threads. | 236 // start running. Signal other worker threads. |
| 235 SignalHasReadyToRunTasksWithLockAcquired(); | 237 SignalHasReadyToRunTasksWithLockAcquired(); |
| 236 | 238 |
| 237 // Exit when shutdown is set and no more tasks are pending. | 239 // Exit when shutdown is set and no more tasks are pending. |
| 238 if (shutdown_) | 240 if (shutdown_) |
| 239 break; | 241 break; |
| 240 | 242 |
| 241 // Wait for more tasks. | 243 // Wait for more tasks. |
| 242 has_ready_to_run_tasks_cv->Wait(); | 244 WaitForMoreTasksWithLockAcquired(has_ready_to_run_tasks_cv); |
| 243 continue; | 245 continue; |
| 244 } | 246 } |
| 245 } | 247 } |
| 246 } | 248 } |
| 247 | 249 |
| 250 void CategorizedWorkerPool::WaitForMoreTasksWithLockAcquired( |
| 251 base::ConditionVariable* has_ready_to_run_tasks_cv) { |
| 252 const base::TimeDelta timeout = |
| 253 base::TimeDelta::FromSeconds(kHeartbeatIntervalInSeconds); |
| 254 while (has_ready_to_run_tasks_cv->TimedWait(timeout)) { |
| 255 TRACE_EVENT_INSTANT0( |
| 256 "heartbeat", "CategorizedWorkerPool::HeartbeatDuringWaitForMoreTasks", |
| 257 TRACE_EVENT_SCOPE_THREAD); |
| 258 } |
| 259 } |
| 260 |
| 248 void CategorizedWorkerPool::FlushForTesting() { | 261 void CategorizedWorkerPool::FlushForTesting() { |
| 249 base::AutoLock lock(lock_); | 262 base::AutoLock lock(lock_); |
| 250 | 263 |
| 251 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) { | 264 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) { |
| 252 has_namespaces_with_finished_running_tasks_cv_.Wait(); | 265 has_namespaces_with_finished_running_tasks_cv_.Wait(); |
| 253 } | 266 } |
| 254 } | 267 } |
| 255 | 268 |
| 256 scoped_refptr<base::SequencedTaskRunner> | 269 scoped_refptr<base::SequencedTaskRunner> |
| 257 CategorizedWorkerPool::CreateSequencedTaskRunner() { | 270 CategorizedWorkerPool::CreateSequencedTaskRunner() { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 | 435 |
| 423 // Overridden from cc::Task: | 436 // Overridden from cc::Task: |
| 424 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() { | 437 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() { |
| 425 closure_.Run(); | 438 closure_.Run(); |
| 426 closure_.Reset(); | 439 closure_.Reset(); |
| 427 } | 440 } |
| 428 | 441 |
| 429 CategorizedWorkerPool::ClosureTask::~ClosureTask() {} | 442 CategorizedWorkerPool::ClosureTask::~ClosureTask() {} |
| 430 | 443 |
| 431 } // namespace content | 444 } // namespace content |
| OLD | NEW |