| 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, though not necessarily on the same thread. |
| 35 // | 37 // |
| 36 // Example: | 38 // Example: |
| 37 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); | 39 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken(); |
| 38 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 40 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Returns a unique token that can be used to sequence tasks posted to | 147 // Returns a unique token that can be used to sequence tasks posted to |
| 146 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. | 148 // PostSequencedWorkerTask(). Valid tokens are alwys nonzero. |
| 147 SequenceToken GetSequenceToken(); | 149 SequenceToken GetSequenceToken(); |
| 148 | 150 |
| 149 // Returns the sequence token associated with the given name. Calling this | 151 // Returns the sequence token associated with the given name. Calling this |
| 150 // function multiple times with the same string will always produce the | 152 // function multiple times with the same string will always produce the |
| 151 // same sequence token. If the name has not been used before, a new token | 153 // same sequence token. If the name has not been used before, a new token |
| 152 // will be created. | 154 // will be created. |
| 153 SequenceToken GetNamedSequenceToken(const std::string& name); | 155 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 154 | 156 |
| 157 // Returns a SequencedTaskRunner wrapper which posts to this |
| 158 // SequencedWorkerPool using the given sequence token. |
| 159 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner( |
| 160 SequenceToken token); |
| 161 |
| 155 // Posts the given task for execution in the worker pool. Tasks posted with | 162 // Posts the given task for execution in the worker pool. Tasks posted with |
| 156 // this function will execute in an unspecified order on a background thread. | 163 // this function will execute in an unspecified order on a background thread. |
| 157 // Returns true if the task was posted. If your tasks have ordering | 164 // Returns true if the task was posted. If your tasks have ordering |
| 158 // requirements, see PostSequencedWorkerTask(). | 165 // requirements, see PostSequencedWorkerTask(). |
| 159 // | 166 // |
| 160 // This class will attempt to delete tasks that aren't run | 167 // This class will attempt to delete tasks that aren't run |
| 161 // (non-block-shutdown semantics) but can't guarantee that this happens. If | 168 // (non-block-shutdown semantics) but can't guarantee that this happens. If |
| 162 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there | 169 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there |
| 163 // will be no workers available to delete these tasks. And there may be | 170 // will be no workers available to delete these tasks. And there may be |
| 164 // tasks with the same sequence token behind those CONTINUE_ON_SHUTDOWN | 171 // tasks with the same sequence token behind those CONTINUE_ON_SHUTDOWN |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // Avoid pulling in too many headers by putting (almost) everything | 267 // Avoid pulling in too many headers by putting (almost) everything |
| 261 // into |inner_|. | 268 // into |inner_|. |
| 262 const scoped_ptr<Inner> inner_; | 269 const scoped_ptr<Inner> inner_; |
| 263 | 270 |
| 264 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 271 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 265 }; | 272 }; |
| 266 | 273 |
| 267 } // namespace base | 274 } // namespace base |
| 268 | 275 |
| 269 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 276 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |