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
 

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::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 97 of file IdentifiableCacheBase.h.

◆ mutex_t

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

Definition at line 96 of file IdentifiableCacheBase.h.

◆ thread_safe

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

Definition at line 31 of file IdentifiableCacheBase.h.

◆ uniqueLock

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

Definition at line 98 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 205 of file IdentifiableCacheBase.cxx.

206 {
207  if (ATH_UNLIKELY(hash >= m_vec.size())) return std::make_pair(false, nullptr);
208  if(p==nullptr) return std::make_pair(false, nullptr);
209  const void* nul=nullptr;
210  if(m_vec[hash].compare_exchange_strong(nul, p, std::memory_order_release, std::memory_order_relaxed)){
211  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
212  return std::make_pair(true, p);
213  }
214  const void* invalid = INVALID;
215  if(m_vec[hash].compare_exchange_strong(invalid, p, std::memory_order_release, std::memory_order_acquire)){
216  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
217  notifyHash(hash);
218  return std::make_pair(true, p);
219  }
220  return std::make_pair(false, invalid);
221 }

◆ 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 252 of file IdentifiableCacheBase.cxx.

254 {
255  std::pair<bool, const void*> b = add(hash, p.get());
256  if(b.first) p.release();
257  return b;
258 }

◆ addLock() [1/2]

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

Definition at line 224 of file IdentifiableCacheBase.cxx.

225 { //Same as method above except we check for invalid state first,
226  // more optimal for calling using writehandle lock method
227  assert(hash < m_vec.size());
228  if(p==nullptr) return std::make_pair(false, nullptr);
229  const void* invalid = INVALID;
230  if(m_vec[hash].compare_exchange_strong(invalid, p, std::memory_order_release, std::memory_order_relaxed)){
231  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
232  notifyHash(hash);
233  return std::make_pair(true, p);
234  }
235  const void* nul=nullptr;
236  if(m_vec[hash].compare_exchange_strong(nul, p, std::memory_order_release, std::memory_order_acquire)){
237  m_currentHashes.fetch_add(1, std::memory_order_relaxed);
238  return std::make_pair(true, p);
239  }
240  return std::make_pair(false, nul);
241 }

◆ addLock() [2/2]

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

Definition at line 243 of file IdentifiableCacheBase.cxx.

245 {
246  std::pair<bool, const void*> b = addLock(hash, p.get());
247  if(b.first) p.release();
248  return b;
249 }

◆ cleanUp()

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

Definition at line 79 of file IdentifiableCacheBase.cxx.

80 {
81  std::atomic_thread_fence(std::memory_order_acquire);
82  if(0 != m_currentHashes.load(std::memory_order_relaxed)){ //Reduce overhead if cache was unused
83  size_t s = m_vec.size();
84  for (size_t i=0; i<s ;i++) {
85  const void* p = m_vec[i].load(std::memory_order_relaxed);
86  if(p && p < ABORTED) deleter (p);
87  }
88  }
89 }

◆ clear()

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

Definition at line 60 of file IdentifiableCacheBase.cxx.

61 {
62  size_t s = m_vec.size();
63  if(0 != m_currentHashes.load(std::memory_order_relaxed)){
64  for (size_t i=0; i<s ;i++) {
65  const void* ptr = m_vec[i].load(std::memory_order_relaxed);
66  m_vec[i].store(nullptr, std::memory_order_relaxed);
67  if (ptr && ptr < ABORTED){
68  deleter (ptr);
69  }
70  }
71  m_currentHashes.store(0, std::memory_order_relaxed);
72  }else{
73  for (size_t i=0; i<s ;i++) m_vec[i].store(nullptr, std::memory_order_relaxed);//Need to clear incase of aborts
74  }
75 }

◆ 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 177 of file IdentifiableCacheBase.cxx.

177  {
178  assert(mask.size() == fullSize());
179  for(IdentifierHash hash : hashes){
180  const void* ptr = get(hash);
181  if(ptr !=nullptr) mask[hash] = true;
182  }
183 }

◆ find()

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

Definition at line 59 of file IdentifiableCache.h.

60  {
61  return std::bit_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 std::bit_cast<const T*> (IdentifiableCacheBase::findWait (hash));
67  }

◆ fullSize()

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

Definition at line 81 of file IdentifiableCacheBase.h.

