MatLogger2  1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
XBot::VariableBuffer Class Reference

The VariableBuffer class implements a memory buffer for a single logged variable. More...

#include <var_buffer.h>

+ Collaboration diagram for XBot::VariableBuffer:

Classes

struct  BufferInfo
 
class  QueueImpl
 The QueueImpl class implements the buffering strategy for a single logged variable. More...
 

Public Types

enum  Mode {
  Mode::producer_consumer,
  Mode::circular_buffer
}
 Enum for specifying the type of buffer. More...
 
typedef std::function< void(BufferInfo)> CallbackType
 

Public Member Functions

 VariableBuffer (std::string name, int dim_rows, int dim_cols, int block_size)
 Constructor. More...
 
void set_on_block_available (CallbackType callback)
 Sets a callback that is used to notify that a new block has been pushed into the queue. More...
 
void set_buffer_mode (VariableBuffer::Mode mode)
 Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer. More...
 
const std::string & get_name () const
 
std::pair< int, int > get_dimension () const
 
template<typename Derived >
bool add_elem (const Eigen::MatrixBase< Derived > &data)
 Add an element to the buffer. More...
 
bool read_block (Eigen::MatrixXd &data, int &valid_elements)
 Reads a whole block from the queue, if one is available. More...
 
bool flush_to_queue ()
 Writes current block to the queue. More...
 
 ~VariableBuffer ()
 

Static Public Member Functions

static int NumBlocks ()
 

Detailed Description

The VariableBuffer class implements a memory buffer for a single logged variable.

This is an internal library component, and it is not meant for direct use.

The memory buffer is splitted into a fixed number of blocks, that make up a "pool" of available memory. When a block is full, it is pushed into a lockfree queue, so that it is available for the consumer thread. As soon as the block is consumed, it is returned back to the pool via another lockfree queue.

Apart from the lockfree queues, no other data is shared between add_elem() and read_block(). So, they can be called concurrently without further synchronization.

Because the lockfree queue is of the Single-Producer-Single-Consumer type, a single thread is allowed to call add_elem and read_block, concurrently.

Definition at line 32 of file var_buffer.h.

Member Typedef Documentation

typedef std::function<void(BufferInfo)> XBot::VariableBuffer::CallbackType

Definition at line 63 of file var_buffer.h.

Member Enumeration Documentation

Enum for specifying the type of buffer.

Enumerator
producer_consumer 
circular_buffer 

Definition at line 40 of file var_buffer.h.

Constructor & Destructor Documentation

VariableBuffer::VariableBuffer ( std::string  name,
int  dim_rows,
int  dim_cols,
int  block_size 
)

Constructor.

Parameters
nameVariable name
dim_rowsSample rows number
dim_colsSample columns number
block_sizeNumber of samples that make up a block

Definition at line 136 of file var_buffer.cpp.

VariableBuffer::~VariableBuffer ( )

Definition at line 272 of file var_buffer.cpp.

Member Function Documentation

template<typename Derived >
bool XBot::VariableBuffer::add_elem ( const Eigen::MatrixBase< Derived > &  data)
inline

Add an element to the buffer.

If there is no space inside the current block, this is pushed into the queue by calling flush_to_queue().

Only a single producer thread is allowed to concurrently call this method.

Parameters
DerivedData type
dataData value
Returns
False if data could not be saved inside the buffer

Definition at line 260 of file var_buffer.h.

bool VariableBuffer::flush_to_queue ( )

Writes current block to the queue.

If a callback was registered through set_on_block_available(), it is called on success.

Returns
True on success (queue was not full)

Definition at line 196 of file var_buffer.cpp.

std::pair< int, int > VariableBuffer::get_dimension ( ) const

Definition at line 150 of file var_buffer.cpp.

const std::string & VariableBuffer::get_name ( ) const

Definition at line 42 of file matlogger2.cpp.

int VariableBuffer::NumBlocks ( )
static

Definition at line 261 of file var_buffer.cpp.

bool XBot::VariableBuffer::read_block ( Eigen::MatrixXd &  data,
int &  valid_elements 
)

Reads a whole block from the queue, if one is available.

The block is then returned to the pool.

Only a single consumer thread is allowed to concurrently call this method.

Parameters
dataMatrix which is filled with the read block (unless the function returns false)
valid_elementsNumber of valid elements contained in the block. This means that only data.leftCols(valid_elements) contains valid data.
Returns
True if valid_elements > 0

Definition at line 165 of file var_buffer.cpp.

void XBot::VariableBuffer::set_buffer_mode ( VariableBuffer::Mode  mode)

Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer.

By default, the producer_consumer mode is used.

NOTE: only call this method before starting using the logger!!

Definition at line 266 of file var_buffer.cpp.

void VariableBuffer::set_on_block_available ( CallbackType  callback)

Sets a callback that is used to notify that a new block has been pushed into the queue.

Definition at line 155 of file var_buffer.cpp.


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