ATLAS Offline Software
Loading...
Searching...
No Matches
EventContainers::IdentifiableCacheBase Class Reference

#include <IdentifiableCacheBase.h>

Inherited by EventContainers::IdentifiableCache< InDetRawDataCollection< PixelRDORawData > >, EventContainers::IdentifiableCache< InDetRawDataCollection< SCT_RDORawData > >, EventContainers::IdentifiableCache< InDetRawDataCollection< TRT_RDORawData > >, EventContainers::IdentifiableCache< PixelClusterCollection >, EventContainers::IdentifiableCache< SCT_ClusterCollection >, EventContainers::IdentifiableCache< TRT_DriftCircleCollection >, EventContainers::IdentifiableCache< CscRawDataCollection >, EventContainers::IdentifiableCache< MdtCsm >, EventContainers::IdentifiableCache< Muon::MM_RawDataCollection >, EventContainers::IdentifiableCache< RpcPad >, EventContainers::IdentifiableCache< Muon::STGC_RawDataCollection >, EventContainers::IdentifiableCache< TgcL1Rdo >, EventContainers::IdentifiableCache< TgcRdo >, EventContainers::IdentifiableCache< Muon::CscPrepDataCollection >, EventContainers::IdentifiableCache< Muon::CscStripPrepDataCollection >, EventContainers::IdentifiableCache< Muon::MdtPrepDataCollection >, EventContainers::IdentifiableCache< Muon::RpcPrepDataCollection >, EventContainers::IdentifiableCache< Muon::TgcPrepDataCollection >, EventContainers::IdentifiableCache< Muon::sTgcPrepDataCollection >, EventContainers::IdentifiableCache< Muon::MMPrepDataCollection >, EventContainers::IdentifiableCache< Muon::RpcCoinDataCollection >, EventContainers::IdentifiableCache< Muon::TgcCoinDataCollection >, EventContainers::IdentifiableCache< SpacePointCollection >, and EventContainers::IdentifiableCache< T >.

Collaboration diagram for EventContainers::IdentifiableCacheBase:

Classes

class  void_unique_ptr
struct  IMaker

Public Types

typedef std::true_type thread_safe
typedef void deleter_f(const void *p)

Public Member Functions

const void * find (IdentifierHash hash) noexcept
 Return payload if there, null if not there.
const void * findWait (IdentifierHash hash)
 Retrieve ptr, will wait if there is something in progress.
const void * get (IdentifierHash hash)
 Try to make payload if not there.
std::vector< IdentifierHashids ()
 In a threaded situation this collection will be valid but will not container hashes later added.
