OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ |
6 #define THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | 6 #define THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 // Queues with normal priority are the default. | 45 // Queues with normal priority are the default. |
46 NORMAL_PRIORITY, | 46 NORMAL_PRIORITY, |
47 // Queues with best effort priority will only be run if all other queues are | 47 // Queues with best effort priority will only be run if all other queues are |
48 // empty. They can be starved by the other queues. | 48 // empty. They can be starved by the other queues. |
49 BEST_EFFORT_PRIORITY, | 49 BEST_EFFORT_PRIORITY, |
50 // Must be the last entry. | 50 // Must be the last entry. |
51 QUEUE_PRIORITY_COUNT, | 51 QUEUE_PRIORITY_COUNT, |
52 FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY, | 52 FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY, |
53 }; | 53 }; |
54 | 54 |
55 // Keep TaskQueue::PumpPolicyToString in sync with this enum. | 55 // Options for constructing a TaskQueue. Once set the |name| and |
56 enum class PumpPolicy { | 56 // |should_monitor_quiescence| are immutable. |
57 // Tasks posted to an incoming queue with an AUTO pump policy will be | |
58 // automatically scheduled for execution or transferred to the work queue | |
59 // automatically. | |
60 AUTO, | |
61 // Tasks posted to an incoming queue with an AFTER_WAKEUP pump policy | |
62 // will be scheduled for execution or transferred to the work queue | |
63 // automatically but only after another queue has executed a task. | |
64 AFTER_WAKEUP, | |
65 // Tasks posted to an incoming queue with a MANUAL will not be | |
66 // automatically scheduled for execution or transferred to the work queue. | |
67 // Instead, the selector should call PumpQueue() when necessary to bring | |
68 // in new tasks for execution. | |
69 MANUAL, | |
70 // Must be last entry. | |
71 PUMP_POLICY_COUNT, | |
72 FIRST_PUMP_POLICY = AUTO, | |
73 }; | |
74 | |
75 // Keep TaskQueue::WakeupPolicyToString in sync with this enum. | |
76 enum class WakeupPolicy { | |
77 // Tasks run on a queue with CAN_WAKE_OTHER_QUEUES wakeup policy can | |
78 // cause queues with the AFTER_WAKEUP PumpPolicy to be woken up. | |
79 CAN_WAKE_OTHER_QUEUES, | |
80 // Tasks run on a queue with DONT_WAKE_OTHER_QUEUES won't cause queues | |
81 // with the AFTER_WAKEUP PumpPolicy to be woken up. | |
82 DONT_WAKE_OTHER_QUEUES, | |
83 // Must be last entry. | |
84 WAKEUP_POLICY_COUNT, | |
85 FIRST_WAKEUP_POLICY = CAN_WAKE_OTHER_QUEUES, | |
86 }; | |
87 | |
88 // Options for constructing a TaskQueue. Once set the |name|, | |
89 // |should_monitor_quiescence| and |wakeup_policy| are immutable. The | |
90 // |pump_policy| can be mutated with |SetPumpPolicy()|. | |
91 struct Spec { | 57 struct Spec { |
92 // Note |name| must have application lifetime. | 58 // Note |name| must have application lifetime. |
93 explicit Spec(const char* name) | 59 explicit Spec(const char* name) |
94 : name(name), | 60 : name(name), |
95 should_monitor_quiescence(false), | 61 should_monitor_quiescence(false), |
96 pump_policy(TaskQueue::PumpPolicy::AUTO), | |
97 wakeup_policy(TaskQueue::WakeupPolicy::CAN_WAKE_OTHER_QUEUES), | |
98 time_domain(nullptr), | 62 time_domain(nullptr), |
99 should_notify_observers(true), | 63 should_notify_observers(true), |
100 should_report_when_execution_blocked(false) {} | 64 should_report_when_execution_blocked(false) {} |
101 | 65 |
102 Spec SetShouldMonitorQuiescence(bool should_monitor) { | 66 Spec SetShouldMonitorQuiescence(bool should_monitor) { |
103 should_monitor_quiescence = should_monitor; | 67 should_monitor_quiescence = should_monitor; |
104 return *this; | 68 return *this; |
105 } | 69 } |
106 | 70 |
107 Spec SetPumpPolicy(PumpPolicy policy) { | |
108 pump_policy = policy; | |
109 return *this; | |
110 } | |
111 | |
112 Spec SetWakeupPolicy(WakeupPolicy policy) { | |
113 wakeup_policy = policy; | |
114 return *this; | |
115 } | |
116 | |
117 Spec SetShouldNotifyObservers(bool run_observers) { | 71 Spec SetShouldNotifyObservers(bool run_observers) { |
118 should_notify_observers = run_observers; | 72 should_notify_observers = run_observers; |
119 return *this; | 73 return *this; |
120 } | 74 } |
121 | 75 |
122 Spec SetTimeDomain(TimeDomain* domain) { | 76 Spec SetTimeDomain(TimeDomain* domain) { |
123 time_domain = domain; | 77 time_domain = domain; |
124 return *this; | 78 return *this; |
125 } | 79 } |
126 | 80 |
127 // See TaskQueueManager::Observer::OnTriedToExecuteBlockedTask. | 81 // See TaskQueueManager::Observer::OnTriedToExecuteBlockedTask. |
128 Spec SetShouldReportWhenExecutionBlocked(bool should_report) { | 82 Spec SetShouldReportWhenExecutionBlocked(bool should_report) { |
129 should_report_when_execution_blocked = should_report; | 83 should_report_when_execution_blocked = should_report; |
130 return *this; | 84 return *this; |
131 } | 85 } |
132 | 86 |
133 const char* name; | 87 const char* name; |
134 bool should_monitor_quiescence; | 88 bool should_monitor_quiescence; |
135 TaskQueue::PumpPolicy pump_policy; | |
136 TaskQueue::WakeupPolicy wakeup_policy; | |
137 TimeDomain* time_domain; | 89 TimeDomain* time_domain; |
138 bool should_notify_observers; | 90 bool should_notify_observers; |
139 bool should_report_when_execution_blocked; | 91 bool should_report_when_execution_blocked; |
140 }; | 92 }; |
141 | 93 |
142 // Intended to be used as an opaque handle to a task posted by | 94 // Intended to be used as an opaque handle to a task posted by |
143 // PostCancellableDelayedTask. | 95 // PostCancellableDelayedTask. |
144 class BLINK_PLATFORM_EXPORT TaskHandle { | 96 class BLINK_PLATFORM_EXPORT TaskHandle { |
145 public: | 97 public: |
146 TaskHandle(); | 98 TaskHandle(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // Enable or disable task execution for this queue. NOTE this must be called | 137 // Enable or disable task execution for this queue. NOTE this must be called |
186 // on the thread this TaskQueue was created by. | 138 // on the thread this TaskQueue was created by. |
187 virtual void SetQueueEnabled(bool enabled) = 0; | 139 virtual void SetQueueEnabled(bool enabled) = 0; |
188 | 140 |
189 // NOTE this must be called on the thread this TaskQueue was created by. | 141 // NOTE this must be called on the thread this TaskQueue was created by. |
190 virtual bool IsQueueEnabled() const = 0; | 142 virtual bool IsQueueEnabled() const = 0; |
191 | 143 |
192 // Returns true if the queue is completely empty. | 144 // Returns true if the queue is completely empty. |
193 virtual bool IsEmpty() const = 0; | 145 virtual bool IsEmpty() const = 0; |
194 | 146 |
195 // Returns true if the queue has work that's ready to execute now, or if it | 147 // Returns true if the queue has work that's ready to execute now. NOTE this |
196 // would have if the queue was pumped. NOTE this must be called on the thread | 148 // must be called on the thread this TaskQueue was created by. |
197 // this TaskQueue was created by. | |
198 virtual bool HasPendingImmediateWork() const = 0; | 149 virtual bool HasPendingImmediateWork() const = 0; |
199 | 150 |
200 // Returns true if tasks can't run now but could if the queue was pumped. | |
201 virtual bool NeedsPumping() const = 0; | |
202 | |
203 // Can be called on any thread. | 151 // Can be called on any thread. |
204 virtual const char* GetName() const = 0; | 152 virtual const char* GetName() const = 0; |
205 | 153 |
206 // Set the priority of the queue to |priority|. NOTE this must be called on | 154 // Set the priority of the queue to |priority|. NOTE this must be called on |
207 // the thread this TaskQueue was created by. | 155 // the thread this TaskQueue was created by. |
208 virtual void SetQueuePriority(QueuePriority priority) = 0; | 156 virtual void SetQueuePriority(QueuePriority priority) = 0; |
209 | 157 |
210 // Returns the current queue priority. | 158 // Returns the current queue priority. |
211 virtual QueuePriority GetQueuePriority() const = 0; | 159 virtual QueuePriority GetQueuePriority() const = 0; |
212 | 160 |
213 // Set the pumping policy of the queue to |pump_policy|. NOTE this must be | |
214 // called on the thread this TaskQueue was created by. | |
215 virtual void SetPumpPolicy(PumpPolicy pump_policy) = 0; | |
216 | |
217 // Returns the current PumpPolicy. NOTE this must be called on the thread this | |
218 // TaskQueue was created by. | |
219 virtual PumpPolicy GetPumpPolicy() const = 0; | |
220 | |
221 // Reloads new tasks from the incoming queue into the work queue, regardless | |
222 // of whether the work queue is empty or not. After this, the function ensures | |
223 // that the tasks in the work queue, if any, are scheduled for execution. | |
224 // | |
225 // This function only needs to be called if automatic pumping is disabled. | |
226 // By default automatic pumping is enabled for all queues. NOTE this must be | |
227 // called on the thread this TaskQueue was created by. | |
228 // | |
229 // The |may_post_dowork| parameter controls whether or not PumpQueue calls | |
230 // TaskQueueManager::MaybeScheduleImmediateWork. | |
231 // TODO(alexclarke): Add a base::RunLoop observer so we can get rid of | |
232 // |may_post_dowork|. | |
233 virtual void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) = 0; | |
234 | |
235 // These functions can only be called on the same thread that the task queue | 161 // These functions can only be called on the same thread that the task queue |
236 // manager executes its tasks on. | 162 // manager executes its tasks on. |
237 virtual void AddTaskObserver( | 163 virtual void AddTaskObserver( |
238 base::MessageLoop::TaskObserver* task_observer) = 0; | 164 base::MessageLoop::TaskObserver* task_observer) = 0; |
239 virtual void RemoveTaskObserver( | 165 virtual void RemoveTaskObserver( |
240 base::MessageLoop::TaskObserver* task_observer) = 0; | 166 base::MessageLoop::TaskObserver* task_observer) = 0; |
241 | 167 |
242 // Set the blame context which is entered and left while executing tasks from | 168 // Set the blame context which is entered and left while executing tasks from |
243 // this task queue. |blame_context| must be null or outlive this task queue. | 169 // this task queue. |blame_context| must be null or outlive this task queue. |
244 // Must be called on the thread this TaskQueue was created by. | 170 // Must be called on the thread this TaskQueue was created by. |
245 virtual void SetBlameContext( | 171 virtual void SetBlameContext( |
246 base::trace_event::BlameContext* blame_context) = 0; | 172 base::trace_event::BlameContext* blame_context) = 0; |
247 | 173 |
248 // Removes the task queue from the previous TimeDomain and adds it to | 174 // Removes the task queue from the previous TimeDomain and adds it to |
249 // |domain|. This is a moderately expensive operation. | 175 // |domain|. This is a moderately expensive operation. |
250 virtual void SetTimeDomain(TimeDomain* domain) = 0; | 176 virtual void SetTimeDomain(TimeDomain* domain) = 0; |
251 | 177 |
252 // Returns the queue's current TimeDomain. Can be called from any thread. | 178 // Returns the queue's current TimeDomain. Can be called from any thread. |
253 virtual TimeDomain* GetTimeDomain() const = 0; | 179 virtual TimeDomain* GetTimeDomain() const = 0; |
254 | 180 |
| 181 // Inserts a barrier into the task queue which inhibits non-delayed tasks |
| 182 // posted after this point, or delayed tasks which are not yet ready to run, |
| 183 // from being executed until the fence is cleared. If a fence already existed |
| 184 // the one supersedes it and previously blocked tasks will now run up until |
| 185 // the new fence is hit. |
| 186 virtual void InsertFence() = 0; |
| 187 |
| 188 // Removes any previously added fence and unblocks execution of any tasks |
| 189 // blocked by it. |
| 190 virtual void RemoveFence() = 0; |
| 191 |
| 192 virtual bool BlockedByFence() const = 0; |
| 193 |
255 protected: | 194 protected: |
256 ~TaskQueue() override {} | 195 ~TaskQueue() override {} |
257 | 196 |
258 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | 197 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
259 }; | 198 }; |
260 | 199 |
261 } // namespace scheduler | 200 } // namespace scheduler |
262 } // namespace blink | 201 } // namespace blink |
263 | 202 |
264 #endif // THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | 203 #endif // THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ |
OLD | NEW |