1 // Copyright 2015 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.
5 #ifndef COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_H_
6 #define COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_H_
8 #include "base/message_loop/message_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "components/scheduler/scheduler_export.h"
14 class SCHEDULER_EXPORT TaskQueue
: public base::SingleThreadTaskRunner
{
18 // Unregisters the task queue after which no tasks posted to it will run and
19 // the TaskQueueManager's reference to it will be released soon.
20 virtual void UnregisterTaskQueue() = 0;
22 // Post a delayed task at an absolute desired run time instead of a time
23 // delta from the current time.
24 virtual bool PostDelayedTaskAt(const tracked_objects::Location
& from_here
,
25 const base::Closure
& task
,
26 base::TimeTicks desired_run_time
) = 0;
29 // Queues with control priority will run before any other queue, and will
30 // explicitly starve other queues. Typically this should only be used for
31 // private queues which perform control operations.
33 // Queues with high priority will be selected preferentially over normal or
34 // best effort queues. The selector will ensure that high priority queues
35 // cannot completely starve normal priority queues.
37 // Queues with normal priority are the default.
39 // Queues with best effort priority will only be run if all other queues are
40 // empty. They can be starved by the other queues.
42 // Queues with this priority are never run. Must be penultimate entry.
44 // Must be the last entry.
46 FIRST_QUEUE_PRIORITY
= CONTROL_PRIORITY
,
49 // Keep TaskQueue::PumpPolicyToString in sync with this enum.
50 enum class PumpPolicy
{
51 // Tasks posted to an incoming queue with an AUTO pump policy will be
52 // automatically scheduled for execution or transferred to the work queue
55 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy
56 // will be scheduled for execution or transferred to the work queue
57 // automatically but only after another queue has executed a task.
59 // Tasks posted to an incoming queue with a MANUAL will not be
60 // automatically scheduled for execution or transferred to the work queue.
61 // Instead, the selector should call PumpQueue() when necessary to bring
62 // in new tasks for execution.
64 // Must be last entry.
66 FIRST_PUMP_POLICY
= AUTO
,
69 // Keep TaskQueue::WakeupPolicyToString in sync with this enum.
70 enum class WakeupPolicy
{
71 // Tasks run on a queue with CAN_WAKE_OTHER_QUEUES wakeup policy can
72 // cause queues with the AFTER_WAKEUP PumpPolicy to be woken up.
73 CAN_WAKE_OTHER_QUEUES
,
74 // Tasks run on a queue with DONT_WAKE_OTHER_QUEUES won't cause queues
75 // with the AFTER_WAKEUP PumpPolicy to be woken up.
76 DONT_WAKE_OTHER_QUEUES
,
77 // Must be last entry.
79 FIRST_WAKEUP_POLICY
= CAN_WAKE_OTHER_QUEUES
,
82 enum class QueueState
{
83 // A queue in the EMPTY state is empty and has no tasks in either the
84 // work or incoming task queue.
86 // A queue in the NEEDS_PUMPING state has no tasks in the work task queue,
87 // but has tasks in the incoming task queue which can be pumped to make them
90 // A queue in the HAS_WORK state has tasks in the work task queue which
95 // Options for constructing a TaskQueue. Once set the |name|,
96 // |should_monitor_quiescence| and |wakeup_policy| are immutable. The
97 // |pump_policy| can be mutated with |SetPumpPolicy()|.
99 // Note |name| must have application lifetime.
100 explicit Spec(const char* name
)
102 should_monitor_quiescence(false),
103 pump_policy(TaskQueue::PumpPolicy::AUTO
),
104 wakeup_policy(TaskQueue::WakeupPolicy::CAN_WAKE_OTHER_QUEUES
),
105 should_notify_observers(true) {}
107 Spec
SetShouldMonitorQuiescence(bool should_monitor
) {
108 should_monitor_quiescence
= should_monitor
;
112 Spec
SetPumpPolicy(PumpPolicy policy
) {
113 pump_policy
= policy
;
117 Spec
SetWakeupPolicy(WakeupPolicy policy
) {
118 wakeup_policy
= policy
;
122 Spec
SetShouldNotifyObservers(bool run_observers
) {
123 should_notify_observers
= run_observers
;
128 bool should_monitor_quiescence
;
129 TaskQueue::PumpPolicy pump_policy
;
130 TaskQueue::WakeupPolicy wakeup_policy
;
131 bool should_notify_observers
;
134 // Returns true if the queue priority is not
135 // TaskQueueSelector::DISABLED_PRIORITY. NOTE this must be called on the
136 // thread this TaskQueue was created by.
137 virtual bool IsQueueEnabled() const = 0;
139 // Returns true if there no tasks in either the work or incoming task queue.
140 // Note that this function involves taking a lock, so calling it has some
141 // overhead. NOTE this must be called on the thread this TaskQueue was created
143 virtual bool IsQueueEmpty() const;
145 // Returns the QueueState. Note that this function involves taking a lock, so
146 // calling it has some overhead.
147 virtual QueueState
GetQueueState() const = 0;
149 // Can be called on any thread.
150 virtual const char* GetName() const = 0;
152 // Set the priority of the queue to |priority|. NOTE this must be called on
153 // the thread this TaskQueue was created by.
154 virtual void SetQueuePriority(QueuePriority priority
) = 0;
156 // Set the pumping policy of the queue to |pump_policy|. NOTE this must be
157 // called on the thread this TaskQueue was created by.
158 virtual void SetPumpPolicy(PumpPolicy pump_policy
) = 0;
160 // Reloads new tasks from the incoming queue into the work queue, regardless
161 // of whether the work queue is empty or not. After this, the function ensures
162 // that the tasks in the work queue, if any, are scheduled for execution.
164 // This function only needs to be called if automatic pumping is disabled.
165 // By default automatic pumping is enabled for all queues. NOTE this must be
166 // called on the thread this TaskQueue was created by.
167 virtual void PumpQueue() = 0;
169 // These functions can only be called on the same thread that the task queue
170 // manager executes its tasks on.
171 virtual void AddTaskObserver(
172 base::MessageLoop::TaskObserver
* task_observer
) = 0;
173 virtual void RemoveTaskObserver(
174 base::MessageLoop::TaskObserver
* task_observer
) = 0;
177 ~TaskQueue() override
{}
179 DISALLOW_COPY_AND_ASSIGN(TaskQueue
);
182 } // namespace scheduler
184 #endif // COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_H_