forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataqueue.h
More file actions
164 lines (140 loc) · 4.28 KB
/
dataqueue.h
File metadata and controls
164 lines (140 loc) · 4.28 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
/*
* Copyright (c) 2015 Meltytech, LLC
* Author: Brian Matherly <code@brianmatherly.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATAQUEUE_H
#define DATAQUEUE_H
#include <QMutex>
#include <QWaitCondition>
#include <QMutexLocker>
#include <deque>
/*!
\class DataQueue
\brief The DataQueue provides a thread safe container for passing data between
objects.
\threadsafe
DataQueue provides a limited size container for passing data between objects.
One object can add data to the queue by calling push() while another object
can remove items from the queue by calling pop().
DataQueue provides configurable behavior for handling overflows. It can
discard the oldest, discard the newest or block the object calling push()
until room has been freed in the queue by another object calling pop().
DataQueue is threadsafe and is therefore most appropriate when passing data
between objects operating in different thread contexts.
*/
template <class T>
class DataQueue
{
public:
//! Overflow behavior modes.
typedef enum {
OverflowModeDiscardOldest = 0, //!< Discard oldest items
OverflowModeDiscardNewest, //!< Discard newest items
OverflowModeWait //!< Wait for space to be free
} OverflowMode;
/*!
Constructs a DataQueue.
The \a size will be the maximum queue size and the \a mode will dictate
overflow behavior.
*/
explicit DataQueue(int maxSize, OverflowMode mode);
//! Destructs a DataQueue.
virtual ~DataQueue();
/*!
Pushes an item into the queue.
If the queue is full and overflow mode is OverflowModeWait then this
function will block until pop() is called.
*/
void push(const T& item);
/*!
Pops an item from the queue.
If the queue is empty then this function will block. If blocking is
undesired, then check the return of count() before calling pop().
*/
T pop();
//! Returns the number of items in the queue.
int count() const;
private:
std::deque<T> m_queue;
int m_maxSize;
OverflowMode m_mode;
mutable QMutex m_mutex;
QWaitCondition m_notEmptyCondition;
QWaitCondition m_notFullCondition;
};
template <class T>
DataQueue<T>::DataQueue(int maxSize, OverflowMode mode)
: m_queue()
, m_maxSize(maxSize)
, m_mode(mode)
, m_mutex(QMutex::NonRecursive)
, m_notEmptyCondition()
, m_notFullCondition()
{
}
template <class T>
DataQueue<T>::~DataQueue()
{
}
template <class T>
void DataQueue<T>::push(const T& item)
{
m_mutex.lock();
if (m_queue.size() == m_maxSize) {
switch(m_mode) {
case OverflowModeDiscardOldest:
m_queue.pop_front();
m_queue.push_back(item);
break;
case OverflowModeDiscardNewest:
// This item is the newest so discard it and exit
break;
case OverflowModeWait:
m_notFullCondition.wait(&m_mutex);
m_queue.push_back(item);
break;
}
} else {
m_queue.push_back(item);
if (m_queue.size() == 1) {
m_notEmptyCondition.wakeOne();
}
}
m_mutex.unlock();
}
template <class T>
T DataQueue<T>::pop()
{
T retVal;
m_mutex.lock();
if (m_queue.size() == 0) {
m_notEmptyCondition.wait(&m_mutex);
}
retVal = m_queue.front();
m_queue.pop_front();
if (m_mode == OverflowModeWait && m_queue.size() == m_maxSize - 1) {
m_notFullCondition.wakeOne();
}
m_mutex.unlock();
return retVal;
}
template <class T>
int DataQueue<T>::count() const
{
QMutexLocker locker(&m_mutex);
return m_queue.size();
}
#endif // DATAQUEUE_H