Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / scheduler / child / task_queue_selector.h
blobd3037c7aa7d3f0eec3b50aea2192029a29ceaf9c
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 a task queue transitions from disabled to enabled.
56 virtual void OnTaskQueueEnabled() = 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 private:
66 // Returns the priority which is next after |priority|.
67 static TaskQueue::QueuePriority NextPriority(
68 TaskQueue::QueuePriority priority);
70 // Return true if |out_queue| contains the queue with the oldest pending task
71 // from the set of queues of |priority|, or false if all queues of that
72 // priority are empty.
73 bool ChooseOldestWithPriority(TaskQueue::QueuePriority priority,
74 internal::TaskQueueImpl** out_queue) const;
76 // Called whenever the selector chooses a task queue for execution with the
77 // priority |priority|.
78 void DidSelectQueueWithPriority(TaskQueue::QueuePriority priority);
80 // Number of high priority tasks which can be run before a normal priority
81 // task should be selected to prevent starvation.
82 // TODO(rmcilroy): Check if this is a good value.
83 static const size_t kMaxStarvationTasks = 5;
85 base::ThreadChecker main_thread_checker_;
86 TaskQueueSets task_queue_sets_;
88 size_t starvation_count_;
89 Observer* task_queue_selector_observer_; // NOT OWNED
90 DISALLOW_COPY_AND_ASSIGN(TaskQueueSelector);
93 } // namespace internal
94 } // namespace scheduler
96 #endif // COMPONENTS_SCHEDULER_CHILD_TASK_QUEUE_SELECTOR_H