1 // Copyright 2013 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 "chrome/browser/chromeos/drive/job_queue.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
14 JobQueue::JobQueue(size_t num_max_concurrent_jobs
,
15 size_t num_priority_levels
)
16 : num_max_concurrent_jobs_(num_max_concurrent_jobs
),
17 queue_(num_priority_levels
) {
20 JobQueue::~JobQueue() {
23 bool JobQueue::PopForRun(int accepted_priority
, JobID
* id
) {
24 DCHECK_LT(accepted_priority
, static_cast<int>(queue_
.size()));
26 // Too many jobs are running already.
27 if (running_
.size() >= num_max_concurrent_jobs_
)
30 // Looks up the queue in the order of priority upto |accepted_priority|.
31 for (int priority
= 0; priority
<= accepted_priority
; ++priority
) {
32 if (!queue_
[priority
].empty()) {
33 *id
= queue_
[priority
].front();
34 queue_
[priority
].pop_front();
42 void JobQueue::GetQueuedJobs(int priority
, std::vector
<JobID
>* jobs
) const {
43 DCHECK_LT(priority
, static_cast<int>(queue_
.size()));
45 jobs
->assign(queue_
[priority
].begin(), queue_
[priority
].end());
48 void JobQueue::Push(JobID id
, int priority
) {
49 DCHECK_LT(priority
, static_cast<int>(queue_
.size()));
51 queue_
[priority
].push_back(id
);
54 void JobQueue::MarkFinished(JobID id
) {
55 size_t num_erased
= running_
.erase(id
);
56 DCHECK_EQ(1U, num_erased
);
59 std::string
JobQueue::ToString() const {
61 for (size_t i
= 0; i
< queue_
.size(); ++i
)
62 pending
+= queue_
[i
].size();
63 return base::StringPrintf("pending: %d, running: %d",
64 static_cast<int>(pending
),
65 static_cast<int>(running_
.size()));
68 size_t JobQueue::GetNumberOfJobs() const {
69 size_t count
= running_
.size();
70 for (size_t i
= 0; i
< queue_
.size(); ++i
)
71 count
+= queue_
[i
].size();
75 void JobQueue::Remove(JobID id
) {
76 for (size_t i
= 0; i
< queue_
.size(); ++i
) {
77 std::deque
<JobID
>::iterator iter
=
78 std::find(queue_
[i
].begin(), queue_
[i
].end(), id
);
79 if (iter
!= queue_
[i
].end()) {
80 queue_
[i
].erase(iter
);