| 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 THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | |
| 6 #define THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/optional.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "public/platform/WebCommon.h" | |
| 14 | |
| 15 namespace base { | |
| 16 namespace trace_event { | |
| 17 class BlameContext; | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 namespace blink { | |
| 22 namespace scheduler { | |
| 23 | |
| 24 class TimeDomain; | |
| 25 | |
| 26 class BLINK_PLATFORM_EXPORT TaskQueue : public base::SingleThreadTaskRunner { | |
| 27 public: | |
| 28 TaskQueue() {} | |
| 29 | |
| 30 class BLINK_PLATFORM_EXPORT Observer { | |
| 31 public: | |
| 32 virtual ~Observer() {} | |
| 33 | |
| 34 // Notify observer that the time at which this queue wants to run | |
| 35 // the next task has changed. |next_wakeup| can be in the past | |
| 36 // (e.g. base::TimeTicks() can be used to notify about immediate work). | |
| 37 // Can be called on any thread | |
| 38 // All methods but SetObserver, SetTimeDomain and GetTimeDomain can be | |
| 39 // called on |queue|. | |
| 40 virtual void OnQueueNextWakeUpChanged(TaskQueue* queue, | |
| 41 base::TimeTicks next_wake_up) = 0; | |
| 42 }; | |
| 43 | |
| 44 // Unregisters the task queue after which no tasks posted to it will run and | |
| 45 // the TaskQueueManager's reference to it will be released soon. | |
| 46 virtual void UnregisterTaskQueue() = 0; | |
| 47 | |
| 48 enum QueuePriority { | |
| 49 // Queues with control priority will run before any other queue, and will | |
| 50 // explicitly starve other queues. Typically this should only be used for | |
| 51 // private queues which perform control operations. | |
| 52 CONTROL_PRIORITY, | |
| 53 // Queues with high priority will be selected preferentially over normal or | |
| 54 // best effort queues. The selector will ensure that high priority queues | |
| 55 // cannot completely starve normal priority queues. | |
| 56 HIGH_PRIORITY, | |
| 57 // Queues with normal priority are the default. | |
| 58 NORMAL_PRIORITY, | |
| 59 // Queues with best effort priority will only be run if all other queues are | |
| 60 // empty. They can be starved by the other queues. | |
| 61 BEST_EFFORT_PRIORITY, | |
| 62 // Must be the last entry. | |
| 63 QUEUE_PRIORITY_COUNT, | |
| 64 FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY, | |
| 65 }; | |
| 66 | |
| 67 // Can be called on any thread. | |
| 68 static const char* PriorityToString(QueuePriority priority); | |
| 69 | |
| 70 enum class QueueType { | |
| 71 // Keep TaskQueue::NameForQueueType in sync. | |
| 72 // This enum is used for a histogram and it should not be re-numbered. | |
| 73 CONTROL = 0, | |
| 74 DEFAULT = 1, | |
| 75 DEFAULT_LOADING = 2, | |
| 76 DEFAULT_TIMER = 3, | |
| 77 UNTHROTTLED = 4, | |
| 78 FRAME_LOADING = 5, | |
| 79 FRAME_TIMER = 6, | |
| 80 FRAME_UNTHROTTLED = 7, | |
| 81 COMPOSITOR = 8, | |
| 82 IDLE = 9, | |
| 83 TEST = 10, | |
| 84 | |
| 85 COUNT = 11 | |
| 86 }; | |
| 87 | |
| 88 // Returns name of the given queue type. Returned string has application | |
| 89 // lifetime. | |
| 90 static const char* NameForQueueType(QueueType queue_type); | |
| 91 | |
| 92 // Options for constructing a TaskQueue. Once set the |name| and | |
| 93 // |should_monitor_quiescence| are immutable. | |
| 94 struct Spec { | |
| 95 explicit Spec(QueueType type) | |
| 96 : type(type), | |
| 97 should_monitor_quiescence(false), | |
| 98 time_domain(nullptr), | |
| 99 should_notify_observers(true), | |
| 100 should_report_when_execution_blocked(false) {} | |
| 101 | |
| 102 Spec SetShouldMonitorQuiescence(bool should_monitor) { | |
| 103 should_monitor_quiescence = should_monitor; | |
| 104 return *this; | |
| 105 } | |
| 106 | |
| 107 Spec SetShouldNotifyObservers(bool run_observers) { | |
| 108 should_notify_observers = run_observers; | |
| 109 return *this; | |
| 110 } | |
| 111 | |
| 112 Spec SetTimeDomain(TimeDomain* domain) { | |
| 113 time_domain = domain; | |
| 114 return *this; | |
| 115 } | |
| 116 | |
| 117 // See TaskQueueManager::Observer::OnTriedToExecuteBlockedTask. | |
| 118 Spec SetShouldReportWhenExecutionBlocked(bool should_report) { | |
| 119 should_report_when_execution_blocked = should_report; | |
| 120 return *this; | |
| 121 } | |
| 122 | |
| 123 QueueType type; | |
| 124 bool should_monitor_quiescence; | |
| 125 TimeDomain* time_domain; | |
| 126 bool should_notify_observers; | |
| 127 bool should_report_when_execution_blocked; | |
| 128 }; | |
| 129 | |
| 130 // An interface that lets the owner vote on whether or not the associated | |
| 131 // TaskQueue should be enabled. | |
| 132 class QueueEnabledVoter { | |
| 133 public: | |
| 134 QueueEnabledVoter() {} | |
| 135 virtual ~QueueEnabledVoter() {} | |
| 136 | |
| 137 // Votes to enable or disable the associated TaskQueue. The TaskQueue will | |
| 138 // only be enabled if all the voters agree it should be enabled, or if there | |
| 139 // are no voters. | |
| 140 // NOTE this must be called on the thread the associated TaskQueue was | |
| 141 // created on. | |
| 142 virtual void SetQueueEnabled(bool enabled) = 0; | |
| 143 | |
| 144 private: | |
| 145 DISALLOW_COPY_AND_ASSIGN(QueueEnabledVoter); | |
| 146 }; | |
| 147 | |
| 148 // Returns an interface that allows the caller to vote on whether or not this | |
| 149 // TaskQueue is enabled. The TaskQueue will be enabled if there are no voters | |
| 150 // or if all agree it should be enabled. | |
| 151 // NOTE this must be called on the thread this TaskQueue was created by. | |
| 152 virtual std::unique_ptr<QueueEnabledVoter> CreateQueueEnabledVoter() = 0; | |
| 153 | |
| 154 // NOTE this must be called on the thread this TaskQueue was created by. | |
| 155 virtual bool IsQueueEnabled() const = 0; | |
| 156 | |
| 157 // Returns true if the queue is completely empty. | |
| 158 virtual bool IsEmpty() const = 0; | |
| 159 | |
| 160 // Returns the number of pending tasks in the queue. | |
| 161 virtual size_t GetNumberOfPendingTasks() const = 0; | |
| 162 | |
| 163 // Returns true if the queue has work that's ready to execute now. | |
| 164 // NOTE: this must be called on the thread this TaskQueue was created by. | |
| 165 virtual bool HasPendingImmediateWork() const = 0; | |
| 166 | |
| 167 // Returns requested run time of next scheduled wake-up for a delayed task | |
| 168 // which is not ready to run. If there are no such tasks or the queue is | |
| 169 // disabled (by a QueueEnabledVoter) it returns base::nullopt. | |
| 170 // NOTE: this must be called on the thread this TaskQueue was created by. | |
| 171 virtual base::Optional<base::TimeTicks> GetNextScheduledWakeUp() = 0; | |
| 172 | |
| 173 // Can be called on any thread. | |
| 174 virtual QueueType GetQueueType() const = 0; | |
| 175 | |
| 176 // Can be called on any thread. | |
| 177 virtual const char* GetName() const = 0; | |
| 178 | |
| 179 // Set the priority of the queue to |priority|. NOTE this must be called on | |
| 180 // the thread this TaskQueue was created by. | |
| 181 virtual void SetQueuePriority(QueuePriority priority) = 0; | |
| 182 | |
| 183 // Returns the current queue priority. | |
| 184 virtual QueuePriority GetQueuePriority() const = 0; | |
| 185 | |
| 186 // These functions can only be called on the same thread that the task queue | |
| 187 // manager executes its tasks on. | |
| 188 virtual void AddTaskObserver( | |
| 189 base::MessageLoop::TaskObserver* task_observer) = 0; | |
| 190 virtual void RemoveTaskObserver( | |
| 191 base::MessageLoop::TaskObserver* task_observer) = 0; | |
| 192 | |
| 193 // Set the blame context which is entered and left while executing tasks from | |
| 194 // this task queue. |blame_context| must be null or outlive this task queue. | |
| 195 // Must be called on the thread this TaskQueue was created by. | |
| 196 virtual void SetBlameContext( | |
| 197 base::trace_event::BlameContext* blame_context) = 0; | |
| 198 | |
| 199 // Removes the task queue from the previous TimeDomain and adds it to | |
| 200 // |domain|. This is a moderately expensive operation. | |
| 201 virtual void SetTimeDomain(TimeDomain* domain) = 0; | |
| 202 | |
| 203 // Returns the queue's current TimeDomain. Can be called from any thread. | |
| 204 virtual TimeDomain* GetTimeDomain() const = 0; | |
| 205 | |
| 206 enum class InsertFencePosition { | |
| 207 NOW, // Tasks posted on the queue up till this point further may run. | |
| 208 // All further tasks are blocked. | |
| 209 BEGINNING_OF_TIME, // No tasks posted on this queue may run. | |
| 210 }; | |
| 211 | |
| 212 // Inserts a barrier into the task queue which prevents tasks with an enqueue | |
| 213 // order greater than the fence from running until either the fence has been | |
| 214 // removed or a subsequent fence has unblocked some tasks within the queue. | |
| 215 // Note: delayed tasks get their enqueue order set once their delay has | |
| 216 // expired, and non-delayed tasks get their enqueue order set when posted. | |
| 217 virtual void InsertFence(InsertFencePosition position) = 0; | |
| 218 | |
| 219 // Removes any previously added fence and unblocks execution of any tasks | |
| 220 // blocked by it. | |
| 221 virtual void RemoveFence() = 0; | |
| 222 | |
| 223 virtual bool BlockedByFence() const = 0; | |
| 224 | |
| 225 virtual void SetObserver(Observer* observer) = 0; | |
| 226 | |
| 227 protected: | |
| 228 ~TaskQueue() override {} | |
| 229 | |
| 230 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | |
| 231 }; | |
| 232 | |
| 233 } // namespace scheduler | |
| 234 } // namespace blink | |
| 235 | |
| 236 #endif // THIRD_PARTY_WEBKIT_PUBLIC_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_H_ | |
| OLD | NEW |