| 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> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, | 52 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, |
| 53 // FROM_HERE, base::Bind(...)); | 53 // FROM_HERE, base::Bind(...)); |
| 54 // | 54 // |
| 55 // You can make named sequence tokens to make it easier to share a token | 55 // You can make named sequence tokens to make it easier to share a token |
| 56 // across different components. | 56 // across different components. |
| 57 // | 57 // |
| 58 // You can also post tasks to the pool without ordering using PostWorkerTask. | 58 // You can also post tasks to the pool without ordering using PostWorkerTask. |
| 59 // These will be executed in an unspecified order. The order of execution | 59 // These will be executed in an unspecified order. The order of execution |
| 60 // between tasks with different sequence tokens is also unspecified. | 60 // between tasks with different sequence tokens is also unspecified. |
| 61 // | 61 // |
| 62 // You must call EnableForProcess() or | |
| 63 // EnableWithRedirectionToTaskSchedulerForProcess() before starting to post | |
| 64 // tasks to a process' SequencedWorkerPools. | |
| 65 // | |
| 66 // This class may be leaked on shutdown to facilitate fast shutdown. The | 62 // This class may be leaked on shutdown to facilitate fast shutdown. The |
| 67 // expected usage, however, is to call Shutdown(), which correctly accounts | 63 // expected usage, however, is to call Shutdown(), which correctly accounts |
| 68 // for CONTINUE_ON_SHUTDOWN behavior and is required for BLOCK_SHUTDOWN | 64 // for CONTINUE_ON_SHUTDOWN behavior and is required for BLOCK_SHUTDOWN |
| 69 // behavior. | 65 // behavior. |
| 70 // | 66 // |
| 71 // Implementation note: This does not use a base::WorkerPool since that does | 67 // Implementation note: This does not use a base::WorkerPool since that does |
| 72 // not enforce shutdown semantics or allow us to specify how many worker | 68 // not enforce shutdown semantics or allow us to specify how many worker |
| 73 // threads to run. For the typical use case of random background work, we don't | 69 // threads to run. For the typical use case of random background work, we don't |
| 74 // necessarily want to be super aggressive about creating threads. | 70 // necessarily want to be super aggressive about creating threads. |
| 75 // | 71 // |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // DEPRECATED. Use SequencedTaskRunnerHandle::Get() instead. Consequentially | 171 // DEPRECATED. Use SequencedTaskRunnerHandle::Get() instead. Consequentially |
| 176 // the only remaining use case is in sequenced_task_runner_handle.cc to | 172 // the only remaining use case is in sequenced_task_runner_handle.cc to |
| 177 // implement that and will soon be removed along with SequencedWorkerPool: | 173 // implement that and will soon be removed along with SequencedWorkerPool: |
| 178 // http://crbug.com/622400. | 174 // http://crbug.com/622400. |
| 179 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); | 175 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); |
| 180 | 176 |
| 181 // Returns a unique token that can be used to sequence tasks posted to | 177 // Returns a unique token that can be used to sequence tasks posted to |
| 182 // PostSequencedWorkerTask(). Valid tokens are always nonzero. | 178 // PostSequencedWorkerTask(). Valid tokens are always nonzero. |
| 183 static SequenceToken GetSequenceToken(); | 179 static SequenceToken GetSequenceToken(); |
| 184 | 180 |
| 185 // Enables posting tasks to this process' SequencedWorkerPools. Cannot be | 181 // Starts redirecting tasks posted to this process' SequencedWorkerPools to |
| 186 // called if already enabled. This is not thread-safe; proper synchronization | 182 // the registered TaskScheduler. This cannot be called after a task has been |
| 187 // is required to use any SequencedWorkerPool method after calling this. | 183 // posted to a SequencedWorkerPool. This is not thread-safe; proper |
| 188 static void EnableForProcess(); | 184 // synchronization is required to use any SequencedWorkerPool method after |
| 189 | 185 // calling this. There must be a registered TaskScheduler when this is called. |
| 190 // Same as EnableForProcess(), but tasks are redirected to the registered | 186 // Ideally, call this on the main thread of a process, before any other |
| 191 // TaskScheduler. There must be a registered TaskScheduler when this is | 187 // threads are created and before any tasks are posted to that process' |
| 192 // called. | 188 // SequencedWorkerPools. |
| 193 // TODO(gab): Remove this if http://crbug.com/622400 fails | 189 // TODO(gab): Remove this if http://crbug.com/622400 fails |
| 194 // (SequencedWorkerPool will be phased out completely otherwise). | 190 // (SequencedWorkerPool will be phased out completely otherwise). |
| 195 static void EnableWithRedirectionToTaskSchedulerForProcess(); | 191 static void RedirectToTaskSchedulerForProcess(); |
| 196 | 192 |
| 197 // Disables posting tasks to this process' SequencedWorkerPools. Calling this | 193 // Stops redirecting tasks posted to this process' SequencedWorkerPools to the |
| 198 // while there are active SequencedWorkerPools is not supported. This is not | 194 // registered TaskScheduler and allows RedirectToTaskSchedulerForProcess() to |
| 199 // thread-safe; proper synchronization is required to use any | 195 // be called even if tasks have already posted to a SequencedWorkerPool in |
| 200 // SequencedWorkerPool method after calling this. | 196 // this process. Calling this while there are active SequencedWorkerPools is |
| 201 static void DisableForProcessForTesting(); | 197 // not supported. This is not thread-safe; proper synchronization is required |
| 202 | 198 // to use any SequencedWorkerPool method after calling this. |
| 203 // Returns true if posting tasks to this process' SequencedWorkerPool is | 199 static void ResetRedirectToTaskSchedulerForProcessForTesting(); |
| 204 // enabled (with or without redirection to TaskScheduler). | |
| 205 static bool IsEnabled(); | |
| 206 | 200 |
| 207 // When constructing a SequencedWorkerPool, there must be a | 201 // When constructing a SequencedWorkerPool, there must be a |
| 208 // ThreadTaskRunnerHandle on the current thread unless you plan to | 202 // ThreadTaskRunnerHandle on the current thread unless you plan to |
| 209 // deliberately leak it. | 203 // deliberately leak it. |
| 210 | 204 |
| 211 // Constructs a SequencedWorkerPool which will lazily create up to | 205 // Constructs a SequencedWorkerPool which will lazily create up to |
| 212 // |max_threads| and a prefix for the thread name to aid in debugging. | 206 // |max_threads| and a prefix for the thread name to aid in debugging. |
| 213 // |max_threads| must be greater than 1. |task_priority| will be used to hint | 207 // |max_threads| must be greater than 1. |task_priority| will be used to hint |
| 214 // base::TaskScheduler for an experiment in which all SequencedWorkerPool | 208 // base::TaskScheduler for an experiment in which all SequencedWorkerPool |
| 215 // tasks will be redirected to it in processes where a base::TaskScheduler was | 209 // tasks will be redirected to it in processes where a base::TaskScheduler was |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 // Avoid pulling in too many headers by putting (almost) everything | 401 // Avoid pulling in too many headers by putting (almost) everything |
| 408 // into |inner_|. | 402 // into |inner_|. |
| 409 const std::unique_ptr<Inner> inner_; | 403 const std::unique_ptr<Inner> inner_; |
| 410 | 404 |
| 411 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 405 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
| 412 }; | 406 }; |
| 413 | 407 |
| 414 } // namespace base | 408 } // namespace base |
| 415 | 409 |
| 416 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 410 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
| OLD | NEW |