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

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: Minimize the header dependency. 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(bool is_slow)
14 : origin_loop_(base::MessageLoopProxy::CreateForCurrentThread()),
15 is_slow_(is_slow),
16 canceled_(false),
17 state_(NONE) {}
18
19 WorkerPoolJob::~WorkerPoolJob() {}
20
21 void WorkerPoolJob::StartJob() {
22 DCHECK(thread_checker_.CalledOnValidThread());
23 DCHECK_EQ(state_, NONE);
24 state_ = RUNNING;
25 WorkerPool::PostTask(
eroman 2010/12/22 00:51:47 PostTask can fail. Please handle that case (at the
willchan no longer on Chromium 2010/12/22 02:04:01 Done.
26 FROM_HERE,
27 NewRunnableMethod(this, &WorkerPoolJob::RunJobOnWorkerPool),
28 is_slow_);
29 }
30
31 void WorkerPoolJob::CancelJob() {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 {
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 // Optimization to avoid work if we're already canceled.
73 bool canceled = false;
74 {
75 AutoLock auto_lock(lock_);
eroman 2010/12/22 00:51:47 This optimization doesn't seem worthwhile. I don't
willchan no longer on Chromium 2010/12/22 02:04:01 OK.
76 DCHECK_EQ(state_, RUNNING);
77 canceled = canceled_;
78 }
79
80 if (canceled) {
81 OnCanceled();
82 {
83 AutoLock auto_lock(lock_);
84 state_ = CANCELED_ON_WORKER;
85 }
86 return;
87 }
88
89 RunJob();
90
91 // Optimization to avoid posting to origin loop if we're already canceled.
92 {
93 AutoLock auto_lock(lock_);
94 canceled = canceled_;
95 }
96
97 if (canceled) {
98 OnCanceled();
99 {
100 AutoLock auto_lock(lock_);
101 state_ = CANCELED_ON_WORKER;
102 }
103 return;
104 }
105
106 {
107 AutoLock auto_lock(lock_);
108 state_ = FINISHING;
109 }
110
111 origin_loop_->PostTask(
eroman 2010/12/22 00:51:47 This is racy. Access to the origin loop MUST be p
willchan no longer on Chromium 2010/12/22 02:04:01 I don't believe it's racy. |origin_loop_| is a Me
eroman 2010/12/22 04:58:48 Oh, my bad! I didn't realize origin_loop_ was a Me
112 FROM_HERE,
113 NewRunnableMethod(this, &WorkerPoolJob::CompleteJobOnOriginLoop));
114 }
115
116 void WorkerPoolJob::CompleteJobOnOriginLoop() {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 // No need to lock, since the only relevant writes happen on the worker
119 // thread, and posting a task will invoke a memory barrier, so those writes
120 // are guaranteed to be visible.
121 DCHECK_EQ(state_, FINISHING);
122
123 // No need to lock, since writes only happen on origin loop.
124 if (canceled_) {
125 OnCanceled();
126 state_ = DONE;
127 return;
128 }
129
130 CompleteJob();
131
132 state_ = DONE;
133 }
134
135 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698