ATLAS Offline Software
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Private Types | Private Attributes | List of all members
EventContainers::IdentifiableCache< T > Class Template Reference

#include <IdentifiableCache.h>

Inheritance diagram for EventContainers::IdentifiableCache< T >:
Collaboration diagram for EventContainers::IdentifiableCache< T >:

Classes

class  Maker
 

Public Types

typedef std::true_type thread_safe
 

Public Member Functions

 IdentifiableCache (IdentifierHash maxHash, const Maker *maker)
 
 IdentifiableCache (IdentifierHash maxHash, const Maker *maker, size_t lockBucketSize)
 
 ~IdentifiableCache ()
 
const T * find (IdentifierHash hash)
 
const T * findWait (IdentifierHash hash)
 
const T * get (IdentifierHash hash)
 
std::pair< bool, const void * > add (IdentifierHash hash, const T *p)
 
std::pair< bool, const void * > add (IdentifierHash hash, std::unique_ptr< T > p)
 
void clearCache ()
 
std::vector< IdentifierHashids ()
 In a threaded situation this collection will be valid but will not container hashes later added. More...
 
std::pair< bool, const void * > add (IdentifierHash hash, const void *p) noexcept
 
std::pair< bool, const void * > add (IdentifierHash hash, void_unique_ptr p) noexcept
 
std::pair< bool, const void * > addLock (IdentifierHash hash, const void *p) noexcept
 
std::pair< bool, const void * > addLock (IdentifierHash hash, void_unique_ptr p) noexcept
 
bool IMakerPresent () const
 
int tryLock (IdentifierHash, IDC_WriteHandleBase &, std::vector< IdentifierHash > &)
 Checks if the item is completed if it is not started it extablishes lock (returns 0), If it is started but not completed it adds to wait list (returns 1) If the item is already completed it returns 2 If the item is aborted it does nothing and returns 3. More...
 
int itemAborted (IdentifierHash)
 Returns 1 is the item has been aborted otherwise 0. More...
 
int itemInProgress (IdentifierHash)
 Returns 1 is the item is inprogress otherwise 0. More...
 
const void * waitFor (IdentifierHash)
 Halts the thread until the require hash is completed or aborted. More...
 
void createSet (const std::vector< IdentifierHash > &hashes, std::vector< bool > &mask)
 Create a set of hashes, updates an IDC mask as appropriate. More...
 
size_t fullSize () const
 
size_t numberOfHashes ()
 In a concurrent situation this number isn't necessarily perfectly synchronised with ids().size() More...
 

Static Public Attributes

static constexpr uintptr_t INVALIDflag = UINTPTR_MAX
 
static constexpr uintptr_t ABORTEDflag = UINTPTR_MAX-1
 
static constexpr size_t s_defaultBucketSize =2
 

Protected Member Functions

void clear (deleter_f *deleter)
 
void cleanUp (deleter_f *deleter)
 
void notifyHash (IdentifierHash hash)
 

Private Types

typedef std::mutex mutex_t
 
typedef std::scoped_lock< mutex_tlock_t
 
typedef std::unique_lock< mutex_tuniqueLock
 

Private Attributes

std::vector< std::atomic< const void * > > m_vec
 
const IMakerm_maker
 
mutex_t m_mutex
 
std::unique_ptr< mutexPair[]> m_HoldingMutexes
 Pool of mutexes used for waiting on completion if in a concurrent environment. More...
 
size_t m_NMutexes
 
std::atomic< size_t > m_currentHashes
 Holds the number of valid hashes in container, in concurrent use it is not guaranteed to be up to date. More...
 

Detailed Description

template<class T>
class EventContainers::IdentifiableCache< T >

Definition at line 27 of file IdentifiableCache.h.

Member Typedef Documentation

◆ lock_t

typedef std::scoped_lock<mutex_t> EventContainers::IdentifiableCacheBase::lock_t
privateinherited

Definition at line 108 of file IdentifiableCacheBase.h.

◆ mutex_t

typedef std::mutex EventContainers::IdentifiableCacheBase::mutex_t
privateinherited

Definition at line 107 of file IdentifiableCacheBase.h.

◆ thread_safe

typedef std::true_type EventContainers::IdentifiableCacheBase::thread_safe
inherited

Definition at line 39 of file IdentifiableCacheBase.h.

◆ uniqueLock

typedef std::unique_lock<mutex_t> EventContainers::IdentifiableCacheBase::uniqueLock
privateinherited

Definition at line 109 of file IdentifiableCacheBase.h.

Constructor & Destructor Documentation

◆ IdentifiableCache() [1/2]

