CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mutex.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_MUTEX_H_
6 #define CVMFS_UTIL_MUTEX_H_
7 
8 #include <pthread.h>
9 
10 #include "util/single_copy.h"
11 
12 #ifdef CVMFS_NAMESPACE_GUARD
13 namespace CVMFS_NAMESPACE_GUARD {
14 #endif
15 
25  enum T {
28  WriteLock
29  };
30 };
31 
32 
41 template <typename T, RAII_Polymorphism::T P = RAII_Polymorphism::None>
42 class RAII : SingleCopy {
43  public:
44  inline explicit RAII(T &object) : ref_(object) { Enter(); }
45  inline explicit RAII(T *object) : ref_(*object) { Enter(); }
46  inline ~RAII() { Leave(); }
47 
48  protected:
49  inline void Enter() { ref_.Lock(); }
50  inline void Leave() { ref_.Unlock(); }
51 
52  private:
53  T &ref_;
54 };
55 
56 
68 template <typename LockableT>
69 class LockGuard : public RAII<LockableT> {
70  public:
71  inline explicit LockGuard(LockableT *object) : RAII<LockableT>(object) {}
72 };
73 
74 
75 template <>
76 inline void RAII<pthread_mutex_t>::Enter() { pthread_mutex_lock(&ref_); }
77 template <>
78 inline void RAII<pthread_mutex_t>::Leave() { pthread_mutex_unlock(&ref_); }
80 
81 
82 template <>
83 inline void RAII<pthread_rwlock_t,
85  pthread_rwlock_rdlock(&ref_);
86 }
87 template <>
88 inline void RAII<pthread_rwlock_t,
90  pthread_rwlock_unlock(&ref_);
91 }
92 template <>
93 inline void RAII<pthread_rwlock_t,
95  pthread_rwlock_wrlock(&ref_);
96 }
97 template <>
98 inline void RAII<pthread_rwlock_t,
100  pthread_rwlock_unlock(&ref_);
101 }
104 
105 #ifdef CVMFS_NAMESPACE_GUARD
106 } // namespace CVMFS_NAMESPACE_GUARD
107 #endif
108 
109 #endif // CVMFS_UTIL_MUTEX_H_
void Leave()
Definition: mutex.h:50
RAII(T &object)
Definition: mutex.h:44
RAII< pthread_rwlock_t, RAII_Polymorphism::ReadLock > ReadLockGuard
Definition: mutex.h:102
T & ref_
Definition: mutex.h:53
RAII(T *object)
Definition: mutex.h:45
LockGuard(LockableT *object)
Definition: mutex.h:71
~RAII()
Definition: mutex.h:46
Definition: mutex.h:42
RAII< pthread_rwlock_t, RAII_Polymorphism::WriteLock > WriteLockGuard
Definition: mutex.h:103
RAII< pthread_mutex_t > MutexLockGuard
Definition: mutex.h:79
void Enter()
Definition: mutex.h:49