Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / queue.h
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership.  You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied.  See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2014 Cloudius Systems, Ltd.
20  */
21
22 #ifndef CEPH_MSG_DPDK_QUEUE_H_
23 #define CEPH_MSG_DPDK_QUEUE_H_
24
25 #include <queue>
26
27 #include "circular_buffer.h"
28
29 template <typename T>
30 class queue {
31   std::queue<T, circular_buffer<T>> _q;
32   size_t _max;
33
34  public:
35   explicit queue(size_t size): _max(size) {}
36
37   // Push an item.
38   //
39   // Returns false if the queue was full and the item was not pushed.
40   bool push(T&& a);
41
42   // pops an item.
43   T pop();
44
45   // Consumes items from the queue, passing them to @func, until @func
46   // returns false or the queue it empty
47   //
48   // Returns false if func returned false.
49   template <typename Func>
50   bool consume(Func&& func);
51
52   // Returns true when the queue is empty.
53   bool empty() const;
54
55   // Returns true when the queue is full.
56   bool full() const;
57
58   size_t size() const { return _q.size(); }
59
60   // Destroy any items in the queue
61   void clear() {
62     while (!_q.empty()) {
63       _q.pop();
64     }
65   }
66 };
67
68 template <typename T>
69 inline bool queue<T>::push(T&& data) {
70   if (_q.size() < _max) {
71     _q.push(std::move(data));
72     notify_not_empty();
73     return true;
74   } else {
75     return false;
76   }
77 }
78
79 template <typename T>
80 inline T queue<T>::pop() {
81   T data = std::move(_q.front());
82   _q.pop();
83   return data;
84 }
85
86 template <typename T>
87 inline bool queue<T>::empty() const {
88   return _q.empty();
89 }
90
91 template <typename T>
92 inline bool queue<T>::full() const {
93   return _q.size() == _max;
94 }
95
96 #endif /* CEPH_MSG_DPDK_QUEUE_H_ */