Single-reader single-writer lock-free ring buffer. More...
Go to the source code of this file.
Data Structures | |
struct | PaUtilRingBuffer |
Functions | |
long | PaUtil_InitializeRingBuffer (PaUtilRingBuffer *rbuf, long elementSizeBytes, long elementCount, void *dataPtr) |
void | PaUtil_FlushRingBuffer (PaUtilRingBuffer *rbuf) |
long | PaUtil_GetRingBufferWriteAvailable (PaUtilRingBuffer *rbuf) |
long | PaUtil_GetRingBufferReadAvailable (PaUtilRingBuffer *rbuf) |
long | PaUtil_WriteRingBuffer (PaUtilRingBuffer *rbuf, const void *data, long elementCount) |
long | PaUtil_ReadRingBuffer (PaUtilRingBuffer *rbuf, void *data, long elementCount) |
long | PaUtil_GetRingBufferWriteRegions (PaUtilRingBuffer *rbuf, long elementCount, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2) |
long | PaUtil_AdvanceRingBufferWriteIndex (PaUtilRingBuffer *rbuf, long elementCount) |
long | PaUtil_GetRingBufferReadRegions (PaUtilRingBuffer *rbuf, long elementCount, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2) |
long | PaUtil_AdvanceRingBufferReadIndex (PaUtilRingBuffer *rbuf, long elementCount) |
Single-reader single-writer lock-free ring buffer.
PaUtilRingBuffer is a ring buffer used to transport samples between different execution contexts (threads, OS callbacks, interrupt handlers) without requiring the use of any locks. This only works when there is a single reader and a single writer (ie. one thread or callback writes to the ring buffer, another thread or callback reads from it).
The PaUtilRingBuffer structure manages a ring buffer containing N elements, where N must be a power of two. An element may be any size (specified in bytes).
The memory area used to store the buffer elements must be allocated by the client prior to calling PaUtil_InitializeRingBuffer() and must outlive the use of the ring buffer.
long PaUtil_AdvanceRingBufferReadIndex | ( | PaUtilRingBuffer * | rbuf, | |
long | elementCount | |||
) |
Advance the read index to the next location to be read.
rbuf | The ring buffer. | |
elementCount | The number of elements to advance. |
References PaUtilRingBuffer::bigMask, and PaUtilRingBuffer::readIndex.
Referenced by PaUtil_ReadRingBuffer().
long PaUtil_AdvanceRingBufferWriteIndex | ( | PaUtilRingBuffer * | rbuf, | |
long | elementCount | |||
) |
Advance the write index to the next location to be written.
rbuf | The ring buffer. | |
elementCount | The number of elements to advance. |
References PaUtilRingBuffer::bigMask, and PaUtilRingBuffer::writeIndex.
Referenced by PaUtil_WriteRingBuffer(), and resetBlioRingBuffers().
void PaUtil_FlushRingBuffer | ( | PaUtilRingBuffer * | rbuf | ) |
Clear buffer. Should only be called when buffer is NOT being read.
rbuf | The ring buffer. |
References PaUtilRingBuffer::readIndex, and PaUtilRingBuffer::writeIndex.
Referenced by PaUtil_InitializeRingBuffer(), and resetBlioRingBuffers().
long PaUtil_GetRingBufferReadAvailable | ( | PaUtilRingBuffer * | rbuf | ) |
Retrieve the number of elements available in the ring buffer for reading.
rbuf | The ring buffer. |
References PaUtilRingBuffer::bigMask, PaUtilRingBuffer::readIndex, and PaUtilRingBuffer::writeIndex.
Referenced by BlioCallback(), GetStreamReadAvailable(), PaUtil_GetRingBufferReadRegions(), PaUtil_GetRingBufferWriteAvailable(), and ReadStream().
long PaUtil_GetRingBufferReadRegions | ( | PaUtilRingBuffer * | rbuf, | |
long | elementCount, | |||
void ** | dataPtr1, | |||
long * | sizePtr1, | |||
void ** | dataPtr2, | |||
long * | sizePtr2 | |||
) |
Get address of region(s) from which we can write data.
rbuf | The ring buffer. | |
elementCount | The number of elements desired. | |
dataPtr1 | The address where the first (or only) region pointer will be stored. | |
sizePtr1 | The address where the first (or only) region length will be stored. | |
dataPtr2 | The address where the second region pointer will be stored if the first region is too small to satisfy elementCount. | |
sizePtr2 | The address where the second region length will be stored if the first region is too small to satisfy elementCount. |
References PaUtilRingBuffer::buffer, PaUtilRingBuffer::bufferSize, PaUtilRingBuffer::elementSizeBytes, PaUtil_GetRingBufferReadAvailable(), PaUtilRingBuffer::readIndex, and PaUtilRingBuffer::smallMask.
Referenced by PaUtil_ReadRingBuffer().
long PaUtil_GetRingBufferWriteAvailable | ( | PaUtilRingBuffer * | rbuf | ) |
Retrieve the number of elements available in the ring buffer for writing.
rbuf | The ring buffer. |
References PaUtilRingBuffer::bufferSize, and PaUtil_GetRingBufferReadAvailable().
Referenced by BlioCallback(), GetStreamWriteAvailable(), PaUtil_GetRingBufferWriteRegions(), waitUntilBlioWriteBufferIsFlushed(), and WriteStream().
long PaUtil_GetRingBufferWriteRegions | ( | PaUtilRingBuffer * | rbuf, | |
long | elementCount, | |||
void ** | dataPtr1, | |||
long * | sizePtr1, | |||
void ** | dataPtr2, | |||
long * | sizePtr2 | |||
) |
Get address of region(s) to which we can write data.
rbuf | The ring buffer. | |
elementCount | The number of elements desired. | |
dataPtr1 | The address where the first (or only) region pointer will be stored. | |
sizePtr1 | The address where the first (or only) region length will be stored. | |
dataPtr2 | The address where the second region pointer will be stored if the first region is too small to satisfy elementCount. | |
sizePtr2 | The address where the second region length will be stored if the first region is too small to satisfy elementCount. |
References PaUtilRingBuffer::buffer, PaUtilRingBuffer::bufferSize, PaUtilRingBuffer::elementSizeBytes, PaUtil_GetRingBufferWriteAvailable(), PaUtilRingBuffer::smallMask, and PaUtilRingBuffer::writeIndex.
Referenced by PaUtil_WriteRingBuffer().
long PaUtil_InitializeRingBuffer | ( | PaUtilRingBuffer * | rbuf, | |
long | elementSizeBytes, | |||
long | elementCount, | |||
void * | dataPtr | |||
) |
Initialize Ring Buffer.
rbuf | The ring buffer. | |
elementSizeBytes | The size of a single data element in bytes. | |
elementCount | The number of elements in the buffer (must be power of 2). | |
dataPtr | A pointer to a previously allocated area where the data will be maintained. It must be elementCount*elementSizeBytes long. |
References PaUtilRingBuffer::bigMask, PaUtilRingBuffer::buffer, PaUtilRingBuffer::bufferSize, PaUtilRingBuffer::elementSizeBytes, PaUtil_FlushRingBuffer(), and PaUtilRingBuffer::smallMask.
Referenced by initializeBlioRingBuffers().
long PaUtil_ReadRingBuffer | ( | PaUtilRingBuffer * | rbuf, | |
void * | data, | |||
long | elementCount | |||
) |
Read data from the ring buffer.
rbuf | The ring buffer. | |
data | The address where the data should be stored. | |
elementCount | The number of elements to be read. |
References PaUtilRingBuffer::elementSizeBytes, PaUtil_AdvanceRingBufferReadIndex(), and PaUtil_GetRingBufferReadRegions().
Referenced by BlioCallback(), and ReadStream().
long PaUtil_WriteRingBuffer | ( | PaUtilRingBuffer * | rbuf, | |
const void * | data, | |||
long | elementCount | |||
) |
Write data to the ring buffer.
rbuf | The ring buffer. | |
data | The address of new data to write to the buffer. | |
elementCount | The number of elements to be written. |
References PaUtilRingBuffer::elementSizeBytes, PaUtil_AdvanceRingBufferWriteIndex(), and PaUtil_GetRingBufferWriteRegions().
Referenced by BlioCallback(), and WriteStream().