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

Side by Side 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 cancellation. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/worker_pool_job.h"
6
7 #include "base/logging.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/worker_pool.h"
10
11 namespace base {
12
13 WorkerPoolJob::WorkerPoolJob()
14 : origin_loop_(base::MessageLoopProxy::CreateForCurrentThread()),
15 canceled_(false),
16 state_(NONE) {}
17
18 WorkerPoolJob::~WorkerPoolJob() {}
19
20 void WorkerPoolJob::StartJob() {
21 DCHECK(thread_checker_.CalledOnValidThread());
22 DCHECK_EQ(state_, NONE);
23 state_ = RUNNING;
24 bool posted = WorkerPool::PostTask(
25 FROM_HERE,
26 NewRunnableMethod(this, &WorkerPoolJob::RunJobOnWorkerPool),
27 true /* is_slow */);
28 DCHECK(posted);
29 }
30
31 void WorkerPoolJob::CancelJob() {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 {
eroman 2010/12/22 23:25:08 nit: extra scope isn't really necessary.
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
34 AutoLock auto_lock(lock_);
35 DCHECK_GT(state_, NONE);
36 DCHECK_LT(state_, DONE);
37 DCHECK(!canceled_);
38 canceled_ = true;
39 }
40 }
41
42 bool WorkerPoolJob::canceled() const {
43 // No thread check since RunJob() implementations may check to see if they've
44 // been canceled so they can bail out early.
45 AutoLock auto_lock(lock_);
46 DCHECK_NE(state_, NONE);
47 return canceled_;
48 }
49
50 bool WorkerPoolJob::is_running() const {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 AutoLock auto_lock(lock_);
53 DCHECK_NE(state_, NONE);
54 return state_ == RUNNING;
55 }
56
57 bool WorkerPoolJob::started() const {
58 DCHECK(thread_checker_.CalledOnValidThread());
59 AutoLock auto_lock(lock_);
60 return state_ != NONE;
61 }
62
63 bool WorkerPoolJob::done() const {
64 DCHECK(thread_checker_.CalledOnValidThread());
65 AutoLock auto_lock(lock_);
66 return state_ > FINISHING;
67 }
68
69 void WorkerPoolJob::RunJobOnWorkerPool() {
70 DCHECK(!thread_checker_.CalledOnValidThread());
71
72 RunJob();
73
74 // Optimization to avoid posting to origin loop if we're already canceled.
75 bool canceled = false;
76 {
77 AutoLock auto_lock(lock_);
78 canceled = canceled_;
79 }
80
81 if (canceled) {
82 {
83 AutoLock auto_lock(lock_);
84 state_ = CANCELED_ON_WORKER;
eroman 2010/12/22 23:25:08 Can these be simplified? Seems like a bool would m
willchan no longer on Chromium 2010/12/24 00:47:34 I think that a single bool would not be able to en
85 }
86 return;
87 }
88
89 {
90 AutoLock auto_lock(lock_);
eroman 2010/12/22 23:25:08 Seeing how you use MessageLoopProxy for origin_loo
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
91 state_ = FINISHING;
92 }
93
94 origin_loop_->PostTask(
95 FROM_HERE,
96 NewRunnableMethod(this, &WorkerPoolJob::CompleteJobOnOriginLoop));
97 }
98
99 void WorkerPoolJob::CompleteJobOnOriginLoop() {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 // No need to lock, since the only relevant writes happen on the worker
102 // thread, and posting a task will invoke a memory barrier, so those writes
103 // are guaranteed to be visible.
104 DCHECK_EQ(state_, FINISHING);
105
106 // No need to lock, since writes only happen on origin loop.
107 if (canceled_) {
108 state_ = DONE;
109 return;
110 }
111
112 CompleteJob();
113
114 state_ = DONE;
115 }
116
117 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698