Lunchbox  1.6.0
scopedMutex.h
00001 
00002 /* Copyright (c) 2006-2012, 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 LUNCHBOX_SCOPEDMUTEX_H
00019 #define LUNCHBOX_SCOPEDMUTEX_H
00020 
00021 #include <lunchbox/condition.h>   // used in inline method
00022 #include <lunchbox/lock.h>        // used in inline method
00023 #include <lunchbox/lockable.h>    // used in inline method
00024 #include <lunchbox/types.h>
00025 
00026 namespace lunchbox
00027 {
00028     class WriteOp;
00029     class ReadOp;
00030 
00032     template< class L, class T > struct ScopedMutexLocker {};
00033     template< class L > struct ScopedMutexLocker< L, WriteOp >
00034     {
00035         static inline void set( L& lock ) { lock.set(); }
00036         static inline void unset( L& lock ) { lock.unset(); }
00037     };
00038     template< class L > struct ScopedMutexLocker< L, ReadOp >
00039     {
00040         static inline void set( L& lock ) { lock.setRead(); }
00041         static inline void unset( L& lock ) { lock.unsetRead(); }
00042     };
00043     template<> struct ScopedMutexLocker< Condition, WriteOp >
00044     {
00045         static inline void set( Condition& cond ) { cond.lock(); }
00046         static inline void unset( Condition& cond ) { cond.unlock(); }
00047     };
00057     template< class L = Lock, class T = WriteOp >
00058     class ScopedMutex
00059     {
00060         typedef ScopedMutexLocker< L, T > LockTraits;
00061 
00062     public:
00072         explicit ScopedMutex( L* lock ) : _lock( lock )
00073             { if( lock ) LockTraits::set( *lock ); }
00074 
00076         explicit ScopedMutex( L& lock ) : _lock( &lock )
00077             { LockTraits::set( lock ); }
00078 
00080         ScopedMutex( const ScopedMutex& rhs ) : _lock( rhs._lock )
00081             { const_cast< ScopedMutex& >( rhs )._lock = 0; }
00082 
00084         ScopedMutex& operator = ( ScopedMutex& rhs )
00085             {
00086                 if( this != &rhs )
00087                 {
00088                     _lock = rhs._lock;
00089                     rhs._lock = 0;
00090                 }
00091                 return *this;
00092             }
00093 
00098         template< typename LB > explicit ScopedMutex( const LB& lockable )
00099                 : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); }
00100 
00102         ~ScopedMutex() { leave(); }
00103 
00105         void leave() { if( _lock ) LockTraits::unset( *_lock ); _lock = 0; }
00106 
00107     private:
00108         ScopedMutex();
00109         L* _lock;
00110     };
00111 
00113     typedef ScopedMutex< SpinLock, ReadOp > ScopedFastRead;
00114 
00116     typedef ScopedMutex< SpinLock, WriteOp > ScopedFastWrite;
00117 
00119     typedef ScopedMutex< Lock, ReadOp > ScopedRead;
00120 
00122     typedef ScopedMutex< Lock, WriteOp > ScopedWrite;
00123 
00125     typedef ScopedMutex< Condition, WriteOp > ScopedCondition;
00126 }
00127 #endif //LUNCHBOX_SCOPEDMUTEX_H