Dali 3D User Interface Engine
List of all members | Public Member Functions | Private Types | Private Member Functions | Private Attributes
Dali::Integration::LocklessBuffer Class Reference

The LocklessBuffer class implements double buffering eligible for multi-(two) threaded use, where it's possible to read from one thread and write from another without requiring a mutex lock to avoid performance hit. More...

#include <lockless-buffer.h>

Public Member Functions

 LocklessBuffer (size_t size)
 Constructor. More...
 
 ~LocklessBuffer ()
 Destructor. More...
 
void Write (const unsigned char *src, size_t size)
 Write data to buffer. More...
 
const unsigned char * Read ()
 Try to swap buffers and read data. More...
 
unsigned int GetSize () const
 

Private Types

enum  BufferState {
  R0W1 = 0, R1W0 = 1, WRITING = 2, UPDATED = 4,
  WRITE_BUFFER_MASK = 1, WRITING_MASK = 2, UPDATED_MASK = 4
}
 Atomically set state. More...
 

Private Member Functions

 LocklessBuffer ()
 undefined default constructor, need to give size on construction More...
 
 LocklessBuffer (const LocklessBuffer &)
 undefined copy constructor More...
 
LocklessBufferoperator= (const LocklessBuffer &)
 undefined assignment operator More...
 

Private Attributes

unsigned char * mBuffer [2]
 bitmap buffers More...
 
BufferState volatile mState
 readbuffer number and whether we're currently writing into writebuffer or not More...
 
size_t mSize
 size of buffers More...
 

Detailed Description

The LocklessBuffer class implements double buffering eligible for multi-(two) threaded use, where it's possible to read from one thread and write from another without requiring a mutex lock to avoid performance hit.

It's intended to be used for reading bitmap data in render thread while still possible to write data in another thread.

Ideally Write() and Read() calls should be alternating, otherwise written data might be thrown away.

The buffers are swapped in the reading thread, just before reading begins. In case the other thread is writing at that moment, buffers are not swapped and previously available data is read. Similarly if Write() is called before a Read() has finished the previous write buffer is overwritten.

Definition at line 47 of file lockless-buffer.h.

Member Enumeration Documentation

Atomically set state.

We're always writing to one buffer and reading from the other. Write() sets WRITING bit when started and unsets it when finished.

Enumerator
R0W1 

Read from buffer 0 write to buffer 1.

R1W0 

Read from buffer 1 write to buffer 0.

WRITING 

Currently writing to buffer.

UPDATED 

Swapping buffer required; there is new data available.

WRITE_BUFFER_MASK 

indicates which buffer to write to

WRITING_MASK 

indicates whether currently writing

UPDATED_MASK 

indicates whether new data is available

Definition at line 87 of file lockless-buffer.h.

Constructor & Destructor Documentation

Dali::Integration::LocklessBuffer::LocklessBuffer ( size_t  size)

Constructor.

Parameters
[in]sizeThe size of buffers in bytes.

Definition at line 30 of file lockless-buffer.cpp.

References mBuffer.

Dali::Integration::LocklessBuffer::~LocklessBuffer ( )

Destructor.

Definition at line 42 of file lockless-buffer.cpp.

References mBuffer.

Dali::Integration::LocklessBuffer::LocklessBuffer ( )
private

undefined default constructor, need to give size on construction

Dali::Integration::LocklessBuffer::LocklessBuffer ( const LocklessBuffer )
private

undefined copy constructor

Member Function Documentation

unsigned int Dali::Integration::LocklessBuffer::GetSize ( ) const
Returns
the buffer size in bytes

Definition at line 93 of file lockless-buffer.cpp.

References mSize.

Referenced by UtcDaliLocklessBufferGetSize01().

Here is the caller graph for this function:

LocklessBuffer& Dali::Integration::LocklessBuffer::operator= ( const LocklessBuffer )
private

undefined assignment operator

const unsigned char * Dali::Integration::LocklessBuffer::Read ( )

Try to swap buffers and read data.

Note
returned value only valid until Read() is called again or object is destroyed
Returns
current read buffer contents

Definition at line 69 of file lockless-buffer.cpp.

References mBuffer, mState, UPDATED, UPDATED_MASK, and WRITE_BUFFER_MASK.

Referenced by Dali::Internal::Adaptor::NativeBitmapBuffer::PrepareTexture(), and anonymous_namespace{utc-Dali-LocklessBuffer.cpp}::ReadTest().

Here is the caller graph for this function:

void Dali::Integration::LocklessBuffer::Write ( const unsigned char *  src,
size_t  size 
)

Write data to buffer.

Parameters
[in]srcdata source
[in]sizesize of data in bytes

Definition at line 48 of file lockless-buffer.cpp.

References DALI_ASSERT_ALWAYS, DALI_ASSERT_DEBUG, mBuffer, mSize, mState, UPDATED, WRITE_BUFFER_MASK, WRITING, and WRITING_MASK.

Referenced by UtcDaliLocklessBufferMultipleWrites01(), UtcDaliLocklessBufferWriteRead01(), and Dali::Internal::Adaptor::NativeBitmapBuffer::Write().

Here is the caller graph for this function:

Member Data Documentation

unsigned char* Dali::Integration::LocklessBuffer::mBuffer[2]
private

bitmap buffers

Definition at line 104 of file lockless-buffer.h.

Referenced by LocklessBuffer(), Read(), Write(), and ~LocklessBuffer().

size_t Dali::Integration::LocklessBuffer::mSize
private

size of buffers

Definition at line 106 of file lockless-buffer.h.

Referenced by GetSize(), and Write().

BufferState volatile Dali::Integration::LocklessBuffer::mState
private

readbuffer number and whether we're currently writing into writebuffer or not

Definition at line 105 of file lockless-buffer.h.

Referenced by Read(), and Write().


The documentation for this class was generated from the following files:
Dali Docs Home
Read more about Dali