forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockingQueue.h
277 lines (244 loc) · 8 KB
/
BlockingQueue.h
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
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef QTAV_BLOCKINGQUEUE_H
#define QTAV_BLOCKINGQUEUE_H
#include <QtCore/QReadWriteLock>
#include <QtCore/QWaitCondition>
//TODO: block full and empty condition separately
template<typename T> class QQueue;
namespace QtAV {
template <typename T, template <typename> class Container = QQueue>
class BlockingQueue
{
public:
BlockingQueue();
void setCapacity(int max); //enqueue is allowed if less than capacity
void setThreshold(int min); //wake up and enqueue
void put(const T& t);
T take();
void setBlocking(bool block); //will wake if false. called when no more data can enqueue
void blockEmpty(bool block);
void blockFull(bool block);
//TODO:setMinBlock,MaxBlock
inline void clear();
inline bool isEmpty() const;
inline bool isEnough() const; //size > thres
inline bool isFull() const; //size >= cap
inline int size() const;
inline int threshold() const;
inline int capacity() const;
class StateChangeCallback
{
public:
virtual ~StateChangeCallback(){}
virtual void call() = 0;
};
void setEmptyCallback(StateChangeCallback* call);
void setThresholdCallback(StateChangeCallback* call);
void setFullCallback(StateChangeCallback* call);
private:
bool block_empty, block_full;
int cap, thres; //static?
Container<T> queue;
mutable QReadWriteLock lock; //locker in const func
QReadWriteLock block_change_lock;
QWaitCondition cond_full, cond_empty;
//upto_threshold_callback, downto_threshold_callback
StateChangeCallback *empty_callback, *threshold_callback, *full_callback;
};
/* cap - thres = 24, about 1s
* if fps is large, then larger capacity and threshold is preferred
*/
template <typename T, template <typename> class Container>
BlockingQueue<T, Container>::BlockingQueue()
:block_empty(true),block_full(true),cap(48),thres(32)
, empty_callback(0)
, threshold_callback(0)
, full_callback(0)
{
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setCapacity(int max)
{
qDebug("queue capacity==>>%d", max);
QWriteLocker locker(&lock);
Q_UNUSED(locker);
cap = max;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setThreshold(int min)
{
qDebug("queue threshold==>>%d", min);
QWriteLocker locker(&lock);
Q_UNUSED(locker);
thres = min;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::put(const T& t)
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
if (queue.size() >= cap) {
//qDebug("queue full"); //too frequent
if (full_callback) {
full_callback->call();
}
if (block_full)
cond_full.wait(&lock);
}
queue.enqueue(t);
cond_empty.wakeAll();
}
template <typename T, template <typename> class Container>
T BlockingQueue<T, Container>::take()
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
if (queue.size() < thres)
cond_full.wakeAll();
if (queue.isEmpty()) {//TODO:always block?
//qDebug("queue empty!!");
if (empty_callback) {
empty_callback->call();
}
if (block_empty)
cond_empty.wait(&lock);
}
//TODO: Why still empty?
if (queue.isEmpty()) {
qWarning("Queue is still empty");
if (empty_callback) {
empty_callback->call();
}
return T();
}
return queue.dequeue();
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setBlocking(bool block)
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
block_empty = block_full = block;
if (!block) {
cond_empty.wakeAll(); //empty still wait. setBlock=>setCapacity(-1)
cond_full.wakeAll();
}
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::blockEmpty(bool block)
{
if (!block) {
cond_empty.wakeAll();
}
QWriteLocker locker(&block_change_lock);
Q_UNUSED(locker);
block_empty = block;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::blockFull(bool block)
{
if (!block) {
cond_full.wakeAll();
}
//DO NOT use the same lock that put() get() use. it may be already locked
//this function usualy called in demux thread, so no lock is ok
QWriteLocker locker(&block_change_lock);
Q_UNUSED(locker);
block_full = block;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::clear()
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
//cond_empty.wakeAll();
cond_full.wakeAll();
queue.clear();
//TODO: assert not empty
}
template <typename T, template <typename> class Container>
bool BlockingQueue<T, Container>::isEmpty() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return queue.isEmpty();
}
template <typename T, template <typename> class Container>
bool BlockingQueue<T, Container>::isEnough() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return queue.size() >= thres;
}
template <typename T, template <typename> class Container>
bool BlockingQueue<T, Container>::isFull() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return queue.size() >= cap;
}
template <typename T, template <typename> class Container>
int BlockingQueue<T, Container>::size() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return queue.size();
}
template <typename T, template <typename> class Container>
int BlockingQueue<T, Container>::threshold() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return thres;
}
template <typename T, template <typename> class Container>
int BlockingQueue<T, Container>::capacity() const
{
QReadLocker locker(&lock);
Q_UNUSED(locker);
return cap;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setEmptyCallback(StateChangeCallback *call)
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
if (empty_callback)
delete empty_callback;
empty_callback = call;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setThresholdCallback(StateChangeCallback *call)
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
if (threshold_callback)
delete threshold_callback;
threshold_callback = call;
}
template <typename T, template <typename> class Container>
void BlockingQueue<T, Container>::setFullCallback(StateChangeCallback *call)
{
QWriteLocker locker(&lock);
Q_UNUSED(locker);
if (full_callback)
delete full_callback;
full_callback = call;
}
} //namespace QtAV
#endif // QTAV_BLOCKINGQUEUE_H