Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
| 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> | |
|
eroman
2010/12/22 00:51:47
Why is this templatized?
Since this is already a
willchan no longer on Chromium
2010/12/22 02:04:01
The job() accessor needs the templated param to co
| |
| 34 class ScopedHandle { | |
| 35 public: | |
| 36 explicit ScopedHandle(JobType* job) : job_(job) {} | |
| 37 | |
| 38 ~ScopedHandle() { | |
| 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 explicit WorkerPoolJob(bool is_slow); | |
|
eroman
2010/12/22 00:51:47
I think we should get rid of this bool.
No consum
willchan no longer on Chromium
2010/12/22 02:04:01
Done.
| |
| 54 virtual ~WorkerPoolJob(); | |
|
eroman
2010/12/22 00:51:47
Please stress somewhere that subclasses MUST be pr
willchan no longer on Chromium
2010/12/22 02:04:01
Done.
| |
| 55 | |
| 56 // Starts the job. Will post itself to the WorkerPool, where it will invoke | |
| 57 // RunJob(). After RunJob() runs, it will post itself back to the origin | |
| 58 // thread, where it will invoke CompleteJob(). This can only be called once | |
| 59 // and must be called on the origin thread. | |
| 60 void StartJob(); | |
| 61 | |
| 62 // Cancels the job. CompleteJob() is guaranteed not to be called when this | |
| 63 // happens. RunJob() may or may not be called, depending on when CancelJob() | |
| 64 // is called. it's fundamentally a race. When cancellation is detected (may | |
|
eroman
2010/12/22 00:51:47
it's --> It's
willchan no longer on Chromium
2010/12/22 02:04:01
Done.
| |
| 65 // be on the WorkerPool thread or the origin thread), OnCanceled() is invoked. | |
|
eroman
2010/12/22 00:51:47
Perhaps a more succinct description is to say that
willchan no longer on Chromium
2010/12/22 02:04:01
I'm going to wait until we settle the cancellation
| |
| 66 // This can only be called once and must be called on the origin thread. It | |
| 67 // should only be called after StartJob() is called and before | |
| 68 // CompleteJob() is called (if it is called). | |
| 69 void CancelJob(); | |
| 70 | |
| 71 // Returns true if CancelJob() has been called. | |
| 72 bool canceled() const; | |
| 73 | |
| 74 // Returns true if the job is running on the worker thread. Obviously if it | |
| 75 // returns true once, there's still no guarantee that it is still running | |
| 76 // after it returns. Once it returns false though, then it's guaranteed to | |
| 77 // stay false. is_running() is only allowed to be called on the origin | |
| 78 // thread. | |
| 79 bool is_running() const; | |
| 80 | |
| 81 // NOTE(willchan): I've restricted this to unit tests only, primarily because | |
| 82 // I haven't found a good reason for allowing subtypes to access this. If | |
| 83 // there is a motivating reason to do so, I am open to it. | |
| 84 #if defined(UNIT_TEST) | |
| 85 scoped_refptr<MessageLoopProxy> origin_loop() const { | |
| 86 return origin_loop_; | |
| 87 } | |
| 88 #endif // defined(UNIT_TEST) | |
| 89 | |
| 90 private: | |
| 91 friend class base::RefCountedThreadSafe<WorkerPoolJob>; | |
| 92 template <typename JobType> friend class ScopedHandle; | |
| 93 | |
| 94 // State machine, primarily for enforcing that certain events only happen | |
| 95 // once. Also lets us implement is_running(). | |
| 96 enum State { | |
| 97 NONE, | |
| 98 RUNNING, | |
| 99 FINISHING, | |
| 100 CANCELED_ON_WORKER, | |
| 101 DONE, | |
| 102 }; | |
| 103 | |
| 104 // API for subtypes to implement. | |
| 105 | |
| 106 // RunJob() runs on the worker thread. If CancelJob() is called on the origin | |
| 107 // thread , then it may never be invoked, but that's a race. | |
| 108 virtual void RunJob() = 0; | |
|
eroman
2010/12/22 00:51:47
Listing pure virtual methods under private section
willchan no longer on Chromium
2010/12/22 02:04:01
No, it's more correct to make them private. That
| |
| 109 | |
| 110 // CompleteJob() runs on the origin thread. If CancelJob() is called, then it | |
| 111 // may never be invoked. | |
| 112 virtual void CompleteJob() = 0; | |
| 113 | |
| 114 // OnCanceled() may be called on either the WorkerPool thread or the origin | |
| 115 // thread. | |
| 116 virtual void OnCanceled() {} | |
|
eroman
2010/12/22 00:51:47
Please, no.
First of all, I don't like default vi
willchan no longer on Chromium
2010/12/22 02:04:01
I share your concerns here. I did it just because
| |
| 117 | |
| 118 void RunJobOnWorkerPool(); | |
| 119 void CompleteJobOnOriginLoop(); | |
| 120 | |
| 121 // Returns true after StartJob() has been called. Only valid to be called on | |
| 122 // the origin thread. | |
| 123 bool started() const; | |
| 124 | |
| 125 // Returns true after job completes successfully or has been canceled. | |
| 126 bool done() const; | |
| 127 | |
| 128 ThreadChecker thread_checker_; | |
| 129 const scoped_refptr<MessageLoopProxy> origin_loop_; | |
| 130 const bool is_slow_; | |
| 131 | |
| 132 mutable Lock lock_; // Protects all member variables below. | |
| 133 bool canceled_; | |
| 134 State state_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(WorkerPoolJob); | |
| 137 }; | |
| 138 | |
| 139 } // namespace base | |
| 140 | |
| 141 #endif // BASE_WORKER_POOL_JOB_H_ | |
| OLD | NEW |