template<class T >
EventContainers::IdentifiableCache< T >::IdentifiableCache ( IdentifierHash  maxHash,
const Maker maker 
)
inline

Definition at line 43 of file IdentifiableCache.h.

44  : IdentifiableCacheBase (maxHash, maker)
45  {
46  }

◆ IdentifiableCache() [2/2]

template<class T >
EventContainers::IdentifiableCache< T >::IdentifiableCache ( IdentifierHash  maxHash,
const Maker maker,
size_t  lockBucketSize 
)
inline

Definition at line 48 of file IdentifiableCache.h.

49  : IdentifiableCacheBase (maxHash, maker, lockBucketSize)
50  {
51  }

◆ ~IdentifiableCache()

template<class T >
EventContainers::IdentifiableCache< T >::~IdentifiableCache ( )
inline

Member Function Documentation

◆ add() [1/4]

template<class T >
std::pair<bool, const void*> EventContainers::IdentifiableCache< T >::add ( IdentifierHash  hash,
const T *  p 
)
inline

Definition at line 74 of file IdentifiableCache.h.

75  {
77  }

◆ add() [2/4]

std::pair< bool, const void * > EventContainers::IdentifiableCacheBase::add ( IdentifierHash  hash,
const void *  p 
)
noexceptinherited

Definition at line 247 of file IdentifiableCacheBase.cxx.

248 {
249  if (ATH_UNLIKELY(hash >= m_vec.size())) return std::make_pair(false, nullptr);
250  if(p==nullptr) return std::make_pair(false, nullptr);
251  const void* nul=nullptr;
252  if(m_vec[hash].compare_exchange_strong(nul, p, std::memory_order_release, std::memory_order_relaxed)){
253  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
254  return std::make_pair(true, p);
255  }
256  const void* invalid = INVALID;
257  if(m_vec[hash].compare_exchange_strong(invalid, p, std::memory_order_release, std::memory_order_acquire)){
258  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
259  notifyHash(hash);
260  return std::make_pair(true, p);
261  }
262  return std::make_pair(false, invalid);
263 }

◆ add() [3/4]

template<class T >
std::pair<bool, const void*> EventContainers::IdentifiableCache< T >::add ( IdentifierHash  hash,
std::unique_ptr< T >  p 
)
inline

Definition at line 79 of file IdentifiableCache.h.

80  {
81  return IdentifiableCacheBase::add (hash, void_unique_ptr(std::move(p)));
82  }

◆ add() [4/4]

std::pair< bool, const void * > EventContainers::IdentifiableCacheBase::add ( IdentifierHash  hash,
void_unique_ptr  p 
)
noexceptinherited

Definition at line 294 of file IdentifiableCacheBase.cxx.

296 {
297  std::pair<bool, const void*> b = add(hash, p.get());
298  if(b.first) p.release();
299  return b;
300 }

◆ addLock() [1/2]

std::pair< bool, const void * > EventContainers::IdentifiableCacheBase::addLock ( IdentifierHash  hash,
const void *  p 
)
noexceptinherited

Definition at line 266 of file IdentifiableCacheBase.cxx.

267 { //Same as method above except we check for invalid state first,
268  // more optimal for calling using writehandle lock method
269  assert(hash < m_vec.size());
270  if(p==nullptr) return std::make_pair(false, nullptr);
271  const void* invalid = INVALID;
272  if(m_vec[hash].compare_exchange_strong(invalid, p, std::memory_order_release, std::memory_order_relaxed)){
273  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
274  notifyHash(hash);
275  return std::make_pair(true, p);
276  }
277  const void* nul=nullptr;
278  if(m_vec[hash].compare_exchange_strong(nul, p, std::memory_order_release, std::memory_order_acquire)){
279  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
280  return std::make_pair(true, p);
281  }
282  return std::make_pair(false, nul);
283 }

◆ addLock() [2/2]

std::pair< bool, const void * > EventContainers::IdentifiableCacheBase::addLock ( IdentifierHash  hash,
void_unique_ptr  p 
)
noexceptinherited

Definition at line 285 of file IdentifiableCacheBase.cxx.

287 {
288  std::pair<bool, const void*> b = addLock(hash, p.get());
289  if(b.first) p.release();
290  return b;
291 }

◆ cleanUp()

void EventContainers::IdentifiableCacheBase::cleanUp ( deleter_f deleter)
protectedinherited

Definition at line 102 of file IdentifiableCacheBase.cxx.

