Equalizer 1.0
|
00001 00002 /* Copyright (c) 2010, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 00004 * This library is free software; you can redistribute it and/or modify it under 00005 * the terms of the GNU Lesser General Public License version 2.1 as published 00006 * by the Free Software Foundation. 00007 * 00008 * This library is distributed in the hope that it will be useful, but WITHOUT 00009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00010 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00011 * details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this library; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00016 */ 00017 00018 #ifndef COBASE_LFQUEUE_H 00019 #define COBASE_LFQUEUE_H 00020 00021 #include <co/base/atomic.h> // member 00022 #include <co/base/debug.h> // used in inline method 00023 #include <co/base/nonCopyable.h> // base class 00024 #include <co/base/thread.h> // thread-safety checks 00025 00026 #include <vector> 00027 00028 namespace co 00029 { 00030 namespace base 00031 { 00046 template< typename T > class LFQueue : public NonCopyable 00047 { 00048 public: 00050 LFQueue( const int32_t size ) 00051 : _data( size + 1 ), _readPos( 0 ), _writePos( 0 ) {} 00052 00054 ~LFQueue() {} 00055 00057 bool isEmpty() const { return _readPos == _writePos; } 00058 00060 void clear() 00061 { 00062 EQ_TS_SCOPED( _reader ); 00063 _readPos = 0; 00064 _writePos = 0; 00065 } 00066 00073 void resize( const int32_t size ) 00074 { 00075 EQASSERT( isEmpty( )); 00076 _readPos = 0; 00077 _writePos = 0; 00078 _data.resize( size + 1 ); 00079 } 00080 00089 bool pop( T& result ) 00090 { 00091 EQ_TS_SCOPED( _reader ); 00092 if( _readPos == _writePos ) 00093 return false; 00094 00095 result = _data[ _readPos ]; 00096 _readPos = (_readPos + 1) % _data.size(); 00097 return true; 00098 } 00099 00108 bool getFront( T& result ) 00109 { 00110 EQ_TS_SCOPED( _reader ); 00111 if( _readPos == _writePos ) 00112 return false; 00113 00114 result = _data[ _readPos ]; 00115 return true; 00116 } 00117 00125 bool push( const T& element ) 00126 { 00127 EQ_TS_SCOPED( _writer ); 00128 int32_t nextPos = (_writePos + 1) % _data.size(); 00129 if( nextPos == _readPos ) 00130 return false; 00131 00132 _data[ _writePos ] = element; 00133 _writePos = nextPos; 00134 return true; 00135 } 00136 00141 size_t getCapacity() const { return _data.size() - 1; } 00142 00143 private: 00144 std::vector< T > _data; 00145 a_int32_t _readPos; 00146 a_int32_t _writePos; 00147 00148 EQ_TS_VAR( _reader ); 00149 EQ_TS_VAR( _writer ); 00150 }; 00151 00152 } 00153 } 00154 #endif // COBASE_LFQUEUE_H