ATLAS Offline Software
Loading...
Searching...
No Matches
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
9namespace 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
109
110 } // namespace Internal
111
112} // namespace xAOD
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
std::shared_timed_mutex m_typeMapMutex
Mutex for the type map.
static THolderCache & instance()
Singleton accessor.
int getRef(void *ptr) const
Get the reference count of an object in memory.
void addClass(const std::type_info &ti, ::TClass *cl)
Add the dictionary for a given type.
std::pair< bool, ::TClass * > getClass(const std::type_info &ti) const
Get the dictionary for a given type info.
std::shared_timed_mutex m_refMapMutex
Mutex for the reference count map.
THolderCache()
Hide the constructor of the class from the outside.
std::map< const std::type_info *, TClass * > m_typeMap
The type map.
int decRef(void *ptr)
Decrease the reference count of an object in memory.
std::map< void *, int > m_refMap
The reference count map.
int incRef(void *ptr)
Increment the reference count of an object in memory.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
std::shared_lock< std::shared_timed_mutex > shared_lock_t
Helper type definition.
std::unique_lock< std::shared_timed_mutex > unique_lock_t
Helper type definition.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.