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 #include "components/scheduler/child/task_queue_sets.h"
7 #include "base/logging.h"
8 #include "components/scheduler/child/task_queue_impl.h"
13 TaskQueueSets::TaskQueueSets(size_t num_sets
)
14 : enqueue_order_to_queue_maps_(num_sets
) {}
16 TaskQueueSets::~TaskQueueSets() {}
18 void TaskQueueSets::RemoveQueue(internal::TaskQueueImpl
* queue
) {
20 bool has_enqueue_order
=
21 queue
->GetWorkQueueFrontTaskEnqueueOrder(&enqueue_order
);
22 if (!has_enqueue_order
)
24 size_t set_index
= queue
->get_task_queue_set_index();
25 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size());
28 enqueue_order_to_queue_maps_
[set_index
].find(enqueue_order
)->second
);
29 enqueue_order_to_queue_maps_
[set_index
].erase(enqueue_order
);
32 void TaskQueueSets::AssignQueueToSet(internal::TaskQueueImpl
* queue
,
34 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size());
36 bool has_enqueue_order
=
37 queue
->GetWorkQueueFrontTaskEnqueueOrder(&enqueue_order
);
38 size_t old_set
= queue
->get_task_queue_set_index();
39 DCHECK_LT(old_set
, enqueue_order_to_queue_maps_
.size());
40 queue
->set_task_queue_set_index(set_index
);
41 if (!has_enqueue_order
)
43 enqueue_order_to_queue_maps_
[old_set
].erase(enqueue_order
);
44 enqueue_order_to_queue_maps_
[set_index
].insert(
45 std::make_pair(enqueue_order
, queue
));
48 void TaskQueueSets::OnPushQueue(internal::TaskQueueImpl
* queue
) {
50 bool has_enqueue_order
=
51 queue
->GetWorkQueueFrontTaskEnqueueOrder(&enqueue_order
);
52 DCHECK(has_enqueue_order
);
53 size_t set_index
= queue
->get_task_queue_set_index();
54 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size()) << " set_index = "
56 enqueue_order_to_queue_maps_
[set_index
].insert(
57 std::make_pair(enqueue_order
, queue
));
60 void TaskQueueSets::OnPopQueue(internal::TaskQueueImpl
* queue
) {
61 size_t set_index
= queue
->get_task_queue_set_index();
62 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size());
63 DCHECK(!enqueue_order_to_queue_maps_
[set_index
].empty()) << " set_index = "
65 DCHECK_EQ(enqueue_order_to_queue_maps_
[set_index
].begin()->second
, queue
)
66 << " set_index = " << set_index
;
68 enqueue_order_to_queue_maps_
[set_index
].erase(
69 enqueue_order_to_queue_maps_
[set_index
].begin());
71 bool has_enqueue_order
=
72 queue
->GetWorkQueueFrontTaskEnqueueOrder(&enqueue_order
);
73 if (!has_enqueue_order
)
75 enqueue_order_to_queue_maps_
[set_index
].insert(
76 std::make_pair(enqueue_order
, queue
));
79 bool TaskQueueSets::GetOldestQueueInSet(
81 internal::TaskQueueImpl
** out_queue
) const {
82 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size());
83 if (enqueue_order_to_queue_maps_
[set_index
].empty())
85 *out_queue
= enqueue_order_to_queue_maps_
[set_index
].begin()->second
;
89 bool TaskQueueSets::IsSetEmpty(size_t set_index
) const {
90 DCHECK_LT(set_index
, enqueue_order_to_queue_maps_
.size())
91 << " set_index = " << set_index
;
92 return enqueue_order_to_queue_maps_
[set_index
].empty();
95 } // namespace internal
96 } // namespace scheduler