summaryrefslogtreecommitdiffstats
path: root/components/scheduler/base/work_queue.h
blob: 23845029f3c2fe2b308529ce7106d29c094074c0 (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
// Copyright 2015 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_BASE_WORK_QUEUE_H_
#define CONTENT_RENDERER_SCHEDULER_BASE_WORK_QUEUE_H_

#include <stddef.h>

#include <set>

#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_argument.h"
#include "components/scheduler/base/enqueue_order.h"
#include "components/scheduler/base/task_queue_impl.h"
#include "components/scheduler/scheduler_export.h"

namespace scheduler {
namespace internal {
class WorkQueueSets;

class SCHEDULER_EXPORT WorkQueue {
 public:
  WorkQueue(TaskQueueImpl* task_queue, const char* name);
  ~WorkQueue();

  // Associates this work queue with the given work queue sets. This must be
  // called before any tasks can be inserted into this work queue.
  void AssignToWorkQueueSets(WorkQueueSets* work_queue_sets);

  // Assigns the current set index.
  void AssignSetIndex(size_t work_queue_set_index);

  void AsValueInto(base::trace_event::TracedValue* state) const;

  // Clears the |work_queue_|.
  void Clear();

  // returns true if the |work_queue_| is empty.
  bool Empty() const { return work_queue_.empty(); }

  // If the |work_queue_| isn't empty, |enqueue_order| gets set to the enqueue
  // order of the front task and the function returns true.  Otherwise the
  // function returns false.
  bool GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const;

  // Returns the first task in this queue or null if the queue is empty.
  const TaskQueueImpl::Task* GetFrontTask() const;

  // Pushes the task onto the |work_queue_| and informs the WorkQueueSets if
  // the head changed.
  void Push(TaskQueueImpl::Task&& task);

  // Pushes the task onto the |work_queue_|, sets the |enqueue_order| and
  // informs the WorkQueueSets if the head changed.
  void PushAndSetEnqueueOrder(TaskQueueImpl::Task&& task,
                              EnqueueOrder enqueue_order);

  // Swap the |work_queue_| with |incoming_queue| and informs the
  // WorkQueueSets if the head changed. Assumes |task_queue_->any_thread_lock_|
  // is locked.
  void SwapLocked(std::queue<TaskQueueImpl::Task>& incoming_queue);

  size_t Size() const { return work_queue_.size(); }

  // Pulls a task off the |work_queue_| and informs the WorkQueueSets.
  TaskQueueImpl::Task TakeTaskFromWorkQueue();

  const char* name() const { return name_; }

  TaskQueueImpl* task_queue() const { return task_queue_; }

  WorkQueueSets* work_queue_sets() const { return work_queue_sets_; }

  size_t work_queue_set_index() const { return work_queue_set_index_; }

  // Test support function. This should not be used in production code.
  void PopTaskForTest();

  // Returns true if the front task in this queue has an older enqueue order
  // than the front task of |other_queue|. Both queue are assumed to be
  // non-empty.
  bool ShouldRunBefore(const WorkQueue* other_queue) const;

 private:
  std::queue<TaskQueueImpl::Task> work_queue_;
  WorkQueueSets* work_queue_sets_;  // NOT OWNED.
  TaskQueueImpl* task_queue_;       // NOT OWNED.
  size_t work_queue_set_index_;
  const char* name_;
};

}  // namespace internal
}  // namespace scheduler

#endif  // CONTENT_RENDERER_SCHEDULER_BASE_WORK_QUEUE_H_