Chromium Code Reviews| 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 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // pool. | 119 // pool. |
| 120 class SequenceToken { | 120 class SequenceToken { |
| 121 public: | 121 public: |
| 122 SequenceToken() : id_(0) {} | 122 SequenceToken() : id_(0) {} |
| 123 ~SequenceToken() {} | 123 ~SequenceToken() {} |
| 124 | 124 |
| 125 bool Equals(const SequenceToken& other) const { | 125 bool Equals(const SequenceToken& other) const { |
| 126 return id_ == other.id_; | 126 return id_ == other.id_; |
| 127 } | 127 } |
| 128 | 128 |
| 129 // An zero |id_| is a sentinel value internally for unsequenced tasks. | |
|
darin (slow to review)
2013/07/05 05:24:19
nit: An -> A, and I think you also meant to put "u
tommycli
2013/07/08 17:42:15
Done.
| |
| 130 bool IsValid() const { | |
| 131 return id_ != 0; | |
| 132 } | |
| 133 | |
| 129 private: | 134 private: |
| 130 friend class SequencedWorkerPool; | 135 friend class SequencedWorkerPool; |
| 131 | 136 |
| 132 explicit SequenceToken(int id) : id_(id) {} | 137 explicit SequenceToken(int id) : id_(id) {} |
| 133 | 138 |
| 134 int id_; | 139 int id_; |
| 135 }; | 140 }; |
| 136 | 141 |
| 137 // Allows tests to perform certain actions. | 142 // Allows tests to perform certain actions. |
| 138 class TestingObserver { | 143 class TestingObserver { |
| 139 public: | 144 public: |
| 140 virtual ~TestingObserver() {} | 145 virtual ~TestingObserver() {} |
| 141 virtual void OnHasWork() = 0; | 146 virtual void OnHasWork() = 0; |
| 142 virtual void WillWaitForShutdown() = 0; | 147 virtual void WillWaitForShutdown() = 0; |
| 143 virtual void OnDestruct() = 0; | 148 virtual void OnDestruct() = 0; |
| 144 }; | 149 }; |
| 145 | 150 |
| 151 // Gets the SequencedToken of the current thread. | |
| 152 // If current thread is not a SequencedWorkerPool worker thread or is running | |
| 153 // an unsequenced task, returns an invalid SequenceToken. | |
| 154 static SequenceToken CurrentThreadSequenceToken(); | |
|
darin (slow to review)
2013/07/05 05:24:19
nit: GetSequenceTokenForCurrentThread might be a s
tommycli
2013/07/08 17:42:15
Done.
| |
| 155 | |
| 146 // When constructing a SequencedWorkerPool, there must be a | 156 // When constructing a SequencedWorkerPool, there must be a |
| 147 // MessageLoop on the current thread unless you plan to deliberately | 157 // MessageLoop on the current thread unless you plan to deliberately |
| 148 // leak it. | 158 // leak it. |
| 149 | 159 |
| 150 // Pass the maximum number of threads (they will be lazily created as needed) | 160 // Pass the maximum number of threads (they will be lazily created as needed) |
| 151 // and a prefix for the thread name to aid in debugging. | 161 // and a prefix for the thread name to aid in debugging. |
| 152 SequencedWorkerPool(size_t max_threads, | 162 SequencedWorkerPool(size_t max_threads, |
| 153 const std::string& thread_name_prefix); | 163 const std::string& thread_name_prefix); |
| 154 | 164 |
| 155 // Like above, but with |observer| for testing. Does not take | 165 // Like above, but with |observer| for testing. Does not take |
| 156 // ownership of |observer|. | 166 // ownership of |observer|. |
| 157 SequencedWorkerPool(size_t max_threads, | 167 SequencedWorkerPool(size_t max_threads, |
| 158 const std::string& thread_name_prefix, | 168 const std::string& thread_name_prefix, |
| 159 TestingObserver* observer); | 169 TestingObserver* observer); |
| 160 | 170 |
| 161 // Returns a unique token that can be used to sequence tasks posted to | 171 // Returns a unique token that can be used to sequence tasks posted to |
| 162 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. | 172 // PostSequencedWorkerTask(). Valid tokens are always nonzero. |
| 163 SequenceToken GetSequenceToken(); | 173 SequenceToken GetSequenceToken(); |
| 164 | 174 |
| 165 // Returns the sequence token associated with the given name. Calling this | 175 // Returns the sequence token associated with the given name. Calling this |
| 166 // function multiple times with the same string will always produce the | 176 // function multiple times with the same string will always produce the |
| 167 // same sequence token. If the name has not been used before, a new token | 177 // same sequence token. If the name has not been used before, a new token |
| 168 // will be created. | 178 // will be created. |
| 169 SequenceToken GetNamedSequenceToken(const std::string& name); | 179 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 170 | 180 |
| 171 // Returns a SequencedTaskRunner wrapper which posts to this | 181 // Returns a SequencedTaskRunner wrapper which posts to this |
| 172 // SequencedWorkerPool using the given sequence token. Tasks with nonzero | 182 // SequencedWorkerPool using the given sequence token. Tasks with nonzero |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 // Avoid pulling in too many headers by putting (almost) everything | 343 // Avoid pulling in too many headers by putting (almost) everything |
| 334 // into |inner_|. | 344 // into |inner_|. |
| 335 const scoped_ptr<Inner> inner_; | 345 const scoped_ptr<Inner> inner_; |
| 336 | 346 |
| 337 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 347 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 338 }; | 348 }; |
| 339 | 349 |
| 340 } // namespace base | 350 } // namespace base |
| 341 | 351 |
| 342 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 352 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |