|
Equalizer
1.3.1-git
|
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/lock.h> // used in inline method 00022 #include <lunchbox/lockable.h> // used in inline method 00023 #include <lunchbox/nonCopyable.h> // base class 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 }; 00052 template< class L = Lock, class T = WriteOp > 00053 class ScopedMutex : public NonCopyable 00054 { 00055 typedef ScopedMutexLocker< L, T > LockTraits; 00056 00057 public: 00067 explicit ScopedMutex( L* lock ) : _lock( lock ) 00068 { if( lock ) LockTraits::set( *lock ); } 00069 00071 explicit ScopedMutex( L& lock ) : _lock( &lock ) 00072 { LockTraits::set( lock ); } 00073 00078 template< typename LB > ScopedMutex( LB& lockable ) 00079 : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); } 00080 00082 ~ScopedMutex() { leave(); } 00083 00085 void leave() { if( _lock ) LockTraits::unset( *_lock ); _lock = 0; } 00086 00087 private: 00088 ScopedMutex(); 00089 L* _lock; 00090 }; 00091 00093 typedef ScopedMutex< SpinLock, ReadOp > ScopedFastRead; 00094 00096 typedef ScopedMutex< SpinLock, WriteOp > ScopedFastWrite; 00097 00099 typedef ScopedMutex< Lock, ReadOp > ScopedRead; 00100 00102 typedef ScopedMutex< Lock, WriteOp > ScopedWrite; 00103 } 00104 #endif //LUNCHBOX_SCOPEDMUTEX_H
1.3.1-git by
1.8.0