The Pedigree Project  0.1
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
RingBuffer< T > Class Template Reference

Utility class to provide a ring buffer. More...

#include <RingBuffer.h>

+ Inheritance diagram for RingBuffer< T >:
+ Collaboration diagram for RingBuffer< T >:

Classes

struct  MonitorTarget
 

Public Member Functions

 RingBuffer (size_t ringSize)
 Constructor - pass in the desired size of the ring buffer.
 
 ~RingBuffer ()
 Destructor - destroys the ring; ensure nothing is calling waitFor.
 
void write (const T &obj, Time::Timestamp &timeout)
 write - write a byte to the ring buffer. More...
 
void write (const T &obj)
 
size_t write (const T *obj, size_t n, Time::Timestamp &timeout)
 write - write the given number of objects to the ring buffer.
 
size_t write (const T *obj, size_t n)
 
read (Time::Timestamp &timeout)
 read - read a byte from the ring buffer. More...
 
read ()
 
size_t read (T *out, size_t n, Time::Timestamp &timeout)
 read - read up to the given number of objects from the ring buffer
 
size_t read (T *out, size_t n)
 
bool dataReady ()
 dataReady - is data ready for reading from the ring buffer?
 
bool canWrite ()
 canWrite - is it possible to write to the ring buffer without blocking?
 
bool waitFor (RingBufferWait::WaitType wait, Time::Timestamp &timeout)
 waitFor - block until the given condition is true (readable/writeable)
 
bool waitFor (RingBufferWait::WaitType wait)
 
void monitor (Thread *pThread, Event *pEvent)
 monitor - add a new Event to be fired when something happens More...
 
void cullMonitorTargets (Thread *pThread)
 Cull all monitor targets pointing to pThread.
 

Private Member Functions

void notifyMonitors ()
 Trigger event for threads waiting on us.
 

Private Attributes

size_t m_RingSize
 
ConditionVariable m_WriteCondition
 
ConditionVariable m_ReadCondition
 
List< T > m_Ring
 
Mutex m_Lock
 
List< MonitorTarget * > m_MonitorTargets
 

Detailed Description

template<class T>
class RingBuffer< T >

Utility class to provide a ring buffer.

Using this class provides safety in accessing the ring buffer as well as the ability to check (with and without blocking) whether the buffer can be read or written to at this time.

The idea of the waitFor function is to provide a way for applications desiring integration with a select()-style interface to block until the condition is met.

Definition at line 61 of file RingBuffer.h.

Member Function Documentation

template<class T>
void RingBuffer< T >::monitor ( Thread pThread,
Event pEvent 
)
inline

monitor - add a new Event to be fired when something happens

This could be a read or a write event; after receiving the event be sure to call dataReady() and/or canWrite() to determine the state of the buffer.

Do not assume that an event means both a read and write will not block. In fact, never assume an event means either will not block. You may need to re-subscribe to the event if something else reads or writes to the ring buffer between the event trigger and your handling.

Definition at line 280 of file RingBuffer.h.

template<class T>
T RingBuffer< T >::read ( Time::Timestamp &  timeout)
inline

read - read a byte from the ring buffer.

Todo:
need to allow read() to fail - use Result<>

Definition at line 138 of file RingBuffer.h.

template<class T>
void RingBuffer< T >::write ( const T &  obj,
Time::Timestamp &  timeout 
)
inline

write - write a byte to the ring buffer.

Todo:
oh dear, writes are always assumed to succeed

Definition at line 79 of file RingBuffer.h.


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