38 #ifndef CORELIB_DATAOBJECT_LOCK_FREE_HPP 39 #define CORELIB_DATAOBJECT_LOCK_FREE_HPP 42 #include "../os/oro_arch.h" 96 const unsigned int BUF_LEN;
107 : data(), counter(), next()
111 DataType data;
mutable oro_atomic_t counter; DataBuf* next;
114 typedef DataBuf*
volatile VolPtrType;
115 typedef DataBuf ValueType;
116 typedef DataBuf* PtrType;
119 VolPtrType write_ptr;
134 : MAX_THREADS(max_threads), BUF_LEN( max_threads + 2),
138 data =
new DataBuf[BUF_LEN];
140 write_ptr = &data[1];
155 virtual DataType
Get()
const {DataType cache;
Get(cache);
return cache; }
164 virtual void Get( DataType& pull )
const 174 if ( reading != read_ptr )
181 pull = reading->data;
191 virtual void Set(
const DataType& push )
202 write_ptr->data = push;
203 PtrType wrote_ptr = write_ptr;
206 while (
oro_atomic_read( &write_ptr->next->counter ) != 0 || write_ptr->next == read_ptr )
208 write_ptr = write_ptr->next;
209 if (write_ptr == wrote_ptr)
214 read_ptr = wrote_ptr;
215 write_ptr = write_ptr->next;
220 for (
unsigned int i = 0; i < BUF_LEN-1; ++i) {
221 data[i].data = sample;
222 data[i].next = &data[i+1];
224 data[BUF_LEN-1].data = sample;
225 data[BUF_LEN-1].next = &data[0];
virtual DataType Get() const
Get a copy of the data.
virtual void data_sample(const DataType &sample)
Provides a data sample to initialize this data object.
T DataType
The type of the data.
int oro_atomic_read(oro_atomic_t *a)
Returns the current counter value of the atomic structure a.
virtual void Set(const DataType &push)
Set the data to a certain value (non blocking).
virtual void Get(DataType &pull) const
Get a copy of the Data (non allocating).
void oro_atomic_inc(oro_atomic_t *a)
Increment a atomically.
This DataObject is a Lock-Free implementation, such that reads and writes can happen concurrently wit...
const unsigned int MAX_THREADS
The maximum number of threads.
A DataObjectInterface implements multi-threaded read/write solutions.
void oro_atomic_set(oro_atomic_t *a, int n)
Sets the current counter value of the atomic structure a to n.
DataObjectLockFree(const T &initial_value=T(), unsigned int max_threads=2)
Construct a DataObjectLockFree by name.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
void oro_atomic_dec(oro_atomic_t *a)
Decrement a atomically.
Structure that contains an int for atomic operations.