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

Side by Side Diff: base/worker_pool_job.h

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 9 years, 12 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 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 #ifndef BASE_WORKER_POOL_JOB_H_
6 #define BASE_WORKER_POOL_JOB_H_
7 #pragma once
8
9 #include "base/basictypes.h"
10 #include "base/lock.h"
11 #include "base/message_loop_proxy.h"
12 #include "base/ref_counted.h"
13 #include "base/thread_checker.h"
14
15 class MessageLoop;
eroman 2010/12/22 23:25:08 this is not used.
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
16
17 namespace base {
18
19 // WARNING: Are you sure you want to use WorkerPool in the first place? The
20 // thread will _not_ be joined, so the job may still be running during shutdown.
21 // Global objects that are deleted by AtExitManager should not be accessed on
22 // the WorkerPool thread, otherwise there is potential for shutdown crashes.
23 //
24 // WorkerPoolJob is a helper class for implementing jobs that get created on an
25 // origin thread, get posted to run on a WorkerPool thread, and then run back on
26 // the origin thread upon completion. WorkerPoolJob also handles cancellation
27 // of a job which prevents it from calling back. Note that we use inheritance
28 // rather than composition here, since WorkerPoolJob also handles lifetime
29 // management via refcounting.
30 class WorkerPoolJob : public base::RefCountedThreadSafe<WorkerPoolJob> {
31 public:
32 // ScopedHandle is provided to automate cancellation of the WorkerPoolJob.
33 template <typename JobType>
34 class ScopedHandle {
35 public:
36 explicit ScopedHandle(JobType* job) : job_(job) {}
37
38 ~ScopedHandle() {
eroman 2010/12/22 23:25:08 Can't this simply be job_->CancelJob() ?
willchan no longer on Chromium 2010/12/24 00:47:34 No. I am a bit anal about cancellation. I want p
eroman 2011/01/05 01:24:52 FWIW we use the pattern of calling Cancel() on alr
39 AutoLock auto_lock(job_->lock_);
40 if (!job_->canceled_ && job_->state_ > NONE &&
41 job_->state_ < CANCELED_ON_WORKER)
42 job_->canceled_ = true;
43 }
44
45 JobType* job() const { return job_.get(); }
46
47 private:
48 const scoped_refptr<JobType> job_;
49 DISALLOW_COPY_AND_ASSIGN(ScopedHandle);
50 };
51
52 protected:
53 WorkerPoolJob();
54
55 // This may be deleted on the origin or worker thread.
56 virtual ~WorkerPoolJob();
57
58 // Starts the job. Will post itself to the WorkerPool, where it will invoke
59 // RunJob(). After RunJob() runs, it will post itself back to the origin
60 // thread, where it will invoke CompleteJob(). This can only be called once
61 // and must be called on the origin thread.
62 void StartJob();
63
64 // Cancels the job. CompleteJob() is guaranteed not to be called when this
65 // happens. May only be called once, and only on the origin thread.
66 void CancelJob();
67
68 // Returns true if CancelJob() has been called.
69 bool canceled() const;
70
71 // Returns true if the job is running on the worker thread. Obviously if it
72 // returns true once, there's still no guarantee that it is still running
73 // after it returns. Once it returns false though, then it's guaranteed to
74 // stay false. is_running() is only allowed to be called on the origin
75 // thread.
76 bool is_running() const;
eroman 2010/12/22 23:25:08 This seems like it should be only expose to unit-t
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
77
78 // NOTE(willchan): I've restricted this to unit tests only, primarily because
79 // I haven't found a good reason for allowing subtypes to access this. If
80 // there is a motivating reason to do so, I am open to it.
81 #if defined(UNIT_TEST)
82 scoped_refptr<MessageLoopProxy> origin_loop() const {
83 return origin_loop_;
84 }
85 #endif // defined(UNIT_TEST)
86
87 private:
88 friend class base::RefCountedThreadSafe<WorkerPoolJob>;
89 template <typename JobType> friend class ScopedHandle;
eroman 2010/12/22 23:25:08 See earlier comment, I don't think we need to frie
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
90
91 // State machine, primarily for enforcing that certain events only happen
92 // once. Also lets us implement is_running().
93 enum State {
94 NONE,
95 RUNNING,
96 FINISHING,
97 CANCELED_ON_WORKER,
98 DONE,
99 };
100
101 // API for subtypes to implement.
102
103 // RunJob() runs on the worker thread. If CancelJob() is called on the origin
104 // thread , then it may never be invoked, but that's a race.
105 virtual void RunJob() = 0;
106
107 // CompleteJob() runs on the origin thread. If CancelJob() is called, then it
108 // may never be invoked.
109 virtual void CompleteJob() = 0;
eroman 2010/12/22 23:25:08 I think this would be better named OnJobCompleted(
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
110
111 void RunJobOnWorkerPool();
112 void CompleteJobOnOriginLoop();
113
114 // Returns true after StartJob() has been called. Only valid to be called on
115 // the origin thread.
116 bool started() const;
eroman 2010/12/22 23:25:08 Is this needed other than for unittests?
willchan no longer on Chromium 2010/12/24 00:47:34 Removed
117
118 // Returns true after job completes successfully or has been canceled.
119 bool done() const;
120
121 ThreadChecker thread_checker_;
122 const scoped_refptr<MessageLoopProxy> origin_loop_;
123
124 mutable Lock lock_; // Protects all member variables below.
125 bool canceled_;
126 State state_;
127
128 DISALLOW_COPY_AND_ASSIGN(WorkerPoolJob);
129 };
130
131 } // namespace base
132
133 #endif // BASE_WORKER_POOL_JOB_H_
OLDNEW
« no previous file with comments | « base/i18n/file_util_icu.cc ('k') | base/worker_pool_job.cc » ('j') | base/worker_pool_job.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698