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 #pragma once
39 
42 
43 namespace youbot {
44 
73  template<class T>
75  public:
79  typedef T DataType;
80 
86  const unsigned int MAX_THREADS; // = 2
87  private:
91  const unsigned int BUF_LEN; // = MAX_THREADS+2
92 
100  struct DataBuf {
102  : data(), counter(), next()
103  {
104  oro_atomic_set(&counter, 0);
105  }
106  DataType data; mutable oro_atomic_t counter; DataBuf* next;
107  };
108 
109  typedef DataBuf* volatile VolPtrType;
111  typedef DataBuf* PtrType;
112 
113  VolPtrType read_ptr;
114  VolPtrType write_ptr;
115 
120  public:
121 
128  DataObjectLockFree( const T& initial_value = T(), unsigned int max_threads = 2 )
129  : MAX_THREADS(max_threads), BUF_LEN( max_threads + 2),
130  read_ptr(0),
131  write_ptr(0)
132  {
133  data = new DataBuf[BUF_LEN];
134  read_ptr = &data[0];
135  write_ptr = &data[1];
136  data_sample(initial_value);
137  }
138 
140  delete[] data;
141  }
142 
150  virtual DataType Get() const {DataType cache; Get(cache); return cache; }
151 
159  virtual void Get( DataType& pull ) const
160  {
161  PtrType reading;
162  // loop to combine Read/Modify of counter
163  // This avoids a race condition where read_ptr
164  // could become write_ptr ( then we would read corrupted data).
165  do {
166  reading = read_ptr; // copy buffer location
167  oro_atomic_inc(&reading->counter); // lock buffer, no more writes
168  // XXX smp_mb
169  if ( reading != read_ptr ) // if read_ptr changed,
170  oro_atomic_dec(&reading->counter); // better to start over.
171  else
172  break;
173  } while ( true );
174  // from here on we are sure that 'reading'
175  // is a valid buffer to read from.
176  pull = reading->data; // takes some time
177  // XXX smp_mb
178  oro_atomic_dec(&reading->counter); // release buffer
179  }
180 
186  virtual void Set( const DataType& push )
187  {
196  // writeout in any case
197  write_ptr->data = push;
198  PtrType wrote_ptr = write_ptr;
199  // if next field is occupied (by read_ptr or counter),
200  // go to next and check again...
201  while ( oro_atomic_read( &write_ptr->next->counter ) != 0 || write_ptr->next == read_ptr )
202  {
203  write_ptr = write_ptr->next;
204  if (write_ptr == wrote_ptr)
205  return; // nothing found, to many readers !
206  }
207 
208  // we will be able to move, so replace read_ptr
209  read_ptr = wrote_ptr;
210  write_ptr = write_ptr->next; // we checked this in the while loop
211  }
212 
213  virtual void data_sample( const DataType& sample ) {
214  // prepare the buffer.
215  for (unsigned int i = 0; i < BUF_LEN-1; ++i) {
216  data[i].data = sample;
217  data[i].next = &data[i+1];
218  }
219  data[BUF_LEN-1].data = sample;
220  data[BUF_LEN-1].next = &data[0];
221  }
222  };
223 }
224 
225 
const unsigned int MAX_THREADS
The maximum number of threads.
DataObjectLockFree(const T &initial_value=T(), unsigned int max_threads=2)
#define oro_atomic_inc(v)
#define oro_atomic_set(v, i)
virtual DataType Get() const
virtual void Get(DataType &pull) const
This DataObject is a Lock-Free implementation, such that reads and writes can happen concurrently wit...
#define oro_atomic_dec(v)
virtual void Set(const DataType &push)
virtual void data_sample(const DataType &sample)
#define oro_atomic_read(v)


youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Jun 10 2019 15:46:24