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 // TaskQueue implementation.
31 bool RunsTasksOnCurrentThread() const override
;
32 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
33 const base::Closure
& task
,
34 base::TimeDelta delay
) override
;
35 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
36 const base::Closure
& task
,
37 base::TimeDelta delay
) override
;
38 bool PostDelayedTaskAt(const tracked_objects::Location
& from_here
,
39 const base::Closure
& task
,
40 base::TimeTicks desired_run_time
) override
;
42 bool IsQueueEnabled() const override
;
43 QueueState
GetQueueState() const override
;
44 void SetQueuePriority(QueuePriority priority
) override
;
45 void PumpQueue() override
;
46 void SetPumpPolicy(PumpPolicy pump_policy
) override
;
48 bool NextPendingDelayedTaskRunTime(
49 base::TimeTicks
* next_pending_delayed_task
);
51 void UpdateWorkQueue(LazyNow
* lazy_now
,
52 bool should_trigger_wakeup
,
53 const base::PendingTask
* previous_task
);
54 base::PendingTask
TakeTaskFromWorkQueue();
56 void WillDeleteTaskQueueManager();
58 base::TaskQueue
& work_queue() { return work_queue_
; }
60 WakeupPolicy
wakeup_policy() const {
61 DCHECK(main_thread_checker_
.CalledOnValidThread());
62 return wakeup_policy_
;
65 const char* GetName() const override
;
67 void AsValueInto(base::trace_event::TracedValue
* state
) const;
69 size_t get_task_queue_set_index() const { return set_index_
; }
71 void set_task_queue_set_index(size_t set_index
) { set_index_
= set_index
; }
73 // If the work queue isn't empty, |age| gets set to the sequence number of the
74 // front task and the dunctio returns true. Otherwise the function returns
76 bool GetWorkQueueFrontTaskAge(int* age
) const;
78 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_
; }
79 bool GetShouldNotifyObservers() const { return should_notify_observers_
; }
81 // Test support functions. These should not be used in production code.
82 void PushTaskOntoWorkQueueForTest(const base::PendingTask
& task
);
83 void PopTaskFromWorkQueueForTest();
84 size_t WorkQueueSizeForTest() const { return work_queue_
.size(); }
86 // Can be called on any thread.
87 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy
);
89 // Can be called on any thread.
90 static const char* WakeupPolicyToString(
91 TaskQueue::WakeupPolicy wakeup_policy
);
93 // Can be called on any thread.
94 static const char* PriorityToString(TaskQueue::QueuePriority priority
);
102 ~TaskQueueImpl() override
;
104 bool PostDelayedTaskImpl(const tracked_objects::Location
& from_here
,
105 const base::Closure
& task
,
106 base::TimeDelta delay
,
108 bool PostDelayedTaskLocked(LazyNow
* lazy_now
,
109 const tracked_objects::Location
& from_here
,
110 const base::Closure
& task
,
111 base::TimeTicks desired_run_time
,
114 // Delayed task posted to the underlying run loop, which locks |lock_| and
115 // calls MoveReadyDelayedTasksToIncomingQueueLocked to process dealyed tasks
116 // that need to be run now.
117 void MoveReadyDelayedTasksToIncomingQueue();
119 // Enqueues any delayed tasks which should be run now on the incoming_queue_
120 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled.
121 // Must be called with |lock_| locked.
122 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow
* lazy_now
);
124 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task
125 // posted on the underlying runloop for the next task's scheduled run time.
126 void ScheduleDelayedWorkLocked(LazyNow
* lazy_now
);
128 void PumpQueueLocked();
129 bool TaskIsOlderThanQueuedTasks(const base::PendingTask
* task
);
130 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup
,
131 const base::PendingTask
* previous_task
);
133 // Push the task onto the |incoming_queue_| and allocate a sequence number
135 void EnqueueTaskLocked(const base::PendingTask
& pending_task
);
137 void TraceQueueSize(bool is_locked
) const;
138 static void QueueAsValueInto(const base::TaskQueue
& queue
,
139 base::trace_event::TracedValue
* state
);
140 static void QueueAsValueInto(const base::DelayedTaskQueue
& queue
,
141 base::trace_event::TracedValue
* state
);
142 static void TaskAsValueInto(const base::PendingTask
& task
,
143 base::trace_event::TracedValue
* state
);
145 // This lock protects all members in the contigious block below.
146 // TODO(alexclarke): Group all the members protected by the lock into a struct
147 mutable base::Lock lock_
;
148 base::PlatformThreadId thread_id_
;
149 TaskQueueManager
* task_queue_manager_
;
150 base::TaskQueue incoming_queue_
;
151 PumpPolicy pump_policy_
;
152 // Queue-local task sequence number for maintaining the order of delayed
153 // tasks which are posted for the exact same time. Note that this will be
154 // replaced by the global sequence number when the delay has elapsed.
155 int delayed_task_sequence_number_
;
156 base::DelayedTaskQueue delayed_task_queue_
;
157 std::set
<base::TimeTicks
> in_flight_kick_delayed_tasks_
;
160 const char* disabled_by_default_tracing_category_
;
161 const char* disabled_by_default_verbose_tracing_category_
;
163 base::ThreadChecker main_thread_checker_
;
164 base::TaskQueue work_queue_
;
165 WakeupPolicy wakeup_policy_
;
167 bool should_monitor_quiescence_
;
168 bool should_notify_observers_
;
170 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl
);
173 } // namespace internal
174 } // namespace scheduler
176 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_