-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblocking_queue.h
More file actions
281 lines (240 loc) · 5.6 KB
/
blocking_queue.h
File metadata and controls
281 lines (240 loc) · 5.6 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef BLOCKING_QUEUE_H
#define BLOCKING_QUEUE_H
#include <QtCore/qqueue.h>
#include <QtCore/qreadwritelock.h>
class EventPrivate;
class Event
{
public:
Event();
~Event();
public:
void set();
void clear();
bool wait(unsigned long time = ULONG_MAX);
bool isSet() const;
quint32 getting() const;
private:
EventPrivate *d;
};
template <typename T>
class BlockingQueue
{
public:
explicit BlockingQueue(quint32 capacity);
BlockingQueue() : BlockingQueue(UINT_MAX) {}
~BlockingQueue();
public:
void setCapacity(quint32 capacity);
bool put(const T &e); // insert e to the tail of queue. blocked until not full.
bool putForcedly(const T& e); // insert e to the tail of queue ignoring capacity.
bool returns(const T &e); // like put() but insert e to the head of queue.
bool returnsForcely(const T& e); // like putForcedly() but insert e to the head of queue.
T get();
T peek();
void clear();
bool remove(const T &e);
public:
inline bool isEmpty();
inline bool isFull();
inline quint32 capacity() const;
inline quint32 size() const;
inline quint32 getting() const;
inline bool contains(const T &e);
private:
QQueue<T> queue;
Event notEmpty;
Event notFull;
QReadWriteLock lock;
quint32 mCapacity;
Q_DISABLE_COPY(BlockingQueue)
};
template <typename T>
BlockingQueue<T>::BlockingQueue(quint32 capacity)
: mCapacity(capacity)
{
notEmpty.clear();
notFull.set();
}
template <typename T>
BlockingQueue<T>::~BlockingQueue()
{
// if (queue.size() > 0) {
// qtng_debug << "queue is free with element left.";
// }
}
template <typename T>
void BlockingQueue<T>::setCapacity(quint32 capacity)
{
lock.lockForWrite();
this->mCapacity = capacity;
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
} else {
notFull.set();
}
lock.unlock();
}
template <typename T>
void BlockingQueue<T>::clear()
{
lock.lockForWrite();
this->queue.clear();
notFull.set();
notEmpty.clear();
lock.unlock();
}
template <typename T>
bool BlockingQueue<T>::remove(const T &e)
{
lock.lockForWrite();
int n = this->queue.removeAll(e);
if (n > 0) {
if (this->queue.isEmpty()) {
notEmpty.clear();
} else {
notEmpty.set();
}
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
} else {
notFull.set();
}
lock.unlock();
return true;
} else {
lock.unlock();
return false;
}
}
template <typename T>
bool BlockingQueue<T>::put(const T &e)
{
if (!notFull.wait()) {
return false;
}
lock.lockForWrite();
queue.enqueue(e);
notEmpty.set();
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
}
lock.unlock();
return true;
}
template <typename T>
bool BlockingQueue<T>::putForcedly(const T& e)
{
lock.lockForWrite();
queue.enqueue(e);
notEmpty.set();
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
}
lock.unlock();
return true;
}
template <typename T>
bool BlockingQueue<T>::returns(const T &e)
{
if (!notFull.wait()) {
return false;
}
lock.lockForWrite();
queue.prepend(e);
notEmpty.set();
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
}
lock.unlock();
return true;
}
template <typename T>
bool BlockingQueue<T>::returnsForcely(const T& e)
{
lock.lockForWrite();
queue.prepend(e);
notEmpty.set();
if (static_cast<quint32>(queue.size()) >= mCapacity) {
notFull.clear();
}
lock.unlock();
return true;
}
template <typename T>
T BlockingQueue<T>::get()
{
if (!notEmpty.wait())
return T();
lock.lockForWrite();
const T &e = queue.dequeue();
if (this->queue.isEmpty()) {
notEmpty.clear();
}
if (static_cast<quint32>(queue.size()) < mCapacity) {
notFull.set();
}
lock.unlock();
return e;
}
template <typename T>
T BlockingQueue<T>::peek()
{
lock.lockForRead();
if (this->queue.isEmpty()) {
lock.unlock();
return T();
}
const T &t = queue.head();
lock.unlock();
return t;
}
template <typename T>
inline bool BlockingQueue<T>::isEmpty()
{
lock.lockForRead();
bool t = queue.isEmpty();
lock.unlock();
return t;
}
template <typename T>
inline bool BlockingQueue<T>::isFull()
{
lock.lockForRead();
bool t = static_cast<quint32>(queue.size()) >= mCapacity;
lock.unlock();
return t;
}
template <typename T>
inline quint32 BlockingQueue<T>::capacity() const
{
const_cast<BlockingQueue<T> *>(this)->lock.lockForRead();
quint32 c = mCapacity;
const_cast<BlockingQueue<T> *>(this)->lock.unlock();
return c;
}
template <typename T>
inline quint32 BlockingQueue<T>::size() const
{
const_cast<BlockingQueue<T> *>(this)->lock.lockForRead();
int s = queue.size();
const_cast<BlockingQueue<T> *>(this)->lock.unlock();
return s;
}
template <typename T>
inline quint32 BlockingQueue<T>::getting() const
{
const_cast<BlockingQueue<T> *>(this)->lock.lockForRead();
int g = notEmpty.getting();
const_cast<BlockingQueue<T> *>(this)->lock.unlock();
return g;
}
template <typename T>
inline bool BlockingQueue<T>::contains(const T &e)
{
const_cast<BlockingQueue<T> *>(this)->lock.lockForRead();
bool t = queue.contains(e);
const_cast<BlockingQueue<T> *>(this)->lock.unlock();
return t;
}
#endif // BLOCKING_QUEUE_H