summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/job_queue.cc
blob: 40a9ed60b8d391e0f3ac50e093ab251969cd3441 (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
// Copyright 2013 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.

#include "chrome/browser/chromeos/drive/job_queue.h"

#include "base/logging.h"
#include "base/strings/stringprintf.h"

namespace drive {

JobQueue::JobQueue(size_t num_max_concurrent_jobs,
                   size_t num_priority_levels)
    : num_max_concurrent_jobs_(num_max_concurrent_jobs),
      queue_(num_priority_levels) {
}

JobQueue::~JobQueue() {
}

bool JobQueue::PopForRun(int accepted_priority, JobID* id) {
  DCHECK_LT(accepted_priority, static_cast<int>(queue_.size()));

  // Too many jobs are running already.
  if (running_.size() >= num_max_concurrent_jobs_)
    return false;

  // Looks up the queue in the order of priority upto |accepted_priority|.
  for (int priority = 0; priority <= accepted_priority; ++priority) {
    if (!queue_[priority].empty()) {
      *id = queue_[priority].front();
      queue_[priority].pop_front();
      running_.insert(*id);
      return true;
    }
  }
  return false;
}

void JobQueue::Push(JobID id, int priority) {
  DCHECK_LT(priority, static_cast<int>(queue_.size()));

  queue_[priority].push_back(id);
}

void JobQueue::MarkFinished(JobID id) {
  size_t num_erased = running_.erase(id);
  DCHECK_EQ(1U, num_erased);
}

std::string JobQueue::ToString() const {
  size_t pending = 0;
  for (size_t i = 0; i < queue_.size(); ++i)
    pending += queue_[i].size();
  return base::StringPrintf("pending: %d, running: %d",
                            static_cast<int>(pending),
                            static_cast<int>(running_.size()));
}

size_t JobQueue::GetNumberOfJobs() const {
  size_t count = running_.size();
  for (size_t i = 0; i < queue_.size(); ++i)
    count += queue_[i].size();
  return count;
}

}  // namespace drive