blob: 0a7949011c6607a33fc08653e0b42b4cf48d1621 [file] [log] [blame]
fdoray67ecfb52016-05-02 14:49:031// 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>
robliaod2ab1f72016-08-02 20:51:499#include <vector>
fdoray67ecfb52016-05-02 14:49:0310
11#include "base/base_export.h"
tzik070c8ffb2017-03-29 05:28:1212#include "base/callback.h"
Francois Doray6d3c649692017-06-16 19:20:2513#include "base/gtest_prod_util.h"
fdoray67ecfb52016-05-02 14:49:0314#include "base/memory/ref_counted.h"
fdoraye72adfa12016-11-02 14:35:2615#include "base/sequenced_task_runner.h"
16#include "base/single_thread_task_runner.h"
fdoray5d16e802017-04-19 14:45:1617#include "base/strings/string_piece.h"
fdoray67ecfb52016-05-02 14:49:0318#include "base/task_runner.h"
fdoray3f6f57a42017-03-28 18:02:3919#include "base/task_scheduler/scheduler_worker_pool_params.h"
robliao94520d72017-05-12 12:43:3520#include "base/task_scheduler/single_thread_task_runner_thread_mode.h"
fdoray67ecfb52016-05-02 14:49:0321#include "base/task_scheduler/task_traits.h"
fdoray45cc90792017-01-05 19:24:3122#include "base/time/time.h"
robliao75dd50b2017-03-29 17:11:1723#include "build/build_config.h"
fdoray67ecfb52016-05-02 14:49:0324
fdoray1028b07d2017-01-31 20:36:1425namespace gin {
26class V8Platform;
27}
28
Francois Doray6d3c649692017-06-16 19:20:2529namespace content {
30// Can't use the FRIEND_TEST_ALL_PREFIXES macro because the test is in a
31// different namespace.
32class BrowserMainLoopTest_CreateThreadsInSingleProcess_Test;
33} // namespace content
34
fdoray67ecfb52016-05-02 14:49:0335namespace base {
36
robliao6f59a9a2016-10-20 17:26:1437class HistogramBase;
Brett Wilsonabbb9602017-09-11 23:26:3938class Location;
robliaod2ab1f72016-08-02 20:51:4939
fdoray67ecfb52016-05-02 14:49:0340// Interface for a task scheduler and static methods to manage the instance used
fdoray0f358ee2017-04-25 21:22:2341// by the post_task.h API.
42//
43// The task scheduler doesn't create threads until Start() is called. Tasks can
44// be posted at any time but will not run until after Start() is called.
45//
46// The instance methods of this class are thread-safe.
47//
48// Note: All base/task_scheduler users should go through post_task.h instead of
49// TaskScheduler except for the one callsite per process which manages the
50// process's instance.
fdoray67ecfb52016-05-02 14:49:0351class BASE_EXPORT TaskScheduler {
52 public:
fdoray3f6f57a42017-03-28 18:02:3953 struct BASE_EXPORT InitParams {
54 InitParams(
55 const SchedulerWorkerPoolParams& background_worker_pool_params_in,
56 const SchedulerWorkerPoolParams&
57 background_blocking_worker_pool_params_in,
58 const SchedulerWorkerPoolParams& foreground_worker_pool_params_in,
59 const SchedulerWorkerPoolParams&
Francois Dorayfbfcc6b2017-08-25 19:44:5860 foreground_blocking_worker_pool_params_in);
fdoray3f6f57a42017-03-28 18:02:3961 ~InitParams();
62
Francois Doray6d3c649692017-06-16 19:20:2563 SchedulerWorkerPoolParams background_worker_pool_params;
64 SchedulerWorkerPoolParams background_blocking_worker_pool_params;
65 SchedulerWorkerPoolParams foreground_worker_pool_params;
66 SchedulerWorkerPoolParams foreground_blocking_worker_pool_params;
fdoray3f6f57a42017-03-28 18:02:3967 };
68
fdoray7bba05e2017-01-25 02:34:4569 // Destroying a TaskScheduler is not allowed in production; it is always
70 // leaked. In tests, it should only be destroyed after JoinForTesting() has
71 // returned.
fdoray67ecfb52016-05-02 14:49:0372 virtual ~TaskScheduler() = default;
73
fdoray0f358ee2017-04-25 21:22:2374 // Allows the task scheduler to create threads and run tasks following the
75 // |init_params| specification. CHECKs on failure.
76 virtual void Start(const InitParams& init_params) = 0;
77
fdoray45cc90792017-01-05 19:24:3178 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero.
fdoray67ecfb52016-05-02 14:49:0379 // For one off tasks that don't require a TaskRunner.
Brett Wilsonabbb9602017-09-11 23:26:3980 virtual void PostDelayedTaskWithTraits(const Location& from_here,
81 const TaskTraits& traits,
82 OnceClosure task,
83 TimeDelta delay) = 0;
fdoray67ecfb52016-05-02 14:49:0384
fdoraye72adfa12016-11-02 14:35:2685 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
86 // using |traits|. Tasks may run in any order and in parallel.
fdoray67ecfb52016-05-02 14:49:0387 virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
fdoraye72adfa12016-11-02 14:35:2688 const TaskTraits& traits) = 0;
89
90 // Returns a SequencedTaskRunner whose PostTask invocations result in
91 // scheduling tasks using |traits|. Tasks run one at a time in posting order.
92 virtual scoped_refptr<SequencedTaskRunner>
93 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits) = 0;
94
95 // Returns a SingleThreadTaskRunner whose PostTask invocations result in
96 // scheduling tasks using |traits|. Tasks run on a single thread in posting
97 // order.
98 virtual scoped_refptr<SingleThreadTaskRunner>
robliao94520d72017-05-12 12:43:3599 CreateSingleThreadTaskRunnerWithTraits(
100 const TaskTraits& traits,
robliao0c097192017-05-16 13:56:37101 SingleThreadTaskRunnerThreadMode thread_mode) = 0;
fdoray67ecfb52016-05-02 14:49:03102
robliao75dd50b2017-03-29 17:11:17103#if defined(OS_WIN)
104 // Returns a SingleThreadTaskRunner whose PostTask invocations result in
105 // scheduling tasks using |traits| in a COM Single-Threaded Apartment. Tasks
106 // run in the same Single-Threaded Apartment in posting order for the returned
107 // SingleThreadTaskRunner. There is not necessarily a one-to-one
108 // correspondence between SingleThreadTaskRunners and Single-Threaded
109 // Apartments. The implementation is free to share apartments or create new
110 // apartments as necessary. In either case, care should be taken to make sure
111 // COM pointers are not smuggled across apartments.
112 virtual scoped_refptr<SingleThreadTaskRunner>
robliao94520d72017-05-12 12:43:35113 CreateCOMSTATaskRunnerWithTraits(
114 const TaskTraits& traits,
robliao0c097192017-05-16 13:56:37115 SingleThreadTaskRunnerThreadMode thread_mode) = 0;
robliao75dd50b2017-03-29 17:11:17116#endif // defined(OS_WIN)
117
robliao6f59a9a2016-10-20 17:26:14118 // Returns a vector of all histograms available in this task scheduler.
119 virtual std::vector<const HistogramBase*> GetHistograms() const = 0;
120
fdoray67ecfb52016-05-02 14:49:03121 // Synchronously shuts down the scheduler. Once this is called, only tasks
122 // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns:
123 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their
124 // execution.
125 // - All posted BLOCK_SHUTDOWN tasks have completed their execution.
126 // - CONTINUE_ON_SHUTDOWN tasks might still be running.
127 // Note that an implementation can keep threads and other resources alive to
128 // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be
129 // called once.
130 virtual void Shutdown() = 0;
131
fdorayfe309b02016-09-26 16:18:58132 // Waits until there are no pending undelayed tasks. May be called in tests
133 // to validate that a condition is met after all undelayed tasks have run.
134 //
135 // Does not wait for delayed tasks. Waits for undelayed tasks posted from
136 // other threads during the call. Returns immediately when shutdown completes.
137 virtual void FlushForTesting() = 0;
138
fdoray7bba05e2017-01-25 02:34:45139 // Joins all threads. Tasks that are already running are allowed to complete
robliaodc6f6212017-02-01 23:13:41140 // their execution. This can only be called once. Using this task scheduler
141 // instance to create task runners or post tasks is not permitted during or
142 // after this call.
fdoray7bba05e2017-01-25 02:34:45143 virtual void JoinForTesting() = 0;
144
fdoray43ec6df2017-04-25 23:16:12145// CreateAndStartWithDefaultParams(), Create(), and SetInstance() register a
146// TaskScheduler to handle tasks posted through the post_task.h API for this
147// process.
148//
149// Processes that need to initialize TaskScheduler with custom params or that
150// need to allow tasks to be posted before the TaskScheduler creates its
151// threads should use Create() followed by Start(). Other processes can use
152// CreateAndStartWithDefaultParams().
153//
154// A registered TaskScheduler is only deleted when a new TaskScheduler is
155// registered. The last registered TaskScheduler is leaked on shutdown. The
156// methods below must not be called when TaskRunners created by a previous
157// TaskScheduler are still alive. The methods are not thread-safe; proper
158// synchronization is required to use the post_task.h API after registering a
159// new TaskScheduler.
robliaod2ab1f72016-08-02 20:51:49160
fdoraydafc3412017-03-21 17:19:29161#if !defined(OS_NACL)
fdoray43ec6df2017-04-25 23:16:12162 // Creates and starts a task scheduler using default params. |name| is used to
fdoraydafc3412017-03-21 17:19:29163 // label threads and histograms. It should identify the component that calls
fdoray43ec6df2017-04-25 23:16:12164 // this. Start() is called by this method; it is invalid to call it again
165 // afterwards. CHECKs on failure. For tests, prefer
166 // base::test::ScopedTaskEnvironment (ensures isolation).
167 static void CreateAndStartWithDefaultParams(StringPiece name);
fdoraydafc3412017-03-21 17:19:29168#endif // !defined(OS_NACL)
fdoray9c7db3712016-11-18 17:10:53169
fdoray43ec6df2017-04-25 23:16:12170 // Creates a ready to start task scheduler. |name| is used to label threads
171 // and histograms. It should identify the component that creates the
172 // TaskScheduler. The task scheduler doesn't create threads until Start() is
173 // called. Tasks can be posted at any time but will not run until after
174 // Start() is called. For tests, prefer base::test::ScopedTaskEnvironment
fdoray3f6f57a42017-03-28 18:02:39175 // (ensures isolation).
fdoray43ec6df2017-04-25 23:16:12176 static void Create(StringPiece name);
177
fdoray67ecfb52016-05-02 14:49:03178 // Registers |task_scheduler| to handle tasks posted through the post_task.h
Francois Dorayc4c3f642017-08-04 14:56:22179 // API for this process. For tests, prefer base::test::ScopedTaskEnvironment
gab4fe88c1e2017-02-24 18:10:46180 // (ensures isolation).
fdoray67ecfb52016-05-02 14:49:03181 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler);
182
gab4fe88c1e2017-02-24 18:10:46183 // Retrieve the TaskScheduler set via SetInstance() or
184 // CreateAndSet(Simple|Default)TaskScheduler(). This should be used very
185 // rarely; most users of TaskScheduler should use the post_task.h API. In
186 // particular, refrain from doing
187 // if (!TaskScheduler::GetInstance()) {
188 // TaskScheduler::SetInstance(...);
189 // base::PostTask(...);
190 // }
191 // instead make sure to SetInstance() early in one determinstic place in the
192 // process' initialization phase.
193 // In doubt, consult with //base/task_scheduler/OWNERS.
fdoray67ecfb52016-05-02 14:49:03194 static TaskScheduler* GetInstance();
fdoray1028b07d2017-01-31 20:36:14195
196 private:
197 friend class gin::V8Platform;
Francois Doray6d3c649692017-06-16 19:20:25198 friend class content::BrowserMainLoopTest_CreateThreadsInSingleProcess_Test;
fdoray1028b07d2017-01-31 20:36:14199
Jeffrey Hed628419b2017-08-23 18:51:51200 // Returns the maximum number of non-single-threaded non-blocked tasks posted
201 // with |traits| that can run concurrently in this TaskScheduler.
fdoray1028b07d2017-01-31 20:36:14202 //
203 // Do not use this method. To process n items, post n tasks that each process
Jeffrey Hed628419b2017-08-23 18:51:51204 // 1 item rather than GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated()
205 // tasks that each process
206 // n/GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated() items.
fdoray1028b07d2017-01-31 20:36:14207 //
208 // TODO(fdoray): Remove this method. https://crbug.com/687264
Jeffrey Hed628419b2017-08-23 18:51:51209 virtual int GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated(
fdoray1028b07d2017-01-31 20:36:14210 const TaskTraits& traits) const = 0;
fdoray67ecfb52016-05-02 14:49:03211};
212
213} // namespace base
214
215#endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_