| 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 CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | |
| 6 #define CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/pending_task.h" | |
| 11 #include "base/threading/thread_checker.h" | |
| 12 #include "base/trace_event/trace_event.h" | |
| 13 #include "base/trace_event/trace_event_argument.h" | |
| 14 #include "components/scheduler/base/lazy_now.h" | |
| 15 #include "components/scheduler/base/task_queue.h" | |
| 16 #include "components/scheduler/scheduler_export.h" | |
| 17 | |
| 18 namespace scheduler { | |
| 19 class TaskQueueManager; | |
| 20 | |
| 21 namespace internal { | |
| 22 | |
| 23 class SCHEDULER_EXPORT TaskQueueImpl final : public TaskQueue { | |
| 24 public: | |
| 25 TaskQueueImpl(TaskQueueManager* task_queue_manager, | |
| 26 const Spec& spec, | |
| 27 const char* disabled_by_default_tracing_category, | |
| 28 const char* disabled_by_default_verbose_tracing_category); | |
| 29 | |
| 30 class SCHEDULER_EXPORT Task : public base::PendingTask { | |
| 31 public: | |
| 32 Task(); | |
| 33 Task(const tracked_objects::Location& posted_from, | |
| 34 const base::Closure& task, | |
| 35 int sequence_number, | |
| 36 bool nestable); | |
| 37 | |
| 38 int enqueue_order() const { | |
| 39 #ifndef NDEBUG | |
| 40 DCHECK(enqueue_order_set_); | |
| 41 #endif | |
| 42 return enqueue_order_; | |
| 43 } | |
| 44 | |
| 45 void set_enqueue_order(int enqueue_order) { | |
| 46 #ifndef NDEBUG | |
| 47 DCHECK(!enqueue_order_set_); | |
| 48 enqueue_order_set_ = true; | |
| 49 #endif | |
| 50 enqueue_order_ = enqueue_order; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 #ifndef NDEBUG | |
| 55 bool enqueue_order_set_; | |
| 56 #endif | |
| 57 // Similar to sequence number, but the |enqueue_order| is set by | |
| 58 // EnqueueTasksLocked and is not initially defined for delayed tasks until | |
| 59 // they are enqueued on the |incoming_queue_|. | |
| 60 int enqueue_order_; | |
| 61 }; | |
| 62 | |
| 63 // TaskQueue implementation. | |
| 64 void UnregisterTaskQueue() override; | |
| 65 bool RunsTasksOnCurrentThread() const override; | |
| 66 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 67 const base::Closure& task, | |
| 68 base::TimeDelta delay) override; | |
| 69 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
| 70 const base::Closure& task, | |
| 71 base::TimeDelta delay) override; | |
| 72 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, | |
| 73 const base::Closure& task, | |
| 74 base::TimeTicks desired_run_time) override; | |
| 75 | |
| 76 bool IsQueueEnabled() const override; | |
| 77 QueueState GetQueueState() const override; | |
| 78 void SetQueuePriority(QueuePriority priority) override; | |
| 79 void PumpQueue() override; | |
| 80 void SetPumpPolicy(PumpPolicy pump_policy) override; | |
| 81 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | |
| 82 void RemoveTaskObserver( | |
| 83 base::MessageLoop::TaskObserver* task_observer) override; | |
| 84 | |
| 85 bool NextPendingDelayedTaskRunTime( | |
| 86 base::TimeTicks* next_pending_delayed_task); | |
| 87 | |
| 88 void UpdateWorkQueue(LazyNow* lazy_now, | |
| 89 bool should_trigger_wakeup, | |
| 90 const Task* previous_task); | |
| 91 Task TakeTaskFromWorkQueue(); | |
| 92 | |
| 93 std::queue<Task>& work_queue() { return work_queue_; } | |
| 94 | |
| 95 WakeupPolicy wakeup_policy() const { | |
| 96 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 97 return wakeup_policy_; | |
| 98 } | |
| 99 | |
| 100 const char* GetName() const override; | |
| 101 | |
| 102 void AsValueInto(base::trace_event::TracedValue* state) const; | |
| 103 | |
| 104 size_t get_task_queue_set_index() const { return set_index_; } | |
| 105 | |
| 106 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } | |
| 107 | |
| 108 // If the work queue isn't empty, |enqueue_order| gets set to the enqueue | |
| 109 // order of the front task and the function returns true. Otherwise the | |
| 110 // function returns false. | |
| 111 bool GetWorkQueueFrontTaskEnqueueOrder(int* enqueue_order) const; | |
| 112 | |
| 113 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | |
| 114 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | |
| 115 | |
| 116 void NotifyWillProcessTask(const base::PendingTask& pending_task); | |
| 117 void NotifyDidProcessTask(const base::PendingTask& pending_task); | |
| 118 | |
| 119 // Delayed task posted to the underlying run loop, which locks |lock_| and | |
| 120 // calls MoveReadyDelayedTasksToIncomingQueueLocked to process dealyed tasks | |
| 121 // that need to be run now. Thread safe, but in practice it's always called | |
| 122 // from the main thread. | |
| 123 void MoveReadyDelayedTasksToIncomingQueue(LazyNow* lazy_now); | |
| 124 | |
| 125 // Test support functions. These should not be used in production code. | |
| 126 void PushTaskOntoWorkQueueForTest(const Task& task); | |
| 127 void PopTaskFromWorkQueueForTest(); | |
| 128 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } | |
| 129 | |
| 130 // Can be called on any thread. | |
| 131 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | |
| 132 | |
| 133 // Can be called on any thread. | |
| 134 static const char* WakeupPolicyToString( | |
| 135 TaskQueue::WakeupPolicy wakeup_policy); | |
| 136 | |
| 137 // Can be called on any thread. | |
| 138 static const char* PriorityToString(TaskQueue::QueuePriority priority); | |
| 139 | |
| 140 private: | |
| 141 enum class TaskType { | |
| 142 NORMAL, | |
| 143 NON_NESTABLE, | |
| 144 }; | |
| 145 | |
| 146 ~TaskQueueImpl() override; | |
| 147 | |
| 148 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, | |
| 149 const base::Closure& task, | |
| 150 base::TimeDelta delay, | |
| 151 TaskType task_type); | |
| 152 bool PostDelayedTaskLocked(LazyNow* lazy_now, | |
| 153 const tracked_objects::Location& from_here, | |
| 154 const base::Closure& task, | |
| 155 base::TimeTicks desired_run_time, | |
| 156 TaskType task_type); | |
| 157 | |
| 158 // Enqueues any delayed tasks which should be run now on the incoming_queue_ | |
| 159 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. | |
| 160 // Must be called with |lock_| locked. | |
| 161 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); | |
| 162 | |
| 163 void PumpQueueLocked(); | |
| 164 bool TaskIsOlderThanQueuedTasks(const Task* task); | |
| 165 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, | |
| 166 const Task* previous_task); | |
| 167 | |
| 168 // Push the task onto the |incoming_queue_| and for auto pumped queues it | |
| 169 // calls MaybePostDoWorkOnMainRunner if the incomming queue was empty. | |
| 170 void EnqueueTaskLocked(const Task& pending_task); | |
| 171 | |
| 172 // Push the task onto the |incoming_queue_| and allocates an | |
| 173 // enqueue_order for it based on |enqueue_order_policy|. Does not call | |
| 174 // MaybePostDoWorkOnMainRunner! | |
| 175 void EnqueueDelayedTaskLocked(const Task& pending_task); | |
| 176 | |
| 177 void TraceQueueSize(bool is_locked) const; | |
| 178 static void QueueAsValueInto(const std::queue<Task>& queue, | |
| 179 base::trace_event::TracedValue* state); | |
| 180 static void QueueAsValueInto(const std::priority_queue<Task>& queue, | |
| 181 base::trace_event::TracedValue* state); | |
| 182 static void TaskAsValueInto(const Task& task, | |
| 183 base::trace_event::TracedValue* state); | |
| 184 | |
| 185 // This lock protects all members in the contigious block below. | |
| 186 // TODO(alexclarke): Group all the members protected by the lock into a struct | |
| 187 mutable base::Lock lock_; | |
| 188 base::PlatformThreadId thread_id_; | |
| 189 TaskQueueManager* task_queue_manager_; | |
| 190 std::queue<Task> incoming_queue_; | |
| 191 PumpPolicy pump_policy_; | |
| 192 std::priority_queue<Task> delayed_task_queue_; | |
| 193 | |
| 194 const char* name_; | |
| 195 const char* disabled_by_default_tracing_category_; | |
| 196 const char* disabled_by_default_verbose_tracing_category_; | |
| 197 | |
| 198 base::ThreadChecker main_thread_checker_; | |
| 199 std::queue<Task> work_queue_; | |
| 200 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; | |
| 201 WakeupPolicy wakeup_policy_; | |
| 202 size_t set_index_; | |
| 203 bool should_monitor_quiescence_; | |
| 204 bool should_notify_observers_; | |
| 205 | |
| 206 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | |
| 207 }; | |
| 208 | |
| 209 } // namespace internal | |
| 210 } // namespace scheduler | |
| 211 | |
| 212 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | |
| OLD | NEW |