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

Unified Diff: base/worker_pool_job.cc

Issue 5710002: Create base::WorkerPoolJob. Use it for HostResolverImpl and DirectoryLister. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 10 years 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
Index: base/worker_pool_job.cc
diff --git a/base/worker_pool_job.cc b/base/worker_pool_job.cc
new file mode 100644
index 0000000000000000000000000000000000000000..51af53c637466501684603fe771d6c75c074ed82
--- /dev/null
+++ b/base/worker_pool_job.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/worker_pool_job.h"
+
+#include "base/logging.h"
+#include "base/message_loop_proxy.h"
+#include "base/worker_pool.h"
+
+namespace base {
+
+WorkerPoolJob::WorkerPoolJob()
+ : origin_loop_(base::MessageLoopProxy::CreateForCurrentThread()),
+ canceled_(false),
+ state_(NONE) {}
+
+WorkerPoolJob::~WorkerPoolJob() {}
+
+void WorkerPoolJob::StartJob() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_EQ(state_, NONE);
eroman 2011/01/05 01:24:52 Can the state DCHECK be more restrictive? i.e, I
+ state_ = RUNNING;
+ bool posted = WorkerPool::PostTask(
+ FROM_HERE,
+ NewRunnableMethod(this, &WorkerPoolJob::RunJobOnWorkerPool),
+ true /* is_slow */);
+ DCHECK(posted);
+}
+
+void WorkerPoolJob::CancelJob() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_NE(state_, NONE);
+ DCHECK_NE(state_, DONE);
eroman 2011/01/05 01:24:52 Can these two checks be combined into: DCHECK_EQ(R
+ DCHECK(!canceled_);
+ canceled_ = true;
+}
+
+bool WorkerPoolJob::canceled() const {
+ // No thread check since RunJob() implementations may check to see if they've
+ // been canceled so they can bail out early.
eroman 2011/01/05 01:24:52 I don't think this comment is true anymore.
+ DCHECK_NE(state_, NONE);
+ return canceled_;
+}
+
+void WorkerPoolJob::RunJobOnWorkerPool() {
+ DCHECK(!thread_checker_.CalledOnValidThread());
+
+ RunJob();
+
+ origin_loop_->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(this, &WorkerPoolJob::CompleteJobOnOriginLoop));
+}
+
+void WorkerPoolJob::CompleteJobOnOriginLoop() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_NE(state_, NONE);
eroman 2011/01/05 01:24:52 Can these two statnments be combined into: DCHECK
+ DCHECK_NE(state_, DONE);
+
+ // No need to lock, since writes only happen on origin loop.
+ if (canceled_) {
+ state_ = DONE;
+ return;
+ }
+
+ OnJobCompleted();
+
+ state_ = DONE;
eroman 2011/01/05 01:24:52 nit: I wander if you want to set state_ to DONE be
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698