OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef COMPONENTS_SCHEDULER_BASE_TASK_QUEUE_H_ | |
6 #define COMPONENTS_SCHEDULER_BASE_TASK_QUEUE_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/trace_event/trace_event.h" | |
12 #include "components/scheduler/scheduler_export.h" | |
13 | |
14 namespace base { | |
15 namespace trace_event { | |
16 class BlameContext; | |
17 } | |
18 } | |
19 | |
20 namespace scheduler { | |
21 class LazyNow; | |
22 class TimeDomain; | |
23 | |
24 class SCHEDULER_EXPORT TaskQueue : public base::SingleThreadTaskRunner { | |
25 public: | |
26 TaskQueue() {} | |
27 | |
28 // Unregisters the task queue after which no tasks posted to it will run and | |
29 // the TaskQueueManager's reference to it will be released soon. | |
30 virtual void UnregisterTaskQueue() = 0; | |
31 | |
32 enum QueuePriority { | |
33 // Queues with control priority will run before any other queue, and will | |
34 // explicitly starve other queues. Typically this should only be used for | |
35 // private queues which perform control operations. | |
36 CONTROL_PRIORITY, | |
37 // Queues with high priority will be selected preferentially over normal or | |
38 // best effort queues. The selector will ensure that high priority queues | |
39 // cannot completely starve normal priority queues. | |
40 HIGH_PRIORITY, | |
41 // Queues with normal priority are the default. | |
42 NORMAL_PRIORITY, | |
43 // Queues with best effort priority will only be run if all other queues are | |
44 // empty. They can be starved by the other queues. | |
45 BEST_EFFORT_PRIORITY, | |
46 // Must be the last entry. | |
47 QUEUE_PRIORITY_COUNT, | |
48 FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY, | |
49 }; | |
50 | |
51 // Keep TaskQueue::PumpPolicyToString in sync with this enum. | |
52 enum class PumpPolicy { | |
53 // Tasks posted to an incoming queue with an AUTO pump policy will be | |
54 // automatically scheduled for execution or transferred to the work queue | |
55 // automatically. | |
56 AUTO, | |
57 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy | |
58 // will be scheduled for execution or transferred to the work queue | |
59 // automatically but only after another queue has executed a task. | |
60 AFTER_WAKEUP, | |
61 // Tasks posted to an incoming queue with a MANUAL will not be | |
62 // automatically scheduled for execution or transferred to the work queue. | |
63 // Instead, the selector should call PumpQueue() when necessary to bring | |
64 // in new tasks for execution. | |
65 MANUAL, | |
66 // Must be last entry. | |
67 PUMP_POLICY_COUNT, | |
68 FIRST_PUMP_POLICY = AUTO, | |
69 }; | |
70 | |
71 // Keep TaskQueue::WakeupPolicyToString in sync with this enum. | |
72 enum class WakeupPolicy { | |
73 // Tasks run on a queue with CAN_WAKE_OTHER_QUEUES wakeup policy can | |
74 // cause queues with the AFTER_WAKEUP PumpPolicy to be woken up. | |
75 CAN_WAKE_OTHER_QUEUES, | |
76 // Tasks run on a queue with DONT_WAKE_OTHER_QUEUES won't cause queues | |
77 // with the AFTER_WAKEUP PumpPolicy to be woken up. | |
78 DONT_WAKE_OTHER_QUEUES, | |
79 // Must be last entry. | |
80 WAKEUP_POLICY_COUNT, | |
81 FIRST_WAKEUP_POLICY = CAN_WAKE_OTHER_QUEUES, | |
82 }; | |
83 | |
84 // Options for constructing a TaskQueue. Once set the |name|, | |
85 // |should_monitor_quiescence| and |wakeup_policy| are immutable. The | |
86 // |pump_policy| can be mutated with |SetPumpPolicy()|. | |
87 struct Spec { | |
88 // Note |name| must have application lifetime. | |
89 explicit Spec(const char* name) | |
90 : name(name), | |
91 should_monitor_quiescence(false), | |
92 pump_policy(TaskQueue::PumpPolicy::AUTO), | |
93 wakeup_policy(TaskQueue::WakeupPolicy::CAN_WAKE_OTHER_QUEUES), | |
94 time_domain(nullptr), | |
95 should_notify_observers(true), | |
96 should_report_when_execution_blocked(false) {} | |
97 | |
98 Spec SetShouldMonitorQuiescence(bool should_monitor) { | |
99 should_monitor_quiescence = should_monitor; | |
100 return *this; | |
101 } | |
102 | |
103 Spec SetPumpPolicy(PumpPolicy policy) { | |
104 pump_policy = policy; | |
105 return *this; | |
106 } | |
107 | |
108 Spec SetWakeupPolicy(WakeupPolicy policy) { | |
109 wakeup_policy = policy; | |
110 return *this; | |
111 } | |
112 | |
113 Spec SetShouldNotifyObservers(bool run_observers) { | |
114 should_notify_observers = run_observers; | |
115 return *this; | |
116 } | |
117 | |
118 Spec SetTimeDomain(TimeDomain* domain) { | |
119 time_domain = domain; | |
120 return *this; | |
121 } | |
122 | |
123 // See TaskQueueManager::Observer::OnTriedToExecuteBlockedTask. | |
124 Spec SetShouldReportWhenExecutionBlocked(bool should_report) { | |
125 should_report_when_execution_blocked = should_report; | |
126 return *this; | |
127 } | |
128 | |
129 const char* name; | |
130 bool should_monitor_quiescence; | |
131 TaskQueue::PumpPolicy pump_policy; | |
132 TaskQueue::WakeupPolicy wakeup_policy; | |
133 TimeDomain* time_domain; | |
134 bool should_notify_observers; | |
135 bool should_report_when_execution_blocked; | |
136 }; | |
137 | |
138 // Enable or disable task execution for this queue. NOTE this must be called | |
139 // on the thread this TaskQueue was created by. | |
140 virtual void SetQueueEnabled(bool enabled) = 0; | |
141 | |
142 // NOTE this must be called on the thread this TaskQueue was created by. | |
143 virtual bool IsQueueEnabled() const = 0; | |
144 | |
145 // Returns true if the queue is completely empty. | |
146 virtual bool IsEmpty() const = 0; | |
147 | |
148 // Returns true if the queue has work that's ready to execute now, or if it | |
149 // would have if the queue was pumped. NOTE this must be called on the thread | |
150 // this TaskQueue was created by. | |
151 virtual bool HasPendingImmediateWork() const = 0; | |
152 | |
153 // Returns true if tasks can't run now but could if the queue was pumped. | |
154 virtual bool NeedsPumping() const = 0; | |
155 | |
156 // Can be called on any thread. | |
157 virtual const char* GetName() const = 0; | |
158 | |
159 // Set the priority of the queue to |priority|. NOTE this must be called on | |
160 // the thread this TaskQueue was created by. | |
161 virtual void SetQueuePriority(QueuePriority priority) = 0; | |
162 | |
163 // Returns the current queue priority. | |
164 virtual QueuePriority GetQueuePriority() const = 0; | |
165 | |
166 // Set the pumping policy of the queue to |pump_policy|. NOTE this must be | |
167 // called on the thread this TaskQueue was created by. | |
168 virtual void SetPumpPolicy(PumpPolicy pump_policy) = 0; | |
169 | |
170 // Returns the current PumpPolicy. NOTE this must be called on the thread this | |
171 // TaskQueue was created by. | |
172 virtual PumpPolicy GetPumpPolicy() const = 0; | |
173 | |
174 // Reloads new tasks from the incoming queue into the work queue, regardless | |
175 // of whether the work queue is empty or not. After this, the function ensures | |
176 // that the tasks in the work queue, if any, are scheduled for execution. | |
177 // | |
178 // This function only needs to be called if automatic pumping is disabled. | |
179 // By default automatic pumping is enabled for all queues. NOTE this must be | |
180 // called on the thread this TaskQueue was created by. | |
181 // | |
182 // The |may_post_dowork| parameter controls whether or not PumpQueue calls | |
183 // TaskQueueManager::MaybeScheduleImmediateWork. | |
184 // TODO(alexclarke): Add a base::RunLoop observer so we can get rid of | |
185 // |may_post_dowork|. | |
186 virtual void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) = 0; | |
187 | |
188 // These functions can only be called on the same thread that the task queue | |
189 // manager executes its tasks on. | |
190 virtual void AddTaskObserver( | |
191 base::MessageLoop::TaskObserver* task_observer) = 0; | |
192 virtual void RemoveTaskObserver( | |
193 base::MessageLoop::TaskObserver* task_observer) = 0; | |
194 | |
195 // Set the blame context which is entered and left while executing tasks from | |
196 // this task queue. |blame_context| must be null or outlive this task queue. | |
197 // Must be called on the thread this TaskQueue was created by. | |
198 virtual void SetBlameContext( | |
199 base::trace_event::BlameContext* blame_context) = 0; | |
200 | |
201 // Removes the task queue from the previous TimeDomain and adds it to | |
202 // |domain|. This is a moderately expensive operation. | |
203 virtual void SetTimeDomain(TimeDomain* domain) = 0; | |
204 | |
205 // Returns the queue's current TimeDomain. Can be called from any thread. | |
206 virtual TimeDomain* GetTimeDomain() const = 0; | |
207 | |
208 protected: | |
209 ~TaskQueue() override {} | |
210 | |
211 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | |
212 }; | |
213 | |
214 } // namespace scheduler | |
215 | |
216 #endif // COMPONENTS_SCHEDULER_BASE_TASK_QUEUE_H_ | |
OLD | NEW |