00001 /*************************************************************************** 00002 tag: Peter Soetens Mon Jan 19 14:11:26 CET 2004 DataObjectLockFree.hpp 00003 00004 DataObjectLockFree.hpp - description 00005 ------------------- 00006 begin : Mon January 19 2004 00007 copyright : (C) 2004 Peter Soetens 00008 email : peter.soetens@mech.kuleuven.ac.be 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU General Public * 00013 * License as published by the Free Software Foundation; * 00014 * version 2 of the License. * 00015 * * 00016 * As a special exception, you may use this file as part of a free * 00017 * software library without restriction. Specifically, if other files * 00018 * instantiate templates or use macros or inline functions from this * 00019 * file, or you compile this file and link it with other files to * 00020 * produce an executable, this file does not by itself cause the * 00021 * resulting executable to be covered by the GNU General Public * 00022 * License. This exception does not however invalidate any other * 00023 * reasons why the executable file might be covered by the GNU General * 00024 * Public License. * 00025 * * 00026 * This library is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00029 * Lesser General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public * 00032 * License along with this library; if not, write to the Free Software * 00033 * Foundation, Inc., 59 Temple Place, * 00034 * Suite 330, Boston, MA 02111-1307 USA * 00035 * * 00036 ***************************************************************************/ 00037 00038 #pragma once 00039 00040 #include <youbot_driver/generic/dataobjectlockfree/target.hpp> 00041 #include <youbot_driver/generic/dataobjectlockfree/os/oro_arch.h> 00042 00043 namespace youbot 00044 { 00045 00074 template<class T> 00075 class DataObjectLockFree 00076 { 00077 public: 00081 typedef T DataType; 00082 00088 const unsigned int MAX_THREADS; // = 2 00089 private: 00093 const unsigned int BUF_LEN; // = MAX_THREADS+2 00094 00102 struct DataBuf 00103 { 00104 DataBuf() : 00105 data(), counter(), next() 00106 { 00107 oro_atomic_set(&counter, 0); 00108 } 00109 DataType data; 00110 mutable oro_atomic_t counter; 00111 DataBuf* next; 00112 }; 00113 00114 typedef DataBuf* volatile VolPtrType; 00115 typedef DataBuf ValueType; 00116 typedef DataBuf* PtrType; 00117 00118 VolPtrType read_ptr; 00119 VolPtrType write_ptr; 00120 00124 DataBuf* data; 00125 public: 00126 00133 DataObjectLockFree(const T& initial_value = T(), unsigned int max_threads = 2) : 00134 MAX_THREADS(max_threads), BUF_LEN(max_threads + 2), read_ptr(0), write_ptr(0) 00135 { 00136 data = new DataBuf[BUF_LEN]; 00137 read_ptr = &data[0]; 00138 write_ptr = &data[1]; 00139 data_sample(initial_value); 00140 } 00141 00142 ~DataObjectLockFree() 00143 { 00144 delete[] data; 00145 } 00146 00154 virtual DataType Get() const 00155 { 00156 DataType cache; 00157 Get(cache); 00158 return cache; 00159 } 00160 00168 virtual void Get(DataType& pull) const 00169 { 00170 PtrType reading; 00171 // loop to combine Read/Modify of counter 00172 // This avoids a race condition where read_ptr 00173 // could become write_ptr ( then we would read corrupted data). 00174 do 00175 { 00176 reading = read_ptr; // copy buffer location 00177 oro_atomic_inc(&reading->counter); // lock buffer, no more writes 00178 // XXX smp_mb 00179 if (reading != read_ptr) // if read_ptr changed, 00180 oro_atomic_dec(&reading->counter); // better to start over. 00181 else 00182 break; 00183 } while (true); 00184 // from here on we are sure that 'reading' 00185 // is a valid buffer to read from. 00186 pull = reading->data; // takes some time 00187 // XXX smp_mb 00188 oro_atomic_dec(&reading->counter); // release buffer 00189 } 00190 00196 virtual void Set(const DataType& push) 00197 { 00206 // writeout in any case 00207 write_ptr->data = push; 00208 PtrType wrote_ptr = write_ptr; 00209 // if next field is occupied (by read_ptr or counter), 00210 // go to next and check again... 00211 while (oro_atomic_read( &write_ptr->next->counter ) != 0 || write_ptr->next == read_ptr) 00212 { 00213 write_ptr = write_ptr->next; 00214 if (write_ptr == wrote_ptr) 00215 return; // nothing found, to many readers ! 00216 } 00217 00218 // we will be able to move, so replace read_ptr 00219 read_ptr = wrote_ptr; 00220 write_ptr = write_ptr->next; // we checked this in the while loop 00221 } 00222 00223 virtual void data_sample(const DataType& sample) 00224 { 00225 // prepare the buffer. 00226 for (unsigned int i = 0; i < BUF_LEN - 1; ++i) 00227 { 00228 data[i].data = sample; 00229 data[i].next = &data[i + 1]; 00230 } 00231 data[BUF_LEN - 1].data = sample; 00232 data[BUF_LEN - 1].next = &data[0]; 00233 } 00234 }; 00235 } 00236