103 {
104  std::atomic_thread_fence(std::memory_order_acquire);
105  if(0 != m_currentHashes.load(std::memory_order_relaxed)){ //Reduce overhead if cache was unused
106  size_t s = m_vec.size();
107  for (size_t i=0; i<s ;i++) {
108  const void* p = m_vec[i].load(std::memory_order_relaxed);
109  if(p && p < ABORTED) deleter (p);
110  }
111  }
112 }

◆ clear()

void EventContainers::IdentifiableCacheBase::clear ( deleter_f deleter)
protectedinherited

Definition at line 83 of file IdentifiableCacheBase.cxx.

84 {
85  size_t s = m_vec.size();
86  if(0 != m_currentHashes.load(std::memory_order_relaxed)){
87  for (size_t i=0; i<s ;i++) {
88  const void* ptr = m_vec[i].load(std::memory_order_relaxed);
89  m_vec[i].store(nullptr, std::memory_order_relaxed);
90  if (ptr && ptr < ABORTED){
91  deleter (ptr);
92  }
93  }
94  m_currentHashes.store(0, std::memory_order_relaxed);
95  }else{
96  for (size_t i=0; i<s ;i++) m_vec[i].store(nullptr, std::memory_order_relaxed);//Need to clear incase of aborts
97  }
98 }

◆ clearCache()

template<class T >
void EventContainers::IdentifiableCache< T >::clearCache ( )
inline

◆ createSet()

void EventContainers::IdentifiableCacheBase::createSet ( const std::vector< IdentifierHash > &  hashes,
std::vector< bool > &  mask 
)
inherited

Create a set of hashes, updates an IDC mask as appropriate.

Definition at line 219 of file IdentifiableCacheBase.cxx.

219  {
220  assert(mask.size() == fullSize());
221  for(IdentifierHash hash : hashes){
222  const void* ptr = get(hash);
223  if(ptr !=nullptr) mask[hash] = true;
224  }
225 }

◆ find()

template<class T >
const T* EventContainers::IdentifiableCache< T >::find ( IdentifierHash  hash)
inline

Definition at line 59 of file IdentifiableCache.h.

60  {
61  return reinterpret_cast<const T*> (IdentifiableCacheBase::find (hash));
62  }

◆ findWait()

template<class T >
const T* EventContainers::IdentifiableCache< T >::findWait ( IdentifierHash  hash)
inline

Definition at line 64 of file IdentifiableCache.h.

65  {
66  return reinterpret_cast<const T*> (IdentifiableCacheBase::findWait (hash));
67  }

◆ fullSize()

size_t EventContainers::IdentifiableCacheBase::fullSize ( ) const
inlineinherited

Definition at line 89 of file IdentifiableCacheBase.h.

89 { return m_vec.size(); }

◆ get()

template<class T >
const T* EventContainers::IdentifiableCache< T >::get ( IdentifierHash  hash)
inline

Definition at line 69 of file IdentifiableCache.h.

70  {
71  return reinterpret_cast<const T*> (IdentifiableCacheBase::get (hash));
72  }

◆ ids()

std::vector< IdentifierHash > EventContainers::IdentifiableCacheBase::ids ( )
inherited

In a threaded situation this collection will be valid but will not container hashes later added.

Definition at line 233 of file IdentifiableCacheBase.cxx.

234 {
235  std::vector<IdentifierHash> ret;
236  ret.reserve (m_currentHashes.load(std::memory_order_relaxed));
237  size_t s = m_vec.size();
238  for (size_t i =0; i<s; i++) {
239  const void* p = m_vec[i].load(std::memory_order_relaxed);
240  if (p && p < ABORTED)
241  ret.push_back (i);
242  }
243  return ret;
244 }

◆ IMakerPresent()

bool EventContainers::IdentifiableCacheBase::IMakerPresent ( ) const
inlineinherited

Definition at line 69 of file IdentifiableCacheBase.h.

69 { return m_maker!=nullptr; }

◆ itemAborted()

int EventContainers::IdentifiableCacheBase::itemAborted ( IdentifierHash  hash)
inherited

Returns 1 is the item has been aborted otherwise 0.

Definition at line 114 of file IdentifiableCacheBase.cxx.

114  {
115  const void* p = m_vec[hash].load(std::memory_order_relaxed); //Relaxed because it is not returning a pointer to anything
116  return (p == ABORTED);
117 }

◆ itemInProgress()

int EventContainers::IdentifiableCacheBase::itemInProgress ( IdentifierHash  hash)
inherited

Returns 1 is the item is inprogress otherwise 0.

Definition at line 120 of file IdentifiableCacheBase.cxx.

120  {
121  const void* p = m_vec[hash].load(std::memory_order_relaxed); //Relaxed because it is not returning a pointer to anything
122  return (p == INVALID);
123 }

◆ notifyHash()

