Chromium Code Reviews| 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 |
|
fdoray
2016/08/02 15:01:29
Maybe write a comment that applies to both CreateA
robliao
2016/08/02 19:29:00
Done.
| |
| 60 // Creates and sets a default task scheduler. CHECKs on failure. | |
| 61 // |worker_pool_params_vector| describes the worker pools to create. | |
| 62 // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| | |
| 63 // of the worker pool in which a task with given traits should run. | |
| 64 static void CreateAndSetDefaultTaskScheduler( | |
| 65 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | |
| 66 const WorkerPoolIndexForTraitsCallback& | |
| 67 worker_pool_index_for_traits_callback); | |
| 68 | |
| 51 // Registers |task_scheduler| to handle tasks posted through the post_task.h | 69 // Registers |task_scheduler| to handle tasks posted through the post_task.h |
| 52 // API for this process. The registered TaskScheduler will only be deleted | 70 // API for this process. The registered TaskScheduler will only be deleted |
| 53 // when a new TaskScheduler is registered (i.e. otherwise leaked on shutdown). | 71 // when a new TaskScheduler is registered (i.e. otherwise leaked on shutdown). |
| 54 // This must not be called when TaskRunners created by the previous | 72 // This must not be called when TaskRunners created by the previous |
| 55 // TaskScheduler are still alive. This method is not thread-safe; proper | 73 // 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 | 74 // synchronization is required to use the post_task.h API after registering a |
| 57 // new TaskScheduler. | 75 // new TaskScheduler. |
| 58 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); | 76 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); |
| 59 | 77 |
| 60 // Retrieve the TaskScheduler set via SetInstance(). This should be used very | 78 // Retrieve the TaskScheduler set via SetInstance(). This should be used very |
|
fdoray
2016/08/02 15:01:29
via SetInstance() or CreateAndSetDefaultTaskSchedu
robliao
2016/08/02 19:29:00
Done.
| |
| 61 // rarely; most users of TaskScheduler should use the post_task.h API. | 79 // rarely; most users of TaskScheduler should use the post_task.h API. |
| 62 static TaskScheduler* GetInstance(); | 80 static TaskScheduler* GetInstance(); |
| 63 }; | 81 }; |
| 64 | 82 |
| 65 } // namespace base | 83 } // namespace base |
| 66 | 84 |
| 67 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 85 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| OLD | NEW |