| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cstddef> | 10 #include <cstddef> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
| 16 #include "base/compiler_specific.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/single_thread_task_runner.h" | |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/task_scheduler/task_traits.h" | 20 #include "base/task_scheduler/task_traits.h" |
| 21 | 21 |
| 22 namespace tracked_objects { | 22 namespace tracked_objects { |
| 23 class Location; | 23 class Location; |
| 24 } // namespace tracked_objects | 24 } // namespace tracked_objects |
| 25 | 25 |
| 26 namespace base { | 26 namespace base { |
| 27 | 27 |
| 28 class SingleThreadTaskRunner; | 28 class SequencedTaskRunner; |
| 29 | 29 |
| 30 template <class T> class DeleteHelper; | 30 template <class T> class DeleteHelper; |
| 31 | 31 |
| 32 class SequencedTaskRunner; | |
| 33 | |
| 34 // A worker thread pool that enforces ordering between sets of tasks. It also | 32 // A worker thread pool that enforces ordering between sets of tasks. It also |
| 35 // allows you to specify what should happen to your tasks on shutdown. | 33 // allows you to specify what should happen to your tasks on shutdown. |
| 36 // | 34 // |
| 37 // To enforce ordering, get a unique sequence token from the pool and post all | 35 // To enforce ordering, get a unique sequence token from the pool and post all |
| 38 // tasks you want to order with the token. All tasks with the same token are | 36 // tasks you want to order with the token. All tasks with the same token are |
| 39 // guaranteed to execute serially, though not necessarily on the same thread. | 37 // guaranteed to execute serially, though not necessarily on the same thread. |
| 40 // This means that: | 38 // This means that: |
| 41 // | 39 // |
| 42 // - No two tasks with the same token will run at the same time. | 40 // - No two tasks with the same token will run at the same time. |
| 43 // | 41 // |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 // function multiple times with the same string will always produce the | 223 // function multiple times with the same string will always produce the |
| 226 // same sequence token. If the name has not been used before, a new token | 224 // same sequence token. If the name has not been used before, a new token |
| 227 // will be created. | 225 // will be created. |
| 228 SequenceToken GetNamedSequenceToken(const std::string& name); | 226 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 229 | 227 |
| 230 // Returns a SequencedTaskRunner wrapper which posts to this | 228 // Returns a SequencedTaskRunner wrapper which posts to this |
| 231 // SequencedWorkerPool using the given sequence token. Tasks with nonzero | 229 // SequencedWorkerPool using the given sequence token. Tasks with nonzero |
| 232 // delay are posted with SKIP_ON_SHUTDOWN behavior and tasks with zero delay | 230 // delay are posted with SKIP_ON_SHUTDOWN behavior and tasks with zero delay |
| 233 // are posted with BLOCK_SHUTDOWN behavior. | 231 // are posted with BLOCK_SHUTDOWN behavior. |
| 234 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner( | 232 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunner( |
| 235 SequenceToken token); | 233 SequenceToken token) WARN_UNUSED_RESULT; |
| 236 | 234 |
| 237 // Returns a SequencedTaskRunner wrapper which posts to this | 235 // Returns a SequencedTaskRunner wrapper which posts to this |
| 238 // SequencedWorkerPool using the given sequence token. Tasks with nonzero | 236 // SequencedWorkerPool using the given sequence token. Tasks with nonzero |
| 239 // delay are posted with SKIP_ON_SHUTDOWN behavior and tasks with zero delay | 237 // delay are posted with SKIP_ON_SHUTDOWN behavior and tasks with zero delay |
| 240 // are posted with the given shutdown behavior. | 238 // are posted with the given shutdown behavior. |
| 241 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunnerWithShutdownBehavior( | 239 scoped_refptr<SequencedTaskRunner> GetSequencedTaskRunnerWithShutdownBehavior( |
| 242 SequenceToken token, | 240 SequenceToken token, |
| 243 WorkerShutdown shutdown_behavior); | 241 WorkerShutdown shutdown_behavior) WARN_UNUSED_RESULT; |
| 244 | 242 |
| 245 // Returns a TaskRunner wrapper which posts to this SequencedWorkerPool using | 243 // Returns a TaskRunner wrapper which posts to this SequencedWorkerPool using |
| 246 // the given shutdown behavior. Tasks with nonzero delay are posted with | 244 // the given shutdown behavior. Tasks with nonzero delay are posted with |
| 247 // SKIP_ON_SHUTDOWN behavior and tasks with zero delay are posted with the | 245 // SKIP_ON_SHUTDOWN behavior and tasks with zero delay are posted with the |
| 248 // given shutdown behavior. | 246 // given shutdown behavior. |
| 249 scoped_refptr<TaskRunner> GetTaskRunnerWithShutdownBehavior( | 247 scoped_refptr<TaskRunner> GetTaskRunnerWithShutdownBehavior( |
| 250 WorkerShutdown shutdown_behavior); | 248 WorkerShutdown shutdown_behavior) WARN_UNUSED_RESULT; |
| 251 | 249 |
| 252 // Posts the given task for execution in the worker pool. Tasks posted with | 250 // Posts the given task for execution in the worker pool. Tasks posted with |
| 253 // this function will execute in an unspecified order on a background thread. | 251 // this function will execute in an unspecified order on a background thread. |
| 254 // Returns true if the task was posted. If your tasks have ordering | 252 // Returns true if the task was posted. If your tasks have ordering |
| 255 // requirements, see PostSequencedWorkerTask(). | 253 // requirements, see PostSequencedWorkerTask(). |
| 256 // | 254 // |
| 257 // This class will attempt to delete tasks that aren't run | 255 // This class will attempt to delete tasks that aren't run |
| 258 // (non-block-shutdown semantics) but can't guarantee that this happens. If | 256 // (non-block-shutdown semantics) but can't guarantee that this happens. If |
| 259 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there | 257 // all worker threads are busy running CONTINUE_ON_SHUTDOWN tasks, there |
| 260 // will be no workers available to delete these tasks. And there may be | 258 // will be no workers available to delete these tasks. And there may be |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 friend class DeleteHelper<SequencedWorkerPool>; | 389 friend class DeleteHelper<SequencedWorkerPool>; |
| 392 | 390 |
| 393 class Inner; | 391 class Inner; |
| 394 class PoolSequencedTaskRunner; | 392 class PoolSequencedTaskRunner; |
| 395 class Worker; | 393 class Worker; |
| 396 | 394 |
| 397 // Returns true if the current thread is processing a task with the given | 395 // Returns true if the current thread is processing a task with the given |
| 398 // sequence_token. | 396 // sequence_token. |
| 399 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; | 397 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; |
| 400 | 398 |
| 401 const scoped_refptr<SingleThreadTaskRunner> constructor_task_runner_; | 399 const scoped_refptr<SequencedTaskRunner> constructor_task_runner_; |
| 402 | 400 |
| 403 // Avoid pulling in too many headers by putting (almost) everything | 401 // Avoid pulling in too many headers by putting (almost) everything |
| 404 // into |inner_|. | 402 // into |inner_|. |
| 405 const std::unique_ptr<Inner> inner_; | 403 const std::unique_ptr<Inner> inner_; |
| 406 | 404 |
| 407 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 405 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 408 }; | 406 }; |
| 409 | 407 |
| 410 } // namespace base | 408 } // namespace base |
| 411 | 409 |
| 412 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 410 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |