| 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 <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
| 18 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 18 #include "base/task_scheduler/task_traits.h" | 19 #include "base/task_scheduler/task_traits.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "build/build_config.h" |
| 20 | 22 |
| 21 namespace gin { | 23 namespace gin { |
| 22 class V8Platform; | 24 class V8Platform; |
| 23 } | 25 } |
| 24 | 26 |
| 25 namespace tracked_objects { | 27 namespace tracked_objects { |
| 26 class Location; | 28 class Location; |
| 27 } | 29 } |
| 28 | 30 |
| 29 namespace base { | 31 namespace base { |
| 30 | 32 |
| 31 class HistogramBase; | 33 class HistogramBase; |
| 32 class SchedulerWorkerPoolParams; | 34 |
| 35 struct BASE_EXPORT TaskSchedulerInitParams { |
| 36 TaskSchedulerInitParams( |
| 37 const SchedulerWorkerPoolParams& background_worker_pool_params_in, |
| 38 const SchedulerWorkerPoolParams& |
| 39 background_blocking_worker_pool_params_in, |
| 40 const SchedulerWorkerPoolParams& foreground_worker_pool_params_in, |
| 41 const SchedulerWorkerPoolParams& |
| 42 foreground_blocking_worker_pool_params_in); |
| 43 |
| 44 // Returns true if this object can be used to initialize a task scheduler. |
| 45 bool IsValid() const; |
| 46 |
| 47 const SchedulerWorkerPoolParams background_worker_pool_params; |
| 48 const SchedulerWorkerPoolParams background_blocking_worker_pool_params; |
| 49 const SchedulerWorkerPoolParams foreground_worker_pool_params; |
| 50 const SchedulerWorkerPoolParams foreground_blocking_worker_pool_params; |
| 51 }; |
| 33 | 52 |
| 34 // Interface for a task scheduler and static methods to manage the instance used | 53 // Interface for a task scheduler and static methods to manage the instance used |
| 35 // by the post_task.h API. Note: all base/task_scheduler users should go through | 54 // 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 | 55 // post_task.h instead of TaskScheduler except for the one callsite per process |
| 37 // which manages the process' instance. | 56 // which manages the process' instance. |
| 38 class BASE_EXPORT TaskScheduler { | 57 class BASE_EXPORT TaskScheduler { |
| 39 public: | 58 public: |
| 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 | |
| 46 // Destroying a TaskScheduler is not allowed in production; it is always | 59 // Destroying a TaskScheduler is not allowed in production; it is always |
| 47 // leaked. In tests, it should only be destroyed after JoinForTesting() has | 60 // leaked. In tests, it should only be destroyed after JoinForTesting() has |
| 48 // returned. | 61 // returned. |
| 49 virtual ~TaskScheduler() = default; | 62 virtual ~TaskScheduler() = default; |
| 50 | 63 |
| 51 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. | 64 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. |
| 52 // For one off tasks that don't require a TaskRunner. | 65 // For one off tasks that don't require a TaskRunner. |
| 53 virtual void PostDelayedTaskWithTraits( | 66 virtual void PostDelayedTaskWithTraits( |
| 54 const tracked_objects::Location& from_here, | 67 const tracked_objects::Location& from_here, |
| 55 const TaskTraits& traits, | 68 const TaskTraits& traits, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // SetInstance() register a TaskScheduler to handle tasks posted through the | 116 // SetInstance() register a TaskScheduler to handle tasks posted through the |
| 104 // post_task.h API for this process. The registered TaskScheduler will only be | 117 // 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. | 118 // 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 | 119 // The methods must not be called when TaskRunners created by the previous |
| 107 // TaskScheduler are still alive. The methods are not thread-safe; proper | 120 // 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 | 121 // synchronization is required to use the post_task.h API after registering a |
| 109 // new TaskScheduler. | 122 // new TaskScheduler. |
| 110 | 123 |
| 111 #if !defined(OS_NACL) | 124 #if !defined(OS_NACL) |
| 112 // Creates and sets a task scheduler using default params. |name| is used to | 125 // 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 | 126 // label threads and histograms. It should identify the component that creates |
| 114 // this. CHECKs on failure. For tests, prefer base::test::ScopedTaskScheduler | 127 // the TaskScheduler. CHECKs on failure. For tests, prefer |
| 115 // (ensures isolation). | 128 // base::test::ScopedTaskScheduler (ensures isolation). |
| 129 // |
| 130 // This method is not available on NaCl because there is no way to get the |
| 131 // number of processors on that platform. |
| 116 static void CreateAndSetSimpleTaskScheduler(const std::string& name); | 132 static void CreateAndSetSimpleTaskScheduler(const std::string& name); |
| 117 #endif // !defined(OS_NACL) | 133 #endif // !defined(OS_NACL) |
| 118 | 134 |
| 119 // Creates and sets a task scheduler with custom worker pools. CHECKs on | 135 // Creates and sets a task scheduler using custom params. |name| is used to |
| 120 // failure. |worker_pool_params_vector| describes the worker pools to create. | 136 // label threads and histograms. It should identify the component that creates |
| 121 // |worker_pool_index_for_traits_callback| returns the index in |worker_pools| | 137 // the TaskScheduler. |init_params| is used to initialize the worker pools. |
| 122 // of the worker pool in which a task with given traits should run. For tests, | 138 // CHECKs on failure. For tests, prefer base::test::ScopedTaskScheduler |
| 123 // prefer base::test::ScopedTaskScheduler (ensures isolation). | 139 // (ensures isolation). |
| 124 static void CreateAndSetDefaultTaskScheduler( | 140 static void CreateAndSetDefaultTaskScheduler( |
| 125 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, | 141 const std::string& name, |
| 126 const WorkerPoolIndexForTraitsCallback& | 142 const TaskSchedulerInitParams& init_params); |
| 127 worker_pool_index_for_traits_callback); | |
| 128 | 143 |
| 129 // Registers |task_scheduler| to handle tasks posted through the post_task.h | 144 // Registers |task_scheduler| to handle tasks posted through the post_task.h |
| 130 // API for this process. For tests, prefer base::test::ScopedTaskScheduler | 145 // API for this process. For tests, prefer base::test::ScopedTaskScheduler |
| 131 // (ensures isolation). | 146 // (ensures isolation). |
| 132 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); | 147 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); |
| 133 | 148 |
| 134 // Retrieve the TaskScheduler set via SetInstance() or | 149 // Retrieve the TaskScheduler set via SetInstance() or |
| 135 // CreateAndSet(Simple|Default)TaskScheduler(). This should be used very | 150 // CreateAndSet(Simple|Default)TaskScheduler(). This should be used very |
| 136 // rarely; most users of TaskScheduler should use the post_task.h API. In | 151 // rarely; most users of TaskScheduler should use the post_task.h API. In |
| 137 // particular, refrain from doing | 152 // particular, refrain from doing |
| (...skipping 17 matching lines...) Expand all Loading... |
| 155 // each process n/GetMaxConcurrentTasksWithTraitsDeprecated() items. | 170 // each process n/GetMaxConcurrentTasksWithTraitsDeprecated() items. |
| 156 // | 171 // |
| 157 // TODO(fdoray): Remove this method. https://crbug.com/687264 | 172 // TODO(fdoray): Remove this method. https://crbug.com/687264 |
| 158 virtual int GetMaxConcurrentTasksWithTraitsDeprecated( | 173 virtual int GetMaxConcurrentTasksWithTraitsDeprecated( |
| 159 const TaskTraits& traits) const = 0; | 174 const TaskTraits& traits) const = 0; |
| 160 }; | 175 }; |
| 161 | 176 |
| 162 } // namespace base | 177 } // namespace base |
| 163 | 178 |
| 164 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 179 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| OLD | NEW |