ADTF  3.18.2
cAsyncMemoryBlock

Thread safe smart pointer for raw memory blocks. More...

Public Member Functions

 cAsyncMemoryBlock ()
 Default constructor.
 
virtual ~cAsyncMemoryBlock ()
 Destructor.
 
tResult Alloc (tInt nSize)
 Allocates new memory for the MemoryBlock *. More...
 
tResult Assign (void *pMem, tInt nSize)
 Assigns memory to the MemoryBlock object which has already been allocated. More...
 
tResult Free ()
 Lock MemoryBlock and free memory is allocated and not assigned *. More...
 
tResult ReadLock (void **ppMem, const tBool bWait=tTrue)
 Try to lock memory for read access. More...
 
tResult WriteLock (void **ppMem, const tBool bWait=tTrue)
 Try to lock memory for write access. More...
 
tResult Unlock (void)
 Unlock MemoryBlock. More...
 
tResult Copy (cAsyncMemoryBlock &oSrc)
 Copies the content of the source MemoryBlock. More...
 
tInt Compare (cAsyncMemoryBlock &oSrc)
 Compares size and content with another MemoryBlock. More...
 
tInt GetSize ()
 Get size of MemoryBlock. More...
 
tResult WaitNewData (tInt nTimeout=-1)
 Wait for the NewDataEvent which is set by unlocking a write access. More...
 

Private Attributes

void * m_pMem
 
tBool m_bMemAssigned
 memory pointer
 
tInt m_nSize
 memory assigned not alloced (not to be freed afterwards)
 
tInt m_nReaderCount
 memory size
 
tBool m_bWriteAccess
 count of active readers
 
tInt m_nWriterWaitCount
 write access currently active
 
std::recursive_mutex m_oMutexChangeState
 number of writers which are waiting
 
std::mutex m_mutexLock
 mutex for short locks (like counter increments)
 
std::mutex m_mutexNoReader
 mutex for locking MemoryBlock for other access
 
std::mutex m_mutexNewData
 Mutex for locking the condVariable depending on 'noReader'.
 
std::condition_variable m_CondEventNoReader
 Mutex for locking the conditional variable depending on 'newData'.
 
std::condition_variable m_CondNewData
 condition_variable to signal a waiting writer that all readers finished
 
std::condition_variable m_CondMutexLock
 event to signal new data
 

Detailed Description

Thread safe smart pointer for raw memory blocks.

Definition at line 21 of file asyncmemoryblock.h.

Member Function Documentation

◆ Alloc()

tResult Alloc ( tInt  nSize)

Allocates new memory for the MemoryBlock *.

Parameters
nSize[in] memory size
Return values
ERR_NOERRORno Error
ERR_RESOURCE_IN_USEmemory already allocated or assigned
ERR_MEMORYno memory available

◆ Assign()

tResult Assign ( void *  pMem,
tInt  nSize 
)

Assigns memory to the MemoryBlock object which has already been allocated.

Parameters
pMem[in] memory pointer
nSize[in] memory size
Return values
ERR_NOERRORno Error
ERR_RESOURCE_IN_USEmemory already allocated or assigned
This method is real-time safe.\nSee @ref page_real_time_safe.\n

◆ Compare()

tInt Compare ( cAsyncMemoryBlock oSrc)

Compares size and content with another MemoryBlock.

Parameters
oSrc[in] Source MemoryBlock to be copied
Return values
0equal
<0lower
>0greater

◆ Copy()

tResult Copy ( cAsyncMemoryBlock oSrc)

Copies the content of the source MemoryBlock.

The size of the destination is adjusted to the source.

Parameters
oSrc[in] Source MemoryBlock to be copied
Return values
STATUS_OKno error
ERR_MEMORYmemory error

◆ Free()

tResult Free ( )

Lock MemoryBlock and free memory is allocated and not assigned *.

Return values
STATUS_OKno Error
ERR_MEMORYno memory allocated or assigned

◆ GetSize()

tInt GetSize ( )

Get size of MemoryBlock.

Returns
Size of MemoryBlock
This method is real-time safe.\nSee @ref page_real_time_safe.\n

◆ ReadLock()

tResult ReadLock ( void **  ppMem,
const tBool  bWait = tTrue 
)

Try to lock memory for read access.

Multiple read access is allowed. Wait (send thread to sleep) if MemoryBlock is locked by a write access. If parameter bWait == tFalse this method returns STATUS_ERROR without waiting if no immediate access is possible. Memory pointer of the Block is returned in the parameter ppMem. Calling ReadLock after WriteLock on the same thread will not block. Only calls from different threads will block.

Parameters
bWait[in] tTrue: wait if access is locked
ppMem[out] Memory pointer of this MemoryBlock
Return values
STATUS_OKno Error
ERR_POINTERppMem is nullptr
ERR_UNEXPECTEDno memory is allocated
ERR_RESOURCE_IN_USEif memory is locked and bWait is tFalse

◆ Unlock()

tResult Unlock ( void  )

Unlock MemoryBlock.

If currently write access: allow read and write access. If currently read access: decrement reader counter and allow write access if no more reader active.

Return values
STATUS_OKno Error
ERR_UNEXPECTEDno access active or no memory allocated

◆ WaitNewData()

tResult WaitNewData ( tInt  nTimeout = -1)

Wait for the NewDataEvent which is set by unlocking a write access.

Parameters
nTimeout[in] Timeout, default = -1 (INFINITE)
Return values
STATUS_OKno error
ERR_TIMEOUTwait error / timeout

◆ WriteLock()

tResult WriteLock ( void **  ppMem,
const tBool  bWait = tTrue 
)

Try to lock memory for write access.

Only one writer and no reader is allowed. Wait (send thread to sleep) if other write or read access is active. If parameter bWait == tFalse this method returns STATUS_ERROR without waiting if no immediate access is possible. Memory pointer of the MemoryBlock is returned in the parameter ppMem. Calling WriteLock will not block if it is called repeatedly from the same thread. Only calls from different threads will result in a block.

Parameters
bWait[in] tTrue: wait if access is locked
ppMem[out] Memory pointer of this MemoryBlock
Return values
STATUS_OKno Error
ERR_POINTERppMem is nullptr
ERR_UNEXPECTEDno memory is allocated
ERR_RESOURCE_IN_USEif memory is locked and bWait is tFalse