void EventContainers::IdentifiableCacheBase::notifyHash ( IdentifierHash  hash)
protectedinherited

Definition at line 166 of file IdentifiableCacheBase.cxx.

167 {
168 #ifndef __cpp_lib_atomic_wait
169  if(m_NMutexes==0) return;
170  size_t slot = hash % m_NMutexes;
171  mutexPair &mutpair = m_HoldingMutexes[slot];
172  lock_t lk(mutpair.mutex);
173  mutpair.condition.notify_all();
174 #else
175  m_vec[hash].notify_all();
176 #endif
177 }

◆ numberOfHashes()

size_t EventContainers::IdentifiableCacheBase::numberOfHashes ( )
inherited

In a concurrent situation this number isn't necessarily perfectly synchronised with ids().size()

Definition at line 228 of file IdentifiableCacheBase.cxx.

229 {
230  return m_currentHashes.load(std::memory_order_relaxed); //Not to be used for syncing
231 }

◆ tryLock()

int EventContainers::IdentifiableCacheBase::tryLock ( IdentifierHash  hash,
IDC_WriteHandleBase lock,
std::vector< IdentifierHash > &  wait 
)
inherited

Checks if the item is completed if it is not started it extablishes lock (returns 0), If it is started but not completed it adds to wait list (returns 1) If the item is already completed it returns 2 If the item is aborted it does nothing and returns 3.

Definition at line 57 of file IdentifiableCacheBase.cxx.

57  {
58  const void *ptr1 =nullptr;
59 
60  if(m_vec[hash].compare_exchange_strong(ptr1, INVALID, std::memory_order_relaxed, std::memory_order_relaxed)){//atomic swap (replaces ptr1 with value)
61  //First call
62 #ifndef __cpp_lib_atomic_wait
63  size_t slot = hash % m_NMutexes;
64  auto &mutexpair = m_HoldingMutexes[slot];
65  lock.LockOn(&m_vec[hash], &mutexpair);
66 #else
67  //Setup the IDC_WriteHandle to "lock" on this hash's pointer
68  lock.LockOn(&m_vec[hash]);
69 #endif
70  return 0;
71  }
72 
73  if(ptr1 == INVALID){
74  //Second call while not finished
75  wait.emplace_back(hash);
76  return 1;
77  }
78  if(ptr1 == ABORTED) return 3;
79  return 2; //Already completed
80 }

◆ waitFor()

const void * EventContainers::IdentifiableCacheBase::waitFor ( IdentifierHash  hash)
inherited

Halts the thread until the require hash is completed or aborted.

Definition at line 135 of file IdentifiableCacheBase.cxx.

136 {
137  std::atomic<const void*> &myatomic = m_vec[hash];
138  const void* item = myatomic.load(std::memory_order_acquire);
139 #ifndef __cpp_lib_atomic_wait
140  if(m_NMutexes != 0 && item == INVALID){
141  size_t slot = hash % m_NMutexes;
142  mutexPair &mutpair = m_HoldingMutexes[slot];
143  uniqueLock lk(mutpair.mutex);
144  while( (item =myatomic.load(std::memory_order_acquire)) == INVALID){
145  mutpair.condition.wait(lk);
146  }
147  }
148 #else
149  //Wait until pointer is set then retrieve and verify
150  while(item == INVALID){//Loop to check for spurious wakeups
151  myatomic.wait(item, std::memory_order_relaxed);
152  item = myatomic.load(std::memory_order_acquire);
153  }
154 #endif
155  return item;
156 }

Member Data Documentation

◆ ABORTEDflag

constexpr uintptr_t EventContainers::IdentifiableCacheBase::ABORTEDflag = UINTPTR_MAX-1
staticconstexprinherited

Definition at line 31 of file IdentifiableCacheBase.h.

◆ INVALIDflag

constexpr uintptr_t EventContainers::IdentifiableCacheBase::INVALIDflag = UINTPTR_MAX
staticconstexprinherited

Definition at line 30 of file IdentifiableCacheBase.h.

◆ m_currentHashes

std::atomic<size_t> EventContainers::IdentifiableCacheBase::m_currentHashes
privateinherited

Holds the number of valid hashes in container, in concurrent use it is not guaranteed to be up to date.

Definition at line 117 of file IdentifiableCacheBase.h.

◆ m_HoldingMutexes

std::unique_ptr<mutexPair[]> EventContainers::IdentifiableCacheBase::m_HoldingMutexes
privateinherited

Pool of mutexes used for waiting on completion if in a concurrent environment.

Definition at line 113 of file IdentifiableCacheBase.h.

◆ m_maker

