| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 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 |
|
|
|
| 16 |
|
|
/** |
| 17 |
|
|
* Used to allow for static polymorphism in the RAII template to statically |
| 18 |
|
|
* decide which 'lock' functions to use, if we have more than one possibility. |
| 19 |
|
|
* (I.e. Read/Write locks) |
| 20 |
|
|
* Note: Static Polymorphism - Strategy Pattern |
| 21 |
|
|
* |
| 22 |
|
|
* TODO(jblomer): eventually replace this by C++11 typed enum |
| 23 |
|
|
*/ |
| 24 |
|
|
struct RAII_Polymorphism { |
| 25 |
|
|
enum T { |
| 26 |
|
|
None, |
| 27 |
|
|
ReadLock, |
| 28 |
|
|
WriteLock |
| 29 |
|
|
}; |
| 30 |
|
|
}; |
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
/** |
| 34 |
|
|
* Basic template wrapper class for any kind of RAII-like behavior. |
| 35 |
|
|
* The user is supposed to provide a template specialization of Enter() and |
| 36 |
|
|
* Leave(). On creation of the RAII object it will call Enter() respectively |
| 37 |
|
|
* Leave() on destruction. The gold standard example is a LockGard (see below). |
| 38 |
|
|
* |
| 39 |
|
|
* Note: Resource Acquisition Is Initialization (Bjarne Stroustrup) |
| 40 |
|
|
*/ |
| 41 |
|
|
template<typename T, RAII_Polymorphism::T P = RAII_Polymorphism::None> |
| 42 |
|
|
class RAII : SingleCopy { |
| 43 |
|
|
public: |
| 44 |
|
2657797014 |
inline explicit RAII(T &object) : ref_(object) { Enter(); } |
| 45 |
|
263886867 |
inline explicit RAII(T *object) : ref_(*object) { Enter(); } |
| 46 |
|
2976808965 |
inline ~RAII() { Leave(); } |
| 47 |
|
|
|
| 48 |
|
|
protected: |
| 49 |
|
184 |
inline void Enter() { ref_.Lock(); } |
| 50 |
|
92 |
inline void Leave() { ref_.Unlock(); } |
| 51 |
|
|
|
| 52 |
|
|
private: |
| 53 |
|
|
T &ref_; |
| 54 |
|
|
}; |
| 55 |
|
|
|
| 56 |
|
|
|
| 57 |
|
|
/** |
| 58 |
|
|
* This is a simple scoped lock implementation. Every object that provides the |
| 59 |
|
|
* methods Lock() and Unlock() should work with it. Classes that will be used |
| 60 |
|
|
* with this template should therefore simply inherit from Lockable. |
| 61 |
|
|
* |
| 62 |
|
|
* Creating a LockGuard object on the stack will lock the provided object. When |
| 63 |
|
|
* the LockGuard runs out of scope it will automatically release the lock. This |
| 64 |
|
|
* ensures a clean unlock in a lot of situations! |
| 65 |
|
|
* |
| 66 |
|
|
* TODO(jblomer): C++11 replace this by a type alias to RAII |
| 67 |
|
|
*/ |
| 68 |
|
|
template<typename LockableT> |
| 69 |
|
|
class LockGuard : public RAII<LockableT> { |
| 70 |
|
|
public: |
| 71 |
|
184 |
inline explicit LockGuard(LockableT *object) : RAII<LockableT>(object) { } |
| 72 |
|
|
}; |
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
|
|
template<> |
| 76 |
|
2817606567 |
inline void RAII<pthread_mutex_t>::Enter() { |
| 77 |
|
2817606567 |
pthread_mutex_lock(&ref_); |
| 78 |
|
2932589606 |
} |
| 79 |
|
|
template<> |
| 80 |
|
2902485724 |
inline void RAII<pthread_mutex_t>::Leave() { |
| 81 |
|
2902485724 |
pthread_mutex_unlock(&ref_); |
| 82 |
|
2901875102 |
} |
| 83 |
|
|
typedef RAII<pthread_mutex_t> MutexLockGuard; |
| 84 |
|
|
|
| 85 |
|
|
|
| 86 |
|
|
template<> |
| 87 |
|
23364593 |
inline void RAII<pthread_rwlock_t, RAII_Polymorphism::ReadLock>::Enter() { |
| 88 |
|
23364593 |
pthread_rwlock_rdlock(&ref_); |
| 89 |
|
23374979 |
} |
| 90 |
|
|
template<> |
| 91 |
|
23365995 |
inline void RAII<pthread_rwlock_t, RAII_Polymorphism::ReadLock>::Leave() { |
| 92 |
|
23365995 |
pthread_rwlock_unlock(&ref_); |
| 93 |
|
23374909 |
} |
| 94 |
|
|
template<> |
| 95 |
|
8910803 |
inline void RAII<pthread_rwlock_t, RAII_Polymorphism::WriteLock>::Enter() { |
| 96 |
|
8910803 |
pthread_rwlock_wrlock(&ref_); |
| 97 |
|
8912093 |
} |
| 98 |
|
|
template<> |
| 99 |
|
8910488 |
inline void RAII<pthread_rwlock_t, RAII_Polymorphism::WriteLock>::Leave() { |
| 100 |
|
8910488 |
pthread_rwlock_unlock(&ref_); |
| 101 |
|
8912228 |
} |
| 102 |
|
|
typedef RAII<pthread_rwlock_t, RAII_Polymorphism::ReadLock> ReadLockGuard; |
| 103 |
|
|
typedef RAII<pthread_rwlock_t, RAII_Polymorphism::WriteLock> WriteLockGuard; |
| 104 |
|
|
|
| 105 |
|
|
#ifdef CVMFS_NAMESPACE_GUARD |
| 106 |
|
|
} // namespace CVMFS_NAMESPACE_GUARD |
| 107 |
|
|
#endif |
| 108 |
|
|
|
| 109 |
|
|
#endif // CVMFS_UTIL_MUTEX_H_ |
| 110 |
|
|
|