Orocos Real-Time Toolkit  2.8.3
Public Types | Public Member Functions | Public Attributes | List of all members
RTT::base::DataObjectLockFree< T > Class Template Reference

This DataObject is a Lock-Free implementation, such that reads and writes can happen concurrently without priority inversions. More...

#include <rtt/base/DataObjectLockFree.hpp>

Inheritance diagram for RTT::base::DataObjectLockFree< T >:
RTT::base::DataObjectInterface< T > RTT::base::DataObject< T >

Public Types

typedef T DataType
 The type of the data. More...
 
typedef boost::shared_ptr< DataObjectInterface< T > > shared_ptr
 Used for shared_ptr management. More...
 

Public Member Functions

 DataObjectLockFree (const T &initial_value=T(), unsigned int max_threads=2)
 Construct a DataObjectLockFree by name. More...
 
 ~DataObjectLockFree ()
 
virtual DataType Get () const
 Get a copy of the data. More...
 
virtual void Get (DataType &pull) const
 Get a copy of the Data (non allocating). More...
 
virtual void Set (const DataType &push)
 Set the data to a certain value (non blocking). More...
 
virtual void data_sample (const DataType &sample)
 Provides a data sample to initialize this data object. More...
 

Public Attributes

const unsigned int MAX_THREADS
 The maximum number of threads. More...
 

Detailed Description

template<class T>
class RTT::base::DataObjectLockFree< T >

This DataObject is a Lock-Free implementation, such that reads and writes can happen concurrently without priority inversions.

When there are more writes than reads, the last write will be returned. The internal buffer can get full if too many concurrent reads are taking to long. In that case, each new read will read the element the previous read returned.

* The following Truth table applies when a Low Priority thread is
* preempted by a High Priority thread :
*
*   L\H | Set | Get |
*   Set | Ok  | Ok  |
*   Get | Ok  | Ok  |
*
* legend : L : Low Priority thread
*          H : High Priority thread
*          Blk: Blocks High Priority thread (bad!)
*          internal::NA : Not allowed !
* 

Further, multiple reads may occur before, during and after a write operation simultaneously. The buffer needs readers+2*writers elements to be guaranteed non blocking.

Definition at line 77 of file DataObjectLockFree.hpp.

Member Typedef Documentation

template<class T >
typedef T RTT::base::DataObjectLockFree< T >::DataType

The type of the data.

Definition at line 84 of file DataObjectLockFree.hpp.

template<class T>
typedef boost::shared_ptr<DataObjectInterface<T> > RTT::base::DataObjectInterface< T >::shared_ptr
inherited

Used for shared_ptr management.

Definition at line 62 of file DataObjectInterface.hpp.

Constructor & Destructor Documentation

template<class T >
RTT::base::DataObjectLockFree< T >::DataObjectLockFree ( const T &  initial_value = T(),
unsigned int  max_threads = 2 
)
inline

Construct a DataObjectLockFree by name.

Parameters
_nameThe name of this DataObject.
initial_valueThe initial value of this DataObject.

Definition at line 133 of file DataObjectLockFree.hpp.

References RTT::base::DataObjectLockFree< T >::data_sample().

template<class T >
RTT::base::DataObjectLockFree< T >::~DataObjectLockFree ( )
inline

Definition at line 144 of file DataObjectLockFree.hpp.

Member Function Documentation

template<class T >
virtual void RTT::base::DataObjectLockFree< T >::data_sample ( const DataType sample)
inlinevirtual

Provides a data sample to initialize this data object.

As such enough storage space can be allocated before the actual writing begins.

Parameters
sample

Implements RTT::base::DataObjectInterface< T >.

Definition at line 218 of file DataObjectLockFree.hpp.

Referenced by RTT::base::DataObjectLockFree< T >::DataObjectLockFree().

template<class T >
virtual DataType RTT::base::DataObjectLockFree< T >::Get ( ) const
inlinevirtual

Get a copy of the data.

This method will allocate memory twice if data is not a value type. Use Get(DataType&) for the non-allocating version.

Returns
A copy of the data.

Implements RTT::base::DataObjectInterface< T >.

Definition at line 155 of file DataObjectLockFree.hpp.

References RTT::base::DataObjectLockFree< T >::Get().

Referenced by RTT::base::DataObjectLockFree< T >::Get().

template<class T >
virtual void RTT::base::DataObjectLockFree< T >::Get ( DataType pull) const
inlinevirtual

Get a copy of the Data (non allocating).

If pull has reserved enough memory to store the copy, no memory will be allocated.

Parameters
pullA copy of the data.

Implements RTT::base::DataObjectInterface< T >.

Definition at line 164 of file DataObjectLockFree.hpp.

References oro_atomic_dec(), and oro_atomic_inc().

template<class T >
virtual void RTT::base::DataObjectLockFree< T >::Set ( const DataType push)
inlinevirtual

Set the data to a certain value (non blocking).

Parameters
pushThe data which must be set.

This method can not be called concurrently (only one producer). With a minimum of 3 buffers, if the write_ptr+1 field is not occupied, it will remain so because the read_ptr is at write_ptr-1 (and can not increment the counter on write_ptr+1). Hence, no locking is needed.

Implements RTT::base::DataObjectInterface< T >.

Definition at line 191 of file DataObjectLockFree.hpp.

References oro_atomic_read().

Member Data Documentation

template<class T >
const unsigned int RTT::base::DataObjectLockFree< T >::MAX_THREADS

The maximum number of threads.

When used in data flow, this is always 2.

Definition at line 91 of file DataObjectLockFree.hpp.


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