Home | History | Annotate | Download | only in drive
      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.
      4 
      5 #include "chrome/browser/chromeos/drive/job_queue.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "base/strings/stringprintf.h"
     11 
     12 namespace drive {
     13 
     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) {
     18 }
     19 
     20 JobQueue::~JobQueue() {
     21 }
     22 
     23 bool JobQueue::PopForRun(int accepted_priority, JobID* id) {
     24   DCHECK_LT(accepted_priority, static_cast<int>(queue_.size()));
     25 
     26   // Too many jobs are running already.
     27   if (running_.size() >= num_max_concurrent_jobs_)
     28     return false;
     29 
     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();
     35       running_.insert(*id);
     36       return true;
     37     }
     38   }
     39   return false;
     40 }
     41 
     42 void JobQueue::GetQueuedJobs(int priority, std::vector<JobID>* jobs) const {
     43   DCHECK_LT(priority, static_cast<int>(queue_.size()));
     44 
     45   jobs->assign(queue_[priority].begin(), queue_[priority].end());
     46 }
     47 
     48 void JobQueue::Push(JobID id, int priority) {
     49   DCHECK_LT(priority, static_cast<int>(queue_.size()));
     50 
     51   queue_[priority].push_back(id);
     52 }
     53 
     54 void JobQueue::MarkFinished(JobID id) {
     55   size_t num_erased = running_.erase(id);
     56   DCHECK_EQ(1U, num_erased);
     57 }
     58 
     59 std::string JobQueue::ToString() const {
     60   size_t pending = 0;
     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()));
     66 }
     67 
     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();
     72   return count;
     73 }
     74 
     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);
     81       break;
     82     }
     83   }
     84 }
     85 
     86 }  // namespace drive
     87