|
| | ConcurrentHashMap (Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >, const Cmp &cmp=Cmp()) |
| | Construct an empty map with Shards independently-locked partitions.
|
| |
| | ConcurrentHashMap (const ConcurrentHashMap &)=delete |
| | Deleted copy constructor: shards own non-copyable std::shared_mutex instances.
|
| |
| ConcurrentHashMap & | operator= (const ConcurrentHashMap &)=delete |
| | Deleted copy assignment operator.
|
| |
| | ConcurrentHashMap (ConcurrentHashMap &&)=delete |
| | Deleted move constructor: no defined ordering exists to safely transfer locked shards out from under concurrent callers.
|
| |
| ConcurrentHashMap & | operator= (ConcurrentHashMap &&)=delete |
| | Deleted move assignment operator.
|
| |
| bool | insert (const Key &key, const T &value) |
| | Insert key with a copy of value, only if key is absent.
|
| |
| bool | insert (const Key &key, T &&value) |
| | Insert key with value moved in, only if key is absent.
|
| |
| void | insert_or_assign (const Key &key, T value) |
| | Insert key with value, or overwrite the existing value if key is already present.
|
| |
| bool | erase (const Key &key) |
| | Remove key if present.
|
| |
| bool | contains (const Key &key) const |
| | Check whether key is present.
|
| |
| std::optional< T > | find_copy (const Key &key) const |
| | Look up key and return a copy of its value, if present.
|
| |
| template<typename F > |
| bool | with_value (const Key &key, F &&f) const |
| | Invoke f with a const reference to key's value, while the owning shard is read-locked.
|
| |
| template<typename F > |
| bool | with_value_mut (const Key &key, F &&f) |
| | Invoke f with a mutable reference to key's value, while the owning shard is write-locked.
|
| |
| void | clear () |
| | Remove every entry from every shard.
|
| |
| size_t | size () const |
| | Return the total number of entries across all shards.
|
| |
| bool | is_empty () const |
| | Check whether every shard is currently empty.
|
| |
template<typename Key, typename
T, class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
class Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >
Sharded concurrent hash map: Shards independently-locked DynMapHashTable partitions, no single global lock.
See the file-level documentation in tpl_concurrent_hash_map.H for the full design rationale, thread-safety contract, and the specific consistency model of size()/snapshot().
- Template Parameters
-
| Key | Key type. Must satisfy whatever Cmp and the hash function require (typically equality-comparable and hashable). |
| T | Mapped value type. |
| Cmp | Key equality comparator, defaulting to Aleph::equal_to<Key>. |
| Shards | Number of independently-locked partitions. Must be at least 1; higher values reduce lock contention between keys that hash to different shards at the cost of Shards separate (smaller) hash tables and mutexes. |
Definition at line 153 of file tpl_concurrent_hash_map.H.
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Check whether key is present.
- Parameters
-
- Returns
true if present at the moment of the check.
- Exceptions
-
| std::system_error | if acquiring the shard's std::shared_mutex fails at the OS level; nothing otherwise. |
- Note
- Safe to call concurrently from any number of threads, including concurrently with other readers of the same shard. A concurrent insert/erase of the same key may change the answer immediately after this returns.
Definition at line 360 of file tpl_concurrent_hash_map.H.
References Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::contains(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for().
Referenced by TEST().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Remove key if present.
- Parameters
-
- Returns
true if key was present and removed, false otherwise.
- Exceptions
-
| std::system_error | if acquiring the shard's std::shared_mutex fails at the OS level; nothing else (unlinking a located entry does not move- or copy-construct T, only destroys it). |
- Note
- Safe to call concurrently from any number of threads. Blocks only other operations on the same shard.
Definition at line 331 of file tpl_concurrent_hash_map.H.
References Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::remove_by_data(), Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::search(), and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for().
Referenced by TEST().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Look up key and return a copy of its value, if present.
- Parameters
-
- Returns
- A copy of the value if
key is present, std::nullopt otherwise.
- Exceptions
-
| Whatever | T's copy constructor throws. Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level. |
- Note
- Safe to call concurrently from any number of threads, including concurrently with other readers of the same shard.
Definition at line 378 of file tpl_concurrent_hash_map.H.
References Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::search(), and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for().
Referenced by TEST().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Insert key with a copy of value, only if key is absent.
- Parameters
-
| [in] | key | Key to insert. |
| [in] | value | Value to copy in. |
- Returns
true if inserted, false if key was already present (the existing value is left untouched).
- Exceptions
-
| Whatever | Key's or T's copy constructor throws, or std::bad_alloc. If it throws, the map is left exactly as it was before the call. Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level. |
- Note
- Safe to call concurrently from any number of threads. Blocks only other operations on the same shard.
Definition at line 255 of file tpl_concurrent_hash_map.H.
References Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::insert(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for(), and value.
Referenced by TEST().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Insert key with value moved in, only if key is absent.
- Parameters
-
| [in] | key | Key to insert. |
| [in,out] | value | Value to move in. |
- Returns
true if inserted, false if key was already present.
- Exceptions
-
| Whatever | Key's copy constructor or T's move constructor throws, or std::bad_alloc. If it throws, the map is left exactly as it was before the call. Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level. |
- Warning
- On a
false return (duplicate key), value has still been moved from: the underlying table constructs the entry's Pair (moving value into it) before discovering the key already exists and discarding that entry. Do not read value after a false return; if you need to reuse it, check contains(key) first, or capture a copy before calling insert.
- Note
- Safe to call concurrently from any number of threads. Blocks only other operations on the same shard.
Definition at line 282 of file tpl_concurrent_hash_map.H.
References Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::insert(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for(), and value.
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Insert key with value, or overwrite the existing value if key is already present.
- Parameters
-
| [in] | key | Key to insert or update. |
| [in] | value | New value, moved into the map either as a fresh entry or via move-assignment over the existing one. |
- Exceptions
-
| Whatever | Key's copy constructor, T's move constructor, or T's move assignment throws, or std::bad_alloc. If updating an existing entry's move assignment throws, that entry is left in whatever state T::operator=(T&&) leaves a failed assignment in (the map itself remains structurally valid: the key is still present). Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level. |
- Note
- Safe to call concurrently from any number of threads. Blocks only other operations on the same shard.
Definition at line 306 of file tpl_concurrent_hash_map.H.
References Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::insert(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::search(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for(), and value.
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Resolve which shard owns key.
- Parameters
-
- Returns
- The shard whose lock and table must be used for
key.
- Precondition
hash_fct_ is a deterministic, pure function of key alone: calling it twice with an equal key must yield the same shard index, or a key can effectively "move" between shards between calls and become invisible to later lookups.
-
hash_fct_ must not throw: this function is noexcept but calls hash_fct_(key) directly, so a throwing hash function would call std::terminate() here instead of propagating.
Definition at line 198 of file tpl_concurrent_hash_map.H.
References Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::hash_fct_, and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shards_.
Referenced by Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::contains(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::erase(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::find_copy(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::insert(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::insert(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::insert_or_assign(), Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::with_value(), and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::with_value_mut().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Invoke f with a const reference to key's value, while the owning shard is read-locked.
- Template Parameters
-
| F | Callable, invocable as f(const T&), whose return value (if any) must not be a reference. |
- Parameters
-
| [in] | key | Key to look up. |
| [in] | f | Callback invoked with the value if key is present. |
- Returns
true if key was present and f was invoked, false otherwise.
- Exceptions
-
| Whatever | f throws, propagated after the lock is released. Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level (before f is ever invoked). |
- Note
- Safe to call concurrently from any number of threads, including concurrently with other readers of the same shard.
f runs with the shard locked: keep it short, and never call back into this same ConcurrentHashMap from within f (that would deadlock if it targets the same shard).
Definition at line 409 of file tpl_concurrent_hash_map.H.
References Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::search(), and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for().
template<typename Key , typename
T , class
Cmp = Aleph::equal_to<Key>, size_t Shards = 64>
Invoke f with a mutable reference to key's value, while the owning shard is write-locked.
- Template Parameters
-
| F | Callable, invocable as f(T&), whose return value (if any) must not be a reference. |
- Parameters
-
| [in] | key | Key to look up. |
| [in] | f | Callback invoked with the value if key is present; may mutate it in place. |
- Returns
true if key was present and f was invoked, false otherwise.
- Exceptions
-
| Whatever | f throws, propagated after the lock is released. Also throws std::system_error if acquiring the shard's std::shared_mutex fails at the OS level (before f is ever invoked). |
- Note
- Safe to call concurrently from any number of threads.
f runs with the shard exclusively locked: keep it short, and never call back into this same ConcurrentHashMap from within f.
Definition at line 442 of file tpl_concurrent_hash_map.H.
References Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::map, Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::Shard::mutex, Aleph::DynMapHashTable< Key, Data, HashTable, Cmp >::search(), and Aleph::ConcurrentHashMap< Key, T, Cmp, Shards >::shard_for().