81 { 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 std::bit_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 191 of file IdentifiableCacheBase.cxx.

192 {
193  std::vector<IdentifierHash> ret;
194  ret.reserve (m_currentHashes.load(std::memory_order_relaxed));
195  size_t s = m_vec.size();
196  for (size_t i =0; i<s; i++) {
197  const void* p = m_vec[i].load(std::memory_order_relaxed);
198  if (p && p < ABORTED)
199  ret.push_back (i);
200  }
201  return ret;
202 }

◆ IMakerPresent()

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

Definition at line 61 of file IdentifiableCacheBase.h.

61 { 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 91 of file IdentifiableCacheBase.cxx.

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

◆ itemInProgress()

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

Returns 1 is the item is inprogress otherwise 0.

Definition at line 97 of file IdentifiableCacheBase.cxx.

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

◆ notifyHash()

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

Definition at line 132 of file IdentifiableCacheBase.cxx.

133 {
134  m_vec[hash].notify_all();
135 }

◆ numberOfHashes()

size_t EventContainers::IdentifiableCacheBase::numberOfHashes ( )
inherited

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

Definition at line 186 of file IdentifiableCacheBase.cxx.

187 {
188  return m_currentHashes.load(std::memory_order_relaxed); //Not to be used for syncing
189 }

◆ 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 40 of file IdentifiableCacheBase.cxx.

40  {
41  const void *ptr1 =nullptr;
42 
43  if(m_vec[hash].compare_exchange_strong(ptr1, INVALID, std::memory_order_relaxed, std::memory_order_relaxed)){//atomic swap (replaces ptr1 with value)
44  //First call
45  //Setup the IDC_WriteHandle to "lock" on this hash's pointer
46  lock.LockOn(&m_vec[hash]);
47  return 0;
48  }
49 
50  if(ptr1 == INVALID){
51  //Second call while not finished
52  wait.emplace_back(hash);
53  return 1;
54  }
55  if(ptr1 == ABORTED) return 3;
56  return 2; //Already completed
57 }

◆ waitFor()

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

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

Definition at line 112 of file IdentifiableCacheBase.cxx.

113 {
114  std::atomic<const void*> &myatomic = m_vec[hash];
115  const void* item = myatomic.load(std::memory_order_acquire);
116  //Wait until pointer is set then retrieve and verify
117  while(item == INVALID){//Loop to check for spurious wakeups
118  myatomic.wait(item, std::memory_order_relaxed);
119  item = myatomic.load(std::memory_order_acquire);
120  }
121  return item;
122 }

Member Data Documentation

◆ ABORTEDflag

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

Definition at line 28 of file IdentifiableCacheBase.h.

◆ INVALIDflag

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

Definition at line 27 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 101 of file IdentifiableCacheBase.h.

◆ m_maker

const IMaker* EventContainers::IdentifiableCacheBase::m_maker
privateinherited

Definition at line 94 of file IdentifiableCacheBase.h.

◆ m_mutex

mutex_t EventContainers::IdentifiableCacheBase::m_mutex
privateinherited

Definition at line 99 of file IdentifiableCacheBase.h.

◆ m_vec

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

Definition at line 92 of file IdentifiableCacheBase.h.


The documentation for this class was generated from the following file:
EventContainers::ABORTED
const void *const ABORTED
Definition: IdentifiableCacheBase.cxx:25
python.root_lsr_rank.hashes
hashes
Definition: root_lsr_rank.py:34
SGTest::store
TestStore store
Definition: TestStore.cxx:23
EventContainers::IdentifiableCacheBase::findWait
const void * findWait(IdentifierHash hash)
Retrieve ptr, will wait if there is something in progress.
Definition: IdentifiableCacheBase.cxx:124
EventContainers::IdentifiableCacheBase::get
const void * get(IdentifierHash hash)
Try to make payload if not there.
Definition: IdentifiableCacheBase.cxx:137
EventContainers::INVALID
const void *const INVALID
Definition: IdentifiableCacheBase.cxx:24
EventContainers::IdentifiableCacheBase::m_maker
const IMaker * m_maker
Definition: IdentifiableCacheBase.h:94
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
python.RatesEmulationExample.lock
lock
Definition: RatesEmulationExample.py:148
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
void_unique_ptr::Deleter
Definition: deleter.h:23
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:459
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
lumiFormat.i
int i
Definition: lumiFormat.py:85
EventContainers::IdentifiableCacheBase::IdentifiableCacheBase
IdentifiableCacheBase(IdentifierHash maxHash, const IMaker *maker)
Definition: IdentifiableCacheBase.cxx:29
EventContainers::IdentifiableCacheBase::fullSize
size_t fullSize() const
Definition: IdentifiableCacheBase.h:81
EventContainers::IdentifiableCacheBase::clear
void clear(deleter_f *deleter)
Definition: IdentifiableCacheBase.cxx:60
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
EventContainers::IdentifiableCacheBase::notifyHash
void notifyHash(IdentifierHash hash)
Definition: IdentifiableCacheBase.cxx:132
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:101
EventContainers::IdentifiableCacheBase::m_vec
std::vector< std::atomic< const void * > > m_vec
Definition: IdentifiableCacheBase.h:92
void_unique_ptr
Definition: deleter.h:17
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:108
EventContainers::IdentifiableCacheBase::find
const void * find(IdentifierHash hash) noexcept
Return payload if there, null if not there.
Definition: IdentifiableCacheBase.cxx:103
EventContainers::IdentifiableCacheBase::add
std::pair< bool, const void * > add(IdentifierHash hash, const void *p) noexcept
Definition: IdentifiableCacheBase.cxx:205
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
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
EventContainers::IdentifiableCacheBase::addLock
std::pair< bool, const void * > addLock(IdentifierHash hash, const void *p) noexcept
Definition: IdentifiableCacheBase.cxx:224
EventContainers::IdentifiableCacheBase::cleanUp
void cleanUp(deleter_f *deleter)
Definition: IdentifiableCacheBase.cxx:79