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.
5 #ifndef CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_
6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_
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/child/lazy_now.h"
15 #include "components/scheduler/child/task_queue.h"
16 #include "components/scheduler/scheduler_export.h"
19 class TaskQueueManager
;
23 class SCHEDULER_EXPORT TaskQueueImpl final
: public TaskQueue
{
25 TaskQueueImpl(TaskQueueManager
* task_queue_manager
,
27 const char* disabled_by_default_tracing_category
,
28 const char* disabled_by_default_verbose_tracing_category
);
30 class SCHEDULER_EXPORT Task
: public base::PendingTask
{
33 Task(const tracked_objects::Location
& posted_from
,
34 const base::Closure
& task
,
38 int enqueue_order() const {
40 DCHECK(enqueue_order_set_
);
42 return enqueue_order_
;
45 void set_enqueue_order(int enqueue_order
) {
47 DCHECK(!enqueue_order_set_
);
48 enqueue_order_set_
= true;
50 enqueue_order_
= enqueue_order
;
55 bool enqueue_order_set_
;
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_|.
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
;
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
;
85 bool NextPendingDelayedTaskRunTime(
86 base::TimeTicks
* next_pending_delayed_task
);
88 void UpdateWorkQueue(LazyNow
* lazy_now
,
89 bool should_trigger_wakeup
,
90 const Task
* previous_task
);
91 Task
TakeTaskFromWorkQueue();
93 std::queue
<Task
>& work_queue() { return work_queue_
; }
95 WakeupPolicy
wakeup_policy() const {
96 DCHECK(main_thread_checker_
.CalledOnValidThread());
97 return wakeup_policy_
;
100 const char* GetName() const override
;
102 void AsValueInto(base::trace_event::TracedValue
* state
) const;
104 size_t get_task_queue_set_index() const { return set_index_
; }
106 void set_task_queue_set_index(size_t set_index
) { set_index_
= set_index
; }
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;
113 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_
; }
114 bool GetShouldNotifyObservers() const { return should_notify_observers_
; }
116 void NotifyWillProcessTask(const base::PendingTask
& pending_task
);
117 void NotifyDidProcessTask(const base::PendingTask
& pending_task
);
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
);
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(); }
130 // Can be called on any thread.
131 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy
);
133 // Can be called on any thread.
134 static const char* WakeupPolicyToString(
135 TaskQueue::WakeupPolicy wakeup_policy
);
137 // Can be called on any thread.
138 static const char* PriorityToString(TaskQueue::QueuePriority priority
);
141 enum class TaskType
{
146 ~TaskQueueImpl() override
;
148 bool PostDelayedTaskImpl(const tracked_objects::Location
& from_here
,
149 const base::Closure
& task
,
150 base::TimeDelta delay
,
152 bool PostDelayedTaskLocked(LazyNow
* lazy_now
,
153 const tracked_objects::Location
& from_here
,
154 const base::Closure
& task
,
155 base::TimeTicks desired_run_time
,
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
);
163 void PumpQueueLocked();
164 bool TaskIsOlderThanQueuedTasks(const Task
* task
);
165 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup
,
166 const Task
* previous_task
);
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
);
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
);
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
);
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_
;
195 const char* disabled_by_default_tracing_category_
;
196 const char* disabled_by_default_verbose_tracing_category_
;
198 base::ThreadChecker main_thread_checker_
;
199 std::queue
<Task
> work_queue_
;
200 base::ObserverList
<base::MessageLoop::TaskObserver
> task_observers_
;
201 WakeupPolicy wakeup_policy_
;
203 bool should_monitor_quiescence_
;
204 bool should_notify_observers_
;
206 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl
);
209 } // namespace internal
210 } // namespace scheduler
212 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_