The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
LockedHandle.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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#pragma once
12
13#include <mutex>
14
34
35template <class T, class MutexType = std::mutex>
37public:
38 LockedHandle( T* ptr, MutexType& mut ) : m_ptr( ptr ), m_mutex( &mut ) {}
39 LockedHandle( T* ptr, MutexType* mut ) : m_ptr( ptr ), m_mutex( mut ) {}
40 LockedHandle( T* ptr ) : m_ptr( ptr ), m_mutex( 0 ) {}
41 LockedHandle() : m_ptr( nullptr ), m_mutex( nullptr ) {}
42
43 void set( T* ptr, MutexType* mut ) {
44 m_ptr = ptr;
45 m_mutex = mut;
46 }
47
48 void setMutex( MutexType* mut ) { m_mutex = mut; }
49
50 T* get() const { return m_ptr; }
51
52 class Guard {
53 Guard( const Guard& a ) = delete;
54
55 public:
56 Guard( Guard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
57
58 Guard( T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
59 ~Guard() { m_mutex->unlock(); }
60 T& operator*() { return *m_ptr; }
61 T* operator->() { return m_ptr; }
62
63 operator T&() { return *m_ptr; }
64
65 private:
66 T* m_ptr{ nullptr };
67 MutexType* m_mutex{ nullptr };
68 };
69
70 class ConstGuard {
71 ConstGuard( const ConstGuard& a ) = delete;
72
73 public:
74 ConstGuard( ConstGuard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
75
76 ConstGuard( const T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
77 ~ConstGuard() { m_mutex->unlock(); }
78 const T& operator*() const { return *m_ptr; }
79 const T* operator->() const { return m_ptr; }
80
81 operator const T&() const { return *m_ptr; }
82
83 private:
84 const T* m_ptr;
85 MutexType* m_mutex;
86 };
87
89 Guard operator*() { return Guard( m_ptr, *m_mutex ); }
90
92 Guard operator->() { return Guard( m_ptr, *m_mutex ); }
93
95 ConstGuard operator*() const { return ConstGuard( m_ptr, *m_mutex ); }
96
98 ConstGuard operator->() const { return ConstGuard( m_ptr, *m_mutex ); }
99
100 operator bool() const { return m_ptr; }
101
102private:
104 mutable MutexType* m_mutex;
105};
ConstGuard(ConstGuard &&a)
const T * operator->() const
ConstGuard(const ConstGuard &a)=delete
const T & operator*() const
ConstGuard(const T *ptr, MutexType &mutex)
MutexType * m_mutex
Guard(T *ptr, MutexType &mutex)
Guard(const Guard &a)=delete
LockedHandle(T *ptr, MutexType &mut)
T * get() const
ConstGuard operator->() const
Aquire and release the lock before and after the const object is accessed.
ConstGuard operator*() const
Aquire and release the lock before and after the const object is accessed.
LockedHandle(T *ptr)
void set(T *ptr, MutexType *mut)
LockedHandle(T *ptr, MutexType *mut)
MutexType * m_mutex
Guard operator->()
Aquire and release the lock before and after the object is accessed.
void setMutex(MutexType *mut)
Guard operator*()
Aquire and release the lock before and after the object is accessed.