| The Gaudi Framework v32r1 (f65d50dc)

LockedHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_LOCKEDHANDLE
2 #define GAUDIKERNEL_LOCKEDHANDLE 1
3
4 #include <mutex>
5
6 // =======================================================================
27 template <class T, class MutexType = std::mutex>
28 class LockedHandle {
29 public:
30 LockedHandle( T* ptr, MutexType& mut ) : m_ptr( ptr ), m_mutex( &mut ) {}
31 LockedHandle( T* ptr, MutexType* mut ) : m_ptr( ptr ), m_mutex( mut ) {}
32 LockedHandle( T* ptr ) : m_ptr( ptr ), m_mutex( 0 ) {}
33 LockedHandle() : m_ptr( nullptr ), m_mutex( nullptr ) {}
34
35 void set( T* ptr, MutexType* mut ) {
36 m_ptr = ptr;
37 m_mutex = mut;
38 }
39
40 void setMutex( MutexType* mut ) { m_mutex = mut; }
41
42 T* get() const { return m_ptr; }
43
44 class Guard {
45 Guard( const Guard& a ) = delete;
46
47 public:
48 Guard( Guard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
49
50 Guard( T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
51 ~Guard() { m_mutex->unlock(); }
52 T& operator*() { return *m_ptr; }
53 T* operator->() { return m_ptr; }
54
55 operator T&() { return *m_ptr; }
56
57 private:
58 T* m_ptr{nullptr};
59 MutexType* m_mutex{nullptr};
60 };
61
62 class ConstGuard {
63 ConstGuard( const ConstGuard& a ) = delete;
64
65 public:
66 ConstGuard( ConstGuard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
67
68 ConstGuard( const T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
69 ~ConstGuard() { m_mutex->unlock(); }
70 const T& operator*() const { return *m_ptr; }
71 const T* operator->() const { return m_ptr; }
72
73 operator const T&() const { return *m_ptr; }
74
75 private:
76 const T* m_ptr;
77 MutexType* m_mutex;
78 };
79
81 Guard operator*() { return Guard( m_ptr, *m_mutex ); }
82
84 Guard operator->() { return Guard( m_ptr, *m_mutex ); }
85
87 ConstGuard operator*() const { return ConstGuard( m_ptr, *m_mutex ); }
88
90 ConstGuard operator->() const { return ConstGuard( m_ptr, *m_mutex ); }
91
92 operator bool() const { return m_ptr; }
93
94 private:
95 T* m_ptr;
96 mutable MutexType* m_mutex;
97 };
98
99 #endif
LockedHandle::operator->
Guard operator->()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:84
LockedHandle::ConstGuard::~ConstGuard
~ConstGuard()
Definition: LockedHandle.h:69
LockedHandle::ConstGuard::m_ptr
const T * m_ptr
Definition: LockedHandle.h:76
LockedHandle::ConstGuard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:77
LockedHandle::Guard::~Guard
~Guard()
Definition: LockedHandle.h:51
LockedHandle::m_ptr
T * m_ptr
Definition: LockedHandle.h:95
LockedHandle::Guard::Guard
Guard(const Guard &a)=delete
LockedHandle::Guard::Guard
Guard(T *ptr, MutexType &mutex)
Definition: LockedHandle.h:50
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const T *ptr, MutexType &mutex)
Definition: LockedHandle.h:68
LockedHandle::LockedHandle
LockedHandle(T *ptr)
Definition: LockedHandle.h:32
LockedHandle::Guard::operator->
T * operator->()
Definition: LockedHandle.h:53
LockedHandle::Guard::operator *
T & operator *()
Definition: LockedHandle.h:52
LockedHandle::get
T * get() const
Definition: LockedHandle.h:42
LockedHandle::Guard::m_ptr
T * m_ptr
Definition: LockedHandle.h:58
LockedHandle::ConstGuard::operator *
const T & operator *() const
Definition: LockedHandle.h:70
LockedHandle::ConstGuard::operator->
const T * operator->() const
Definition: LockedHandle.h:71
LockedHandle::Guard
Definition: LockedHandle.h:44
LockedHandle::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:96
LockedHandle::set
void set(T *ptr, MutexType *mut)
Definition: LockedHandle.h:35
LockedHandle
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const ConstGuard &a)=delete
LockedHandle::Guard::Guard
Guard(Guard &&a)
Definition: LockedHandle.h:48
LockedHandle::Guard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:59
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType *mut)
Definition: LockedHandle.h:31
LockedHandle::ConstGuard
Definition: LockedHandle.h:62
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType &mut)
Definition: LockedHandle.h:30
LockedHandle::ConstGuard::ConstGuard
ConstGuard(ConstGuard &&a)
Definition: LockedHandle.h:66
LockedHandle::operator *
Guard operator *()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:81
LockedHandle::LockedHandle
LockedHandle()
Definition: LockedHandle.h:33
LockedHandle::operator->
ConstGuard operator->() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:90
LockedHandle::setMutex
void setMutex(MutexType *mut)
Definition: LockedHandle.h:40