blob: 5d9344bcbad7e52502f645ce3001026621d1dbe2 [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"
12#include "base/callback_forward.h"
13#include "base/memory/ref_counted.h"
fdoraye72adfa12016-11-02 14:35:2614#include "base/sequenced_task_runner.h"
15#include "base/single_thread_task_runner.h"
fdoray67ecfb52016-05-02 14:49:0316#include "base/task_runner.h"
17#include "base/task_scheduler/task_traits.h"
fdoray45cc90792017-01-05 19:24:3118#include "base/time/time.h"
fdoray67ecfb52016-05-02 14:49:0319
20namespace tracked_objects {
21class Location;
22}
23
24namespace base {
25
robliao6f59a9a2016-10-20 17:26:1426class HistogramBase;
robliaod2ab1f72016-08-02 20:51:4927class SchedulerWorkerPoolParams;
28
fdoray67ecfb52016-05-02 14:49:0329// Interface for a task scheduler and static methods to manage the instance used
fdoray5bd4a9e12016-08-03 16:15:5730// 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.
fdoray67ecfb52016-05-02 14:49:0333class BASE_EXPORT TaskScheduler {
34 public:
robliaod2ab1f72016-08-02 20:51:4935 // 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
fdoray67ecfb52016-05-02 14:49:0341 virtual ~TaskScheduler() = default;
42
fdoray45cc90792017-01-05 19:24:3143 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero.
fdoray67ecfb52016-05-02 14:49:0344 // For one off tasks that don't require a TaskRunner.
fdoray45cc90792017-01-05 19:24:3145 virtual void PostDelayedTaskWithTraits(
46 const tracked_objects::Location& from_here,
47 const TaskTraits& traits,
48 const Closure& task,
49 TimeDelta delay) = 0;
fdoray67ecfb52016-05-02 14:49:0350
fdoraye72adfa12016-11-02 14:35:2651 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
52 // using |traits|. Tasks may run in any order and in parallel.
fdoray67ecfb52016-05-02 14:49:0353 virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
fdoraye72adfa12016-11-02 14:35:2654 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;
fdoray67ecfb52016-05-02 14:49:0366
robliao6f59a9a2016-10-20 17:26:1467 // Returns a vector of all histograms available in this task scheduler.
68 virtual std::vector<const HistogramBase*> GetHistograms() const = 0;
69
fdoray67ecfb52016-05-02 14:49:0370 // 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
fdorayfe309b02016-09-26 16:18:5881 // 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
fdoray9c7db3712016-11-18 17:10:5388 // 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.
robliaod2ab1f72016-08-02 20:51:4996
fdoray9c7db3712016-11-18 17:10:5397 // 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.
robliaod2ab1f72016-08-02 20:51:49103 // |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
fdoray67ecfb52016-05-02 14:49:03110 // Registers |task_scheduler| to handle tasks posted through the post_task.h
robliaod2ab1f72016-08-02 20:51:49111 // API for this process.
fdoray67ecfb52016-05-02 14:49:03112 static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler);
113
robliaod2ab1f72016-08-02 20:51:49114 // 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.
fdoray67ecfb52016-05-02 14:49:03117 static TaskScheduler* GetInstance();
118};
119
120} // namespace base
121
122#endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_