GNU CommonC++
Public Member Functions | Static Public Attributes | Protected Member Functions | Related Functions | List of all members
ost::Buffer Class Referenceabstract

The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be used to transfer objects between one or more producer and consumer threads. More...

#include <buffer.h>

Inheritance diagram for ost::Buffer:
ost::Conditional ost::FixedBuffer

Public Member Functions

 Buffer (size_t capacity)
 Create a buffer object of known capacity. More...
 
virtual ~Buffer ()
 In derived functions, may be used to free the actual memory used to hold buffered data. More...
 
size_t getSize (void)
 Return the capacity of the buffer as specified at creation. More...
 
size_t getUsed (void)
 Return the current capacity in use for the buffer. More...
 
size_t wait (void *buf, timeout_t timeout=0)
 Let one or more threads wait for an object to become available in the buffer. More...
 
size_t post (void *buf, timeout_t timeout=0)
 Post an object into the buffer and enable a waiting thread to receive it. More...
 
size_t peek (void *buf)
 Peek at the current content (first object) in the buffer. More...
 
virtual bool isValid (void)
 New virtual to test if buffer is a valid object. More...
 
- Public Member Functions inherited from ost::Conditional
 Conditional (const char *id=NULL)
 Create an instance of a conditional. More...
 
virtual ~Conditional ()
 Destroy the conditional. More...
 
void signal (bool broadcast)
 Signal a conditional object and a waiting threads. More...
 
bool wait (timeout_t timer=0, bool locked=false)
 Wait to be signaled from another thread. More...
 
void enterMutex (void)
 Locks the conditional's mutex for this thread. More...
 
void lock (void)
 In the future we will use lock in place of enterMutex since the conditional composite is not a recursive mutex, and hence using enterMutex may cause confusion in expectation with the behavior of the Mutex class. More...
 
bool tryEnterMutex (void)
 Tries to lock the conditional for the current thread. More...
 
bool test (void)
 
void leaveMutex (void)
 Leaving a mutex frees that mutex for use by another thread. More...
 
void unlock (void)
 

Static Public Attributes

static const size_t timeout
 value to return when a timed operation returned with a timeout. More...
 

Protected Member Functions

virtual size_t onPeek (void *buf)=0
 Invoke derived class buffer peeking method. More...
 
virtual size_t onWait (void *buf)=0
 Invoke derived class object request from buffer. More...
 
virtual size_t onPost (void *buf)=0
 Invoke derived class posting of object to buffer. More...
 

Related Functions

(Note that these are not member functions.)

size_t get (Buffer &b, void *o, timeout_t t=0)
 
size_t put (Buffer &b, void *o, timeout_t t=0)
 
size_t peek (Buffer &b, void *o)
 

Detailed Description

The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be used to transfer objects between one or more producer and consumer threads.

Producer threads post objects into the buffer, and consumer threads wait for and receive objects from the buffer. Semaphores are used to to block the buffer from overflowing and indicate when there is data available, and mutexes are used to protect multiple consumers and producer threads from stepping over each other.

The buffer class is an abstract class in that the actual data being buffered is not directly specified within the buffer class itself. The buffer class should be used as a base class for a class that actually impliments buffering and which may be aware of the data types actually are being buffered. A template class could be created based on buffer for this purpose. Another possibility is to create a class derived from both Thread and Buffer which can be used to implement message passing threads.

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m

Producer/Consumer buffer for use between threads.

Constructor & Destructor Documentation

◆ Buffer()

ost::Buffer::Buffer ( size_t  capacity)

Create a buffer object of known capacity.

Parameters
capacityis the integer capacity of the buffer.

◆ ~Buffer()

virtual ost::Buffer::~Buffer ( )
virtual

In derived functions, may be used to free the actual memory used to hold buffered data.

Member Function Documentation

◆ getSize()

size_t ost::Buffer::getSize ( void  )
inline

Return the capacity of the buffer as specified at creation.

Returns
size of buffer.

◆ getUsed()

size_t ost::Buffer::getUsed ( void  )
inline

Return the current capacity in use for the buffer.

Free space is technically getSize() - getUsed().

Returns
integer used capacity of the buffer.
See also
getSize

◆ isValid()

virtual bool ost::Buffer::isValid ( void  )
virtual

New virtual to test if buffer is a valid object.

Returns
true if object is valid.

Reimplemented in ost::FixedBuffer.

◆ onPeek()

virtual size_t ost::Buffer::onPeek ( void *  buf)
protectedpure virtual

Invoke derived class buffer peeking method.

Returns
size of object found.
Parameters
bufpointer to copy contents of head of buffer to.

Implemented in ost::FixedBuffer.

◆ onPost()

virtual size_t ost::Buffer::onPost ( void *  buf)
protectedpure virtual

Invoke derived class posting of object to buffer.

Returns
size of object posted.
Parameters
bufpointer to object being posted to the buffer.

Implemented in ost::FixedBuffer.

◆ onWait()

virtual size_t ost::Buffer::onWait ( void *  buf)
protectedpure virtual

Invoke derived class object request from buffer.

Returns
size of object returned.
Parameters
bufpointer to hold object returned from the buffer.

Implemented in ost::FixedBuffer.

◆ peek()

size_t ost::Buffer::peek ( void *  buf)

Peek at the current content (first object) in the buffer.

Returns
size of object in the buffer.
Parameters
bufpointer to store object found in the buffer.

Referenced by peek().

◆ post()

size_t ost::Buffer::post ( void *  buf,
timeout_t  timeout = 0 
)

Post an object into the buffer and enable a waiting thread to receive it.

Returns
size of object posted in bytes.
Parameters
bufpointer to object to store in the buffer.
timeouttime to wait.

Referenced by put().

◆ wait()

size_t ost::Buffer::wait ( void *  buf,
timeout_t  timeout = 0 
)

Let one or more threads wait for an object to become available in the buffer.

The waiting thread(s) will wait forever if no object is ever placed into the buffer.

Returns
size of object passed by buffer in bytes.
Parameters
bufpointer to store object retrieved from the buffer.
timeouttime to wait.

Referenced by get().

Friends And Related Function Documentation

◆ get()

size_t get ( Buffer b,
void *  o,
timeout_t  t = 0 
)
related

References wait().

◆ peek()

size_t peek ( Buffer b,
void *  o 
)
related
Examples
SampleSocketPort.cpp.

References peek().

◆ put()

size_t put ( Buffer b,
void *  o,
timeout_t  t = 0 
)
related

References post().

Member Data Documentation

◆ timeout

const size_t ost::Buffer::timeout
static

value to return when a timed operation returned with a timeout.


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