|
ATLAS Offline Software
|
Go to the documentation of this file.
14 #ifndef CXXUTILS_CONCURRENTHASHMAPIMPL_H
15 #define CXXUTILS_CONCURRENTHASHMAPIMPL_H
30 class ConcurrentHashmapImplTest;
53 concept IsConcurrentHashmapPayload = std::is_standard_layout_v<T> &&
54 std::is_trivial_v<T> &&
66 :
public std::unique_lock<std::mutex>
69 using std::unique_lock<std::mutex>::unique_lock;
91 template <
unsigned ENTRIES_PER_CACHELINE>
202 template <
template <
class>
class UPDATER_,
203 typename HASHER_ = std::hash<uintptr_t>,
204 typename MATCHER_ = std::equal_to<uintptr_t>,
205 uintptr_t NULLVAL_ = 0,
206 uintptr_t TOMBSTONE_ = NULLVAL_>
217 static constexpr uintptr_t
nullval = NULLVAL_;
221 static constexpr
size_t INVALID =
static_cast<size_t>(-1);
240 static_assert (
CACHELINE >=
sizeof (entry_t) &&
249 friend class ::ConcurrentHashmapImplTest;
285 static void*
operator new (size_t,
size_t capacity);
291 void operator delete (
void*
p);
379 const typename Updater_t::Context_t& ctx);
517 std::pair<const_iterator, bool>
520 const typename Updater_t::Context_t& ctx);
537 std::pair<const_iterator, bool>
541 const typename Updater_t::Context_t& ctx);
610 const_iterator
end()
const;
621 const typename Updater_t::Context_t& ctx);
630 const_iterator
clear (
const typename Updater_t::Context_t& ctx);
652 const typename Updater_t::Context_t& ctx);
660 void quiescent (
const typename Updater_t::Context_t& ctx);
700 bool grow (
const Lock_t&
lock,
size_t new_capacity,
const typename Updater_t::Context_t& ctx);
732 #endif // not CXXUTILS_CONCURRENTHASHMAPIMPL_H
const Matcher_t & matcher() const
Return the matcher object.
Table * m_table
The current table instance. Must be holding the mutex to access this.
const_iterator clear(size_t capacity, const typename Updater_t::Context_t &ctx)
Erase the table and change the capacity.
ConcurrentHashmapImpl & operator=(const ConcurrentHashmapImpl &)=delete
static constexpr uintptr_t tombstone
Tombstone key value. Must be different from nullval to allow erasures.
const size_t m_stride
Base increment between probes.
Helper to generate hash probes.
std::atomic< val_t > m_key
One entry in the hash table.
uintptr_t ConcurrentHashmapVal_t
Type used for keys and values — an unsigned big enough to hold a pointer.
void swap(ConcurrentHashmapImpl &other)
Swap this container with another.
size_t capacity() const
The number of entries in the table.
size_t probeWrite(val_t key, size_t hash, bool &insert)
Find a table entry for writing.
size_t m_nprobes
Number of probes tried so far.
bool erase(const Lock_t &lock, val_t key, size_t hash)
Erase an entry from the table, with external locking.
const entry_t & entry(size_t offset) const
Return the entry for an offset.
size_t m_bucket
Index of the start of the cacheline currently being probed.
Table(size_t capacity, const Hasher_t &hasher=Hasher_t(), const Matcher_t &matcher=Matcher_t())
Constructor.
std::pair< const_iterator, bool > put(val_t key, size_t hash, val_t val, bool overwrite, const typename Updater_t::Context_t &ctx)
Add an entry to the table.
size_t erased() const
The number of erased elements in the current table.
void prev()
Move the iterator back to the previous occupied entry.
size_t probeRead(val_t key, size_t hash) const
Find a table entry for reading.
static uint64_t round_up(uint64_t)
CHMTableIterator(size_t hash, size_t mask, size_t maskBits, size_t probeLimit)
Constructor.
Updater_t m_updater
Updater object managing memory. See above.
void next()
Advance the iterator to the next occupied entry.
void quiescent(const typename Updater_t::Context_t &ctx)
Called when this thread is no longer referencing anything from this container.
Hash table allowing concurrent, lockless reads.
Atomic min/max functions.
const Matcher_t & m_matcher
The key match object.
static constexpr size_t CACHELINE
Assumed length in bytes of one cache line.
void forceClear()
Erase the table by filling it with nulls.
ConcurrentHashmapVal_t val_t
Type used for keys and values — an unsigned big enough to hold a pointer.
size_t capacity() const
Return the current table size.
static constexpr uintptr_t nullval
Null key value.
typename Updater_t::Context_t Context_t
Context type for the updater.
val_t key() const
Return the key for this iterator.
size_t m_offset
The current position in the table.
bool operator!=(const DataVector< T > &a, const DataVector< T > &b)
Based on operator==.
bool grow(const Lock_t &lock, const typename Updater_t::Context_t &ctx)
Make the table larger.
const_iterator_range range() const
Return a range that can be used to iterate over the container.
static constexpr size_t ENTRIES_PER_CACHELINE
Number of entries in one cache line.
size_t size() const
Return the number of items currently stored.
std::atomic< val_t > m_val
ConcurrentHashmapImpl(const ConcurrentHashmapImpl &)=delete
const_iterator(const Table &table, bool end)
Constructor.
ConcurrentHashmapImpl(Updater_t &&updater, size_t capacity_in, const Hasher_t &hasher, const Matcher_t &matcher, const typename Updater_t::Context_t &ctx)
Constructor.
const size_t m_mask
Mask for table indices (capacity-1).
Lock_t lock()
Take a lock on the container.
const_iterator get(val_t key, size_t hash) const
Look up an entry in the table.
const size_t m_boffs
Offset of the first entry we probe within its cacheline.
bool erase(val_t key, size_t hash)
Erase an entry from the table.
Bidirectional iterator over occupied table entries.
std::atomic< size_t > m_size
Number of entries in the map.
const size_t m_maskBits
Number of bits in the mask.
entry_t & entry(size_t offset)
Return the entry for an offset (non-const).
const Hasher_t m_hasher
The hash object.
const Matcher_t m_matcher
The key match object.
bool next()
Move to the next probe.
std::atomic< size_t > m_erased
Number of entries that have been erased.
const Table & m_table
The table over which we're iterating.
const size_t m_capacity
Number of entries in the table. Must be a power of 2.
static constexpr size_t ENTRIES_PER_CACHELINE_MASK
Mask for the within-cacheline part of indices.
bool grow(const Lock_t &lock, size_t new_capacity, const typename Updater_t::Context_t &ctx)
Make the table larger.
bool valid() const
Check that the iterator is valid (not pointing at the end).
size_t offset() const
Offset of the element currently being probed.
Compatibility helpers for using some pieces of C++20 concepts with older compilers.
const_iterator(const Table &table, size_t offset)
Constructor.
static constexpr size_t ENTRIES_PER_CACHELINE_MASK
Mask for the cache line part of an index.
const_iterator begin() const
A begin iterator for the container.
const size_t m_mask
Mask for the table; i.e., capacity-1, but with the low bits corresponding to ENTRIES_PER_CACHELINE al...
Helper to allow for external locking with put().
static constexpr size_t INVALID
Used to represent an invalid table index.
std::pair< const_iterator, const_iterator > const_iterator_range
Two iterators defining a range.
const_iterator end() const
An end iterator for the container.
std::pair< const_iterator, bool > put(const Lock_t &lock, val_t key, size_t hash, val_t val, bool overwrite, const typename Updater_t::Context_t &ctx)
Add an entry to the table, with external locking.
std::atomic< size_t > m_longestProbe
Longest probe needed so far.
val_t value() const
Return the value for this iterator.
Hasher Hasher_t
Hash object.
Matcher Matcher_t
Key match object.
const size_t m_maxProbe
Maximum number of probes allowed before resizing the table.
const Hasher_t & m_hasher
The hash object.
Updater_t & updater()
Access the Updater instance.
const_iterator clear(const typename Updater_t::Context_t &ctx)
Erase the table (don't change the capacity).
std::mutex m_mutex
Mutex to serialize changes to the map.
size_t nprobes() const
Return the number of probes performed so far.
const size_t m_probeLimit
Maximum number of probes to try.
void reserve(size_t capacity, const typename Updater_t::Context_t &ctx)
Increase the table capacity.
const Hasher_t & hasher() const
Return the hasher object.