ATLAS Offline Software
THolderCache.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 
3 // Local include(s):
4 #include "THolderCache.h"
5 #include <mutex>
6 
8 
9 namespace xAOD {
10 
11  namespace Internal {
12 
13  // These mutex and lock types were chosen to provide good performance for
14  // reading the "type map" and "reference map" variables very often, while
15  // only writing to them sparingly.
16  //
17  // This is especially true for the "type map", which is not modified in
18  // a typical job after the first event. The "reference map" is a bit
19  // different, that does get modified throughout the whole job. It just
20  // seemed easier to use the same mutex/lock types for both variable. But
21  // The mutex/lock for the "reference map" could be re-visited if
22  // performance tests show contention for it.
23 
25  typedef std::shared_lock< std::shared_timed_mutex > shared_lock_t;
27  typedef std::unique_lock< std::shared_timed_mutex > unique_lock_t;
28 
30  static THolderCache cache ATLAS_THREAD_SAFE; // this class is thread-safe
31  return cache;
32  }
33 
34  std::pair< bool, ::TClass* >
35  THolderCache::getClass( const std::type_info& ti ) const {
36 
37  // Get a "read lock":
39 
40  // Look for the type in the cache:
41  auto itr = m_typeMap.find( &ti );
42  if( itr != m_typeMap.end() ) {
43  return std::pair< bool, ::TClass* >( true, itr->second );
44  } else {
45  return std::pair< bool, ::TClass* >( false, nullptr );
46  }
47  }
48 
49  void THolderCache::addClass( const std::type_info& ti, ::TClass* cl ) {
50 
51  // Get a "write lock":
53 
54  // Extend the map:
55  m_typeMap[ &ti ] = cl;
56  }
57 
58  int THolderCache::getRef( void* ptr ) const {
59 
60  // Get a "read lock":
62 
63  // Get the reference count:
64  auto itr = m_refMap.find( ptr );
65  if( itr != m_refMap.end() ) {
66  return itr->second;
67  } else {
68  return 0;
69  }
70  }
71 
72  int THolderCache::incRef( void* ptr ) {
73 
74  // Get a "write lock":
76 
77  // Increment the reference count, and return the new value:
78  return ++( m_refMap[ ptr ] );
79  }
80 
81  int THolderCache::decRef( void* ptr ) {
82 
83  // Get a "write lock":
85 
86  // Check if we know about this pointer:
87  auto itr = m_refMap.find( ptr );
88  if( itr == m_refMap.end() ) {
89  return 0;
90  }
91 
92  // Decrease the reference count, and remember the value:
93  const int count = --( m_refMap[ ptr ] );
94 
95  // If the reference count went down to zero, let's forget about this
96  // pointer:
97  if( count == 0 ) {
98  m_refMap.erase( itr );
99  }
100 
101  // Return the new count:
102  return count;
103  }
104 
106  : m_typeMap(), m_refMap(), m_typeMapMutex(), m_refMapMutex() {
107 
108  }
109 
110  } // namespace Internal
111 
112 } // namespace xAOD
xAOD::Internal::THolderCache::getRef
int getRef(void *ptr) const
Get the reference count of an object in memory.
Definition: THolderCache.cxx:58
xAOD::Internal::THolderCache::m_refMap
std::map< void *, int > m_refMap
The reference count map.
Definition: THolderCache.h:59
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
xAOD::Internal::THolderCache::addClass
void addClass(const std::type_info &ti, ::TClass *cl)
Add the dictionary for a given type.
Definition: THolderCache.cxx:49
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
xAOD::Internal::THolderCache::getClass
std::pair< bool, ::TClass * > getClass(const std::type_info &ti) const
Get the dictionary for a given type info.
Definition: THolderCache.cxx:35
xAOD::Internal::THolderCache
Singleton, thread-safe THolder cache.
Definition: THolderCache.h:31
xAOD::Internal::THolderCache::m_typeMap
std::map< const std::type_info *, TClass * > m_typeMap
The type map.
Definition: THolderCache.h:57
xAOD::Internal::THolderCache::m_typeMapMutex
std::shared_timed_mutex m_typeMapMutex
Mutex for the type map.
Definition: THolderCache.h:62
xAOD::Internal::THolderCache::instance
static THolderCache & instance()
Singleton accessor.
Definition: THolderCache.cxx:29
xAOD::Internal::THolderCache::incRef
int incRef(void *ptr)
Increment the reference count of an object in memory.
Definition: THolderCache.cxx:72
xAOD::Internal::THolderCache::THolderCache
THolderCache()
Hide the constructor of the class from the outside.
Definition: THolderCache.cxx:105
xAOD::Internal::unique_lock_t
std::unique_lock< std::shared_timed_mutex > unique_lock_t
Helper type definition.
Definition: THolderCache.cxx:27
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
checker_macros.h
Define macros for attributes used to control the static checker.
THolderCache.h
xAOD::Internal::THolderCache::decRef
int decRef(void *ptr)
Decrease the reference count of an object in memory.
Definition: THolderCache.cxx:81
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:26
xAOD::Internal::THolderCache::m_refMapMutex
std::shared_timed_mutex m_refMapMutex
Mutex for the reference count map.
Definition: THolderCache.h:64
xAOD::Internal::shared_lock_t
std::shared_lock< std::shared_timed_mutex > shared_lock_t
Helper type definition.
Definition: THolderCache.cxx:25