Chromium Code Reviews| 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 |