fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| 6 | #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| 7 | |
| 8 | #include <memory> |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 9 | #include <vector> |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 10 | |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/callback_forward.h" |
| 13 | #include "base/memory/ref_counted.h" |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 14 | #include "base/sequenced_task_runner.h" |
| 15 | #include "base/single_thread_task_runner.h" |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 16 | #include "base/task_runner.h" |
| 17 | #include "base/task_scheduler/task_traits.h" |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame^] | 18 | #include "base/time/time.h" |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 19 | |
| 20 | namespace tracked_objects { |
| 21 | class Location; |
| 22 | } |
| 23 | |
| 24 | namespace base { |
| 25 | |
robliao | 6f59a9a | 2016-10-20 17:26:14 | [diff] [blame] | 26 | class HistogramBase; |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 27 | class SchedulerWorkerPoolParams; |
| 28 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 29 | // Interface for a task scheduler and static methods to manage the instance used |
fdoray | 5bd4a9e1 | 2016-08-03 16:15:57 | [diff] [blame] | 30 | // by the post_task.h API. Note: all base/task_scheduler users should go through |
| 31 | // post_task.h instead of TaskScheduler except for the one callsite per process |
| 32 | // which manages the process' instance. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 33 | class BASE_EXPORT TaskScheduler { |
| 34 | public: |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 35 | // Returns the index of the worker pool in which a task with |traits| should |
| 36 | // run. This should be coded in a future-proof way: new traits should |
| 37 | // gracefully map to a default pool. |
| 38 | using WorkerPoolIndexForTraitsCallback = |
| 39 | Callback<size_t(const TaskTraits& traits)>; |
| 40 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 41 | virtual ~TaskScheduler() = default; |
| 42 | |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame^] | 43 | // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 44 | // For one off tasks that don't require a TaskRunner. |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame^] | 45 | virtual void PostDelayedTaskWithTraits( |
| 46 | const tracked_objects::Location& from_here, |
| 47 | const TaskTraits& traits, |
| 48 | const Closure& task, |
| 49 | TimeDelta delay) = 0; |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 50 | |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 51 | // Returns a TaskRunner whose PostTask invocations result in scheduling tasks |
| 52 | // using |traits|. Tasks may run in any order and in parallel. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 53 | virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 54 | const TaskTraits& traits) = 0; |
| 55 | |
| 56 | // Returns a SequencedTaskRunner whose PostTask invocations result in |
| 57 | // scheduling tasks using |traits|. Tasks run one at a time in posting order. |
| 58 | virtual scoped_refptr<SequencedTaskRunner> |
| 59 | CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits) = 0; |
| 60 | |
| 61 | // Returns a SingleThreadTaskRunner whose PostTask invocations result in |
| 62 | // scheduling tasks using |traits|. Tasks run on a single thread in posting |
| 63 | // order. |
| 64 | virtual scoped_refptr<SingleThreadTaskRunner> |
| 65 | CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits) = 0; |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 66 | |
robliao | 6f59a9a | 2016-10-20 17:26:14 | [diff] [blame] | 67 | // Returns a vector of all histograms available in this task scheduler. |
| 68 | virtual std::vector<const HistogramBase*> GetHistograms() const = 0; |
| 69 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 70 | // Synchronously shuts down the scheduler. Once this is called, only tasks |
| 71 | // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns: |
| 72 | // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
| 73 | // execution. |
| 74 | // - All posted BLOCK_SHUTDOWN tasks have completed their execution. |
| 75 | // - CONTINUE_ON_SHUTDOWN tasks might still be running. |
| 76 | // Note that an implementation can keep threads and other resources alive to |
| 77 | // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be |
| 78 | // called once. |
| 79 | virtual void Shutdown() = 0; |
| 80 | |
fdoray | fe309b0 | 2016-09-26 16:18:58 | [diff] [blame] | 81 | // Waits until there are no pending undelayed tasks. May be called in tests |
| 82 | // to validate that a condition is met after all undelayed tasks have run. |
| 83 | // |
| 84 | // Does not wait for delayed tasks. Waits for undelayed tasks posted from |
| 85 | // other threads during the call. Returns immediately when shutdown completes. |
| 86 | virtual void FlushForTesting() = 0; |
| 87 | |
fdoray | 9c7db371 | 2016-11-18 17:10:53 | [diff] [blame] | 88 | // CreateAndSetSimpleTaskScheduler(), CreateAndSetDefaultTaskScheduler(), and |
| 89 | // SetInstance() register a TaskScheduler to handle tasks posted through the |
| 90 | // post_task.h API for this process. The registered TaskScheduler will only be |
| 91 | // deleted when a new TaskScheduler is registered and is leaked on shutdown. |
| 92 | // The methods must not be called when TaskRunners created by the previous |
| 93 | // TaskScheduler are still alive. The methods are not thread-safe; proper |
| 94 | // synchronization is required to use the post_task.h API after registering a |
| 95 | // new TaskScheduler. |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 96 | |
fdoray | 9c7db371 | 2016-11-18 17:10:53 | [diff] [blame] | 97 | // Creates and sets a task scheduler with one worker pool that can have up to |
| 98 | // |max_threads| threads. CHECKs on failure. |
| 99 | static void CreateAndSetSimpleTaskScheduler(int max_threads); |
| 100 | |
| 101 | // Creates and sets a task scheduler with custom worker pools. CHECKs on |
| 102 | // failure. |worker_pool_params_vector| describes the worker pools to create. |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 103 | // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| |
| 104 | // of the worker pool in which a task with given traits should run. |
| 105 | static void CreateAndSetDefaultTaskScheduler( |
| 106 | const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, |
| 107 | const WorkerPoolIndexForTraitsCallback& |
| 108 | worker_pool_index_for_traits_callback); |
| 109 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 110 | // Registers |task_scheduler| to handle tasks posted through the post_task.h |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 111 | // API for this process. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 112 | static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); |
| 113 | |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 114 | // Retrieve the TaskScheduler set via CreateAndSetDefaultTaskScheduler() or |
| 115 | // SetInstance(). This should be used very rarely; most users of TaskScheduler |
| 116 | // should use the post_task.h API. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 117 | static TaskScheduler* GetInstance(); |
| 118 | }; |
| 119 | |
| 120 | } // namespace base |
| 121 | |
| 122 | #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |