Lunchbox  1.11.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scopedMutex.h
1 
2 /* Copyright (c) 2006-2013, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_SCOPEDMUTEX_H
19 #define LUNCHBOX_SCOPEDMUTEX_H
20 
21 #include <lunchbox/condition.h> // used in inline method
22 #include <lunchbox/lock.h> // used in inline method
23 #include <lunchbox/lockable.h> // used in inline method
24 #include <lunchbox/types.h>
25 
26 namespace lunchbox
27 {
28 class WriteOp;
29 class ReadOp;
30 
32 template< class L, class T > struct ScopedMutexLocker {};
33 template< class L > struct ScopedMutexLocker< L, WriteOp >
34 {
35  static inline void set( L& lock ) { lock.set(); }
36  static inline void unset( L& lock ) { lock.unset(); }
37 };
38 template< class L > struct ScopedMutexLocker< L, ReadOp >
39 {
40  static inline void set( L& lock ) { lock.setRead(); }
41  static inline void unset( L& lock ) { lock.unsetRead(); }
42 };
43 template<> struct ScopedMutexLocker< Condition, WriteOp >
44 {
45  static inline void set( Condition& cond ) { cond.lock(); }
46  static inline void unset( Condition& cond ) { cond.unlock(); }
47 };
58 template< class L = Lock, class T = WriteOp >
60 {
61  typedef ScopedMutexLocker< L, T > LockTraits;
62 
63 public:
73  explicit ScopedMutex( L* lock ) : _lock( lock )
74  { if( lock ) LockTraits::set( *lock ); }
75 
77  explicit ScopedMutex( L& lock ) : _lock( &lock )
78  { LockTraits::set( lock ); }
79 
81  ScopedMutex( const ScopedMutex& rhs ) : _lock( rhs._lock )
82  { const_cast< ScopedMutex& >( rhs )._lock = 0; }
83 
86  {
87  if( this != &rhs )
88  {
89  _lock = rhs._lock;
90  rhs._lock = 0;
91  }
92  return *this;
93  }
94 
99  template< typename LB > explicit ScopedMutex( const LB& lockable )
100  : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); }
101 
104 
106  void leave() { if( _lock ) LockTraits::unset( *_lock ); _lock = 0; }
107 
108 private:
109  ScopedMutex();
110  L* _lock;
111 }; // LB_DEPRECATED;
112 
115 
118 
121 
124 
127 }
128 #endif //LUNCHBOX_SCOPEDMUTEX_H
ScopedMutex< SpinLock, WriteOp > ScopedFastWrite
A scoped mutex for a fast uncontended write operation.
Definition: scopedMutex.h:117
Basic type definitions not provided by the operating system.
ScopedMutex< Condition, WriteOp > ScopedCondition
A scoped mutex for a write operation on a condition.
Definition: scopedMutex.h:126
ScopedMutex(const LB &lockable)
Construct a new scoped mutex for the given Lockable data structure.
Definition: scopedMutex.h:99
ScopedMutex(const ScopedMutex &rhs)
Move lock from rhs to new mutex.
Definition: scopedMutex.h:81
ScopedMutex & operator=(ScopedMutex &rhs)
Move lock from rhs to this mutex.
Definition: scopedMutex.h:85
void leave()
Leave and unlock the mutex immediately.
Definition: scopedMutex.h:106
ScopedMutex< Lock, WriteOp > ScopedWrite
A scoped mutex for a write operation.
Definition: scopedMutex.h:123
A scoped mutex.
Definition: scopedMutex.h:59
ScopedMutex(L &lock)
Construct a new scoped mutex and set the given lock.
Definition: scopedMutex.h:77
ScopedMutex< Lock, ReadOp > ScopedRead
A scoped mutex for a read operation.
Definition: scopedMutex.h:120
ScopedMutex(L *lock)
Construct a new scoped mutex and set the given lock.
Definition: scopedMutex.h:73
~ScopedMutex()
Destruct the scoped mutex and unset the mutex.
Definition: scopedMutex.h:103
ScopedMutex< SpinLock, ReadOp > ScopedFastRead
A scoped mutex for a fast uncontended read operation.
Definition: scopedMutex.h:114