The Pedigree Project  0.1
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Buffer< T, allowShortOperation > Class Template Reference

#include <Buffer.h>

+ Inheritance diagram for Buffer< T, allowShortOperation >:
+ Collaboration diagram for Buffer< T, allowShortOperation >:

Classes

struct  MonitorTarget
 
struct  Segment
 

Public Member Functions

 Buffer (size_t bufferSize)
 
size_t write (const T *buffer, size_t count, bool block=true)
 
size_t read (T *buffer, size_t count, bool block=true)
 
void disableWrites ()
 
void disableReads ()
 
bool enableWrites ()
 
bool enableReads ()
 
size_t getDataSize ()
 
size_t getSize ()
 
bool canWrite (bool block)
 
bool canRead (bool block)
 
void wipe ()
 
void monitor (Thread *pThread, Event *pEvent)
 
void monitor (Semaphore *pSemaphore)
 
void cullMonitorTargets (Thread *pThread)
 
void cullMonitorTargets (Semaphore *pSemaphore)
 
void cullMonitorTargets (Event *pEvent)
 

Private Member Functions

 WITHOUT_IMPLICIT_CONSTRUCTORS (Buffer)
 
void notifyMonitors ()
 
void addSegment (const T *buffer, size_t count)
 

Private Attributes

size_t m_BufferSize
 
size_t m_DataSize
 
Mutex m_Lock
 
ConditionVariable m_WriteCondition
 
ConditionVariable m_ReadCondition
 
List< Segment * > m_Segments
 
List< MonitorTarget * > m_MonitorTargets
 
bool m_bCanRead
 
bool m_bCanWrite
 

Static Private Attributes

static const size_t m_SegmentSize = 32768
 

Friends

class UnixSocket
 

Detailed Description

template<class T, bool allowShortOperation = false>
class Buffer< T, allowShortOperation >

Provides a buffer of a specific size and utility functions for integration with e.g. File or other kernel systems.

allowShortOperation defines the action to take on overflow. If true, the buffer's write() operation is permitted to return a size less than requested if the buffer would overflow. Otherwise, the implementation is required to block until bytes are present (unless blocking has been explicitly denied).

Note that an attempt to read when writing is disabled that would block will always return the number of bytes read so far (or zero if none yet). The same is true for an attempt to write when reading is disabled if that would block.

Definition at line 49 of file Buffer.h.

Member Function Documentation

template<class T, bool allowShortOperation>
void Buffer< T, allowShortOperation >::addSegment ( const T *  buffer,
size_t  count 
)
private

Create a new segment with the given data.

Definition at line 592 of file Buffer.cc.

References Buffer< T, allowShortOperation >::Segment::data, and Buffer< T, allowShortOperation >::Segment::size.

template<class T , bool allowShortOperation>
bool Buffer< T, allowShortOperation >::canRead ( bool  block)

Check if the buffer can be read from.

Todo:
handle errors better

Definition at line 437 of file Buffer.cc.

Referenced by ConsolePhysicalFile::readBytewise(), PsAuxFile::select(), and Pipe::select().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
bool Buffer< T, allowShortOperation >::canWrite ( bool  block)

Check if the buffer can be written to.

Note
This does not guarantee the next write() will succeed.

Definition at line 409 of file Buffer.cc.

Referenced by PsAuxFile::select(), Pipe::select(), and ConsoleMasterFile::writeBytewise().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::cullMonitorTargets ( Thread pThread)

Remove monitoring targets for the given thread.

Definition at line 503 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::cullMonitorTargets ( Semaphore pSemaphore)

Remove monitoring targets for the given Semaphore.

Definition at line 524 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::cullMonitorTargets ( Event pEvent)

Remove monitoring targets for the given Event.

Definition at line 546 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::disableReads ( )

Disable further reads from the buffer. This will wake up all writers waiting on reader.

Definition at line 368 of file Buffer.cc.

Referenced by Pipe::decreaseRefCount().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::disableWrites ( )

Disable further writes to the buffer. This will wake up all readers waiting on a writer.

Definition at line 358 of file Buffer.cc.

Referenced by Pipe::decreaseRefCount().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
bool Buffer< T, allowShortOperation >::enableReads ( )

Enable reads from the buffer.

Returns
the previous state of reads.

Definition at line 387 of file Buffer.cc.

Referenced by Pipe::isFifo().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
bool Buffer< T, allowShortOperation >::enableWrites ( )

Enable writes to the buffer.

Returns
the previous state of writes.

Definition at line 378 of file Buffer.cc.

Referenced by Pipe::isFifo().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
size_t Buffer< T, allowShortOperation >::getDataSize ( )

Get the number of bytes in the buffer now.

Definition at line 396 of file Buffer.cc.

template<class T , bool allowShortOperation>
size_t Buffer< T, allowShortOperation >::getSize ( )

Get the full size of the buffer (potential storage).

Definition at line 403 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::monitor ( Thread pThread,
Event pEvent 
)

Add an event to be sent to the given thread upon a data change.

Note
An event does not guarantee the next operation will succeed.

Definition at line 483 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::monitor ( Semaphore pSemaphore)

Add a Semaphore to be signaled when data changes.

Definition at line 493 of file Buffer.cc.

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::notifyMonitors ( )
private

Helper function to send events upon completing an action.

Clears all monitors as a side effect.

Definition at line 568 of file Buffer.cc.

References Semaphore::release(), and Thread::sendEvent().

template<class T, bool allowShortOperation>
size_t Buffer< T, allowShortOperation >::read ( T *  buffer,
size_t  count,
bool  block = true 
)

Read

Parameters
countvalues into
buffer,optionallyblocking if no more values are available to be read yet.

Definition at line 236 of file Buffer.cc.

References Buffer< T, allowShortOperation >::Segment::data, Buffer< T, allowShortOperation >::Segment::reader, and Buffer< T, allowShortOperation >::Segment::size.

Referenced by PsAuxFile::readBytewise(), Pipe::readBytewise(), ConsoleMasterFile::readBytewise(), ConsoleSlaveFile::readBytewise(), and ConsolePhysicalFile::readBytewise().

+ Here is the caller graph for this function:

template<class T , bool allowShortOperation>
void Buffer< T, allowShortOperation >::wipe ( )

Wipes the buffer.

Definition at line 466 of file Buffer.cc.

Referenced by Pipe::isFifo().

+ Here is the caller graph for this function:

template<class T, bool allowShortOperation>
size_t Buffer< T, allowShortOperation >::write ( const T *  buffer,
size_t  count,
bool  block = true 
)

Write

Parameters
countvalues from
buffer,optionallyblocking before writing if there is insufficient space.

Definition at line 64 of file Buffer.cc.

References Buffer< T, allowShortOperation >::Segment::data, and Buffer< T, allowShortOperation >::Segment::size.

Referenced by PsAuxFile::select(), and Pipe::writeBytewise().

+ Here is the caller graph for this function:

Member Data Documentation

template<class T, bool allowShortOperation = false>
const size_t Buffer< T, allowShortOperation >::m_SegmentSize = 32768
staticprivate

Controls the size of each segment.

Definition at line 166 of file Buffer.h.


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