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#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_JOB_QUEUE_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_JOB_QUEUE_H_
7
8#include <deque>
9#include <set>
10#include <vector>
11
12#include "chrome/browser/chromeos/drive/job_list.h"
13
14namespace drive {
15
16// Priority queue for managing jobs in JobScheduler.
17class JobQueue {
18 public:
19  // Creates a queue that allows |num_max_concurrent_jobs| concurrent job
20  // execution and has |num_priority_levels| levels of priority.
21  JobQueue(size_t num_max_concurrent_jobs, size_t num_priority_levels);
22  ~JobQueue();
23
24  // Pushes a job |id| of |priority|. The job with the smallest priority value
25  // is popped first (lower values are higher priority). In the same priority,
26  // the queue is "first-in first-out".
27  void Push(JobID id, int priority);
28
29  // Pops the first job which meets |accepted_priority| (i.e. the first job in
30  // the queue with equal or higher priority (lower value)), and the limit of
31  // concurrent job count is satisfied.
32  //
33  // For instance, if |accepted_priority| is 1, the first job with priority 0
34  // (higher priority) in the queue is picked even if a job with priority 1 was
35  // pushed earlier. If there is no job with priority 0, the first job with
36  // priority 1 in the queue is picked.
37  bool PopForRun(int accepted_priority, JobID* id);
38
39  // Gets queued jobs with the given priority.
40  void GetQueuedJobs(int priority, std::vector<JobID>* jobs) const;
41
42  // Marks a running job |id| as finished running. This decreases the count
43  // of running parallel jobs and makes room for other jobs to be popped.
44  void MarkFinished(JobID id);
45
46  // Generates a string representing the internal state for logging.
47  std::string ToString() const;
48
49  // Gets the total number of jobs in the queue.
50  size_t GetNumberOfJobs() const;
51
52  // Removes the job from the queue.
53  void Remove(JobID id);
54
55 private:
56  size_t num_max_concurrent_jobs_;
57  std::vector<std::deque<JobID> > queue_;
58  std::set<JobID> running_;
59
60  DISALLOW_COPY_AND_ASSIGN(JobQueue);
61};
62
63}  // namespace drive
64
65#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_JOB_QUEUE_H_
66