| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 
| 6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 
| 7 | 7 | 
| 8 #include <memory> | 8 #include <memory> | 
|  | 9 #include <vector> | 
| 9 | 10 | 
| 10 #include "base/base_export.h" | 11 #include "base/base_export.h" | 
| 11 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" | 
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" | 
| 13 #include "base/task_runner.h" | 14 #include "base/task_runner.h" | 
| 14 #include "base/task_scheduler/task_traits.h" | 15 #include "base/task_scheduler/task_traits.h" | 
| 15 | 16 | 
| 16 namespace tracked_objects { | 17 namespace tracked_objects { | 
| 17 class Location; | 18 class Location; | 
| 18 } | 19 } | 
| 19 | 20 | 
| 20 namespace base { | 21 namespace base { | 
| 21 | 22 | 
|  | 23 class SchedulerWorkerPoolParams; | 
|  | 24 | 
| 22 // Interface for a task scheduler and static methods to manage the instance used | 25 // Interface for a task scheduler and static methods to manage the instance used | 
| 23 // by the post_task.h API. | 26 // by the post_task.h API. | 
| 24 class BASE_EXPORT TaskScheduler { | 27 class BASE_EXPORT TaskScheduler { | 
| 25  public: | 28  public: | 
|  | 29   // Returns the index of the worker pool in which a task with |traits| should | 
|  | 30   // run. This should be coded in a future-proof way: new traits should | 
|  | 31   // gracefully map to a default pool. | 
|  | 32   using WorkerPoolIndexForTraitsCallback = | 
|  | 33       Callback<size_t(const TaskTraits& traits)>; | 
|  | 34 | 
| 26   virtual ~TaskScheduler() = default; | 35   virtual ~TaskScheduler() = default; | 
| 27 | 36 | 
| 28   // Posts |task| with specific |traits|. | 37   // Posts |task| with specific |traits|. | 
| 29   // For one off tasks that don't require a TaskRunner. | 38   // For one off tasks that don't require a TaskRunner. | 
| 30   virtual void PostTaskWithTraits(const tracked_objects::Location& from_here, | 39   virtual void PostTaskWithTraits(const tracked_objects::Location& from_here, | 
| 31                                   const TaskTraits& traits, | 40                                   const TaskTraits& traits, | 
| 32                                   const Closure& task) = 0; | 41                                   const Closure& task) = 0; | 
| 33 | 42 | 
| 34   // Returns a TaskRunner whose PostTask invocations will result in scheduling | 43   // Returns a TaskRunner whose PostTask invocations will result in scheduling | 
| 35   // Tasks with |traits| which will be executed according to |execution_mode|. | 44   // Tasks with |traits| which will be executed according to |execution_mode|. | 
| 36   virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 45   virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 
| 37       const TaskTraits& traits, | 46       const TaskTraits& traits, | 
| 38       ExecutionMode execution_mode) = 0; | 47       ExecutionMode execution_mode) = 0; | 
| 39 | 48 | 
| 40   // Synchronously shuts down the scheduler. Once this is called, only tasks | 49   // Synchronously shuts down the scheduler. Once this is called, only tasks | 
| 41   // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns: | 50   // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns: | 
| 42   // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their | 51   // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their | 
| 43   //   execution. | 52   //   execution. | 
| 44   // - All posted BLOCK_SHUTDOWN tasks have completed their execution. | 53   // - All posted BLOCK_SHUTDOWN tasks have completed their execution. | 
| 45   // - CONTINUE_ON_SHUTDOWN tasks might still be running. | 54   // - CONTINUE_ON_SHUTDOWN tasks might still be running. | 
| 46   // Note that an implementation can keep threads and other resources alive to | 55   // Note that an implementation can keep threads and other resources alive to | 
| 47   // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be | 56   // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be | 
| 48   // called once. | 57   // called once. | 
| 49   virtual void Shutdown() = 0; | 58   virtual void Shutdown() = 0; | 
| 50 | 59 | 
|  | 60   // CreateAndSetDefaultTaskScheduler() and SetInstance() register a | 
|  | 61   // TaskScheduler to handle tasks posted through the post_task.h API for this | 
|  | 62   // process. The registered TaskScheduler will only be deleted when a new | 
|  | 63   // TaskScheduler is registered and is leaked on shutdown. The methods must | 
|  | 64   // not be called when TaskRunners created by the previous TaskScheduler are | 
|  | 65   // still alive. The methods are not thread-safe; proper synchronization is | 
|  | 66   // required to use the post_task.h API after registering a new TaskScheduler. | 
|  | 67 | 
|  | 68   // Creates and sets a default task scheduler. CHECKs on failure. | 
|  | 69   // |worker_pool_params_vector| describes the worker pools to create. | 
|  | 70   // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| | 
|  | 71   // of the worker pool in which a task with given traits should run. | 
|  | 72   static void CreateAndSetDefaultTaskScheduler( | 
|  | 73       const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | 
|  | 74       const WorkerPoolIndexForTraitsCallback& | 
|  | 75           worker_pool_index_for_traits_callback); | 
|  | 76 | 
| 51   // Registers |task_scheduler| to handle tasks posted through the post_task.h | 77   // Registers |task_scheduler| to handle tasks posted through the post_task.h | 
| 52   // API for this process. The registered TaskScheduler will only be deleted | 78   // API for this process. | 
| 53   // when a new TaskScheduler is registered (i.e. otherwise leaked on shutdown). |  | 
| 54   // This must not be called when TaskRunners created by the previous |  | 
| 55   // TaskScheduler are still alive. This method is not thread-safe; proper |  | 
| 56   // synchronization is required to use the post_task.h API after registering a |  | 
| 57   // new TaskScheduler. |  | 
| 58   static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); | 79   static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); | 
| 59 | 80 | 
| 60   // Retrieve the TaskScheduler set via SetInstance(). This should be used very | 81   // Retrieve the TaskScheduler set via CreateAndSetDefaultTaskScheduler() or | 
| 61   // rarely; most users of TaskScheduler should use the post_task.h API. | 82   // SetInstance(). This should be used very rarely; most users of TaskScheduler | 
|  | 83   // should use the post_task.h API. | 
| 62   static TaskScheduler* GetInstance(); | 84   static TaskScheduler* GetInstance(); | 
| 63 }; | 85 }; | 
| 64 | 86 | 
| 65 }  // namespace base | 87 }  // namespace base | 
| 66 | 88 | 
| 67 #endif  // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 89 #endif  // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 
| OLD | NEW | 
|---|