Lunchbox
1.4.0
|
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/nonCopyable.h> // base class 00025 #include <lunchbox/types.h> 00026 00027 namespace lunchbox 00028 { 00029 class WriteOp; 00030 class ReadOp; 00031 00033 template< class L, class T > struct ScopedMutexLocker {}; 00034 template< class L > struct ScopedMutexLocker< L, WriteOp > 00035 { 00036 static inline void set( L& lock ) { lock.set(); } 00037 static inline void unset( L& lock ) { lock.unset(); } 00038 }; 00039 template< class L > struct ScopedMutexLocker< L, ReadOp > 00040 { 00041 static inline void set( L& lock ) { lock.setRead(); } 00042 static inline void unset( L& lock ) { lock.unsetRead(); } 00043 }; 00044 template<> struct ScopedMutexLocker< Condition, WriteOp > 00045 { 00046 static inline void set( Condition& cond ) { cond.lock(); } 00047 static inline void unset( Condition& cond ) { cond.unlock(); } 00048 }; 00058 template< class L = Lock, class T = WriteOp > 00059 class ScopedMutex : public NonCopyable 00060 { 00061 typedef ScopedMutexLocker< L, T > LockTraits; 00062 00063 public: 00073 explicit ScopedMutex( L* lock ) : _lock( lock ) 00074 { if( lock ) LockTraits::set( *lock ); } 00075 00077 explicit ScopedMutex( L& lock ) : _lock( &lock ) 00078 { LockTraits::set( lock ); } 00079 00084 template< typename LB > ScopedMutex( LB& lockable ) 00085 : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); } 00086 00088 ~ScopedMutex() { leave(); } 00089 00091 void leave() { if( _lock ) LockTraits::unset( *_lock ); _lock = 0; } 00092 00093 private: 00094 ScopedMutex(); 00095 L* _lock; 00096 }; 00097 00099 typedef ScopedMutex< SpinLock, ReadOp > ScopedFastRead; 00100 00102 typedef ScopedMutex< SpinLock, WriteOp > ScopedFastWrite; 00103 00105 typedef ScopedMutex< Lock, ReadOp > ScopedRead; 00106 00108 typedef ScopedMutex< Lock, WriteOp > ScopedWrite; 00109 00111 typedef ScopedMutex< Condition, WriteOp > ScopedCondition; 00112 } 00113 #endif //LUNCHBOX_SCOPEDMUTEX_H