Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3027)

Unified Diff: cc/worker_pool.cc

Issue 12217105: cc: Check for completed raster tasks at interval. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Post task to impl thread when worker pool becomes idle. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« cc/worker_pool.h ('K') | « cc/worker_pool.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/worker_pool.cc
diff --git a/cc/worker_pool.cc b/cc/worker_pool.cc
index 7d3428aa74d2f8bde286b73cb418549fb1504a70..b56d572835c0171136e320a8c64d917a9673613c 100644
--- a/cc/worker_pool.cc
+++ b/cc/worker_pool.cc
@@ -28,6 +28,7 @@ class WorkerPoolTaskImpl : public internal::WorkerPoolTask {
virtual void Run(RenderingStats* rendering_stats) OVERRIDE {
task_.Run(rendering_stats);
+ base::subtle::Release_Store(&completed_, 1);
}
private:
@@ -36,22 +37,24 @@ class WorkerPoolTaskImpl : public internal::WorkerPoolTask {
const char* kWorkerThreadNamePrefix = "Compositor";
-// Allow two pending tasks per worker. This keeps resource usage
-// low while making sure workers aren't unnecessarily idle.
-const int kNumPendingTasksPerWorker = 2;
-
} // namespace
namespace internal {
WorkerPoolTask::WorkerPoolTask(const base::Closure& reply)
: reply_(reply) {
+ base::subtle::Acquire_Store(&completed_, 0);
}
WorkerPoolTask::~WorkerPoolTask() {
}
+bool WorkerPoolTask::IsPending() {
+ return base::subtle::Acquire_Load(&completed_) == 0;
+}
+
void WorkerPoolTask::Completed() {
+ DCHECK_EQ(base::subtle::Acquire_Load(&completed_), 1);
reply_.Run();
}
@@ -60,7 +63,6 @@ void WorkerPoolTask::Completed() {
WorkerPool::Worker::Worker(WorkerPool* worker_pool, const std::string name)
: base::Thread(name.c_str()),
worker_pool_(worker_pool),
- weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
rendering_stats_(make_scoped_ptr(new RenderingStats)),
record_rendering_stats_(false) {
Start();
@@ -80,23 +82,20 @@ void WorkerPool::Worker::StopAfterCompletingAllPendingTasks() {
// all tasks have finished running.
while (!pending_tasks_.empty())
OnTaskCompleted();
-
- // Cancel all pending replies.
- weak_ptr_factory_.InvalidateWeakPtrs();
}
void WorkerPool::Worker::PostTask(scoped_ptr<internal::WorkerPoolTask> task) {
- DCHECK_LT(num_pending_tasks(), kNumPendingTasksPerWorker);
-
RenderingStats* stats =
record_rendering_stats_ ? rendering_stats_.get() : NULL;
- message_loop_proxy()->PostTaskAndReply(
+ worker_pool_->WillPostMoreWork();
+
+ message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&Worker::RunTask,
base::Unretained(task.get()),
- base::Unretained(stats)),
- base::Bind(&Worker::OnTaskCompleted, weak_ptr_factory_.GetWeakPtr()));
+ base::Unretained(worker_pool_),
+ base::Unretained(stats)));
pending_tasks_.push_back(task.Pass());
@@ -113,8 +112,11 @@ void WorkerPool::Worker::Init() {
// static
void WorkerPool::Worker::RunTask(
- internal::WorkerPoolTask* task, RenderingStats* rendering_stats) {
+ internal::WorkerPoolTask* task,
+ WorkerPool* worker_pool,
+ RenderingStats* rendering_stats) {
task->Run(rendering_stats);
+ worker_pool->OnWorkCompleted();
nduca 2013/02/13 01:33:18 Use suffix to OnWorkCompletedOnWorkerThread
reveman 2013/02/13 08:12:51 Done.
}
void WorkerPool::Worker::OnTaskCompleted() {
@@ -126,8 +128,20 @@ void WorkerPool::Worker::OnTaskCompleted() {
worker_pool_->DidNumPendingTasksChange();
}
-WorkerPool::WorkerPool(size_t num_threads)
- : workers_need_sorting_(false),
+void WorkerPool::Worker::CheckForCompletedTasks() {
+ while (!pending_tasks_.empty()) {
+ if (pending_tasks_.front()->IsPending())
+ return;
+
+ OnTaskCompleted();
+ }
+}
+
+WorkerPool::WorkerPool(WorkerPoolClient* client, size_t num_threads)
+ : client_(client),
+ origin_loop_(base::MessageLoopProxy::current()),
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+ workers_need_sorting_(false),
shutdown_(false) {
const std::string thread_name_prefix = kWorkerThreadNamePrefix;
while (workers_.size() < num_threads) {
@@ -136,11 +150,15 @@ WorkerPool::WorkerPool(size_t num_threads)
this,
thread_name_prefix + StringPrintf("Worker%d", thread_number).c_str()));
}
+ base::subtle::Acquire_Store(&work_count_, 0);
}
WorkerPool::~WorkerPool() {
Shutdown();
STLDeleteElements(&workers_);
+ // Cancel any pending idle callback.
+ weak_ptr_factory_.InvalidateWeakPtrs();
+ DCHECK_EQ(base::subtle::Acquire_Load(&work_count_), 0);
}
void WorkerPool::Shutdown() {
@@ -164,10 +182,12 @@ void WorkerPool::PostTaskAndReply(
reply)).PassAs<internal::WorkerPoolTask>());
}
-bool WorkerPool::IsBusy() {
- Worker* worker = GetWorkerForNextTask();
-
- return worker->num_pending_tasks() >= kNumPendingTasksPerWorker;
+void WorkerPool::CheckForCompletedTasks() {
+ for (WorkerVector::iterator it = workers_.begin();
+ it != workers_.end(); it++) {
+ Worker* worker = *it;
+ worker->CheckForCompletedTasks();
+ }
}
void WorkerPool::SetRecordRenderingStats(bool record_rendering_stats) {
@@ -204,6 +224,25 @@ WorkerPool::Worker* WorkerPool::GetWorkerForNextTask() {
return workers_.front();
}
+void WorkerPool::WillPostMoreWork() {
+ // Instantiate new idle handler before pool becomes active.
+ if (base::subtle::Barrier_AtomicIncrement(&work_count_, 1) == 1) {
+ idle_handler_ = base::Bind(&WorkerPool::OnIdle,
brianderson 2013/02/13 01:19:57 I do not see where idle_handler_ is reset or set t
reveman 2013/02/13 08:12:51 latest patch initialize this once in the construct
+ weak_ptr_factory_.GetWeakPtr());
+ }
+}
+
+void WorkerPool::OnWorkCompleted() {
+ // Post idle handler task when pool work count reaches 0.
+ if (base::subtle::Barrier_AtomicIncrement(&work_count_, -1) == 0) {
+ origin_loop_->PostTask(FROM_HERE, idle_handler_);
+ }
+}
+
+void WorkerPool::OnIdle() {
+ client_->OnIdle();
+}
+
void WorkerPool::DidNumPendingTasksChange() {
workers_need_sorting_ = true;
}
« cc/worker_pool.h ('K') | « cc/worker_pool.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698