GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/mutex.h
Date: 2025-07-13 02:35:07
Exec Total Coverage
Lines: 24 24 100.0%
Branches: 0 0 -%

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 769331895 inline explicit RAII(T &object) : ref_(object) { Enter(); }
45 129598705 inline explicit RAII(T *object) : ref_(*object) { Enter(); }
46 914269186 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 800657457 inline void RAII<pthread_mutex_t>::Enter() {
77 800657457 pthread_mutex_lock(&ref_);
78 827319358 }
79 template<>
80 822327289 inline void RAII<pthread_mutex_t>::Leave() {
81 822327289 pthread_mutex_unlock(&ref_);
82 819803706 }
83 typedef RAII<pthread_mutex_t> MutexLockGuard;
84
85
86 template<>
87 23892184 inline void RAII<pthread_rwlock_t, RAII_Polymorphism::ReadLock>::Enter() {
88 23892184 pthread_rwlock_rdlock(&ref_);
89 23906066 }
90 template<>
91 23899797 inline void RAII<pthread_rwlock_t, RAII_Polymorphism::ReadLock>::Leave() {
92 23899797 pthread_rwlock_unlock(&ref_);
93 23906146 }
94 template<>
95 21224760 inline void RAII<pthread_rwlock_t, RAII_Polymorphism::WriteLock>::Enter() {
96 21224760 pthread_rwlock_wrlock(&ref_);
97 21225862 }
98 template<>
99 21224152 inline void RAII<pthread_rwlock_t, RAII_Polymorphism::WriteLock>::Leave() {
100 21224152 pthread_rwlock_unlock(&ref_);
101 21225938 }
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