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; | 25 class MessageLoopProxy; |
26 | 26 |
27 template <class T> class DeleteHelper; | 27 template <class T> class DeleteHelper; |
28 | 28 |
| 29 class SequencedTaskRunner; |
| 30 |
29 // A worker thread pool that enforces ordering between sets of tasks. It also | 31 // A worker thread pool that enforces ordering between sets of tasks. It also |
30 // allows you to specify what should happen to your tasks on shutdown. | 32 // allows you to specify what should happen to your tasks on shutdown. |
31 // | 33 // |
32 // To enforce ordering, get a unique sequence token from the pool and post all | 34 // To enforce ordering, get a unique sequence token from the pool and post all |
33 // tasks you want to order with the token. All tasks with the same token are | 35 // tasks you want to order with the token. All tasks with the same token are |
34 // guaranteed to execute serially, though not necessarily on the same thread. | 36 // guaranteed to execute serially (i.e. there is no execution overlap |
| 37 // whatsoever), though not necessarily on the same thread. |
35 // | 38 // |
36 // Example: | 39 // Example: |
37 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); | 40 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); |
38 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 41 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
39 // FROM_HERE, base::Bind(...)); | 42 // FROM_HERE, base::Bind(...)); |
40 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 43 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
41 // FROM_HERE, base::Bind(...)); | 44 // FROM_HERE, base::Bind(...)); |
42 // | 45 // |
43 // You can make named sequence tokens to make it easier to share a token | 46 // You can make named sequence tokens to make it easier to share a token |
44 // across different components. | 47 // across different components. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 virtual void WillWaitForShutdown() = 0; | 128 virtual void WillWaitForShutdown() = 0; |
126 virtual void OnDestruct() = 0; | 129 virtual void OnDestruct() = 0; |
127 }; | 130 }; |
128 | 131 |
129 // Pass the maximum number of threads (they will be lazily created as needed) | 132 // 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. | 133 // and a prefix for the thread name to ad in debugging. |
131 SequencedWorkerPool(size_t max_threads, | 134 SequencedWorkerPool(size_t max_threads, |
132 const std::string& thread_name_prefix); | 135 const std::string& thread_name_prefix); |
133 | 136 |
134 // Returns a unique token that can be used to sequence tasks posted to | 137 // Returns a unique token that can be used to sequence tasks posted to |
135 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. | 138 // PostSequencedWorkerTask(). Valid tokens are always nonzero. |
136 SequenceToken GetSequenceToken(); | 139 SequenceToken GetSequenceToken(); |
137 | 140 |
138 // Returns the sequence token associated with the given name. Calling this | 141 // Returns the sequence token associated with the given name. Calling this |
139 // function multiple times with the same string will always produce the | 142 // 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 | 143 // same sequence token. If the name has not been used before, a new token |
141 // will be created. | 144 // will be created. |
142 SequenceToken GetNamedSequenceToken(const std::string& name); | 145 SequenceToken GetNamedSequenceToken(const std::string& name); |
143 | 146 |
| 147 // Returns a SequencedTaskRunner wrapper for this SequencedWorkerPool. |
| 148 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner(); |
| 149 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner( |
| 150 SequenceToken token); |
| 151 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner( |
| 152 const std::string& token_name); |
| 153 |
144 // Posts the given task for execution in the worker pool. Tasks posted with | 154 // Posts the given task for execution in the worker pool. Tasks posted with |
145 // this function will execute in an unspecified order on a background thread. | 155 // this function will execute in an unspecified order on a background thread. |
146 // Returns true if the task was posted. If your tasks have ordering | 156 // Returns true if the task was posted. If your tasks have ordering |
147 // requirements, see PostSequencedWorkerTask(). | 157 // requirements, see PostSequencedWorkerTask(). |
148 // | 158 // |
149 // This class will attempt to delete tasks that aren't run | 159 // This class will attempt to delete tasks that aren't run |
150 // (non-block-shutdown semantics) but can't guarantee that this happens. If | 160 // (non-block-shutdown semantics) but can't guarantee that this happens. If |
151 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there | 161 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there |
152 // will be no workers available to delete these tasks. And there may be | 162 // will be no workers available to delete these tasks. And there may be |
153 // tasks with the same sequence token behind those CONTINUE_ON_SHUTDOWN | 163 // tasks with the same sequence token behind those CONTINUE_ON_SHUTDOWN |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // Avoid pulling in too many headers by putting (almost) everything | 262 // Avoid pulling in too many headers by putting (almost) everything |
253 // into |inner_|. | 263 // into |inner_|. |
254 const scoped_ptr<Inner> inner_; | 264 const scoped_ptr<Inner> inner_; |
255 | 265 |
256 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 266 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
257 }; | 267 }; |
258 | 268 |
259 } // namespace base | 269 } // namespace base |
260 | 270 |
261 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 271 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
OLD | NEW |