18 #ifndef LUNCHBOX_SCOPEDMUTEX_H
19 #define LUNCHBOX_SCOPEDMUTEX_H
21 #include <lunchbox/condition.h>
22 #include <lunchbox/lock.h>
23 #include <lunchbox/lockable.h>
32 template<
class L,
class T >
struct ScopedMutexLocker {};
33 template<
class L >
struct ScopedMutexLocker< L, WriteOp >
35 static inline void set( L& lock ) { lock.set(); }
36 static inline void unset( L& lock ) { lock.unset(); }
38 template<
class L >
struct ScopedMutexLocker< L, ReadOp >
40 static inline void set( L& lock ) { lock.setRead(); }
41 static inline void unset( L& lock ) { lock.unsetRead(); }
43 template<>
struct ScopedMutexLocker< Condition, WriteOp >
45 static inline void set( Condition& cond ) { cond.lock(); }
46 static inline void unset( Condition& cond ) { cond.unlock(); }
57 template<
class L = Lock,
class T = WriteOp >
60 typedef ScopedMutexLocker< L, T > LockTraits;
73 {
if( lock ) LockTraits::set( *lock ); }
77 { LockTraits::set( lock ); }
98 template<
typename LB >
explicit ScopedMutex(
const LB& lockable )
99 : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); }
105 void leave() {
if( _lock ) LockTraits::unset( *_lock ); _lock = 0; }
127 #endif //LUNCHBOX_SCOPEDMUTEX_H