std::pair< bool, const void * > add (IdentifierHash hash, const void *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
std::pair< bool, const void * > add (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.
int itemAborted (IdentifierHash)
 Returns 1 is the item has been aborted otherwise 0.
int itemInProgress (IdentifierHash)
 Returns 1 is the item is inprogress otherwise 0.
const void * waitFor (IdentifierHash)
 Halts the thread until the require hash is completed or aborted.
void createSet (const std::vector< IdentifierHash > &hashes, std::vector< bool > &mask)
 Create a set of hashes, updates an IDC mask as appropriate.
size_t fullSize () const
size_t numberOfHashes ()
 In a concurrent situation this number isn't necessarily perfectly synchronised with ids().size().

Static Public Attributes

static constexpr uintptr_t INVALIDflag = UINTPTR_MAX
static constexpr uintptr_t ABORTEDflag = UINTPTR_MAX-1

Protected Member Functions

 IdentifiableCacheBase (IdentifierHash maxHash, const IMaker *maker)
 ~IdentifiableCacheBase ()
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.

Friends

class InternalOnline

Detailed Description

Definition at line 22 of file IdentifiableCacheBase.h.

Member Typedef Documentation

◆ deleter_f

typedef void EventContainers::IdentifiableCacheBase::deleter_f(const void *p)

Definition at line 14 of file IdentifiableCacheBase.h.

◆ lock_t

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

Definition at line 97 of file IdentifiableCacheBase.h.

◆ mutex_t

Definition at line 96 of file IdentifiableCacheBase.h.

◆ thread_safe

Definition at line 31 of file IdentifiableCacheBase.h.

◆ uniqueLock

Definition at line 98 of file IdentifiableCacheBase.h.

Constructor & Destructor Documentation

◆ IdentifiableCacheBase()

EventContainers::IdentifiableCacheBase::IdentifiableCacheBase ( IdentifierHash maxHash,
const IMaker * maker )
protected

Definition at line 28 of file IdentifiableCacheBase.cxx.

30 : m_vec(maxHash),
31 m_maker (maker),
33{
34}
std::vector< std::atomic< const void * > > m_vec
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...

◆ ~IdentifiableCacheBase()

EventContainers::IdentifiableCacheBase::~IdentifiableCacheBase ( )
protecteddefault

Member Function Documentation

◆ add() [1/2]

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

Definition at line 203 of file IdentifiableCacheBase.cxx.

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

◆ add() [2/2]

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

Definition at line 250 of file IdentifiableCacheBase.cxx.

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

◆ addLock() [1/2]

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

Definition at line 222 of file IdentifiableCacheBase.cxx.

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

◆ addLock() [2/2]

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

Definition at line 241 of file IdentifiableCacheBase.cxx.

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

◆ cleanUp()

void EventContainers::IdentifiableCacheBase::cleanUp ( deleter_f * deleter)
protected

Definition at line 78 of file IdentifiableCacheBase.cxx.

79{
80 if(0 != m_currentHashes.load(std::memory_order_acquire)){ //Reduce overhead if cache was unused
81 size_t s = m_vec.size();
82 for (size_t i=0; i<s ;i++) {
83 const void* p = m_vec[i].load(std::memory_order_relaxed);
84 if(p && p < ABORTED) deleter (p);
85 }
86 }
87}
IPayloadDeleter & deleter()
Return a reference to the payload deleter object.
const void *const ABORTED

◆ clear()

void EventContainers::IdentifiableCacheBase::clear ( deleter_f * deleter)
protected

Definition at line 59 of file IdentifiableCacheBase.cxx.

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

◆ createSet()

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

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

Definition at line 175 of file IdentifiableCacheBase.cxx.

175 {
176 assert(mask.size() == fullSize());
177 for(IdentifierHash hash : hashes){
178 const void* ptr = get(hash);
179 if(ptr !=nullptr) mask[hash] = true;
180 }
181}
const void * get(IdentifierHash hash)
Try to make payload if not there.

◆ find()

const void * EventContainers::IdentifiableCacheBase::find ( IdentifierHash hash)
noexcept

Return payload if there, null if not there.

Definition at line 101 of file IdentifiableCacheBase.cxx.

102{
103 if (hash >= m_vec.size()) [[unlikely]] return nullptr;
104 const void* p = m_vec[hash].load(std::memory_order_acquire);
105 if (p >= ABORTED)
106 return nullptr;
107 return p;
108}

◆ findWait()

const void * EventContainers::IdentifiableCacheBase::findWait ( IdentifierHash hash)

Retrieve ptr, will wait if there is something in progress.

Definition at line 122 of file IdentifiableCacheBase.cxx.

123{
124 if (hash >= m_vec.size()) [[unlikely]] return nullptr;
125 const void* p = waitFor(hash);
126 if(p>=ABORTED) return nullptr;
127 return p;
128}
const void * waitFor(IdentifierHash)
Halts the thread until the require hash is completed or aborted.

◆ fullSize()

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

Definition at line 81 of file IdentifiableCacheBase.h.

81{ return m_vec.size(); }

◆ get()

const void * EventContainers::IdentifiableCacheBase::get ( IdentifierHash hash)

Try to make payload if not there.

Definition at line 135 of file IdentifiableCacheBase.cxx.

136{
137 // If it's there already, return directly without locking.
138 const void* ptr = nullptr;
139 if (hash >= m_vec.size()) [[unlikely]] return ptr;
140
141 if(m_vec[hash].compare_exchange_strong(ptr, INVALID) ) {//Exchanges ptr with current value!!
142 // Make the payload.
143 if(m_maker == nullptr){
144 m_vec[hash].store( ABORTED );
145 return nullptr;
146 }
147 uniqueLock lock(m_mutex, std::defer_lock);
148 if(!m_maker->m_IsReEntrant) lock.lock();//Allow reentrant or non reentrant makers
149
150 try {
151 ptr = m_maker->typelessMake (hash).release();
152 }
153 catch (...) {
154 m_vec[hash].store( ABORTED );
155 notifyHash(hash);
156 throw;
157 }
158 assert(m_vec[hash] == INVALID);
159 if(ptr){
160 m_vec[hash].store( ptr );
162 }else{
163 m_vec[hash].store( ABORTED );
164 }
165 notifyHash(hash);
166 }
167 else if(ptr == INVALID){
168 ptr= waitFor(hash);
169 }
170 if(ptr == ABORTED) return nullptr;
171 assert(ptr < ABORTED);
172 return ptr;
173}
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.

◆ ids()

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

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

Definition at line 189 of file IdentifiableCacheBase.cxx.

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

◆ IMakerPresent()

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

Definition at line 61 of file IdentifiableCacheBase.h.

61{ return m_maker!=nullptr; }

◆ itemAborted()

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

Returns 1 is the item has been aborted otherwise 0.

Definition at line 89 of file IdentifiableCacheBase.cxx.

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

◆ itemInProgress()

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

Returns 1 is the item is inprogress otherwise 0.

Definition at line 95 of file IdentifiableCacheBase.cxx.

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

◆ notifyHash()

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

Definition at line 130 of file IdentifiableCacheBase.cxx.

131{
132 m_vec[hash].notify_all();
133}

◆ numberOfHashes()

size_t EventContainers::IdentifiableCacheBase::numberOfHashes ( )

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

Definition at line 184 of file IdentifiableCacheBase.cxx.

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

◆ tryLock()

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

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

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

◆ waitFor()

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

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

Definition at line 110 of file IdentifiableCacheBase.cxx.

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

◆ InternalOnline

friend class InternalOnline
friend

Definition at line 93 of file IdentifiableCacheBase.h.

Member Data Documentation

◆ ABORTEDflag

uintptr_t EventContainers::IdentifiableCacheBase::ABORTEDflag = UINTPTR_MAX-1
staticconstexpr

Definition at line 28 of file IdentifiableCacheBase.h.

◆ INVALIDflag

uintptr_t EventContainers::IdentifiableCacheBase::INVALIDflag = UINTPTR_MAX
staticconstexpr

Definition at line 27 of file IdentifiableCacheBase.h.

◆ m_currentHashes

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

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
private

Definition at line 94 of file IdentifiableCacheBase.h.

◆ m_mutex

mutex_t EventContainers::IdentifiableCacheBase::m_mutex
private

Definition at line 99 of file IdentifiableCacheBase.h.

◆ m_vec

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

Definition at line 92 of file IdentifiableCacheBase.h.


The documentation for this class was generated from the following files: