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