Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / scheduler / child / task_queue_sets.cc
blob3d5dcae2d6da5bd4c6a536dab5ec1c3936e1b078
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"
10 namespace scheduler {
11 namespace internal {
13 TaskQueueSets::TaskQueueSets(size_t num_sets) : age_to_queue_maps_(num_sets) {}
15 TaskQueueSets::~TaskQueueSets() {}
17 void TaskQueueSets::RemoveQueue(internal::TaskQueueImpl* queue) {
18 int age;
19 bool has_age = queue->GetWorkQueueFrontTaskAge(&age);
20 if (!has_age)
21 return;
22 size_t set_index = queue->get_task_queue_set_index();
23 DCHECK_LT(set_index, age_to_queue_maps_.size());
24 age_to_queue_maps_[set_index].erase(age);
27 void TaskQueueSets::AssignQueueToSet(internal::TaskQueueImpl* queue,
28 size_t set_index) {
29 DCHECK_LT(set_index, age_to_queue_maps_.size());
30 int age;
31 bool has_age = queue->GetWorkQueueFrontTaskAge(&age);
32 size_t old_set = queue->get_task_queue_set_index();
33 DCHECK_LT(old_set, age_to_queue_maps_.size());
34 queue->set_task_queue_set_index(set_index);
35 if (!has_age)
36 return;
37 age_to_queue_maps_[old_set].erase(age);
38 age_to_queue_maps_[set_index].insert(std::make_pair(age, queue));
41 void TaskQueueSets::OnPushQueue(internal::TaskQueueImpl* queue) {
42 int age;
43 bool has_age = queue->GetWorkQueueFrontTaskAge(&age);
44 DCHECK(has_age);
45 size_t set_index = queue->get_task_queue_set_index();
46 DCHECK_LT(set_index, age_to_queue_maps_.size()) << " set_index = "
47 << set_index;
48 age_to_queue_maps_[set_index].insert(std::make_pair(age, queue));
51 void TaskQueueSets::OnPopQueue(internal::TaskQueueImpl* queue) {
52 size_t set_index = queue->get_task_queue_set_index();
53 DCHECK_LT(set_index, age_to_queue_maps_.size());
54 DCHECK(!age_to_queue_maps_[set_index].empty()) << " set_index = "
55 << set_index;
56 DCHECK_EQ(age_to_queue_maps_[set_index].begin()->second, queue)
57 << " set_index = " << set_index;
58 // O(1) amortised.
59 age_to_queue_maps_[set_index].erase(age_to_queue_maps_[set_index].begin());
60 int age;
61 bool has_age = queue->GetWorkQueueFrontTaskAge(&age);
62 if (!has_age)
63 return;
64 age_to_queue_maps_[set_index].insert(std::make_pair(age, queue));
67 bool TaskQueueSets::GetOldestQueueInSet(
68 size_t set_index,
69 internal::TaskQueueImpl** out_queue) const {
70 DCHECK_LT(set_index, age_to_queue_maps_.size());
71 if (age_to_queue_maps_[set_index].empty())
72 return false;
73 *out_queue = age_to_queue_maps_[set_index].begin()->second;
74 return true;
77 } // namespace internal
78 } // namespace scheduler