Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / scheduler / child / task_queue_selector.h
blob8b5cd3df8a510cd6c2a9f5da9d618833e3240104
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 COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_SELECTOR_H_
6 #define COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_SELECTOR_H_
8 #include <set>
10 #include "base/compiler_specific.h"
11 #include "base/pending_task.h"
12 #include "base/threading/thread_checker.h"
13 #include "components/scheduler/child/task_queue_sets.h"
14 #include "components/scheduler/scheduler_export.h"
16 namespace scheduler {
17 namespace internal {
19 // TaskQueueSelector is used by the SchedulerHelper to enable prioritization
20 // of particular task queues.
21 class SCHEDULER_EXPORT TaskQueueSelector {
22 public:
23 TaskQueueSelector();
24 ~TaskQueueSelector();
26 // Set the priority of |queue| to |priority|.
27 void SetQueuePriority(internal::TaskQueueImpl* queue,
28 TaskQueue::QueuePriority priority);
30 // Whether |queue| is enabled.
31 bool IsQueueEnabled(const internal::TaskQueueImpl* queue) const;
33 // Called to register a queue that can be selected. This function is called
34 // on the main thread.
35 void AddQueue(internal::TaskQueueImpl* queue);
37 // The specified work will no longer be considered for selection. This
38 // function is called on the main thread.
39 void RemoveQueue(internal::TaskQueueImpl* queue);
41 // Called to choose the work queue from which the next task should be taken
42 // and run. Return true if |out_queue| indicates the queue to service or
43 // false to avoid running any task.
45 // This function is called on the main thread.
46 bool SelectQueueToService(internal::TaskQueueImpl** out_queue);
48 // Serialize the selector state for tracing.
49 void AsValueInto(base::trace_event::TracedValue* state) const;
51 class SCHEDULER_EXPORT Observer {
52 public:
53 virtual ~Observer() {}
55 // Called when |queue| transitions from disabled to enabled.
56 virtual void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) = 0;
59 // Called once to set the Observer. This function is called
60 // on the main thread. If |observer| is null, then no callbacks will occur.
61 void SetTaskQueueSelectorObserver(Observer* observer);
63 TaskQueueSets* GetTaskQueueSets() { return &task_queue_sets_; }
65 // Returns true if all the enabled work queues are empty. Returns false
66 // otherwise.
67 bool EnabledWorkQueuesEmpty() const;
69 private:
70 // Returns the priority which is next after |priority|.
71 static TaskQueue::QueuePriority NextPriority(
72 TaskQueue::QueuePriority priority);
74 // Return true if |out_queue| contains the queue with the oldest pending task
75 // from the set of queues of |priority|, or false if all queues of that
76 // priority are empty.
77 bool ChooseOldestWithPriority(TaskQueue::QueuePriority priority,
78 internal::TaskQueueImpl** out_queue) const;
80 // Called whenever the selector chooses a task queue for execution with the
81 // priority |priority|.
82 void DidSelectQueueWithPriority(TaskQueue::QueuePriority priority);
84 // Number of high priority tasks which can be run before a normal priority
85 // task should be selected to prevent starvation.
86 // TODO(rmcilroy): Check if this is a good value.
87 static const size_t kMaxStarvationTasks = 5;
89 base::ThreadChecker main_thread_checker_;
90 TaskQueueSets task_queue_sets_;
92 size_t starvation_count_;
93 Observer* task_queue_selector_observer_; // NOT OWNED
94 DISALLOW_COPY_AND_ASSIGN(TaskQueueSelector);
97 } // namespace internal
98 } // namespace scheduler
100 #endif // COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_SELECTOR_H