const IMaker* EventContainers::IdentifiableCacheBase::m_maker
privateinherited

Definition at line 105 of file IdentifiableCacheBase.h.

◆ m_mutex

mutex_t EventContainers::IdentifiableCacheBase::m_mutex
privateinherited

Definition at line 110 of file IdentifiableCacheBase.h.

◆ m_NMutexes

size_t EventContainers::IdentifiableCacheBase::m_NMutexes
privateinherited

Definition at line 114 of file IdentifiableCacheBase.h.

◆ m_vec

std::vector<std::atomic<const void*> > EventContainers::IdentifiableCacheBase::m_vec
privateinherited

Definition at line 103 of file IdentifiableCacheBase.h.

◆ s_defaultBucketSize

constexpr size_t EventContainers::IdentifiableCacheBase::s_defaultBucketSize =2
staticconstexprinherited

Definition at line 36 of file IdentifiableCacheBase.h.


The documentation for this class was generated from the following file:
EventContainers::ABORTED
const void *const ABORTED
Definition: IdentifiableCacheBase.cxx:24
python.root_lsr_rank.hashes
hashes
Definition: root_lsr_rank.py:34
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:71
EventContainers::IdentifiableCacheBase::m_HoldingMutexes
std::unique_ptr< mutexPair[]> m_HoldingMutexes
Pool of mutexes used for waiting on completion if in a concurrent environment.
Definition: IdentifiableCacheBase.h:113
EventContainers::IdentifiableCacheBase::findWait
const void * findWait(IdentifierHash hash)
Retrieve ptr, will wait if there is something in progress.
Definition: IdentifiableCacheBase.cxx:158
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
EventContainers::IdentifiableCacheBase::get
const void * get(IdentifierHash hash)
Try to make payload if not there.
Definition: IdentifiableCacheBase.cxx:179
EventContainers::INVALID
const void *const INVALID
Definition: IdentifiableCacheBase.cxx:23
EventContainers::IdentifiableCacheBase::m_maker
const IMaker * m_maker
Definition: IdentifiableCacheBase.h:105
EventContainers::IdentifiableCacheBase::lock_t
std::scoped_lock< mutex_t > lock_t
Definition: IdentifiableCacheBase.h:108
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
EventContainers::IdentifiableCacheBase::m_NMutexes
size_t m_NMutexes
Definition: IdentifiableCacheBase.h:114
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
void_unique_ptr::Deleter
Definition: deleter.h:22
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
lumiFormat.i
int i
Definition: lumiFormat.py:85
EventContainers::IdentifiableCacheBase::IdentifiableCacheBase
IdentifiableCacheBase(IdentifierHash maxHash, const IMaker *maker, size_t lockBucketSize)
Definition: IdentifiableCacheBase.cxx:35
EventContainers::IdentifiableCacheBase::fullSize
size_t fullSize() const
Definition: IdentifiableCacheBase.h:89
EventContainers::IdentifiableCacheBase::uniqueLock
std::unique_lock< mutex_t > uniqueLock
Definition: IdentifiableCacheBase.h:109
EventContainers::IdentifiableCacheBase::clear
void clear(deleter_f *deleter)
Definition: IdentifiableCacheBase.cxx:83
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
EventContainers::IdentifiableCacheBase::notifyHash
void notifyHash(IdentifierHash hash)
Definition: IdentifiableCacheBase.cxx:166
EventContainers::IdentifiableCacheBase::m_currentHashes
std::atomic< size_t > m_currentHashes
Holds the number of valid hashes in container, in concurrent use it is not guaranteed to be up to dat...
Definition: IdentifiableCacheBase.h:117
item
Definition: ItemListSvc.h:43
EventContainers::IdentifiableCacheBase::m_vec
std::vector< std::atomic< const void * > > m_vec
Definition: IdentifiableCacheBase.h:103
void_unique_ptr
Definition: deleter.h:16
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
EventContainers::IdentifiableCacheBase::find
const void * find(IdentifierHash hash) noexcept
Return payload if there, null if not there.
Definition: IdentifiableCacheBase.cxx:126
EventContainers::IdentifiableCacheBase::add
std::pair< bool, const void * > add(IdentifierHash hash, const void *p) noexcept
Definition: IdentifiableCacheBase.cxx:247
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
EventContainers::IdentifiableCacheBase::addLock
std::pair< bool, const void * > addLock(IdentifierHash hash, const void *p) noexcept
Definition: IdentifiableCacheBase.cxx:266
EventContainers::IdentifiableCacheBase::cleanUp
void cleanUp(deleter_f *deleter)
Definition: IdentifiableCacheBase.cxx:102