-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fifo.h
193 lines (177 loc) · 7.77 KB
/
fifo.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
#ifndef FIFO_H
#define FIFO_H
#include <QtDebug>
#include <QMutex>
#include <QScopedPointer>
#include <QSharedPointer>
#include "pa_ringbuffer.h"
#include "util/class.h"
#include "util/math.h"
#include "util/reference.h"
template <class DataType>
class FIFO {
public:
explicit FIFO(int size)
: m_data(NULL) {
size = roundUpToPowerOf2(size);
// If we can't represent the next higher power of 2 then bail.
if (size < 0) {
return;
}
m_data = new DataType[size];
memset(m_data, 0, sizeof(DataType) * size);
PaUtil_InitializeRingBuffer(
&m_ringBuffer, sizeof(DataType), size, m_data);
}
virtual ~FIFO() {
delete [] m_data;
}
int readAvailable() const {
return PaUtil_GetRingBufferReadAvailable(&m_ringBuffer);
}
int writeAvailable() const {
return PaUtil_GetRingBufferWriteAvailable(&m_ringBuffer);
}
int read(DataType* pData, int count) {
return PaUtil_ReadRingBuffer(&m_ringBuffer, pData, count);
}
int write(const DataType* pData, int count) {
return PaUtil_WriteRingBuffer(&m_ringBuffer, pData, count);
}
void writeBlocking(const DataType* pData, int count) {
int written = 0;
while (written < count) {
written += write(pData + written, count - written);
}
}
int aquireWriteRegions(int count,
DataType** dataPtr1, ring_buffer_size_t* sizePtr1,
DataType** dataPtr2, ring_buffer_size_t* sizePtr2) {
return PaUtil_GetRingBufferWriteRegions(&m_ringBuffer, count,
(void**)dataPtr1, sizePtr1, (void**)dataPtr2, sizePtr2);
}
int releaseWriteRegions(int count) {
return PaUtil_AdvanceRingBufferWriteIndex(&m_ringBuffer, count);
}
int aquireReadRegions(int count,
DataType** dataPtr1, ring_buffer_size_t* sizePtr1,
DataType** dataPtr2, ring_buffer_size_t* sizePtr2) {
return PaUtil_GetRingBufferReadRegions(&m_ringBuffer, count,
(void**)dataPtr1, sizePtr1, (void**)dataPtr2, sizePtr2);
}
int releaseReadRegions(int count) {
return PaUtil_AdvanceRingBufferReadIndex(&m_ringBuffer, count);
}
int flushReadData(int count) {
int flush = math_min(readAvailable(), count);
return PaUtil_AdvanceRingBufferReadIndex(&m_ringBuffer, flush);
}
private:
DataType* m_data;
PaUtilRingBuffer m_ringBuffer;
DISALLOW_COPY_AND_ASSIGN(FIFO<DataType>);
};
// MessagePipe represents one side of a TwoWayMessagePipe. The direction of the
// pipe is with respect to the owner so sender and receiver are
// perspective-dependent. If serializeWrites is true then calls to writeMessages
// will be serialized with a mutex.
template <class SenderMessageType, class ReceiverMessageType>
class MessagePipe {
public:
MessagePipe(FIFO<SenderMessageType>& receiver_messages,
FIFO<ReceiverMessageType>& sender_messages,
BaseReferenceHolder* pTwoWayMessagePipeReference,
bool serialize_writes)
: m_receiver_messages(receiver_messages),
m_sender_messages(sender_messages),
m_pTwoWayMessagePipeReference(pTwoWayMessagePipeReference),
m_bSerializeWrites(serialize_writes) {
}
// Returns the number of ReceiverMessageType messages waiting to be read by
// the receiver. Non-blocking.
inline int messageCount() const {
return m_sender_messages.readAvailable();
}
// Read a ReceiverMessageType written by the receiver addressed to the
// sender. Non-blocking.
inline int readMessages(ReceiverMessageType* messages, int count) {
return m_sender_messages.read(messages, count);
}
// Writes up to 'count' messages from the 'message' array to the receiver
// and returns the number of successfully written messages. If
// serializeWrites is active, this method is blocking.
inline int writeMessages(const SenderMessageType* messages, int count) {
if (m_bSerializeWrites) {
m_serializationMutex.lock();
}
int result = m_receiver_messages.write(messages, count);
if (m_bSerializeWrites) {
m_serializationMutex.unlock();
}
return result;
}
private:
QMutex m_serializationMutex;
FIFO<SenderMessageType>& m_receiver_messages;
FIFO<ReceiverMessageType>& m_sender_messages;
QScopedPointer<BaseReferenceHolder> m_pTwoWayMessagePipeReference;
bool m_bSerializeWrites;
#define COMMA ,
DISALLOW_COPY_AND_ASSIGN(MessagePipe<SenderMessageType COMMA ReceiverMessageType>);
#undef COMMA
};
// TwoWayMessagePipe is a bare-bones wrapper around the above FIFO class that
// facilitates non-blocking two-way communication. To keep terminology clear,
// there are two sides to the message pipe, the sender side and the receiver
// side. The non-blocking aspect of the underlying FIFO class requires that the
// sender methods and target methods each only be called from a single thread,
// or alternatively guarded with a mutex. The most common use-case of this class
// is sending and receiving messages with the callback thread without the
// callback thread blocking.
//
// This class is an implementation detail and cannot be instantiated
// directly. Use makeTwoWayMessagePipe(...) to create a two-way pipe.
template <class SenderMessageType, class ReceiverMessageType>
class TwoWayMessagePipe {
public:
// Creates a TwoWayMessagePipe with SenderMessageType and
// ReceiverMessageType as the message types. Returns a pair of MessagePipes,
// the first is the sender's pipe (sends SenderMessageType and receives
// ReceiverMessageType messages) and the second is the receiver's pipe
// (sends ReceiverMessageType and receives SenderMessageType messages).
static QPair<MessagePipe<SenderMessageType, ReceiverMessageType>*,
MessagePipe<ReceiverMessageType, SenderMessageType>*> makeTwoWayMessagePipe(
int sender_fifo_size,
int receiver_fifo_size,
bool serialize_sender_writes,
bool serialize_receiver_writes) {
QSharedPointer<TwoWayMessagePipe<SenderMessageType, ReceiverMessageType> > pipe(
new TwoWayMessagePipe<SenderMessageType, ReceiverMessageType>(
sender_fifo_size, receiver_fifo_size));
return QPair<MessagePipe<SenderMessageType, ReceiverMessageType>*,
MessagePipe<ReceiverMessageType, SenderMessageType>*>(
new MessagePipe<SenderMessageType, ReceiverMessageType>(
pipe->m_receiver_messages, pipe->m_sender_messages,
new ReferenceHolder<TwoWayMessagePipe<SenderMessageType, ReceiverMessageType> >(pipe),
serialize_sender_writes),
new MessagePipe<ReceiverMessageType, SenderMessageType>(
pipe->m_sender_messages, pipe->m_receiver_messages,
new ReferenceHolder<TwoWayMessagePipe<SenderMessageType, ReceiverMessageType> >(pipe),
serialize_receiver_writes));
}
private:
TwoWayMessagePipe(int sender_fifo_size, int receiver_fifo_size)
: m_receiver_messages(receiver_fifo_size),
m_sender_messages(sender_fifo_size) {
}
// Messages waiting to be delivered to the receiver.
FIFO<SenderMessageType> m_receiver_messages;
// Messages waiting to be delivered to the sender.
FIFO<ReceiverMessageType> m_sender_messages;
// This #define is because the macro gets confused by the template
// parameters.
#define COMMA ,
DISALLOW_COPY_AND_ASSIGN(TwoWayMessagePipe<SenderMessageType COMMA ReceiverMessageType>);
#undef COMMA
};
#endif /* FIFO_H */