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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 static SequenceToken GetSequenceTokenForCurrentThread(); | 165 static SequenceToken GetSequenceTokenForCurrentThread(); |
166 | 166 |
167 // Returns the SequencedWorkerPool that owns this thread, or null if the | 167 // Returns the SequencedWorkerPool that owns this thread, or null if the |
168 // current thread is not a SequencedWorkerPool worker thread. | 168 // current thread is not a SequencedWorkerPool worker thread. |
169 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); | 169 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); |
170 | 170 |
171 // Returns a unique token that can be used to sequence tasks posted to | 171 // Returns a unique token that can be used to sequence tasks posted to |
172 // PostSequencedWorkerTask(). Valid tokens are always nonzero. | 172 // PostSequencedWorkerTask(). Valid tokens are always nonzero. |
173 static SequenceToken GetSequenceToken(); | 173 static SequenceToken GetSequenceToken(); |
174 | 174 |
175 // Invoke this once on the main thread of a process, before any other threads | 175 // Invoke this to start redirecting tasks posted to this process' |
176 // are created and before any tasks are posted to that process' | 176 // SequencedWorkerPools to the registered TaskScheduler. This cannot be called |
177 // SequencedWorkerPools but after TaskScheduler was instantiated, to force all | 177 // after a task has been posted to a SequencedWorkerPool. This is not thread- |
178 // SequencedWorkerPools in that process to redirect their tasks to the | 178 // safe; proper synchronization is required to use any method of |
179 // TaskScheduler. Note: SequencedWorkerPool instances with |max_threads == 1| | 179 // SequencedWorkerPool after calling this. There must be a registered |
180 // will be special cased to send all of their work as | 180 // TaskScheduler when this is called. Ideally, call this on the main thread of |
181 // ExecutionMode::SINGLE_THREADED. | 181 // a process, before any other threads are created and before any tasks are |
| 182 // posted to that process' SequencedWorkerPools. |
| 183 // Note: SequencedWorkerPool instances with |max_threads == 1| will be special |
| 184 // cased to send all of their work as ExecutionMode::SINGLE_THREADED. |
182 // TODO(gab): Remove this if http://crbug.com/622400 fails | 185 // TODO(gab): Remove this if http://crbug.com/622400 fails |
183 // (SequencedWorkerPool will be phased out completely otherwise). | 186 // (SequencedWorkerPool will be phased out completely otherwise). |
184 static void RedirectSequencedWorkerPoolsToTaskSchedulerForProcess(); | 187 static void RedirectToTaskSchedulerForProcess(); |
| 188 |
| 189 // Stops redirecting tasks posted to this process' SequencedWorkerPools to the |
| 190 // registered TaskScheduler. Also allows RedirectToTaskSchedulerForProcess() |
| 191 // to be called again after tasks have been posted to this process' |
| 192 // SequencedWorkerPools. Calling this while there are active |
| 193 // SequencedWorkerPools isn't supported. This is not thread-safe; proper |
| 194 // synchronization is required to call any method of SequencedWorkerPool after |
| 195 // calling this. |
| 196 static void ResetRedirectToTaskSchedulerForProcessForTesting(); |
185 | 197 |
186 // When constructing a SequencedWorkerPool, there must be a | 198 // When constructing a SequencedWorkerPool, there must be a |
187 // ThreadTaskRunnerHandle on the current thread unless you plan to | 199 // ThreadTaskRunnerHandle on the current thread unless you plan to |
188 // deliberately leak it. | 200 // deliberately leak it. |
189 | 201 |
190 // Pass the maximum number of threads (they will be lazily created as needed) | 202 // Pass the maximum number of threads (they will be lazily created as needed) |
191 // and a prefix for the thread name to aid in debugging. |task_priority| will | 203 // and a prefix for the thread name to aid in debugging. |task_priority| will |
192 // be used to hint base::TaskScheduler for an experiment in which all | 204 // be used to hint base::TaskScheduler for an experiment in which all |
193 // SequencedWorkerPool tasks will be redirected to it in processes where a | 205 // SequencedWorkerPool tasks will be redirected to it in processes where a |
194 // base::TaskScheduler was instantiated. | 206 // base::TaskScheduler was instantiated. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 void FlushForTesting(); | 351 void FlushForTesting(); |
340 | 352 |
341 // Spuriously signal that there is work to be done. | 353 // Spuriously signal that there is work to be done. |
342 void SignalHasWorkForTesting(); | 354 void SignalHasWorkForTesting(); |
343 | 355 |
344 // Implements the worker pool shutdown. This should be called during app | 356 // Implements the worker pool shutdown. This should be called during app |
345 // shutdown, and will discard/join with appropriate tasks before returning. | 357 // shutdown, and will discard/join with appropriate tasks before returning. |
346 // After this call, subsequent calls to post tasks will fail. | 358 // After this call, subsequent calls to post tasks will fail. |
347 // | 359 // |
348 // Must be called from the same thread this object was constructed on. | 360 // Must be called from the same thread this object was constructed on. |
| 361 // |
| 362 // No-op when redirection to the TaskScheduler is enabled. You must shutdown |
| 363 // the TaskScheduler to wait for SKIP_ON_SHUTDOWN tasks that were already |
| 364 // running and for all BLOCK_SHUTDOWN tasks to complete their execution. |
349 void Shutdown() { Shutdown(0); } | 365 void Shutdown() { Shutdown(0); } |
350 | 366 |
351 // A variant that allows an arbitrary number of new blocking tasks to be | 367 // A variant that allows an arbitrary number of new blocking tasks to be |
352 // posted during shutdown. The tasks cannot be posted within the execution | 368 // posted during shutdown. The tasks cannot be posted within the execution |
353 // context of tasks whose shutdown behavior is not BLOCKING_SHUTDOWN. Once | 369 // context of tasks whose shutdown behavior is not BLOCKING_SHUTDOWN. Once |
354 // the limit is reached, subsequent calls to post task fail in all cases. | 370 // the limit is reached, subsequent calls to post task fail in all cases. |
355 // Must be called from the same thread this object was constructed on. | 371 // Must be called from the same thread this object was constructed on. |
| 372 // |
| 373 // No-op when redirection to the TaskScheduler is enabled. You must shutdown |
| 374 // the TaskScheduler to wait for SKIP_ON_SHUTDOWN tasks that were already |
| 375 // running and for all BLOCK_SHUTDOWN tasks to complete their execution. |
356 void Shutdown(int max_new_blocking_tasks_after_shutdown); | 376 void Shutdown(int max_new_blocking_tasks_after_shutdown); |
357 | 377 |
358 // Check if Shutdown was called for given threading pool. This method is used | 378 // Check if Shutdown was called for given threading pool. This method is used |
359 // for aborting time consuming operation to avoid blocking shutdown. | 379 // for aborting time consuming operation to avoid blocking shutdown. |
360 // | 380 // |
361 // Can be called from any thread. | 381 // Can be called from any thread. |
362 bool IsShutdownInProgress(); | 382 bool IsShutdownInProgress(); |
363 | 383 |
364 protected: | 384 protected: |
365 ~SequencedWorkerPool() override; | 385 ~SequencedWorkerPool() override; |
(...skipping 12 matching lines...) Expand all Loading... |
378 // Avoid pulling in too many headers by putting (almost) everything | 398 // Avoid pulling in too many headers by putting (almost) everything |
379 // into |inner_|. | 399 // into |inner_|. |
380 const std::unique_ptr<Inner> inner_; | 400 const std::unique_ptr<Inner> inner_; |
381 | 401 |
382 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); | 402 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); |
383 }; | 403 }; |
384 | 404 |
385 } // namespace base | 405 } // namespace base |
386 | 406 |
387 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ | 407 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ |
OLD | NEW |