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> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
18 | 18 |
19 namespace tracked_objects { | 19 namespace tracked_objects { |
20 class Location; | 20 class Location; |
21 } // namespace tracked_objects | 21 } // namespace tracked_objects |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 | 24 |
| 25 class MessageLoopProxy; |
| 26 |
| 27 template <class T> class DeleteHelper; |
| 28 |
25 // A worker thread pool that enforces ordering between sets of tasks. It also | 29 // A worker thread pool that enforces ordering between sets of tasks. It also |
26 // allows you to specify what should happen to your tasks on shutdown. | 30 // allows you to specify what should happen to your tasks on shutdown. |
27 // | 31 // |
28 // To enforce ordering, get a unique sequence token from the pool and post all | 32 // To enforce ordering, get a unique sequence token from the pool and post all |
29 // tasks you want to order with the token. All tasks with the same token are | 33 // tasks you want to order with the token. All tasks with the same token are |
30 // guaranteed to execute serially, though not necessarily on the same thread. | 34 // guaranteed to execute serially, though not necessarily on the same thread. |
31 // | 35 // |
32 // Example: | 36 // Example: |
33 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); | 37 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); |
34 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 38 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 explicit SequenceToken(int id) : id_(id) {} | 116 explicit SequenceToken(int id) : id_(id) {} |
113 | 117 |
114 int id_; | 118 int id_; |
115 }; | 119 }; |
116 | 120 |
117 // Allows tests to perform certain actions. | 121 // Allows tests to perform certain actions. |
118 class TestingObserver { | 122 class TestingObserver { |
119 public: | 123 public: |
120 virtual ~TestingObserver() {} | 124 virtual ~TestingObserver() {} |
121 virtual void WillWaitForShutdown() = 0; | 125 virtual void WillWaitForShutdown() = 0; |
| 126 virtual void OnDestruct() = 0; |
122 }; | 127 }; |
123 | 128 |
124 // Pass the maximum number of threads (they will be lazily created as needed) | 129 // Pass the maximum number of threads (they will be lazily created as needed) |
125 // and a prefix for the thread name to ad in debugging. | 130 // and a prefix for the thread name to ad in debugging. |
126 SequencedWorkerPool(size_t max_threads, | 131 SequencedWorkerPool(size_t max_threads, |
127 const std::string& thread_name_prefix); | 132 const std::string& thread_name_prefix); |
128 | 133 |
129 // Returns a unique token that can be used to sequence tasks posted to | 134 // Returns a unique token that can be used to sequence tasks posted to |
130 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. | 135 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. |
131 SequenceToken GetSequenceToken(); | 136 SequenceToken GetSequenceToken(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 // After this call, subsequent calls to post tasks will fail. | 223 // After this call, subsequent calls to post tasks will fail. |
219 void Shutdown(); | 224 void Shutdown(); |
220 | 225 |
221 // Called by tests to set the testing observer. This is NULL by default | 226 // Called by tests to set the testing observer. This is NULL by default |
222 // and ownership of the pointer is kept with the caller. | 227 // and ownership of the pointer is kept with the caller. |
223 void SetTestingObserver(TestingObserver* observer); | 228 void SetTestingObserver(TestingObserver* observer); |
224 | 229 |
225 protected: | 230 protected: |
226 virtual ~SequencedWorkerPool(); | 231 virtual ~SequencedWorkerPool(); |
227 | 232 |
| 233 virtual void OnDestruct() const OVERRIDE; |
| 234 |
228 private: | 235 private: |
229 friend class RefCountedThreadSafe<SequencedWorkerPool>; | 236 friend class RefCountedThreadSafe<SequencedWorkerPool>; |
| 237 friend class DeleteHelper<SequencedWorkerPool>; |
230 | 238 |
231 class Inner; | 239 class Inner; |
232 class Worker; | 240 class Worker; |
233 | 241 |
234 // Avoid pulling in too many headers by putting everything into | 242 const scoped_refptr<MessageLoopProxy> constructor_message_loop_; |
235 // |inner_|. | 243 |
| 244 // Avoid pulling in too many headers by putting (almost) everything |
| 245 // into |inner_|. |
236 const scoped_ptr<Inner> inner_; | 246 const scoped_ptr<Inner> inner_; |
237 | 247 |
238 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 248 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
239 }; | 249 }; |
240 | 250 |
241 } // namespace base | 251 } // namespace base |
242 | 252 |
243 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 253 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
OLD | NEW |