The Gaudi Framework  master (37c0b60a)
LockedHandle.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_LOCKEDHANDLE
12 #define GAUDIKERNEL_LOCKEDHANDLE 1
13 
14 #include <mutex>
15 
16 // =======================================================================
37 template <class T, class MutexType = std::mutex>
38 class LockedHandle {
39 public:
40  LockedHandle( T* ptr, MutexType& mut ) : m_ptr( ptr ), m_mutex( &mut ) {}
41  LockedHandle( T* ptr, MutexType* mut ) : m_ptr( ptr ), m_mutex( mut ) {}
42  LockedHandle( T* ptr ) : m_ptr( ptr ), m_mutex( 0 ) {}
44 
45  void set( T* ptr, MutexType* mut ) {
46  m_ptr = ptr;
47  m_mutex = mut;
48  }
49 
50  void setMutex( MutexType* mut ) { m_mutex = mut; }
51 
52  T* get() const { return m_ptr; }
53 
54  class Guard {
55  Guard( const Guard& a ) = delete;
56 
57  public:
58  Guard( Guard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
59 
60  Guard( T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
61  ~Guard() { m_mutex->unlock(); }
62  T& operator*() { return *m_ptr; }
63  T* operator->() { return m_ptr; }
64 
65  operator T&() { return *m_ptr; }
66 
67  private:
68  T* m_ptr{ nullptr };
69  MutexType* m_mutex{ nullptr };
70  };
71 
72  class ConstGuard {
73  ConstGuard( const ConstGuard& a ) = delete;
74 
75  public:
76  ConstGuard( ConstGuard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
77 
78  ConstGuard( const T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
79  ~ConstGuard() { m_mutex->unlock(); }
80  const T& operator*() const { return *m_ptr; }
81  const T* operator->() const { return m_ptr; }
82 
83  operator const T&() const { return *m_ptr; }
84 
85  private:
86  const T* m_ptr;
87  MutexType* m_mutex;
88  };
89 
91  Guard operator*() { return Guard( m_ptr, *m_mutex ); }
92 
94  Guard operator->() { return Guard( m_ptr, *m_mutex ); }
95 
97  ConstGuard operator*() const { return ConstGuard( m_ptr, *m_mutex ); }
98 
100  ConstGuard operator->() const { return ConstGuard( m_ptr, *m_mutex ); }
101 
102  operator bool() const { return m_ptr; }
103 
104 private:
105  T* m_ptr;
106  mutable MutexType* m_mutex;
107 };
108 
109 #endif
LockedHandle::Guard::Guard
Guard(T *ptr, MutexType &mutex)
Definition: LockedHandle.h:60
LockedHandle::Guard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:69
LockedHandle::ConstGuard::operator->
const T * operator->() const
Definition: LockedHandle.h:81
LockedHandle::setMutex
void setMutex(MutexType *mut)
Definition: LockedHandle.h:50
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType &mut)
Definition: LockedHandle.h:40
LockedHandle::ConstGuard::operator*
const T & operator*() const
Definition: LockedHandle.h:80
LockedHandle::Guard::Guard
Guard(Guard &&a)
Definition: LockedHandle.h:58
LockedHandle::set
void set(T *ptr, MutexType *mut)
Definition: LockedHandle.h:45
LockedHandle::get
T * get() const
Definition: LockedHandle.h:52
LockedHandle::LockedHandle
LockedHandle()
Definition: LockedHandle.h:43
LockedHandle::Guard::~Guard
~Guard()
Definition: LockedHandle.h:61
LockedHandle::operator*
Guard operator*()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:91
LockedHandle::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:106
LockedHandle::ConstGuard::m_ptr
const T * m_ptr
Definition: LockedHandle.h:86
LockedHandle::Guard::operator->
T * operator->()
Definition: LockedHandle.h:63
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:87
LockedHandle::LockedHandle
LockedHandle(T *ptr)
Definition: LockedHandle.h:42
LockedHandle::Guard
Definition: LockedHandle.h:54
LockedHandle::Guard::Guard
Guard(const Guard &a)=delete
LockedHandle::ConstGuard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:87
LockedHandle::m_ptr
T * m_ptr
Definition: LockedHandle.h:105
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const ConstGuard &a)=delete
LockedHandle::operator*
ConstGuard operator*() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:97
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const T *ptr, MutexType &mutex)
Definition: LockedHandle.h:78
LockedHandle::ConstGuard::ConstGuard
ConstGuard(ConstGuard &&a)
Definition: LockedHandle.h:76
LockedHandle::operator->
Guard operator->()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:94
LockedHandle::Guard::operator*
T & operator*()
Definition: LockedHandle.h:62
LockedHandle::Guard::m_ptr
T * m_ptr
Definition: LockedHandle.h:68
LockedHandle::ConstGuard::~ConstGuard
~ConstGuard()
Definition: LockedHandle.h:79
LockedHandle::operator->
ConstGuard operator->() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:100
LockedHandle
Definition: LockedHandle.h:38
LockedHandle::ConstGuard
Definition: LockedHandle.h:72
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType *mut)
Definition: LockedHandle.h:41