00001 /*************************************************************************** 00002 tag: Peter Soetens Thu Jan 13 10:24:51 CET 2005 BufferLockFree.hpp 00003 00004 BufferLockFree.hpp - description 00005 ------------------- 00006 begin : Thu January 13 2005 00007 copyright : (C) 2005 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 ORO_BUFFER_LOCK_FREE_HPP 00039 #define ORO_BUFFER_LOCK_FREE_HPP 00040 00041 #include "../os/oro_arch.h" 00042 #include "../os/CAS.hpp" 00043 #include "BufferInterface.hpp" 00044 #include "../internal/AtomicMWSRQueue.hpp" 00045 #include "../internal/TsPool.hpp" 00046 #include <vector> 00047 00048 #ifdef ORO_PRAGMA_INTERFACE 00049 #pragma interface 00050 #endif 00051 00052 namespace RTT 00053 { namespace base { 00054 00055 00056 using os::CAS; 00057 00067 template< class T> 00068 class BufferLockFree 00069 : public BufferInterface<T> 00070 { 00071 public: 00072 typedef typename BufferInterface<T>::reference_t reference_t; 00073 typedef typename BufferInterface<T>::param_t param_t; 00074 typedef typename BufferInterface<T>::size_type size_type; 00075 typedef T value_t; 00076 private: 00077 typedef T Item; 00078 internal::AtomicMWSRQueue<Item*> bufs; 00079 // is mutable because of reference counting. 00080 mutable internal::TsPool<Item> mpool; 00081 public: 00086 BufferLockFree( unsigned int bufsize, const T& initial_value = T()) 00087 : bufs( bufsize ), mpool(bufsize + 1) 00088 { 00089 mpool.data_sample( initial_value ); 00090 } 00091 00092 ~BufferLockFree() { 00093 // free all items still in the buffer. 00094 clear(); 00095 } 00096 00097 virtual void data_sample( const T& sample ) 00098 { 00099 mpool.data_sample(sample); 00100 } 00101 00102 00103 size_type capacity() const 00104 { 00105 return bufs.capacity(); 00106 } 00107 00108 size_type size() const 00109 { 00110 return bufs.size(); 00111 } 00112 00113 bool empty() const 00114 { 00115 return bufs.isEmpty(); 00116 } 00117 00118 bool full() const 00119 { 00120 return bufs.isFull(); 00121 } 00122 00123 void clear() 00124 { 00125 Item* item; 00126 while ( bufs.dequeue(item) ) 00127 mpool.deallocate( item ); 00128 } 00129 00130 bool Push( param_t item) 00131 { 00132 Item* mitem = mpool.allocate(); 00133 if ( mitem == 0 ) // queue full. 00134 return false; 00135 // copy over. 00136 *mitem = item; 00137 if (bufs.enqueue( mitem ) == false ) { 00138 //got memory, but buffer is full 00139 //this can happen, as the memory pool is 00140 //bigger than the buffer 00141 mpool.deallocate( mitem ); 00142 return false; 00143 } 00144 return true; 00145 } 00146 00147 size_type Push(const std::vector<T>& items) 00148 { 00149 int towrite = items.size(); 00150 typename std::vector<T>::const_iterator it; 00151 for( it = items.begin(); it != items.end(); ++it) 00152 if ( this->Push( *it ) == false ) 00153 break; 00154 return towrite - (items.end() - it); 00155 } 00156 00157 00158 bool Pop( reference_t item ) 00159 { 00160 Item* ipop; 00161 if (bufs.dequeue( ipop ) == false ) 00162 return false; 00163 item = *ipop; 00164 if (mpool.deallocate( ipop ) == false ) 00165 assert(false); 00166 return true; 00167 } 00168 00169 size_type Pop(std::vector<T>& items ) 00170 { 00171 Item* ipop; 00172 items.clear(); 00173 while( bufs.dequeue(ipop) ) { 00174 items.push_back( *ipop ); 00175 if (mpool.deallocate(ipop) == false) 00176 assert(false); 00177 } 00178 return items.size(); 00179 } 00180 00181 value_t* PopWithoutRelease() 00182 { 00183 Item* ipop; 00184 if (bufs.dequeue( ipop ) == false ) 00185 return 0; 00186 return ipop; 00187 } 00188 00189 void Release(value_t *item) 00190 { 00191 if (mpool.deallocate( item ) == false ) 00192 assert(false); 00193 } 00194 }; 00195 }} 00196 00197 #endif