blob: 4b07c88998ba922f46914f7f88055b939e1947cc [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>
fdoraydafc3412017-03-21 17:19:299#include <string>
robliaod2ab1f72016-08-02 20:51:4910#include <vector>
fdoray67ecfb52016-05-02 14:49:0311
12#include "base/base_export.h"
13#include "base/callback_forward.h"
14#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"
fdoray67ecfb52016-05-02 14:49:0317#include "base/task_runner.h"
18#include "base/task_scheduler/task_traits.h"
fdoray45cc90792017-01-05 19:24:3119#include "base/time/time.h"
fdoray67ecfb52016-05-02 14:49:0320
fdoray1028b07d2017-01-31 20:36:1421namespace gin {
22class V8Platform;
23}
24
fdoray67ecfb52016-05-02 14:49:0325namespace tracked_objects {
26class Location;
27}
28
29namespace base {
30
robliao6f59a9a2016-10-20 17:26:1431class HistogramBase;
robliaod2ab1f72016-08-02 20:51:4932class SchedulerWorkerPoolParams;
33
fdoray67ecfb52016-05-02 14:49:0334// Interface for a task scheduler and static methods to manage the instance used
fdoray5bd4a9e12016-08-03 16:15:5735// 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.
fdoray67ecfb52016-05-02 14:49:0338class BASE_EXPORT TaskScheduler {
39 public:
robliaod2ab1f72016-08-02 20:51:4940 // 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
fdoray7bba05e2017-01-25 02:34:4546 // 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.
fdoray67ecfb52016-05-02 14:49:0349 virtual ~TaskScheduler() = default;
50
fdoray45cc90792017-01-05 19:24:3151 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero.
fdoray67ecfb52016-05-02 14:49:0352 // For one off tasks that don't require a TaskRunner.
fdoray45cc90792017-01-05 19:24:3153 virtual void PostDelayedTaskWithTraits(
54 const tracked_objects::Location& from_here,
55 const TaskTraits& traits,
56 const Closure& task,
57 TimeDelta delay) = 0;
fdoray67ecfb52016-05-02 14:49:0358
fdoraye72adfa12016-11-02 14:35:2659 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
60 // using |traits|. Tasks may run in any order and in parallel.
fdoray67ecfb52016-05-02 14:49:0361 virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
fdoraye72adfa12016-11-02 14:35:2662 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;
fdoray67ecfb52016-05-02 14:49:0374
robliao6f59a9a2016-10-20 17:26:1475 // Returns a vector of all histograms available in this task scheduler.
76 virtual std::vector<const HistogramBase*> GetHistograms() const = 0;
77
fdoray67ecfb52016-05-02 14:49:0378 // 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
fdorayfe309b02016-09-26 16:18:5889 // 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
fdoray7bba05e2017-01-25 02:34:4596 // Joins all threads. Tasks that are already running are allowed to complete
robliaodc6f6212017-02-01 23:13:4197 // 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.
fdoray7bba05e2017-01-25 02:34:45100 virtual void JoinForTesting() = 0;
101
fdoray9c7db3712016-11-18 17:10:53102 // 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.
robliaod2ab1f72016-08-02 20:51:49110
fdoraydafc3412017-03-21 17:19:29111#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)
fdoray9c7db3712016-11-18 17:10:53118
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.
robliaod2ab1f72016-08-02 20:51:49121 // |worker_pool_index_for_traits_callback| returns the index in |worker_pools|
gab4fe88c1e2017-02-24 18:10:46122 // of the worker pool in which a task with given traits should run. For tests,
123 // prefer base::test::ScopedTaskScheduler (ensures isolation).
robliaod2ab1f72016-08-02 20:51:49124 static void CreateAndSetDefaultTaskScheduler(
125 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector,
126 const WorkerPoolIndexForTraitsCallback&
127 worker_pool_index_for_traits_callback);
128
fdoray67ecfb52016-05-02 14:49:03129 // Registers |task_scheduler| to handle tasks posted through the post_task.h
gab4fe88c1e2017-02-24 18:10:46130 // API for this process. For tests, prefer base::test::ScopedTaskScheduler
131 // (ensures isolation).
fdoray67ecfb52016-05-02 14:49:03132 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler);
133
gab4fe88c1e2017-02-24 18:10:46134 // 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.
fdoray67ecfb52016-05-02 14:49:03145 static TaskScheduler* GetInstance();
fdoray1028b07d2017-01-31 20:36:14146
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;
fdoray67ecfb52016-05-02 14:49:03160};
161
162} // namespace base
163
164#endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_