Orocos Real-Time Toolkit  2.6.0
DataObjectLockFree.hpp
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 #ifndef CORELIB_DATAOBJECT_LOCK_FREE_HPP
00039 #define CORELIB_DATAOBJECT_LOCK_FREE_HPP
00040 
00041 
00042 #include "../os/oro_arch.h"
00043 #include "DataObjectInterface.hpp"
00044 
00045 namespace RTT
00046 { namespace base {
00047 
00076     template<class T>
00077     class DataObjectLockFree
00078         : public DataObjectInterface<T>
00079     {
00080     public:
00084         typedef T DataType;
00085 
00091         const unsigned int MAX_THREADS; // = 2
00092     private:
00096         const unsigned int BUF_LEN; // = MAX_THREADS+2
00097 
00105         struct DataBuf {
00106             DataBuf()
00107                 : data(), counter(), next()
00108             {
00109                 oro_atomic_set(&counter, 0);
00110             }
00111             DataType data; mutable oro_atomic_t counter; 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),
00135               read_ptr(0),
00136               write_ptr(0)
00137         {
00138             data = new DataBuf[BUF_LEN];
00139             read_ptr = &data[0];
00140             write_ptr = &data[1];
00141             data_sample(initial_value);
00142         }
00143 
00144         ~DataObjectLockFree() {
00145             delete[] data;
00146         }
00147 
00155         virtual DataType Get() const {DataType cache; Get(cache); return cache; }
00156 
00164         virtual void Get( DataType& pull ) const
00165         {
00166             PtrType reading;
00167             // loop to combine Read/Modify of counter
00168             // This avoids a race condition where read_ptr
00169             // could become write_ptr ( then we would read corrupted data).
00170             do {
00171                 reading = read_ptr;            // copy buffer location
00172                 oro_atomic_inc(&reading->counter); // lock buffer, no more writes
00173                 // XXX smp_mb
00174                 if ( reading != read_ptr )     // if read_ptr changed,
00175                     oro_atomic_dec(&reading->counter); // better to start over.
00176                 else
00177                     break;
00178             } while ( true );
00179             // from here on we are sure that 'reading'
00180             // is a valid buffer to read from.
00181             pull = reading->data;               // takes some time
00182             // XXX smp_mb
00183             oro_atomic_dec(&reading->counter);       // release buffer
00184         }
00185 
00191         virtual void Set( const DataType& push )
00192         {
00201             // writeout in any case
00202             write_ptr->data = push;
00203             PtrType wrote_ptr = write_ptr;
00204             // if next field is occupied (by read_ptr or counter),
00205             // go to next and check again...
00206             while ( oro_atomic_read( &write_ptr->next->counter ) != 0 || write_ptr->next == read_ptr )
00207                 {
00208                     write_ptr = write_ptr->next;
00209                     if (write_ptr == wrote_ptr)
00210                         return; // nothing found, to many readers !
00211                 }
00212 
00213             // we will be able to move, so replace read_ptr
00214             read_ptr  = wrote_ptr;
00215             write_ptr = write_ptr->next; // we checked this in the while loop
00216         }
00217 
00218         virtual void data_sample( const DataType& sample ) {
00219             // prepare the buffer.
00220             for (unsigned int i = 0; i < BUF_LEN-1; ++i) {
00221                 data[i].data = sample;
00222                 data[i].next = &data[i+1];
00223             }
00224             data[BUF_LEN-1].data = sample;
00225             data[BUF_LEN-1].next = &data[0];
00226         }
00227     };
00228 }}
00229 
00230 #endif
00231