28 jassert (bufferSize > 0);
38 auto vs = validStart.
get();
39 auto ve = validEnd.
get();
58 int& startIndex2,
int& blockSize2)
const noexcept
60 auto vs = validStart.get();
61 auto ve = validEnd.get();
96 int& startIndex2,
int& blockSize2)
const noexcept
98 auto vs = validStart.get();
99 auto ve = validEnd.get();
134template <AbstractFifo::ReadOrWrite mode>
136 : startIndex1 (
other.startIndex1),
137 blockSize1 (
other.blockSize1),
138 startIndex2 (
other.startIndex2),
139 blockSize2 (
other.blockSize2)
144template <AbstractFifo::ReadOrWrite mode>
145AbstractFifo::ScopedReadWrite<mode>&
146AbstractFifo::ScopedReadWrite<mode>::operator= (ScopedReadWrite&& other)
noexcept
152template <AbstractFifo::ReadOrWrite mode>
153void AbstractFifo::ScopedReadWrite<mode>::swap (ScopedReadWrite& other)
noexcept
155 std::swap (other.fifo, fifo);
156 std::swap (other.startIndex1, startIndex1);
157 std::swap (other.blockSize1, blockSize1);
158 std::swap (other.startIndex2, startIndex2);
159 std::swap (other.blockSize2, blockSize2);
162template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>;
163template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>;
177 struct WriteThread :
public Thread
179 WriteThread (AbstractFifo& f,
int* b, Random rng)
180 : Thread (
"fifo writer"), fifo (f), buffer (b), random (rng)
194 while (! threadShouldExit())
196 int num = random.nextInt (2000) + 1;
198 auto writer = fifo.write (num);
200 jassert (writer.blockSize1 >= 0 && writer.blockSize2 >= 0);
201 jassert (writer.blockSize1 == 0
202 || (writer.startIndex1 >= 0 && writer.startIndex1 < fifo.getTotalSize()));
203 jassert (writer.blockSize2 == 0
204 || (writer.startIndex2 >= 0 && writer.startIndex2 < fifo.getTotalSize()));
206 writer.forEach ([
this, &n] (
int index) { this->buffer[index] = n++; });
215 void runTest()
override
217 beginTest (
"AbstractFifo");
220 AbstractFifo fifo (numElementsInArray (buffer));
222 WriteThread writer (fifo, buffer, getRandom());
225 Random r = getRandom();
226 r.combineSeed (12345);
228 for (
int count = 100000; --count >= 0;)
230 int num = r.nextInt (6000) + 1;
232 auto reader = fifo.read (num);
234 if (! (reader.blockSize1 >= 0 && reader.blockSize2 >= 0)
235 && (reader.blockSize1 == 0
236 || (reader.startIndex1 >= 0 && reader.startIndex1 < fifo.getTotalSize()))
237 && (reader.blockSize2 == 0
238 || (reader.startIndex2 >= 0 && reader.startIndex2 < fifo.getTotalSize())))
240 expect (
false,
"prepareToRead returned -ve values");
246 reader.forEach ([&failed, &buffer, &n] (
int index)
248 failed = (buffer[index] != n++) || failed;
253 expect (
false,
"read values were incorrect");
260static AbstractFifoTests fifoUnitTests;
ScopedReadWrite()=default
Construct an unassigned reader/writer.
void reset() noexcept
Clears the buffer positions, so that it appears empty.
void prepareToWrite(int numToWrite, int &startIndex1, int &blockSize1, int &startIndex2, int &blockSize2) const noexcept
Returns the location within the buffer at which an incoming block of data should be written.
int getTotalSize() const noexcept
Returns the total size of the buffer being managed.
void prepareToRead(int numWanted, int &startIndex1, int &blockSize1, int &startIndex2, int &blockSize2) const noexcept
Returns the location within the buffer from which the next block of data should be read.
AbstractFifo(int capacity) noexcept
Creates a FIFO to manage a buffer with the specified capacity.
void finishedRead(int numRead) noexcept
Called after reading from the FIFO, to indicate that this many items have now been consumed.
int getFreeSpace() const noexcept
Returns the number of items that can currently be added to the buffer without it overflowing.
void finishedWrite(int numWritten) noexcept
Called after writing from the FIFO, to indicate that this many items have been added.
int getNumReady() const noexcept
Returns the number of items that can currently be read from the buffer.
~AbstractFifo()
Destructor.
void setTotalSize(int newSize) noexcept
Changes the buffer's total size.
Holds a resizable array of primitive or copy-by-value objects.
This is a base class for classes that perform a unit test.
Type get() const noexcept
Atomically reads and returns the current value.