Equalizer 1.0

mtQueue.h

00001 
00002 /* Copyright (c) 2005-2011, 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_MTQUEUE_H
00019 #define COBASE_MTQUEUE_H
00020 
00021 #include <co/base/condition.h>
00022 #include <co/base/debug.h>
00023 
00024 #include <queue>
00025 #include <string.h>
00026 
00027 namespace co
00028 {
00029 namespace base
00030 {
00036     template< typename T > class MTQueue
00037     {
00038     public:
00040         MTQueue() {}
00041 
00043         MTQueue( const MTQueue< T >& from ) : _queue( from._queue ) {}
00044 
00046         ~MTQueue() {}
00047 
00049         MTQueue< T >& operator = ( const MTQueue< T >& from )
00050             {
00051                 _cond.lock();
00052                 _queue = from._queue;
00053                 _cond.signal();
00054                 _cond.unlock();
00055                 return *this;
00056             }
00057 
00059         bool isEmpty() const { return _queue.empty(); }
00060 
00062         size_t getSize() const { return _queue.size(); }
00063 
00070         size_t waitSize( const size_t minSize ) const
00071             {
00072                 _cond.lock();
00073                 while( _queue.size() < minSize )
00074                     _cond.wait();
00075                 const size_t size = _queue.size();
00076                 _cond.unlock();
00077                 return size;
00078             }
00079 
00081         void clear()
00082             {
00083                 _cond.lock();
00084                 _queue.clear();
00085                 _cond.unlock();
00086             }
00087 
00092         T pop()
00093             {
00094                 _cond.lock();
00095                 while( _queue.empty( ))
00096                     _cond.wait();
00097                 
00098                 EQASSERT( !_queue.empty( ));
00099                 T element = _queue.front();
00100                 _queue.pop_front();
00101                 _cond.unlock();
00102                 return element;
00103             }
00104 
00113         bool tryPop( T& result )
00114             {
00115                 _cond.lock();
00116                 if( _queue.empty( ))
00117                 {
00118                     _cond.unlock();
00119                     return false;
00120                 }
00121 
00122                 result = _queue.front();
00123                 _queue.pop_front();
00124                 _cond.unlock();
00125                 return true;
00126             }   
00127 
00134         bool getFront( T& result ) const
00135             {
00136                 _cond.lock();
00137                 if( _queue.empty( ))
00138                 {
00139                     _cond.unlock();
00140                     return false;
00141                 }
00142                 // else
00143                 result = _queue.front();
00144                 _cond.unlock();
00145                 return true;
00146             }
00147 
00154         bool getBack( T& result ) const
00155             {
00156                 _cond.lock();
00157                 if( _queue.empty( ))
00158                 {
00159                     _cond.unlock();
00160                     return false;
00161                 }
00162                 // else
00163                 result = _queue.back();
00164                 _cond.unlock();
00165                 return true;
00166             }
00167 
00169         void push( const T& element )
00170             {
00171                 _cond.lock();
00172                 _queue.push_back( element );
00173                 _cond.signal();
00174                 _cond.unlock();
00175             }
00176 
00178         void push( const std::vector< T >& elements )
00179             {
00180                 _cond.lock();
00181                 _queue.insert( _queue.end(), elements.begin(), elements.end( ));
00182                 _cond.signal();
00183                 _cond.unlock();
00184             }
00185 
00187         void pushFront( const T& element )
00188             {
00189                 _cond.lock();
00190                 _queue.push_front( element );
00191                 _cond.signal();
00192                 _cond.unlock();
00193             }
00194 
00196         void pushFront( const std::vector< T >& elements )
00197             {
00198                 _cond.lock();
00199                 _queue.insert( _queue.begin(),
00200                                elements.begin(), elements.end( ));
00201                 _cond.signal();
00202                 _cond.unlock();
00203             }
00204 
00205 
00206     private:
00207         std::deque< T > _queue;
00208         mutable Condition _cond;
00209     };
00210 }
00211 }
00212 #endif //COBASE_MTQUEUE_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3