Equalizer 1.0

monitor.h

00001 
00002 /* Copyright (c) 2006-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_MONITOR_H
00019 #define COBASE_MONITOR_H
00020 
00021 #include <co/base/nonCopyable.h> // base class
00022 #include <co/base/condition.h>   // member
00023 #include <co/base/types.h>
00024 
00025 #include <errno.h>
00026 #include <string.h>
00027 #include <iostream>
00028 #include <typeinfo>
00029 
00030 namespace co
00031 {
00032 namespace base
00033 {
00041     template< typename T > class Monitor : public NonCopyable
00042     {
00043     public:
00045         Monitor() : _value( T( 0 )) {}
00046 
00048         Monitor( const T& value ) : _value( value ) {}
00049         
00051         ~Monitor() {}
00052 
00056         Monitor& operator++ ()
00057             {
00058                 _cond.lock();
00059                 ++_value;
00060                 _cond.broadcast();
00061                 _cond.unlock();
00062                 return *this;
00063             }
00064 
00066         Monitor& operator-- ()
00067             {
00068                 _cond.lock();
00069                 --_value;
00070                 _cond.broadcast();
00071                 _cond.unlock();
00072                 return *this;
00073             }
00074 
00076         Monitor& operator = ( const T& value )
00077             {
00078                 set( value );
00079                 return *this;
00080             }
00081 
00083         Monitor& operator |= ( const T& value )
00084             {
00085                 _cond.lock();
00086                 _value |= value;
00087                 _cond.broadcast();
00088                 _cond.unlock();
00089                 return *this;
00090             }
00091 
00093         void set( const T& value )
00094             {
00095                 _cond.lock();
00096                 _value = value;
00097                 _cond.broadcast();
00098                 _cond.unlock();
00099             }
00101 
00109         const T& waitEQ( const T& value ) const
00110             {
00111                 _cond.lock();
00112                 while( _value != value )
00113                     _cond.wait();
00114                 _cond.unlock();
00115                 return value;
00116             }
00117 
00123         const T& waitNE( const T& value ) const
00124             {
00125                 _cond.lock();
00126                 while( _value == value )
00127                     _cond.wait();
00128                 const T& newValue = _value;
00129                 _cond.unlock();
00130                 return newValue;
00131             }
00132 
00138         const T& waitNE( const T& v1, const T& v2 ) const
00139             {
00140                 _cond.lock();
00141                 while( _value == v1 || _value == v2 )
00142                     _cond.wait();
00143                 const T& newValue = _value;
00144                 _cond.unlock();
00145                 return newValue;
00146             }
00147 
00154         const T& waitGE( const T& value ) const
00155             {
00156                 _cond.lock();
00157                 while( _value < value )
00158                     _cond.wait();
00159                 const T& newValue = _value;
00160                 _cond.unlock();
00161                 return newValue;
00162             }
00163 
00170         const T& waitLE( const T& value ) const
00171             {
00172                 _cond.lock();
00173                 while( _value > value )
00174                     _cond.wait();
00175                 const T& newValue = _value;
00176                 _cond.unlock();
00177                 return newValue;
00178             }
00179 
00181 
00184         bool operator == ( const T& value ) const { return _value == value; }
00185         bool operator != ( const T& value ) const { return _value != value; }
00186         bool operator < ( const T& value ) const { return _value < value; }
00187         bool operator > ( const T& value ) const { return _value > value; }
00188         bool operator <= ( const T& value ) const { return _value <= value; }
00189         bool operator >= ( const T& value ) const { return _value >= value; }
00190 
00191         bool operator == ( const Monitor<T>& rhs ) const
00192             { return _value == rhs._value; }
00193         bool operator != ( const Monitor<T>& rhs ) const
00194             { return _value != rhs._value; }
00195         bool operator < ( const Monitor<T>& rhs ) const
00196             { return _value < rhs._value; }
00197         bool operator > ( const Monitor<T>& rhs ) const
00198             { return _value > rhs._value; }
00199         bool operator <= ( const Monitor<T>& rhs ) const
00200             { return _value <= rhs._value; }
00201         bool operator >= ( const Monitor<T>& rhs ) const
00202             { return _value >= rhs._value; }
00204 
00208         const T& operator->() const { return _value; }
00209 
00211         const T& get() const { return _value; }
00212 
00214         T operator + ( const T& value ) const { return _value + value; }
00215 
00217         T operator | ( const T& value ) const
00218             { return static_cast< T >( _value | value ); }
00219 
00221         T operator & ( const T& value ) const
00222             { return static_cast< T >( _value & value ); }
00224 
00225     private:
00226         T _value;
00227         mutable Condition _cond;
00228     };
00229 
00230     typedef Monitor< bool >     Monitorb; 
00231     typedef Monitor< uint32_t > Monitoru; 
00232 
00234     template< typename T >
00235     inline std::ostream& operator << ( std::ostream& os,
00236                                        const Monitor<T>& monitor )
00237     {
00238         os << "Monitor< " << monitor.get() << " >";
00239         return os;
00240     }
00241 
00242     template<> inline Monitor< bool >& Monitor< bool >::operator++ ()
00243     {
00244         _cond.lock();
00245         assert( !_value );
00246         _value = !_value;
00247         _cond.broadcast();
00248         _cond.unlock();
00249         return *this;
00250     }
00251 
00252     template<> inline Monitor< bool >& Monitor< bool >::operator-- ()
00253     {
00254         _cond.lock();
00255         assert( !_value );
00256         _value = !_value;
00257         _cond.broadcast();
00258         _cond.unlock();
00259         return *this;
00260     }
00261 
00262     template<> 
00263     inline Monitor< bool >& Monitor< bool >::operator |= ( const bool& value )
00264     {
00265         if( value )
00266             _value = value;
00267         return *this;
00268     }
00269 }
00270 }
00271 #endif //COBASE_MONITOR_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3