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