ATLAS Offline Software
IdentifiableContainerMT.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef EVENTCONTAINERS_IDENTIFIABLECONTAINERMT_H
6 #define EVENTCONTAINERS_IDENTIFIABLECONTAINERMT_H
7 
19 #include "Identifier/Identifier.h"
21 #include <memory>
22 #include "GaudiKernel/DataObject.h"
27 
28 template < class T>
30 {
31 
32 public:
33 
35  friend class IdentifiableContainerMT<T>;
38  public:
43  static void Swap(IDC_WriteHandle& a, IDC_WriteHandle& b) noexcept {
44  if(&a == &b) return;
45  std::swap(a.m_IDC_ptr, b.m_IDC_ptr);
46  std::swap(a.m_hashId, b.m_hashId);
47  std::swap(a.m_atomic, b.m_atomic);
48 #ifndef __cpp_lib_atomic_wait
49  std::swap(a.m_mut, b.m_mut);
50 #endif
51  }
53  Swap(*this, other);
54  }
55 
56  [[nodiscard]] StatusCode addOrDelete(std::unique_ptr<T> ptr){
57  if(ATH_UNLIKELY(m_hashId >= m_IDC_ptr->m_link->fullSize())) return StatusCode::FAILURE;
58  StatusCode sc = m_IDC_ptr->addLock(std::move(ptr), m_hashId);
59  IDC_WriteHandleBase::ReleaseLock();
60  return sc;
61  }
62  bool alreadyPresent() { return m_IDC_ptr->m_link->tryAddFromCache(m_hashId, *this); }
64  bool OnlineAndPresentInAnotherView() { return m_IDC_ptr->m_OnlineMode && m_IDC_ptr->m_link->tryAddFromCache(m_hashId, *this); }
65  };
66 
67 
68 
69  typedef T IDENTIFIABLE;
72  typedef std::vector<std::atomic<const void*> >::size_type size_type;
73  typedef T*& reference;
74  typedef T* const & const_reference;
75  typedef T* value_type;
76  typedef T** pointer;
77  typedef T* const * const_pointer;
78  typedef T base_value_type;
79 
80 
82  {
83 
84  public:
85 
89  const_iterator(const const_iterator&) = default;
90  const_iterator& operator = ( const const_iterator & ) = default; //move operators may be useful for shared_ptr
92  ~const_iterator() = default;
93 
94 
97  ++m_itr;
98  return *this;
99  }
100 
103  const_iterator tmp = *this;
104  ++*this;
105  return tmp;
106  }
107 
108  const T* cptr () const {
109  return reinterpret_cast<const T*>( m_itr->second );
110  }
111 
112  const T* operator * () const {
113  return reinterpret_cast<const T*>( m_itr->second );
114  }
115 
116  const T* operator ->() const { return (operator*()); }
117 
119  bool operator != ( const const_iterator &it ) const {
120  return m_itr!= it.m_itr;
121  }
122 
124  bool operator == ( const const_iterator &it ) const {
125  return m_itr == it.m_itr;
126  }
127 
129  //Calling this on an end iterator is undefined behaviour
131  return m_itr->first;
132  }
133 
134  protected:
135  friend class IdentifiableContainerMT<T>;
136 
138 
139 
141  };
142 
143  friend class IDC_WriteHandle;
144 
147 
149 
152 
154 
155  virtual bool hasExternalCache() const override final { return m_OnlineMode; }
156 
157 
161  virtual const T* indexFindPtr( IdentifierHash hashId ) const override final;
162 
164  return m_link->indexFind(hashId);
165  }
166 
169  virtual StatusCode addCollection(const T* coll, IdentifierHash hashId) override final;
170 
173  virtual StatusCode addOrDelete(std::unique_ptr<T>, IdentifierHash hashId) override final;
174  virtual StatusCode addOrDelete(std::unique_ptr<const T>, IdentifierHash hashId) override final;
175 
177  StatusCode addOrDelete(std::unique_ptr<T>, IdentifierHash hashId, bool &deleted);
178 
180  StatusCode addLock(std::unique_ptr<T> ptr, IdentifierHash hashId);
181 
184  virtual bool tryAddFromCache(IdentifierHash hashId) override final;
185 
189 
190  StatusCode fetchOrCreate(const std::vector<IdentifierHash> &hashId);
191 
192  //This is a method to support bad behaviour in old code. Remove ASAP
193  virtual StatusCode naughtyRetrieve ATLAS_NOT_THREAD_SAFE (IdentifierHash hashId, T* &collToRetrieve) const override final;
194 
195 
199 
201  virtual void cleanup() override final;
202 
204  virtual size_t fullSize() const override final {
205  return m_link->fullSize();
206  }
207 
209  size_t size() const {
210  return m_link->fullSize();
211  }
212 
213  void prepareItr() const { m_link->wait(); }
214 
216  virtual size_t numberOfCollections() const override final{
217  return IdentifiableContainerBase::numberOfCollections();
218  }
219 
220  const std::vector < EventContainers::hashPair<T> >& GetAllHashPtrPair() const{
221  static_assert(sizeof(const T*) == sizeof(const void*) && std::is_pointer<const T*>::value);
222  static_assert(sizeof(EventContainers::hashPair<T>) == sizeof(EventContainers::hashPair<void>));
223  return reinterpret_cast<const std::vector < EventContainers::hashPair<T> >&>
224  (m_link->getAllHashPtrPair());
225  }
226 
231  virtual std::vector<IdentifierHash> GetAllCurrentHashes() const override final {
232  return IdentifiableContainerBase::GetAllCurrentHashes();
233  }
234 
237  return const_iterator(m_link->cbegin());
238  }
239 
240 
242  const_iterator end() const {
243  return const_iterator(m_link->cend());
244  }
245 
247  bool empty() const {
248  return numberOfCollections()==0;
249  }
250 
252  {
253  IDC_WriteHandle lock;
254  lock.m_hashId = hash;
255  lock.m_IDC_ptr = this;
256  return lock;
257  }
258 };
259 
260 template < class T>
261 T* //Please don't do this we want to get rid of this
263 {
264  return reinterpret_cast<T*>(m_link->removeCollection(hashId));
265 }
266 
267 
268 
269 // Constructor for OFFLINE style IDC
270 template < class T>
271 IdentifiableContainerMT<T>::IdentifiableContainerMT(IdentifierHash maxHash) : IdentifiableContainerBase(maxHash)
272 {
273 }
274 
275 template < class T>
277  IdentifiableContainerBase(maxHash, mode)
278 {
279 }
280 
281 // Constructor for ONLINE style IDC
282 template < class T>
283 IdentifiableContainerMT<T>::IdentifiableContainerMT(ICACHE *cache) : IdentifiableContainerBase(cache)
284 {
285 }
286 
290 template < class T>
291 const T*
293 {
294  return reinterpret_cast<const T* > (IdentifiableContainerBase::indexFindPtr(hashId));
295 }
296 
297 // insert collection into container with id hash
298 template < class T>
301 {
302  // update m_hashids
303  if (ATH_UNLIKELY(! IdentifiableContainerBase::insert(hashId, coll))) return StatusCode::FAILURE;
304  return StatusCode::SUCCESS;
305 
306 }
307 
308 
309 
310 template < class T>
311 void
313 {
314  IdentifiableContainerBase::cleanup(void_unique_ptr::Deleter<T>::deleter);
315 }
316 
317 template < class T>
320 {
321  return IdentifiableContainerBase::fetchOrCreate(hashId);
322 }
323 
324 template < class T>
326 IdentifiableContainerMT<T>::fetchOrCreate(const std::vector<IdentifierHash> &hashIds)
327 {
328  return IdentifiableContainerBase::fetchOrCreate(hashIds);
329 }
330 
331 
332 template < class T>
333 bool
335 {
336  return IdentifiableContainerBase::tryAddFromCache(hashId);
337 }
338 
339 template < class T >
340 StatusCode
341 IdentifiableContainerMT<T>::naughtyRetrieve(IdentifierHash hashId, T* &collToRetrieve) const
342 {
343  if(ATH_UNLIKELY(m_OnlineMode)) return StatusCode::FAILURE;//NEVER ALLOW FOR EXTERNAL CACHE
344  else {
345  auto p = reinterpret_cast<const T* > (m_link->findIndexPtr(hashId));//collToRetrieve can be null on success
346  collToRetrieve = const_cast<T*>(p);
347  return StatusCode::SUCCESS;
348  }
349 }
350 
351 template < class T>
354 {
355  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
356  auto ptr = uptr.release();
357  bool b = IdentifiableContainerBase::insert(hashId, ptr);
358  if(!b) delete ptr;
359  return StatusCode::SUCCESS;
360 }
361 
362 template < class T>
364 IdentifiableContainerMT<T>::addOrDelete(std::unique_ptr<const T> uptr, IdentifierHash hashId)
365 {
366  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
367  auto ptr = uptr.release();
368  bool b = IdentifiableContainerBase::insert(hashId, ptr);
369  if(!b) delete ptr;
370  return StatusCode::SUCCESS;
371 }
372 
373 template < class T>
376 {
377  return m_link->addLock(hashId, ptr.release());
378 }
379 
380 template < class T>
382 IdentifiableContainerMT<T>::addOrDelete(std::unique_ptr<T> uptr, IdentifierHash hashId, bool &deleted)
383 {
384  if(ATH_UNLIKELY(hashId >= m_link->fullSize())) return StatusCode::FAILURE;
385  auto ptr = uptr.release();
386  bool b = IdentifiableContainerBase::insert(hashId, ptr);
387  if(!b) delete ptr;
388  deleted = !b;
389  return StatusCode::SUCCESS;
390 }
391 
392 #endif
393 
IdentifiableContainerMT::IDC_WriteHandle::operator=
IDC_WriteHandle & operator=(const IDC_WriteHandle &other)=delete
IdentifiableContainerMT::~IdentifiableContainerMT
~IdentifiableContainerMT()
Definition: IdentifiableContainerMT.h:153
IdentifiableContainerMT::IDC_WriteHandle::m_hashId
IdentifierHash m_hashId
Definition: IdentifiableContainerMT.h:37
IdentifiableContainerMT::base_value_type
T base_value_type
Definition: IdentifiableContainerMT.h:78
IdentifiableContainerMT::IDC_WriteHandle::m_IDC_ptr
IdentifiableContainerMT< T > * m_IDC_ptr
Definition: IdentifiableContainerMT.h:36
IdentifiableContainerMT::addCollection
virtual StatusCode addCollection(const T *coll, IdentifierHash hashId) override final
insert collection into container with id hash if IDC should not take ownership of collection,...
Definition: IdentifiableContainerMT.h:300
IdentifiableContainerMT::getWriteHandle
IDC_WriteHandle getWriteHandle(IdentifierHash hash)
Definition: IdentifiableContainerMT.h:251
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator()
iterator constructor
Definition: IdentifiableContainerMT.h:87
IdentifiableCache.h
IdentifiableContainerMT::IDC_WriteHandle::operator=
IDC_WriteHandle & operator=(IDC_WriteHandle &&other) noexcept=delete
IdentifiableContainerMT::const_iterator::operator==
bool operator==(const const_iterator &it) const
comparison operator
Definition: IdentifiableContainerMT.h:124
EventContainers::IDC_WriteHandleBase::IDC_WriteHandleBase
IDC_WriteHandleBase()
Definition: IDC_WriteHandleBase.h:34
IdentifiableContainerMT::reference
T *& reference
Definition: IdentifiableContainerMT.h:73
IdentifiableContainerMT::IDC_WriteHandle::OnlineAndPresentInAnotherView
bool OnlineAndPresentInAnotherView()
This method is to avoid calling an expensive operation in the offline case.
Definition: IdentifiableContainerMT.h:64
IIdentifiableCont.h
skel.it
it
Definition: skel.GENtoEVGEN.py:396
IdentifiableContainerMT::size
size_t size() const
Duplicate of fullSize for backwards compatability.
Definition: IdentifiableContainerMT.h:209
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(const const_iterator &)=default
IdentifiableContainerMT::size_type
std::vector< std::atomic< const void * > >::size_type size_type
Definition: IdentifiableContainerMT.h:72
IdentifiableContainerMT::IDENTIFIABLE
T IDENTIFIABLE
Definition: IdentifiableContainerMT.h:69
athena.value
value
Definition: athena.py:124
IdentifiableContainerMT::MyType
IdentifiableContainerMT< T > MyType
Definition: IdentifiableContainerMT.h:71
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
IdentifiableContainerMT::IDC_WriteHandle::alreadyPresent
bool alreadyPresent()
Definition: IdentifiableContainerMT.h:62
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:560
EventContainers::hashPair
Definition: I_InternalIDC.h:25
IdentifiableContainerMT::const_iterator::operator!=
bool operator!=(const const_iterator &it) const
comparison operator
Definition: IdentifiableContainerMT.h:119
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(EventContainers::I_InternalIDC::InternalConstItr itr)
Definition: IdentifiableContainerMT.h:137
void_unique_ptr::Deleter
Definition: deleter.h:22
IdentifiableContainerMT::value_type
T * value_type
Definition: IdentifiableContainerMT.h:75
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
EventContainers::IIdentifiableCont
Definition: IIdentifiableCont.h:13
IdentifiableContainerMT::empty
bool empty() const
return true if container is empty
Definition: IdentifiableContainerMT.h:247
IdentifiableContainerMT::const_iterator::operator=
const_iterator & operator=(const const_iterator &)=default
AthUnlikelyMacros.h
IdentifiableContainerMT::hasExternalCache
virtual bool hasExternalCache() const override final
Definition: IdentifiableContainerMT.h:155
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle(IDC_WriteHandle &&other)
Definition: IdentifiableContainerMT.h:52
IdentifiableContainerMT::ICACHE
EventContainers::IdentifiableCache< T > ICACHE
Definition: IdentifiableContainerMT.h:70
IdentifiableContainerMT::prepareItr
void prepareItr() const
Definition: IdentifiableContainerMT.h:213
EventContainers::IdentifiableContainerBase::m_link
std::unique_ptr< I_InternalIDC > m_link
Definition: IdentifiableContainerBase.h:26
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
IdentifiableContainerMT::numberOfCollections
virtual size_t numberOfCollections() const override final
return number of collections
Definition: IdentifiableContainerMT.h:216
IdentifiableContainerMT::const_iterator::operator*
const T * operator*() const
Definition: IdentifiableContainerMT.h:112
IdentifiableContainerMT::GetAllCurrentHashes
virtual std::vector< IdentifierHash > GetAllCurrentHashes() const override final
Returns a collection of all hashes availiable in this IDC.
Definition: IdentifiableContainerMT.h:231
IdentifiableContainerMT::const_pointer
T *const * const_pointer
Definition: IdentifiableContainerMT.h:77
IdentifiableContainerMT::addOrDelete
virtual StatusCode addOrDelete(std::unique_ptr< T >, IdentifierHash hashId) override final
Tries to add the item to the cache, if the item already exists then it is deleted This is a convenien...
Definition: IdentifiableContainerMT.h:353
IdentifiableContainerMT::addLock
StatusCode addLock(std::unique_ptr< T > ptr, IdentifierHash hashId)
Like the other add methods but optimized for changing from the inprogress state.
Definition: IdentifiableContainerMT.h:375
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IdentifiableContainerMT::fullSize
virtual size_t fullSize() const override final
return full size of container
Definition: IdentifiableContainerMT.h:204
IdentifiableContainerMT::tryAddFromCache
virtual bool tryAddFromCache(IdentifierHash hashId) override final
Looks in the cache to see if item already exists if not it returns false, If it does exist it incorpo...
Definition: IdentifiableContainerMT.h:334
IdentifiableContainerMT::indexFind
const_iterator indexFind(IdentifierHash hashId) const
Definition: IdentifiableContainerMT.h:163
IdentifiableContainerMT::IDC_WriteHandle
Definition: IdentifiableContainerMT.h:34
IDC_WriteHandleBase.h
IdentifiableContainerMT::end
const_iterator end() const
return const_iterator for end of container
Definition: IdentifiableContainerMT.h:242
Preparation.mode
mode
Definition: Preparation.py:94
IdentifiableContainerMT::removeCollection
T * removeCollection(IdentifierHash hashId)
remove collection from container for id hash, returning it (and ownership) to client
Definition: IdentifiableContainerMT.h:262
IdentifiableContainerMT::const_iterator
Definition: IdentifiableContainerMT.h:82
IdentifiableContainerMT::begin
const_iterator begin() const
return const_iterator for first entry
Definition: IdentifiableContainerMT.h:236
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
EventContainers::IdentifiableContainerBase
Definition: IdentifiableContainerBase.h:14
IdentifiableContainerMT::const_iterator::hashId
IdentifierHash hashId() const
hashId of the pointed-to element
Definition: IdentifiableContainerMT.h:130
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
EventContainers::Mode
Mode
Definition: IdentifiableContainerBase.h:13
IdentifiableContainerMT::const_iterator::cptr
const T * cptr() const
Definition: IdentifiableContainerMT.h:108
IdentifiableContainerMT::const_reference
T *const & const_reference
Definition: IdentifiableContainerMT.h:74
IdentifiableContainerMT::IDC_WriteHandle::addOrDelete
StatusCode addOrDelete(std::unique_ptr< T > ptr)
Definition: IdentifiableContainerMT.h:56
IdentifiableContainerMT::indexFindPtr
virtual const T * indexFindPtr(IdentifierHash hashId) const override final
return pointer on the found entry or null if out of range using hashed index - fast version,...
Definition: IdentifiableContainerMT.h:292
IdentifiableContainerMT::pointer
T ** pointer
Definition: IdentifiableContainerMT.h:76
EventContainers::IIdentifiableCont::naughtyRetrieve
virtual StatusCode naughtyRetrieve(IdentifierHash hashId, T *&collToRetrieve) const =0
a
TList * a
Definition: liststreamerinfos.cxx:10
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
IdentifiableContainerMT::const_iterator::operator->
const T * operator->() const
Definition: IdentifiableContainerMT.h:116
IdentifiableContainerMT::fetchOrCreate
StatusCode fetchOrCreate(IdentifierHash hashId)
Tries will look for item in cache, if it doesn't exist will call the cache IMAKER If cache doesn't ha...
Definition: IdentifiableContainerMT.h:319
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle(const IDC_WriteHandle &other)=delete
IdentifiableContainerMT::IDC_WriteHandle::Swap
static void Swap(IDC_WriteHandle &a, IDC_WriteHandle &b) noexcept
Definition: IdentifiableContainerMT.h:43
IdentifiableContainerMT::GetAllHashPtrPair
const std::vector< EventContainers::hashPair< T > > & GetAllHashPtrPair() const
Definition: IdentifiableContainerMT.h:220
EventContainers::IDC_WriteHandleBase
Definition: IDC_WriteHandleBase.h:27
IdentifiableContainerMT::const_iterator::m_itr
EventContainers::I_InternalIDC::InternalConstItr m_itr
Definition: IdentifiableContainerMT.h:140
IdentifiableContainerMT::IDC_WriteHandle::IDC_WriteHandle
IDC_WriteHandle()
Definition: IdentifiableContainerMT.h:39
IdentifiableContainerMT::addOrDelete
virtual StatusCode addOrDelete(std::unique_ptr< const T >, IdentifierHash hashId) override final
EventContainers::IdentifiableContainerBase::m_OnlineMode
bool m_OnlineMode
Definition: IdentifiableContainerBase.h:24
IdentifiableContainerMT
Definition: IdentifiableContainerMT.h:30
IdentifiableContainerMT::ATLAS_NOT_THREAD_SAFE
virtual StatusCode naughtyRetrieve ATLAS_NOT_THREAD_SAFE(IdentifierHash hashId, T *&collToRetrieve) const override final
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
IdentifiableContainerBase.h
IdentifiableContainerMT::IdentifiableContainerMT
IdentifiableContainerMT(IdentifierHash hashMax)
constructor initializes the collection the hashmax, OFFLINE usages pattern
Definition: IdentifiableContainerMT.h:271
IdentifiableContainerMT::const_iterator::operator++
const_iterator & operator++()
increment operator
Definition: IdentifiableContainerMT.h:96
IdentifiableContainerMT::const_iterator::const_iterator
const_iterator(const_iterator &&)=default
EventContainers::I_InternalIDC::InternalConstItr
std::vector< hashPair >::const_iterator InternalConstItr
Definition: I_InternalIDC.h:37
IdentifiableContainerMT::cleanup
virtual void cleanup() override final
reset m_hashids and call IdentifiableCache's cleanup
Definition: IdentifiableContainerMT.h:312
EventContainers::IdentifiableCache
Definition: IdentifiableCache.h:29
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
IdentifiableContainerMT::const_iterator::~const_iterator
~const_iterator()=default