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> |
fdoray | dafc341 | 2017-03-21 17:19:29 | [diff] [blame^] | 9 | #include <string> |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 10 | #include <vector> |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 11 | |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/callback_forward.h" |
| 14 | #include "base/memory/ref_counted.h" |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 15 | #include "base/sequenced_task_runner.h" |
| 16 | #include "base/single_thread_task_runner.h" |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 17 | #include "base/task_runner.h" |
| 18 | #include "base/task_scheduler/task_traits.h" |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame] | 19 | #include "base/time/time.h" |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 20 | |
fdoray | 1028b07d | 2017-01-31 20:36:14 | [diff] [blame] | 21 | namespace gin { |
| 22 | class V8Platform; |
| 23 | } |
| 24 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 25 | namespace tracked_objects { |
| 26 | class Location; |
| 27 | } |
| 28 | |
| 29 | namespace base { |
| 30 | |
robliao | 6f59a9a | 2016-10-20 17:26:14 | [diff] [blame] | 31 | class HistogramBase; |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 32 | class SchedulerWorkerPoolParams; |
| 33 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 34 | // Interface for a task scheduler and static methods to manage the instance used |
fdoray | 5bd4a9e1 | 2016-08-03 16:15:57 | [diff] [blame] | 35 | // by the post_task.h API. Note: all base/task_scheduler users should go through |
| 36 | // post_task.h instead of TaskScheduler except for the one callsite per process |
| 37 | // which manages the process' instance. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 38 | class BASE_EXPORT TaskScheduler { |
| 39 | public: |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 40 | // Returns the index of the worker pool in which a task with |traits| should |
| 41 | // run. This should be coded in a future-proof way: new traits should |
| 42 | // gracefully map to a default pool. |
| 43 | using WorkerPoolIndexForTraitsCallback = |
| 44 | Callback<size_t(const TaskTraits& traits)>; |
| 45 | |
fdoray | 7bba05e | 2017-01-25 02:34:45 | [diff] [blame] | 46 | // Destroying a TaskScheduler is not allowed in production; it is always |
| 47 | // leaked. In tests, it should only be destroyed after JoinForTesting() has |
| 48 | // returned. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 49 | virtual ~TaskScheduler() = default; |
| 50 | |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame] | 51 | // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 52 | // For one off tasks that don't require a TaskRunner. |
fdoray | 45cc9079 | 2017-01-05 19:24:31 | [diff] [blame] | 53 | virtual void PostDelayedTaskWithTraits( |
| 54 | const tracked_objects::Location& from_here, |
| 55 | const TaskTraits& traits, |
| 56 | const Closure& task, |
| 57 | TimeDelta delay) = 0; |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 58 | |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 59 | // Returns a TaskRunner whose PostTask invocations result in scheduling tasks |
| 60 | // using |traits|. Tasks may run in any order and in parallel. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 61 | virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
fdoray | e72adfa1 | 2016-11-02 14:35:26 | [diff] [blame] | 62 | const TaskTraits& traits) = 0; |
| 63 | |
| 64 | // Returns a SequencedTaskRunner whose PostTask invocations result in |
| 65 | // scheduling tasks using |traits|. Tasks run one at a time in posting order. |
| 66 | virtual scoped_refptr<SequencedTaskRunner> |
| 67 | CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits) = 0; |
| 68 | |
| 69 | // Returns a SingleThreadTaskRunner whose PostTask invocations result in |
| 70 | // scheduling tasks using |traits|. Tasks run on a single thread in posting |
| 71 | // order. |
| 72 | virtual scoped_refptr<SingleThreadTaskRunner> |
| 73 | CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits) = 0; |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 74 | |
robliao | 6f59a9a | 2016-10-20 17:26:14 | [diff] [blame] | 75 | // Returns a vector of all histograms available in this task scheduler. |
| 76 | virtual std::vector<const HistogramBase*> GetHistograms() const = 0; |
| 77 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 78 | // Synchronously shuts down the scheduler. Once this is called, only tasks |
| 79 | // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns: |
| 80 | // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
| 81 | // execution. |
| 82 | // - All posted BLOCK_SHUTDOWN tasks have completed their execution. |
| 83 | // - CONTINUE_ON_SHUTDOWN tasks might still be running. |
| 84 | // Note that an implementation can keep threads and other resources alive to |
| 85 | // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be |
| 86 | // called once. |
| 87 | virtual void Shutdown() = 0; |
| 88 | |
fdoray | fe309b0 | 2016-09-26 16:18:58 | [diff] [blame] | 89 | // Waits until there are no pending undelayed tasks. May be called in tests |
| 90 | // to validate that a condition is met after all undelayed tasks have run. |
| 91 | // |
| 92 | // Does not wait for delayed tasks. Waits for undelayed tasks posted from |
| 93 | // other threads during the call. Returns immediately when shutdown completes. |
| 94 | virtual void FlushForTesting() = 0; |
| 95 | |
fdoray | 7bba05e | 2017-01-25 02:34:45 | [diff] [blame] | 96 | // Joins all threads. Tasks that are already running are allowed to complete |
robliao | dc6f621 | 2017-02-01 23:13:41 | [diff] [blame] | 97 | // their execution. This can only be called once. Using this task scheduler |
| 98 | // instance to create task runners or post tasks is not permitted during or |
| 99 | // after this call. |
fdoray | 7bba05e | 2017-01-25 02:34:45 | [diff] [blame] | 100 | virtual void JoinForTesting() = 0; |
| 101 | |
fdoray | 9c7db371 | 2016-11-18 17:10:53 | [diff] [blame] | 102 | // CreateAndSetSimpleTaskScheduler(), CreateAndSetDefaultTaskScheduler(), and |
| 103 | // SetInstance() register a TaskScheduler to handle tasks posted through the |
| 104 | // post_task.h API for this process. The registered TaskScheduler will only be |
| 105 | // deleted when a new TaskScheduler is registered and is leaked on shutdown. |
| 106 | // The methods must not be called when TaskRunners created by the previous |
| 107 | // TaskScheduler are still alive. The methods are not thread-safe; proper |
| 108 | // synchronization is required to use the post_task.h API after registering a |
| 109 | // new TaskScheduler. |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 110 | |
fdoray | dafc341 | 2017-03-21 17:19:29 | [diff] [blame^] | 111 | #if !defined(OS_NACL) |
| 112 | // Creates and sets a task scheduler using default params. |name| is used to |
| 113 | // label threads and histograms. It should identify the component that calls |
| 114 | // this. CHECKs on failure. For tests, prefer base::test::ScopedTaskScheduler |
| 115 | // (ensures isolation). |
| 116 | static void CreateAndSetSimpleTaskScheduler(const std::string& name); |
| 117 | #endif // !defined(OS_NACL) |
fdoray | 9c7db371 | 2016-11-18 17:10:53 | [diff] [blame] | 118 | |
| 119 | // Creates and sets a task scheduler with custom worker pools. CHECKs on |
| 120 | // failure. |worker_pool_params_vector| describes the worker pools to create. |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 121 | // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| |
gab | 4fe88c1e | 2017-02-24 18:10:46 | [diff] [blame] | 122 | // of the worker pool in which a task with given traits should run. For tests, |
| 123 | // prefer base::test::ScopedTaskScheduler (ensures isolation). |
robliao | d2ab1f7 | 2016-08-02 20:51:49 | [diff] [blame] | 124 | static void CreateAndSetDefaultTaskScheduler( |
| 125 | const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, |
| 126 | const WorkerPoolIndexForTraitsCallback& |
| 127 | worker_pool_index_for_traits_callback); |
| 128 | |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 129 | // Registers |task_scheduler| to handle tasks posted through the post_task.h |
gab | 4fe88c1e | 2017-02-24 18:10:46 | [diff] [blame] | 130 | // API for this process. For tests, prefer base::test::ScopedTaskScheduler |
| 131 | // (ensures isolation). |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 132 | static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); |
| 133 | |
gab | 4fe88c1e | 2017-02-24 18:10:46 | [diff] [blame] | 134 | // Retrieve the TaskScheduler set via SetInstance() or |
| 135 | // CreateAndSet(Simple|Default)TaskScheduler(). This should be used very |
| 136 | // rarely; most users of TaskScheduler should use the post_task.h API. In |
| 137 | // particular, refrain from doing |
| 138 | // if (!TaskScheduler::GetInstance()) { |
| 139 | // TaskScheduler::SetInstance(...); |
| 140 | // base::PostTask(...); |
| 141 | // } |
| 142 | // instead make sure to SetInstance() early in one determinstic place in the |
| 143 | // process' initialization phase. |
| 144 | // In doubt, consult with //base/task_scheduler/OWNERS. |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 145 | static TaskScheduler* GetInstance(); |
fdoray | 1028b07d | 2017-01-31 20:36:14 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | friend class gin::V8Platform; |
| 149 | |
| 150 | // Returns the maximum number of non-single-threaded tasks posted with |
| 151 | // |traits| that can run concurrently in this TaskScheduler. |
| 152 | // |
| 153 | // Do not use this method. To process n items, post n tasks that each process |
| 154 | // 1 item rather than GetMaxConcurrentTasksWithTraitsDeprecated() tasks that |
| 155 | // each process n/GetMaxConcurrentTasksWithTraitsDeprecated() items. |
| 156 | // |
| 157 | // TODO(fdoray): Remove this method. https://crbug.com/687264 |
| 158 | virtual int GetMaxConcurrentTasksWithTraitsDeprecated( |
| 159 | const TaskTraits& traits) const = 0; |
fdoray | 67ecfb5 | 2016-05-02 14:49:03 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace base |
| 163 | |
| 164 | #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |