summaryrefslogtreecommitdiffstats
path: root/content/renderer/scheduler/renderer_task_queue_selector.h
blob: cfea89f43b2550b2898e1fa4bb477860c78185e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_TASK_QUEUE_SELECTOR_H
#define CONTENT_RENDERER_SCHEDULER_RENDERER_TASK_QUEUE_SELECTOR_H

#include <set>

#include "base/compiler_specific.h"
#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
#include "content/renderer/scheduler/task_queue_selector.h"

namespace content {

// A RendererTaskQueueSelector is a TaskQueueSelector which is used by the
// RendererScheduler to enable prioritization of particular task queues.
class CONTENT_EXPORT RendererTaskQueueSelector
    : NON_EXPORTED_BASE(public TaskQueueSelector) {
 public:
  enum QueuePriority {
    // Queues with control priority will run before any other queue, and will
    // explicitly starve other queues. Typically this should only be used for
    // private queues which perform control operations.
    CONTROL_PRIORITY,
    // Queues with high priority will be selected preferentially over normal or
    // best effort queues. The selector will ensure that high priority queues
    // cannot completely starve normal priority queues.
    HIGH_PRIORITY,
    // Queues with normal priority are the default.
    NORMAL_PRIORITY,
    // Queues with best effort priority will only be run if all other queues are
    // empty. They can be starved by the other queues.
    BEST_EFFORT_PRIORITY,
    // Must be the last entry.
    QUEUE_PRIORITY_COUNT,
    FIRST_QUEUE_PRIORITY = CONTROL_PRIORITY,
  };

  RendererTaskQueueSelector();
  ~RendererTaskQueueSelector() override;

  // Set the priority of |queue_index| to |priority|.
  void SetQueuePriority(size_t queue_index, QueuePriority priority);

  // Enable the |queue_index| with a priority of |priority|. By default all
  // queues are enabled with normal priority.
  void EnableQueue(size_t queue_index, QueuePriority priority);

  // Disable the |queue_index|.
  void DisableQueue(size_t queue_index);

  // TaskQueueSelector implementation:
  void RegisterWorkQueues(
      const std::vector<const base::TaskQueue*>& work_queues) override;
  bool SelectWorkQueueToService(size_t* out_queue_index) override;
  void AsValueInto(base::trace_event::TracedValue* state) const override;

 private:
  // Returns true if queueA contains an older task than queueB.
  static bool IsOlder(const base::TaskQueue* queueA,
                      const base::TaskQueue* queueB);

  // Returns the priority which is next after |priority|.
  static QueuePriority NextPriority(QueuePriority priority);

  static const char* PriorityToString(QueuePriority priority);

  // Return true if |out_queue_index| indicates the index of the queue with
  // the oldest pending task from the set of queues of |priority|, or
  // false if all queues of that priority are empty.
  bool ChooseOldestWithPriority(QueuePriority priority,
                                size_t* out_queue_index) const;

  // Returns true if |queue_index| is enabled with the given |priority|.
  bool QueueEnabledWithPriority(size_t queue_index,
                                QueuePriority priority) const;

  // Called whenever the selector chooses a task queue for execution with the
  // priority |priority|.
  void DidSelectQueueWithPriority(QueuePriority priority);

  // Number of high priority tasks which can be run before a normal priority
  // task should be selected to prevent starvation.
  // TODO(rmcilroy): Check if this is a good value.
  static const size_t kMaxStarvationTasks = 5;

  base::ThreadChecker main_thread_checker_;
  std::vector<const base::TaskQueue*> work_queues_;
  std::set<size_t> queue_priorities_[QUEUE_PRIORITY_COUNT];
  size_t starvation_count_;
  DISALLOW_COPY_AND_ASSIGN(RendererTaskQueueSelector);
};

}  // namespace content

#endif  // CONTENT_RENDERER_SCHEDULER_RENDERER_TASK_QUEUE_SELECTOR_H