ATLAS Offline Software
IdentifiableCacheBase.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // $Id: IdentifiableCacheBase.h 791541 2017-01-09 10:43:53Z smh $
8 
9 
10 
11 #ifndef EVENTCONTAINERS_IDENTIFIABLECACHEBASE_H
12 #define EVENTCONTAINERS_IDENTIFIABLECACHEBASE_H
13 
14 
16 #include <mutex>
17 #include <atomic>
19 
20 
21 namespace EventContainers {
22 
23 
24 
26 {
27 
28 public:
29 //here for access from other classes
30 static constexpr uintptr_t INVALIDflag = UINTPTR_MAX;
31 static constexpr uintptr_t ABORTEDflag = UINTPTR_MAX-1;
32 
33 #ifndef __cpp_lib_atomic_wait
34 //Buckets are not necessary once we have atomic waiting
35 //This code can be removed when C++20(+) is firmly adopted.
36 static constexpr size_t s_defaultBucketSize =2;
37 #endif
38 
39 typedef std::true_type thread_safe;
40 
42 
43  struct IMaker
44  {
45  bool m_IsReEntrant = false;
46  virtual ~IMaker() {}
47  virtual void_unique_ptr typelessMake (IdentifierHash hash) const = 0; // unique_ptr<T>??
48  };
49 
51  const void* find (IdentifierHash hash) noexcept;
53  const void* findWait (IdentifierHash hash);
54 
56  const void* get (IdentifierHash hash);
57 
59  std::vector<IdentifierHash> ids();
60 
61  std::pair<bool, const void*> add (IdentifierHash hash, const void* p) noexcept;
62 
63  // addLock is same as method above except we check for invalid state first,
64  // more optimal for calling using writehandle lock method
65  std::pair<bool, const void*> addLock (IdentifierHash hash, const void* p) noexcept;
66  std::pair<bool, const void*> addLock (IdentifierHash hash, void_unique_ptr p) noexcept;
67  std::pair<bool, const void*> add (IdentifierHash hash, void_unique_ptr p) noexcept;
68 
69  bool IMakerPresent() const { return m_maker!=nullptr; }
70 
75  int tryLock(IdentifierHash, IDC_WriteHandleBase &, std::vector<IdentifierHash>&);
76 
79 
82 
84  const void* waitFor(IdentifierHash);
85 
87  void createSet (const std::vector<IdentifierHash>& hashes, std::vector<bool> &mask);
88 
89  size_t fullSize() const { return m_vec.size(); }
91  size_t numberOfHashes();
92 
93 protected:
94 #ifndef __cpp_lib_atomic_wait
95  IdentifiableCacheBase (IdentifierHash maxHash, const IMaker* maker, size_t lockBucketSize);
96 #endif
97  IdentifiableCacheBase (IdentifierHash maxHash, const IMaker* maker);
99  void clear (deleter_f* deleter);
100  void cleanUp(deleter_f* deleter);//Call once before destruction
102 private:
103  std::vector<std::atomic<const void*> > m_vec;
104  friend class InternalOnline;
105  const IMaker* m_maker;
106 
108  typedef std::scoped_lock<mutex_t> lock_t;
109  typedef std::unique_lock<mutex_t> uniqueLock;
111 #ifndef __cpp_lib_atomic_wait
112  std::unique_ptr<mutexPair[]> m_HoldingMutexes;
114  size_t m_NMutexes;
115 #endif
116  std::atomic<size_t> m_currentHashes;
118 };
119 
120 
121 } // namespace EventContainers
122 
123 
124 #endif // not EVENTCONTAINERS_IDENTIFIABLECACHEBASE_H
python.root_lsr_rank.hashes
hashes
Definition: root_lsr_rank.py:34
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
EventContainers::InternalOnline
Definition: InternalOnline.h:26
EventContainers::IdentifiableCacheBase
Definition: IdentifiableCacheBase.h:26
EventContainers::IdentifiableCacheBase::get
const void * get(IdentifierHash hash)
Try to make payload if not there.
Definition: IdentifiableCacheBase.cxx:179
EventContainers::IdentifiableCacheBase::ABORTEDflag
static constexpr uintptr_t ABORTEDflag
Definition: IdentifiableCacheBase.h:31
EventContainers::IdentifiableCacheBase::IMaker::m_IsReEntrant
bool m_IsReEntrant
Definition: IdentifiableCacheBase.h:45
EventContainers::IdentifiableCacheBase::numberOfHashes
size_t numberOfHashes()
In a concurrent situation this number isn't necessarily perfectly synchronised with ids()....
Definition: IdentifiableCacheBase.cxx:228
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
EventContainers::IdentifiableCacheBase::s_defaultBucketSize
static constexpr size_t s_defaultBucketSize
Definition: IdentifiableCacheBase.h:36
EventContainers::IdentifiableCacheBase::m_mutex
mutex_t m_mutex
Definition: IdentifiableCacheBase.h:110
EventContainers::IdentifiableCacheBase::mutex_t
std::mutex mutex_t
Definition: IdentifiableCacheBase.h:107
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
EventContainers::IdentifiableCacheBase::m_NMutexes
size_t m_NMutexes
Definition: IdentifiableCacheBase.h:114
EventContainers
Definition: T_AthenaPoolCreateFuncs.h:33
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
EventContainers::IdentifiableCacheBase::~IdentifiableCacheBase
~IdentifiableCacheBase()
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
EventContainers::IdentifiableCacheBase::thread_safe
std::true_type thread_safe
Definition: IdentifiableCacheBase.h:39
deleter_f
void deleter_f(const void *p)
Definition: deleter.h:12
EventContainers::IdentifiableCacheBase::IdentifiableCacheBase
IdentifiableCacheBase(IdentifierHash maxHash, const IMaker *maker, size_t lockBucketSize)
Definition: IdentifiableCacheBase.cxx:35
IDC_WriteHandleBase.h
EventContainers::IdentifiableCacheBase::tryLock
int tryLock(IdentifierHash, IDC_WriteHandleBase &, std::vector< IdentifierHash > &)
Checks if the item is completed if it is not started it extablishes lock (returns 0),...
Definition: IdentifiableCacheBase.cxx:57
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::IMakerPresent
bool IMakerPresent() const
Definition: IdentifiableCacheBase.h:69
EventContainers::IdentifiableCacheBase::clear
void clear(deleter_f *deleter)
Definition: IdentifiableCacheBase.cxx:83
EventContainers::IdentifiableCacheBase::itemAborted
int itemAborted(IdentifierHash)
Returns 1 is the item has been aborted otherwise 0.
Definition: IdentifiableCacheBase.cxx:114
IdentifierHash.h
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
EventContainers::IdentifiableCacheBase::m_vec
std::vector< std::atomic< const void * > > m_vec
Definition: IdentifiableCacheBase.h:103
EventContainers::IdentifiableCacheBase::createSet
void createSet(const std::vector< IdentifierHash > &hashes, std::vector< bool > &mask)
Create a set of hashes, updates an IDC mask as appropriate.
Definition: IdentifiableCacheBase.cxx:219
void_unique_ptr
Definition: deleter.h:16
EventContainers::IdentifiableCacheBase::IMaker::typelessMake
virtual void_unique_ptr typelessMake(IdentifierHash hash) const =0
EventContainers::IdentifiableCacheBase::itemInProgress
int itemInProgress(IdentifierHash)
Returns 1 is the item is inprogress otherwise 0.
Definition: IdentifiableCacheBase.cxx:120
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::waitFor
const void * waitFor(IdentifierHash)
Halts the thread until the require hash is completed or aborted.
Definition: IdentifiableCacheBase.cxx:135
EventContainers::IdentifiableCacheBase::add
std::pair< bool, const void * > add(IdentifierHash hash, const void *p) noexcept
Definition: IdentifiableCacheBase.cxx:247
EventContainers::IdentifiableCacheBase::ids
std::vector< IdentifierHash > ids()
In a threaded situation this collection will be valid but will not container hashes later added.
Definition: IdentifiableCacheBase.cxx:233
EventContainers::IDC_WriteHandleBase
Definition: IDC_WriteHandleBase.h:27
EventContainers::IdentifiableCacheBase::IMaker
Definition: IdentifiableCacheBase.h:44
deleter.h
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::IMaker::~IMaker
virtual ~IMaker()
Definition: IdentifiableCacheBase.h:46
EventContainers::IdentifiableCacheBase::INVALIDflag
static constexpr uintptr_t INVALIDflag
Definition: IdentifiableCacheBase.h:30
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