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

class  ActiveOperation
 
struct  Segment
 

Public Member Functions

 Buffer (size_t bufferSize)
 
size_t write (const T *buffer, size_t count, bool block=true)
 
size_t writeAvailable (const T *buffer, size_t count, bool atomic=false)
 
size_t writeAtomic (const T *buffer, size_t count, bool block=true)
 
bool tryWrite (const T *buffer, size_t count)
 
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)
 
uint64_t readableGeneration () const
 
uint64_t writableGeneration () const
 
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)
 
bool beginOperation ()
 
void endOperation ()
 
void notifyMonitors ()
 
void notifyMonitorsLocked ()
 
size_t writeLocked (const T *buffer, size_t count)
 
void addSegment (const T *buffer, size_t count)
 

Private Attributes

size_t m_BufferSize
 
size_t m_DataSize
 
Atomic< uint64_t > m_ReadableGeneration
 
Atomic< uint64_t > m_WritableGeneration
 
Mutex m_Lock
 
ConditionVariable m_WriteCondition
 
ConditionVariable m_ReadCondition
 
ConditionVariable m_DrainCondition
 
List< Segment * > m_Segments
 
BufferMonitors m_Monitors
 
bool m_bCanRead
 
bool m_bCanWrite
 
bool m_bClosing
 
size_t m_ActiveOperations
 

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 53 of file Buffer.h.

Constructor & Destructor Documentation

◆ Buffer()

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

Definition at line 41 of file Buffer.cc.

◆ ~Buffer()

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

Definition at line 58 of file Buffer.cc.

Member Function Documentation

◆ addSegment()

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 727 of file Buffer.cc.

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

◆ beginOperation()

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

Definition at line 82 of file Buffer.cc.

◆ canRead()

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

Check if the buffer can be read from.

Definition at line 525 of file Buffer.cc.

References LockGuard< T >::disown(), and ConditionVariable::mutexAcquired().

Referenced by ConsoleFile::select(), ConsolePhysicalFile::select(), PsAuxFile::select(), UnixSocket::select(), TextIO::select(), InputFile::select(), EvdevFile::select(), and TextIO::writeStr().

+ Here is the caller graph for this function:

◆ canWrite()

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 491 of file Buffer.cc.

References LockGuard< T >::disown(), and ConditionVariable::mutexAcquired().

Referenced by ConsoleFile::inputLineDiscipline(), ConsoleFile::select(), PsAuxFile::select(), and TextIO::select().

+ Here is the caller graph for this function:

◆ cullMonitorTargets() [1/3]

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

Remove monitoring targets for the given Event.

Definition at line 646 of file Buffer.cc.

◆ cullMonitorTargets() [2/3]

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

Remove monitoring targets for the given Semaphore.

Definition at line 633 of file Buffer.cc.

◆ cullMonitorTargets() [3/3]

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

Remove monitoring targets for the given thread.

Definition at line 620 of file Buffer.cc.

◆ disableReads()

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

Disable further reads from the buffer. This wakes all operations whose wait predicate can no longer succeed.

Definition at line 422 of file Buffer.cc.

◆ disableWrites()

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

Disable further writes to the buffer. This wakes all operations whose wait predicate can no longer succeed.

Definition at line 406 of file Buffer.cc.

Referenced by Ps2Controller::uninitialise().

+ Here is the caller graph for this function:

◆ enableReads()

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

Enable reads from the buffer.

Returns
the previous state of reads.

Definition at line 454 of file Buffer.cc.

◆ enableWrites()

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

Enable writes to the buffer.

Returns
the previous state of writes.

Definition at line 438 of file Buffer.cc.

Referenced by Ps2Controller::initialise3().

+ Here is the caller graph for this function:

◆ endOperation()

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

Definition at line 93 of file Buffer.cc.

◆ getDataSize()

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

Get the number of bytes in the buffer now.

Definition at line 470 of file Buffer.cc.

◆ getSize()

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

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

Definition at line 481 of file Buffer.cc.

◆ monitor() [1/2]

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

Add a Semaphore to be signaled when data changes.

Definition at line 607 of file Buffer.cc.

◆ monitor() [2/2]

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 594 of file Buffer.cc.

◆ notifyMonitors()

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 659 of file Buffer.cc.

◆ notifyMonitorsLocked()

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

Sends and retires monitor targets while m_Lock is held.

Definition at line 672 of file Buffer.cc.

◆ read()

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

◆ readableGeneration()

template<class T , bool allowShortOperation>
uint64_t Buffer< T, allowShortOperation >::readableGeneration ( ) const

Rising-edge sequences updated atomically with the protected predicates.

Definition at line 559 of file Buffer.cc.

Referenced by UnixSocket::readinessGenerations().

+ Here is the caller graph for this function:

