Miosix  2.0alpha1
miosix::BufferQueue< T, size, numbuf > Class Template Reference

#include <queue.h>

Public Member Functions

 BufferQueue ()
 
bool isEmpty () const
 
bool isFull () const
 
unsigned int bufferMaxSize () const
 
unsigned int numberOfBuffers () const
 
bool tryGetWritableBuffer (T *&buffer)
 
void bufferFilled (unsigned int actualSize)
 
unsigned char availableForWriting () const
 
bool tryGetReadableBuffer (const T *&buffer, unsigned int &actualSize)
 
void bufferEmptied ()
 
unsigned char availableForReading () const
 
void reset ()
 

Detailed Description

template<typename T, unsigned int size, unsigned char numbuf = 2>
class miosix::BufferQueue< T, size, numbuf >

A class to handle double buffering, but also triple buffering and in general N-buffering. Works between two threads but is especially suited to synchronize between a thread and an interrupt routine.
Note that unlike Queue, this class is only a data structure and not a synchronization primitive. The synchronization between the thread and the IRQ (or the other thread) must be done by the caller.
The internal implementation treats the buffers as a circular queue of N elements, hence the name.

Template Parameters
Ttype of elements of the buffer, usually char or unsigned char
sizemaximum size of a buffer
numbufnumber of buffers, the default is two resulting in a double buffering scheme. Values 0 and 1 are forbidden

Constructor & Destructor Documentation

template<typename T , unsigned int size, unsigned char numbuf = 2>
miosix::BufferQueue< T, size, numbuf >::BufferQueue ( )
inline

Constructor, all buffers are empty

Member Function Documentation

template<typename T , unsigned int size, unsigned char numbuf = 2>
unsigned char miosix::BufferQueue< T, size, numbuf >::availableForReading ( ) const
inline
Returns
The number of buffers available for reading (0, to numbuf)
template<typename T , unsigned int size, unsigned char numbuf = 2>
unsigned char miosix::BufferQueue< T, size, numbuf >::availableForWriting ( ) const
inline
Returns
the number of buffers available for writing (0 to numbuf)
template<typename T , unsigned int size, unsigned char numbuf = 2>
void miosix::BufferQueue< T, size, numbuf >::bufferEmptied ( )
inline

After having called tryGetReadableBuffer() to retrieve a buffer and having read it, this member function allows to mark the buffer as available on the writer side.

template<typename T , unsigned int size, unsigned char numbuf = 2>
void miosix::BufferQueue< T, size, numbuf >::bufferFilled ( unsigned int  actualSize)
inline

After having called tryGetWritableBuffer() to retrieve a buffer and having filled it, this member function allows to mark the buffer as available on the reader side.

Parameters
actualSizeactual size of buffer. It usually equals bufferMaxSize but can be a lower value in case there is less available data
template<typename T , unsigned int size, unsigned char numbuf = 2>
unsigned int miosix::BufferQueue< T, size, numbuf >::bufferMaxSize ( ) const
inline
Returns
the maximum size of a buffer
template<typename T , unsigned int size, unsigned char numbuf = 2>
bool miosix::BufferQueue< T, size, numbuf >::isEmpty ( ) const
inline
Returns
true if no buffer is available for reading
template<typename T , unsigned int size, unsigned char numbuf = 2>
bool miosix::BufferQueue< T, size, numbuf >::isFull ( ) const
inline
Returns
true if no buffer is available for writing
template<typename T , unsigned int size, unsigned char numbuf = 2>
unsigned int miosix::BufferQueue< T, size, numbuf >::numberOfBuffers ( ) const
inline
Returns
the maximum number of buffers
template<typename T , unsigned int size, unsigned char numbuf = 2>
void miosix::BufferQueue< T, size, numbuf >::reset ( )
inline

Reset the buffers. As a consequence, the queue becomes empty.

template<typename T , unsigned int size, unsigned char numbuf = 2>
bool miosix::BufferQueue< T, size, numbuf >::tryGetReadableBuffer ( const T *&  buffer,
unsigned int &  actualSize 
)
inline

This member function allows to retrieve a buffer ready to be read, if available.

Parameters
bufferthe available buffer will be assigned here if available
actualSizethe actual size of the buffer, as reported by the writer side
Returns
true if a readable buffer has been found, false otherwise. In this case the buffer and actualSize parameters are not modified
template<typename T , unsigned int size, unsigned char numbuf = 2>
bool miosix::BufferQueue< T, size, numbuf >::tryGetWritableBuffer ( T *&  buffer)
inline

This member function allows to retrieve a buffer ready to be written, if available.

Parameters
bufferthe available buffer will be assigned here if available
Returns
true if a writable buffer has been found, false otherwise. In this case the buffer parameter is not modified

The documentation for this class was generated from the following file: