| 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 // Returns false if current thread is executing an unsequenced task. | |
| 130 bool IsValid() const { | |
| 131 return id_ != 0; | |
| 132 } | |
| 133 | |
| 134 private: | 129 private: |
| 135 friend class SequencedWorkerPool; | 130 friend class SequencedWorkerPool; |
| 136 | 131 |
| 137 explicit SequenceToken(int id) : id_(id) {} | 132 explicit SequenceToken(int id) : id_(id) {} |
| 138 | 133 |
| 139 int id_; | 134 int id_; |
| 140 }; | 135 }; |
| 141 | 136 |
| 142 // Allows tests to perform certain actions. | 137 // Allows tests to perform certain actions. |
| 143 class TestingObserver { | 138 class TestingObserver { |
| 144 public: | 139 public: |
| 145 virtual ~TestingObserver() {} | 140 virtual ~TestingObserver() {} |
| 146 virtual void OnHasWork() = 0; | 141 virtual void OnHasWork() = 0; |
| 147 virtual void WillWaitForShutdown() = 0; | 142 virtual void WillWaitForShutdown() = 0; |
| 148 virtual void OnDestruct() = 0; | 143 virtual void OnDestruct() = 0; |
| 149 }; | 144 }; |
| 150 | 145 |
| 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 GetSequenceTokenForCurrentThread(); | |
| 155 | |
| 156 // When constructing a SequencedWorkerPool, there must be a | 146 // When constructing a SequencedWorkerPool, there must be a |
| 157 // MessageLoop on the current thread unless you plan to deliberately | 147 // MessageLoop on the current thread unless you plan to deliberately |
| 158 // leak it. | 148 // leak it. |
| 159 | 149 |
| 160 // Pass the maximum number of threads (they will be lazily created as needed) | 150 // Pass the maximum number of threads (they will be lazily created as needed) |
| 161 // and a prefix for the thread name to aid in debugging. | 151 // and a prefix for the thread name to aid in debugging. |
| 162 SequencedWorkerPool(size_t max_threads, | 152 SequencedWorkerPool(size_t max_threads, |
| 163 const std::string& thread_name_prefix); | 153 const std::string& thread_name_prefix); |
| 164 | 154 |
| 165 // Like above, but with |observer| for testing. Does not take | 155 // Like above, but with |observer| for testing. Does not take |
| 166 // ownership of |observer|. | 156 // ownership of |observer|. |
| 167 SequencedWorkerPool(size_t max_threads, | 157 SequencedWorkerPool(size_t max_threads, |
| 168 const std::string& thread_name_prefix, | 158 const std::string& thread_name_prefix, |
| 169 TestingObserver* observer); | 159 TestingObserver* observer); |
| 170 | 160 |
| 171 // Returns a unique token that can be used to sequence tasks posted to | 161 // Returns a unique token that can be used to sequence tasks posted to |
| 172 // PostSequencedWorkerTask(). Valid tokens are always nonzero. | 162 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. |
| 173 SequenceToken GetSequenceToken(); | 163 SequenceToken GetSequenceToken(); |
| 174 | 164 |
| 175 // Returns the sequence token associated with the given name. Calling this | 165 // Returns the sequence token associated with the given name. Calling this |
| 176 // function multiple times with the same string will always produce the | 166 // function multiple times with the same string will always produce the |
| 177 // same sequence token. If the name has not been used before, a new token | 167 // same sequence token. If the name has not been used before, a new token |
| 178 // will be created. | 168 // will be created. |
| 179 SequenceToken GetNamedSequenceToken(const std::string& name); | 169 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 180 | 170 |
| 181 // Returns a SequencedTaskRunner wrapper which posts to this | 171 // Returns a SequencedTaskRunner wrapper which posts to this |
| 182 // SequencedWorkerPool using the given sequence token. Tasks with nonzero | 172 // SequencedWorkerPool using the given sequence token. Tasks with nonzero |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 // Avoid pulling in too many headers by putting (almost) everything | 333 // Avoid pulling in too many headers by putting (almost) everything |
| 344 // into |inner_|. | 334 // into |inner_|. |
| 345 const scoped_ptr<Inner> inner_; | 335 const scoped_ptr<Inner> inner_; |
| 346 | 336 |
| 347 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 337 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 348 }; | 338 }; |
| 349 | 339 |
| 350 } // namespace base | 340 } // namespace base |
| 351 | 341 |
| 352 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 342 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |