Orocos Real-Time Toolkit  2.8.3
DataObjectLockFree.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:26 CET 2004 DataObjectLockFree.hpp
3 
4  DataObjectLockFree.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef CORELIB_DATAOBJECT_LOCK_FREE_HPP
39 #define CORELIB_DATAOBJECT_LOCK_FREE_HPP
40 
41 
42 #include "../os/oro_arch.h"
43 #include "DataObjectInterface.hpp"
44 
45 namespace RTT
46 { namespace base {
47 
76  template<class T>
78  : public DataObjectInterface<T>
79  {
80  public:
84  typedef T DataType;
85 
91  const unsigned int MAX_THREADS; // = 2
92  private:
96  const unsigned int BUF_LEN; // = MAX_THREADS+2
97 
105  struct DataBuf {
106  DataBuf()
107  : data(), counter(), next()
108  {
109  oro_atomic_set(&counter, 0);
110  }
111  DataType data; mutable oro_atomic_t counter; DataBuf* next;
112  };
113 
114  typedef DataBuf* volatile VolPtrType;
115  typedef DataBuf ValueType;
116  typedef DataBuf* PtrType;
117 
118  VolPtrType read_ptr;
119  VolPtrType write_ptr;
120 
124  DataBuf* data;
125  public:
126 
133  DataObjectLockFree( const T& initial_value = T(), unsigned int max_threads = 2 )
134  : MAX_THREADS(max_threads), BUF_LEN( max_threads + 2),
135  read_ptr(0),
136  write_ptr(0)
137  {
138  data = new DataBuf[BUF_LEN];
139  read_ptr = &data[0];
140  write_ptr = &data[1];
141  data_sample(initial_value);
142  }
143 
145  delete[] data;
146  }
147 
155  virtual DataType Get() const {DataType cache; Get(cache); return cache; }
156 
164  virtual void Get( DataType& pull ) const
165  {
166  PtrType reading;
167  // loop to combine Read/Modify of counter
168  // This avoids a race condition where read_ptr
169  // could become write_ptr ( then we would read corrupted data).
170  do {
171  reading = read_ptr; // copy buffer location
172  oro_atomic_inc(&reading->counter); // lock buffer, no more writes
173  // XXX smp_mb
174  if ( reading != read_ptr ) // if read_ptr changed,
175  oro_atomic_dec(&reading->counter); // better to start over.
176  else
177  break;
178  } while ( true );
179  // from here on we are sure that 'reading'
180  // is a valid buffer to read from.
181  pull = reading->data; // takes some time
182  // XXX smp_mb
183  oro_atomic_dec(&reading->counter); // release buffer
184  }
185 
191  virtual void Set( const DataType& push )
192  {
201  // writeout in any case
202  write_ptr->data = push;
203  PtrType wrote_ptr = write_ptr;
204  // if next field is occupied (by read_ptr or counter),
205  // go to next and check again...
206  while ( oro_atomic_read( &write_ptr->next->counter ) != 0 || write_ptr->next == read_ptr )
207  {
208  write_ptr = write_ptr->next;
209  if (write_ptr == wrote_ptr)
210  return; // nothing found, to many readers !
211  }
212 
213  // we will be able to move, so replace read_ptr
214  read_ptr = wrote_ptr;
215  write_ptr = write_ptr->next; // we checked this in the while loop
216  }
217 
218  virtual void data_sample( const DataType& sample ) {
219  // prepare the buffer.
220  for (unsigned int i = 0; i < BUF_LEN-1; ++i) {
221  data[i].data = sample;
222  data[i].next = &data[i+1];
223  }
224  data[BUF_LEN-1].data = sample;
225  data[BUF_LEN-1].next = &data[0];
226  }
227  };
228 }}
229 
230 #endif
231 
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.
Definition: Activity.cpp:51
void oro_atomic_dec(oro_atomic_t *a)
Decrement a atomically.
Structure that contains an int for atomic operations.
Definition: oro_arch.h:10