OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_CHILD_SCHEDULER_PRIORITIZING_TASK_QUEUE_SELECTOR_H_ | |
6 #define CONTENT_CHILD_SCHEDULER_PRIORITIZING_TASK_QUEUE_SELECTOR_H_ | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/threading/thread_checker.h" | |
12 #include "content/child/scheduler/task_queue_selector.h" | |
13 #include "content/common/content_export.h" | |
14 | |
15 namespace content { | |
16 | |
17 // A PrioritizingTaskQueueSelector is a TaskQueueSelector which is used by the | |
18 // SchedulerHelper to enable prioritization of particular task queues. | |
19 class CONTENT_EXPORT PrioritizingTaskQueueSelector | |
20 : NON_EXPORTED_BASE(public TaskQueueSelector) { | |
21 public: | |
22 enum QueuePriority { | |
23 // Queues with control priority will run before any other queue, and will | |
24 // explicitly starve other queues. Typically this should only be used for | |
25 // private queues which perform control operations. | |
26 CONTROL_PRIORITY, | |
27 // Queues with high priority will be selected preferentially over normal or | |
28 // best effort queues. The selector will ensure that high priority queues | |
29 // cannot completely starve normal priority queues. | |
30 HIGH_PRIORITY, | |
31 // Queues with normal priority are the default. | |
32 NORMAL_PRIORITY, | |
33 // Queues with best effort priority will only be run if all other queues are | |
34 // empty. They can be starved by the other queues. | |
35 BEST_EFFORT_PRIORITY, | |
36 // Must be the last entry. | |
37 QUEUE_PRIORITY_COUNT, | |
38 FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY, | |
39 }; | |
40 | |
41 PrioritizingTaskQueueSelector(); | |
42 ~PrioritizingTaskQueueSelector() override; | |
43 | |
44 // Set the priority of |queue_index| to |priority|. | |
45 void SetQueuePriority(size_t queue_index, QueuePriority priority); | |
46 | |
47 // Enable the |queue_index| with a priority of |priority|. By default all | |
48 // queues are enabled with normal priority. | |
49 void EnableQueue(size_t queue_index, QueuePriority priority); | |
50 | |
51 // Disable the |queue_index|. | |
52 void DisableQueue(size_t queue_index); | |
53 | |
54 // Whether |queue_index| is enabled. | |
55 bool IsQueueEnabled(size_t queue_index) const; | |
56 | |
57 // TaskQueueSelector implementation: | |
58 void RegisterWorkQueues( | |
59 const std::vector<const base::TaskQueue*>& work_queues) override; | |
60 bool SelectWorkQueueToService(size_t* out_queue_index) override; | |
61 void AsValueInto(base::trace_event::TracedValue* state) const override; | |
62 void SetTaskQueueSelectorObserver(Observer* observer) override; | |
63 | |
64 protected: | |
65 // Disable the |queue_index|. Returns true if the queue was previously enabled | |
66 // otherwise returns false. | |
67 bool DisableQueueInternal(size_t queue_index); | |
68 | |
69 private: | |
70 // Returns true if queueA contains an older task than queueB. | |
71 static bool IsOlder(const base::TaskQueue* queueA, | |
72 const base::TaskQueue* queueB); | |
73 | |
74 // Returns the priority which is next after |priority|. | |
75 static QueuePriority NextPriority(QueuePriority priority); | |
76 | |
77 static const char* PriorityToString(QueuePriority priority); | |
78 | |
79 // Return true if |out_queue_index| indicates the index of the queue with | |
80 // the oldest pending task from the set of queues of |priority|, or | |
81 // false if all queues of that priority are empty. | |
82 bool ChooseOldestWithPriority(QueuePriority priority, | |
83 size_t* out_queue_index) const; | |
84 | |
85 // Returns true if |queue_index| is enabled with the given |priority|. | |
86 bool QueueEnabledWithPriority(size_t queue_index, | |
87 QueuePriority priority) const; | |
88 | |
89 // Called whenever the selector chooses a task queue for execution with the | |
90 // priority |priority|. | |
91 void DidSelectQueueWithPriority(QueuePriority priority); | |
92 | |
93 // Number of high priority tasks which can be run before a normal priority | |
94 // task should be selected to prevent starvation. | |
95 // TODO(rmcilroy): Check if this is a good value. | |
96 static const size_t kMaxStarvationTasks = 5; | |
97 | |
98 base::ThreadChecker main_thread_checker_; | |
99 std::vector<const base::TaskQueue*> work_queues_; | |
100 std::set<size_t> queue_priorities_[QUEUE_PRIORITY_COUNT]; | |
101 size_t starvation_count_; | |
102 Observer* task_queue_selector_observer_; // NOT OWNED | |
103 DISALLOW_COPY_AND_ASSIGN(PrioritizingTaskQueueSelector); | |
104 }; | |
105 | |
106 } // namespace content | |
107 | |
108 #endif // CONTENT_CHILD_SCHEDULER_PRIORITIZING_TASK_QUEUE_SELECTOR_H_ | |
OLD | NEW |