| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 explicit SequenceToken(int id) : id_(id) {} | 116 explicit SequenceToken(int id) : id_(id) {} |
| 117 | 117 |
| 118 int id_; | 118 int id_; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 // Allows tests to perform certain actions. | 121 // Allows tests to perform certain actions. |
| 122 class TestingObserver { | 122 class TestingObserver { |
| 123 public: | 123 public: |
| 124 virtual ~TestingObserver() {} | 124 virtual ~TestingObserver() {} |
| 125 virtual void OnHasWork() = 0; |
| 125 virtual void WillWaitForShutdown() = 0; | 126 virtual void WillWaitForShutdown() = 0; |
| 126 virtual void OnDestruct() = 0; | 127 virtual void OnDestruct() = 0; |
| 127 }; | 128 }; |
| 128 | 129 |
| 129 // Pass the maximum number of threads (they will be lazily created as needed) | 130 // Pass the maximum number of threads (they will be lazily created as needed) |
| 130 // and a prefix for the thread name to ad in debugging. | 131 // and a prefix for the thread name to ad in debugging. |
| 131 SequencedWorkerPool(size_t max_threads, | 132 SequencedWorkerPool(size_t max_threads, |
| 132 const std::string& thread_name_prefix); | 133 const std::string& thread_name_prefix); |
| 133 | 134 |
| 135 // Like above, but with |observer| for testing. Does not take |
| 136 // ownership of |observer|. |
| 137 SequencedWorkerPool(size_t max_threads, |
| 138 const std::string& thread_name_prefix, |
| 139 TestingObserver* observer); |
| 140 |
| 134 // Returns a unique token that can be used to sequence tasks posted to | 141 // Returns a unique token that can be used to sequence tasks posted to |
| 135 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. | 142 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. |
| 136 SequenceToken GetSequenceToken(); | 143 SequenceToken GetSequenceToken(); |
| 137 | 144 |
| 138 // Returns the sequence token associated with the given name. Calling this | 145 // Returns the sequence token associated with the given name. Calling this |
| 139 // function multiple times with the same string will always produce the | 146 // function multiple times with the same string will always produce the |
| 140 // same sequence token. If the name has not been used before, a new token | 147 // same sequence token. If the name has not been used before, a new token |
| 141 // will be created. | 148 // will be created. |
| 142 SequenceToken GetNamedSequenceToken(const std::string& name); | 149 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 143 | 150 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // unit tests when you want to validate something that should have happened. | 219 // unit tests when you want to validate something that should have happened. |
| 213 // | 220 // |
| 214 // Note that calling this will not prevent other threads from posting work to | 221 // Note that calling this will not prevent other threads from posting work to |
| 215 // the queue while the calling thread is waiting on Flush(). In this case, | 222 // the queue while the calling thread is waiting on Flush(). In this case, |
| 216 // Flush will return only when there's no more work in the queue. Normally, | 223 // Flush will return only when there's no more work in the queue. Normally, |
| 217 // this doesn't come up since in a test, all the work is being posted from | 224 // this doesn't come up since in a test, all the work is being posted from |
| 218 // the main thread. | 225 // the main thread. |
| 219 void FlushForTesting(); | 226 void FlushForTesting(); |
| 220 | 227 |
| 221 // Spuriously signal that there is work to be done. | 228 // Spuriously signal that there is work to be done. |
| 222 void TriggerSpuriousWorkSignalForTesting(); | 229 void SignalHasWorkForTesting(); |
| 223 | |
| 224 // Get the number of times the work signal has been triggered. | |
| 225 int GetWorkSignalCountForTesting() const; | |
| 226 | 230 |
| 227 // Implements the worker pool shutdown. This should be called during app | 231 // Implements the worker pool shutdown. This should be called during app |
| 228 // shutdown, and will discard/join with appropriate tasks before returning. | 232 // shutdown, and will discard/join with appropriate tasks before returning. |
| 229 // After this call, subsequent calls to post tasks will fail. | 233 // After this call, subsequent calls to post tasks will fail. |
| 230 // | 234 // |
| 231 // Must be called from the same thread this object was constructed on. | 235 // Must be called from the same thread this object was constructed on. |
| 232 void Shutdown(); | 236 void Shutdown(); |
| 233 | 237 |
| 234 // Called by tests to set the testing observer. This is NULL by default | |
| 235 // and ownership of the pointer is kept with the caller. | |
| 236 void SetTestingObserver(TestingObserver* observer); | |
| 237 | |
| 238 protected: | 238 protected: |
| 239 virtual ~SequencedWorkerPool(); | 239 virtual ~SequencedWorkerPool(); |
| 240 | 240 |
| 241 virtual void OnDestruct() const OVERRIDE; | 241 virtual void OnDestruct() const OVERRIDE; |
| 242 | 242 |
| 243 private: | 243 private: |
| 244 friend class RefCountedThreadSafe<SequencedWorkerPool>; | 244 friend class RefCountedThreadSafe<SequencedWorkerPool>; |
| 245 friend class DeleteHelper<SequencedWorkerPool>; | 245 friend class DeleteHelper<SequencedWorkerPool>; |
| 246 | 246 |
| 247 class Inner; | 247 class Inner; |
| 248 class Worker; | 248 class Worker; |
| 249 | 249 |
| 250 const scoped_refptr<MessageLoopProxy> constructor_message_loop_; | 250 const scoped_refptr<MessageLoopProxy> constructor_message_loop_; |
| 251 | 251 |
| 252 // Avoid pulling in too many headers by putting (almost) everything | 252 // Avoid pulling in too many headers by putting (almost) everything |
| 253 // into |inner_|. | 253 // into |inner_|. |
| 254 const scoped_ptr<Inner> inner_; | 254 const scoped_ptr<Inner> inner_; |
| 255 | 255 |
| 256 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 256 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 257 }; | 257 }; |
| 258 | 258 |
| 259 } // namespace base | 259 } // namespace base |
| 260 | 260 |
| 261 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 261 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |