mcloud  1.0.0
MCloud API library for cmcc cloud service
taskqueue.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Gary Wang <gary.wang@canonical.com>
17  */
18 
19 #ifndef MCLOUD_API_TASKQUEUE_H_
20 #define MCLOUD_API_TASKQUEUE_H_
21 
22 #include <memory>
23 #include <deque>
24 #include <mutex>
25 
26 namespace mcloud {
27 namespace api {
28 
33 template<typename T, typename Container = std::deque<T>>
34 class TaskQueue {
35  public:
36  typedef std::shared_ptr<TaskQueue> Ptr;
37  typedef typename Container::size_type size_type;
38  typedef typename Container::const_iterator const_iterator;
39 
40  TaskQueue() = default;
41 
42  virtual ~TaskQueue() = default;
43 
44  TaskQueue(TaskQueue &&queue) {
45  std::lock_guard<std::mutex> lock(mutex_);
46  tasks_ = std::move(queue.tasks_);
47  }
48 
49  TaskQueue(const TaskQueue &queue) {
50  std::lock_guard<std::mutex> lock(mutex_);
51  tasks_ = queue.tasks_;
52  }
53 
54  TaskQueue &operator= (const TaskQueue &queue) {
55  if (this != &queue) {
56  std::lock_guard<std::mutex> lock1(mutex_);
57  std::lock_guard<std::mutex> lock2(queue.mutex_);
58  tasks_ = queue.tasks_;
59  }
60 
61  return *this;
62  }
63 
64  typename Container::iterator begin() {
65  std::lock_guard<std::mutex> lock(mutex_);
66  return tasks_.begin();
67  }
68 
69  typename Container::iterator end() {
70  std::lock_guard<std::mutex> lock(mutex_);
71  return tasks_.end();
72  }
73 
74  typename Container::iterator cbegin() const {
75  std::lock_guard<std::mutex> lock(mutex_);
76  return tasks_.cbegin();
77  }
78 
79  typename Container::iterator cend() const {
80  std::lock_guard<std::mutex> lock(mutex_);
81  return tasks_.cend();
82  }
83 
84  size_type size() const {
85  std::lock_guard<std::mutex> lock(mutex_);
86  return tasks_.size();
87  }
88 
89  bool empty() const {
90  std::lock_guard<std::mutex> lock(mutex_);
91  return tasks_.empty();
92  }
93 
94  void push(const T &task) {
95  std::lock_guard<std::mutex> lock(mutex_);
96  tasks_.push_back(task);
97  }
98 
99  void push(const TaskQueue & queue) {
100  std::lock_guard<std::mutex> lock(mutex_);
101  for (const T & task: queue.tasks_){
102  tasks_.push_back(task);
103  }
104  }
105 
106  bool try_pop(T &task) {
107  std::lock_guard<std::mutex> lock(mutex_);
108 
109  if (tasks_.empty())
110  return false;
111 
112  task = tasks_.front();
113  tasks_.pop_front();
114  return true;
115  }
116 
117  const T & operator[](int index) const {
118  std::lock_guard<std::mutex> lock(mutex_);
119  return tasks_[index];
120  }
121 
122  private:
123  Container tasks_;
124 
125  mutable std::mutex mutex_;
126 };
127 
128 }
129 }
130 
131 #endif // MCLOUD_API_TASKQUEUE_H_
A thread-safe deque template.
Definition: taskqueue.h:34
Container::iterator cend() const
Definition: taskqueue.h:79
Container::const_iterator const_iterator
Definition: taskqueue.h:38
Container::iterator cbegin() const
Definition: taskqueue.h:74
void push(const TaskQueue &queue)
Definition: taskqueue.h:99
TaskQueue(TaskQueue &&queue)
Definition: taskqueue.h:44
const T & operator[](int index) const
Definition: taskqueue.h:117
TaskQueue & operator=(const TaskQueue &queue)
Definition: taskqueue.h:54
TaskQueue(const TaskQueue &queue)
Definition: taskqueue.h:49
Container::iterator begin()
Definition: taskqueue.h:64
std::shared_ptr< TaskQueue > Ptr
Definition: taskqueue.h:36
size_type size() const
Definition: taskqueue.h:84
virtual ~TaskQueue()=default
void push(const T &task)
Definition: taskqueue.h:94
Definition: client.h:37
bool try_pop(T &task)
Definition: taskqueue.h:106
bool empty() const
Definition: taskqueue.h:89
Container::size_type size_type
Definition: taskqueue.h:37
Container::iterator end()
Definition: taskqueue.h:69