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(); }
58 template<
class L = Lock,
class T = WriteOp >
61 typedef ScopedMutexLocker< L, T > LockTraits;
74 {
if( lock ) LockTraits::set( *lock ); }
78 { LockTraits::set( lock ); }
99 template<
typename LB >
explicit ScopedMutex(
const LB& lockable )
100 : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); }
106 void leave() {
if( _lock ) LockTraits::unset( *_lock ); _lock = 0; }
128 #endif //LUNCHBOX_SCOPEDMUTEX_H
ScopedMutex< SpinLock, WriteOp > ScopedFastWrite
A scoped mutex for a fast uncontended write operation.
Basic type definitions not provided by the operating system.
ScopedMutex< Condition, WriteOp > ScopedCondition
A scoped mutex for a write operation on a condition.
ScopedMutex(const LB &lockable)
Construct a new scoped mutex for the given Lockable data structure.
ScopedMutex(const ScopedMutex &rhs)
Move lock from rhs to new mutex.
ScopedMutex & operator=(ScopedMutex &rhs)
Move lock from rhs to this mutex.
void leave()
Leave and unlock the mutex immediately.
ScopedMutex< Lock, WriteOp > ScopedWrite
A scoped mutex for a write operation.
ScopedMutex(L &lock)
Construct a new scoped mutex and set the given lock.
ScopedMutex< Lock, ReadOp > ScopedRead
A scoped mutex for a read operation.
ScopedMutex(L *lock)
Construct a new scoped mutex and set the given lock.
~ScopedMutex()
Destruct the scoped mutex and unset the mutex.
ScopedMutex< SpinLock, ReadOp > ScopedFastRead
A scoped mutex for a fast uncontended read operation.