diff options
author | tarasovalexey <tarasovalexey@yandex-team.com> | 2024-11-08 15:31:32 +0300 |
---|---|---|
committer | tarasovalexey <tarasovalexey@yandex-team.com> | 2024-11-08 15:47:53 +0300 |
commit | a296f771628eff568d2ecc7f3e86d378cb7ca517 (patch) | |
tree | df93095beaaec8f50f318862c47144de70d23861 | |
parent | 7e027f21b3b681b401a99f046a8e4d64aec5de0e (diff) | |
download | ydb-a296f771628eff568d2ecc7f3e86d378cb7ca517.tar.gz |
Support customization of TLockOps for TGuard in TConcurrentHashMap
commit_hash:ea73897466c683c4f852d5c9b6eebee419e6b8ab
-rw-r--r-- | library/cpp/containers/concurrent_hash/concurrent_hash.h | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/library/cpp/containers/concurrent_hash/concurrent_hash.h b/library/cpp/containers/concurrent_hash/concurrent_hash.h index 88da30fd78..74573ecc09 100644 --- a/library/cpp/containers/concurrent_hash/concurrent_hash.h +++ b/library/cpp/containers/concurrent_hash/concurrent_hash.h @@ -12,11 +12,12 @@ namespace NPrivate { }; } -template <typename K, typename V, size_t BucketCount = 64, typename L = TAdaptiveLock> +template <typename K, typename V, size_t BucketCount = 64, typename L = TAdaptiveLock, class TLockOps = TCommonLockOps<L>> class TConcurrentHashMap { public: using TActualMap = THashMap<K, V>; using TLock = L; + using TBucketGuard = TGuard<TLock, TLockOps>; struct TBucket { friend class TConcurrentHashMap; @@ -88,13 +89,13 @@ public: void Insert(const K& key, const V& value) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); bucket.Map[key] = value; } void InsertUnique(const K& key, const V& value) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); if (!bucket.Map.insert(std::make_pair(key, value)).second) { Y_ABORT("non-unique key"); } @@ -102,14 +103,14 @@ public: V& InsertIfAbsent(const K& key, const V& value) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); return bucket.Map.insert(std::make_pair(key, value)).first->second; } template <typename TKey, typename... Args> V& EmplaceIfAbsent(TKey&& key, Args&&... args) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); if (V* value = bucket.TryGetUnsafe(key)) { return *value; } @@ -123,7 +124,7 @@ public: template <typename Callable> V& InsertIfAbsentWithInit(const K& key, Callable initFunc) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); if (V* value = bucket.TryGetUnsafe(key)) { return *value; } @@ -133,13 +134,13 @@ public: V Get(const K& key) const { const TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); return bucket.GetUnsafe(key); } bool Get(const K& key, V& result) const { const TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); if (const V* value = bucket.TryGetUnsafe(key)) { result = *value; return true; @@ -149,13 +150,13 @@ public: V Remove(const K& key) { TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); return bucket.RemoveUnsafe(key); } bool Has(const K& key) const { const TBucket& bucket = GetBucketForKey(key); - TGuard<TLock> guard(bucket.Mutex); + TBucketGuard guard(bucket.Mutex); return bucket.HasUnsafe(key); } }; |