◆ tryWrite()

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

Writes the complete input without waiting for lock ownership or space. Returns false without changing the buffer when the whole input cannot be accepted immediately. An empty input is a successful no-op.

Definition at line 275 of file Buffer.cc.

◆ wipe()

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

Wipes the buffer.

Definition at line 569 of file Buffer.cc.

Referenced by Ps2Controller::initialise3().

+ Here is the caller graph for this function:

◆ writableGeneration()

template<class T , bool allowShortOperation>
uint64_t Buffer< T, allowShortOperation >::writableGeneration ( ) const

Definition at line 564 of file Buffer.cc.

◆ write()

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 103 of file Buffer.cc.

Referenced by ConsoleFile::inputLineDiscipline(), and TextIO::writeStr().

+ Here is the caller graph for this function:

◆ writeAtomic()

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

Writes the complete input as one indivisible operation. If the input fits in the buffer but there is not enough room yet, a blocking caller waits before publishing any of it.

Definition at line 227 of file Buffer.cc.

◆ writeAvailable()

template<class T , bool allowShortOperation>
size_t Buffer< T, allowShortOperation >::writeAvailable ( const T *  buffer,
size_t  count,
bool  atomic = false 
)

Waits for buffer lock ownership, then writes up to the currently available capacity without waiting for a reader. Returns zero if either side closed. When atomic is true, insufficient capacity leaves the buffer unchanged.

Definition at line 201 of file Buffer.cc.

Referenced by ConsoleFile::inputLineDiscipline(), and Ps2Controller::irq().

+ Here is the caller graph for this function:

◆ writeLocked()

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

Appends up to count values while m_Lock is held.

Definition at line 679 of file Buffer.cc.

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

Friends And Related Symbol Documentation

◆ UnixSocket

template<class T , bool allowShortOperation = false>
friend class UnixSocket
friend

Definition at line 54 of file Buffer.h.

Member Data Documentation

◆ m_ActiveOperations

template<class T , bool allowShortOperation = false>
size_t Buffer< T, allowShortOperation >::m_ActiveOperations
private

Definition at line 269 of file Buffer.h.

◆ m_bCanRead

template<class T , bool allowShortOperation = false>
bool Buffer< T, allowShortOperation >::m_bCanRead
private

Definition at line 266 of file Buffer.h.

◆ m_bCanWrite

template<class T , bool allowShortOperation = false>
bool Buffer< T, allowShortOperation >::m_bCanWrite
private

Definition at line 267 of file Buffer.h.

◆ m_bClosing

template<class T , bool allowShortOperation = false>
bool Buffer< T, allowShortOperation >::m_bClosing
private

Definition at line 268 of file Buffer.h.

◆ m_BufferSize

template<class T , bool allowShortOperation = false>
size_t Buffer< T, allowShortOperation >::m_BufferSize
private

Definition at line 252 of file Buffer.h.

◆ m_DataSize

template<class T , bool allowShortOperation = false>
size_t Buffer< T, allowShortOperation >::m_DataSize
private

Definition at line 253 of file Buffer.h.

◆ m_DrainCondition

template<class T , bool allowShortOperation = false>
ConditionVariable Buffer< T, allowShortOperation >::m_DrainCondition
private

Definition at line 261 of file Buffer.h.

◆ m_Lock

template<class T , bool allowShortOperation = false>
Mutex Buffer< T, allowShortOperation >::m_Lock
private

Definition at line 257 of file Buffer.h.

◆ m_Monitors

template<class T , bool allowShortOperation = false>
BufferMonitors Buffer< T, allowShortOperation >::m_Monitors
private

Definition at line 264 of file Buffer.h.

◆ m_ReadableGeneration

template<class T , bool allowShortOperation = false>
Atomic<uint64_t> Buffer< T, allowShortOperation >::m_ReadableGeneration
private

Definition at line 254 of file Buffer.h.

◆ m_ReadCondition

template<class T , bool allowShortOperation = false>
ConditionVariable Buffer< T, allowShortOperation >::m_ReadCondition
private

Definition at line 260 of file Buffer.h.

◆ m_Segments

template<class T , bool allowShortOperation = false>
List<Segment*> Buffer< T, allowShortOperation >::m_Segments
private

Definition at line 263 of file Buffer.h.

◆ m_SegmentSize

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 233 of file Buffer.h.

◆ m_WritableGeneration

template<class T , bool allowShortOperation = false>
Atomic<uint64_t> Buffer< T, allowShortOperation >::m_WritableGeneration
private

Definition at line 255 of file Buffer.h.

◆ m_WriteCondition

template<class T , bool allowShortOperation = false>
ConditionVariable Buffer< T, allowShortOperation >::m_WriteCondition
private

Definition at line 259 of file Buffer.h.


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