Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: components/scheduler/base/task_queue_selector.h

Issue 2118903002: scheduler: Move the Blink scheduler into Blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_SELECTOR_H_
6 #define COMPONENTS_SCHEDULER_BASE_TASK_QUEUE_SELECTOR_H_
7
8 #include <stddef.h>
9
10 #include <set>
11
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "base/pending_task.h"
15 #include "base/threading/thread_checker.h"
16 #include "components/scheduler/base/work_queue_sets.h"
17 #include "components/scheduler/scheduler_export.h"
18
19 namespace scheduler {
20 namespace internal {
21
22 // TaskQueueSelector is used by the SchedulerHelper to enable prioritization
23 // of particular task queues.
24 class SCHEDULER_EXPORT TaskQueueSelector {
25 public:
26 TaskQueueSelector();
27 ~TaskQueueSelector();
28
29 // Called to register a queue that can be selected. This function is called
30 // on the main thread.
31 void AddQueue(internal::TaskQueueImpl* queue);
32
33 // The specified work will no longer be considered for selection. This
34 // function is called on the main thread.
35 void RemoveQueue(internal::TaskQueueImpl* queue);
36
37 // Make |queue| eligible for selection. This function is called on the main
38 // thread. Must only be called if |queue| is disabled.
39 void EnableQueue(internal::TaskQueueImpl* queue);
40
41 // Disable selection from |queue|. If task blocking is enabled for the queue,
42 // Observer::OnTriedToSelectBlockedWorkQueue will be emitted if the
43 // SelectWorkQueueToService tries to select this disabled queue for execution.
44 // Must only be called if |queue| is enabled.
45 void DisableQueue(internal::TaskQueueImpl* queue);
46
47 // Called get or set the priority of |queue|.
48 void SetQueuePriority(internal::TaskQueueImpl* queue,
49 TaskQueue::QueuePriority priority);
50
51 // Called to choose the work queue from which the next task should be taken
52 // and run. Return true if |out_work_queue| indicates the queue to service or
53 // false to avoid running any task.
54 //
55 // This function is called on the main thread.
56 bool SelectWorkQueueToService(WorkQueue** out_work_queue);
57
58 // Serialize the selector state for tracing.
59 void AsValueInto(base::trace_event::TracedValue* state) const;
60
61 class SCHEDULER_EXPORT Observer {
62 public:
63 virtual ~Observer() {}
64
65 // Called when |queue| transitions from disabled to enabled.
66 virtual void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) = 0;
67
68 // Called when the selector tried to select a task from a disabled work
69 // queue. See TaskQueue::Spec::SetShouldReportWhenExecutionBlocked. A single
70 // call to SelectWorkQueueToService will only result in up to one
71 // blocking notification even if multiple disabled queues could have been
72 // selected.
73 virtual void OnTriedToSelectBlockedWorkQueue(
74 internal::WorkQueue* work_queue) = 0;
75 };
76
77 // Called once to set the Observer. This function is called
78 // on the main thread. If |observer| is null, then no callbacks will occur.
79 void SetTaskQueueSelectorObserver(Observer* observer);
80
81 // Returns true if all the enabled work queues are empty. Returns false
82 // otherwise.
83 bool EnabledWorkQueuesEmpty() const;
84
85 protected:
86 class SCHEDULER_EXPORT PrioritizingSelector {
87 public:
88 PrioritizingSelector(TaskQueueSelector* task_queue_selector,
89 const char* name);
90
91 void ChangeSetIndex(internal::TaskQueueImpl* queue,
92 TaskQueue::QueuePriority priority);
93 void AddQueue(internal::TaskQueueImpl* queue,
94 TaskQueue::QueuePriority priority);
95 void RemoveQueue(internal::TaskQueueImpl* queue);
96
97 bool SelectWorkQueueToService(TaskQueue::QueuePriority max_priority,
98 WorkQueue** out_work_queue,
99 bool* out_chose_delayed_over_immediate);
100
101 WorkQueueSets* delayed_work_queue_sets() {
102 return &delayed_work_queue_sets_;
103 }
104 WorkQueueSets* immediate_work_queue_sets() {
105 return &immediate_work_queue_sets_;
106 }
107
108 const WorkQueueSets* delayed_work_queue_sets() const {
109 return &delayed_work_queue_sets_;
110 }
111 const WorkQueueSets* immediate_work_queue_sets() const {
112 return &immediate_work_queue_sets_;
113 }
114
115 bool ChooseOldestWithPriority(TaskQueue::QueuePriority priority,
116 bool* out_chose_delayed_over_immediate,
117 WorkQueue** out_work_queue) const;
118
119 #if DCHECK_IS_ON() || !defined(NDEBUG)
120 bool CheckContainsQueueForTest(const internal::TaskQueueImpl* queue) const;
121 #endif
122
123 private:
124 bool ChooseOldestImmediateTaskWithPriority(
125 TaskQueue::QueuePriority priority,
126 WorkQueue** out_work_queue) const;
127
128 bool ChooseOldestDelayedTaskWithPriority(TaskQueue::QueuePriority priority,
129 WorkQueue** out_work_queue) const;
130
131 // Return true if |out_queue| contains the queue with the oldest pending
132 // task from the set of queues of |priority|, or false if all queues of that
133 // priority are empty. In addition |out_chose_delayed_over_immediate| is set
134 // to true iff we chose a delayed work queue in favour of an immediate work
135 // queue.
136 bool ChooseOldestImmediateOrDelayedTaskWithPriority(
137 TaskQueue::QueuePriority priority,
138 bool* out_chose_delayed_over_immediate,
139 WorkQueue** out_work_queue) const;
140
141 const TaskQueueSelector* task_queue_selector_;
142 WorkQueueSets delayed_work_queue_sets_;
143 WorkQueueSets immediate_work_queue_sets_;
144
145 DISALLOW_COPY_AND_ASSIGN(PrioritizingSelector);
146 };
147
148 // Return true if |out_queue| contains the queue with the oldest pending task
149 // from the set of queues of |priority|, or false if all queues of that
150 // priority are empty. In addition |out_chose_delayed_over_immediate| is set
151 // to true iff we chose a delayed work queue in favour of an immediate work
152 // queue. This method will force select an immediate task if those are being
153 // starved by delayed tasks.
154 void SetImmediateStarvationCountForTest(size_t immediate_starvation_count);
155
156 PrioritizingSelector* enabled_selector_for_test() {
157 return &enabled_selector_;
158 }
159
160 private:
161 // Returns the priority which is next after |priority|.
162 static TaskQueue::QueuePriority NextPriority(
163 TaskQueue::QueuePriority priority);
164
165 bool SelectWorkQueueToServiceInternal(WorkQueue** out_work_queue);
166
167 // Called whenever the selector chooses a task queue for execution with the
168 // priority |priority|.
169 void DidSelectQueueWithPriority(TaskQueue::QueuePriority priority,
170 bool chose_delayed_over_immediate);
171
172 // No enabled queue could be selected, check if we could have chosen a
173 // disabled (blocked) work queue instead.
174 void TrySelectingBlockedQueue();
175
176 // Check if we could have chosen a disabled (blocked) work queue instead.
177 // |chosen_enabled_queue| is the enabled queue that got chosen.
178 void TrySelectingBlockedQueueOverEnabledQueue(
179 const WorkQueue& chosen_enabled_queue);
180
181 // Number of high priority tasks which can be run before a normal priority
182 // task should be selected to prevent starvation.
183 // TODO(rmcilroy): Check if this is a good value.
184 static const size_t kMaxHighPriorityStarvationTasks = 5;
185
186 // Maximum number of delayed tasks tasks which can be run while there's a
187 // waiting non-delayed task.
188 static const size_t kMaxDelayedStarvationTasks = 3;
189
190 private:
191 base::ThreadChecker main_thread_checker_;
192
193 PrioritizingSelector enabled_selector_;
194 PrioritizingSelector blocked_selector_;
195 size_t immediate_starvation_count_;
196 size_t high_priority_starvation_count_;
197 size_t num_blocked_queues_to_report_;
198
199 Observer* task_queue_selector_observer_; // NOT OWNED
200 DISALLOW_COPY_AND_ASSIGN(TaskQueueSelector);
201 };
202
203 } // namespace internal
204 } // namespace scheduler
205
206 #endif // COMPONENTS_SCHEDULER_BASE_TASK_QUEUE_SELECTOR_H
OLDNEW
« no previous file with comments | « components/scheduler/base/task_queue_manager_unittest.cc ('k') | components/scheduler/base/task_queue_selector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698