diff --git a/AsyncKeyedLock/AsyncKeyedLock.csproj b/AsyncKeyedLock/AsyncKeyedLock.csproj index ee7ccd3..25f638a 100644 --- a/AsyncKeyedLock/AsyncKeyedLock.csproj +++ b/AsyncKeyedLock/AsyncKeyedLock.csproj @@ -8,7 +8,7 @@ https://github.com/MarkCiliaVincenti/AsyncKeyedLock MIT MIT - 6.0.4-rc5 + 6.0.4-rc6 logo.png Optimizations and fixing of more rare race conditions. An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), limiting concurrent threads sharing the same key to a specified number. diff --git a/AsyncKeyedLock/AsyncKeyedLockDictionary.cs b/AsyncKeyedLock/AsyncKeyedLockDictionary.cs index 4df33a4..122ae03 100644 --- a/AsyncKeyedLock/AsyncKeyedLockDictionary.cs +++ b/AsyncKeyedLock/AsyncKeyedLockDictionary.cs @@ -82,7 +82,7 @@ public AsyncKeyedLockReleaser GetOrAdd(TKey key) { if (PoolingEnabled) { - if (TryGetValue(key, out var releaser) && releaser.TryIncrement()) + if (TryGetValue(key, out var releaser) && releaser.TryIncrement(key)) { return releaser; } @@ -100,7 +100,7 @@ public AsyncKeyedLockReleaser GetOrAdd(TKey key) { return releaser; } - if (releaser.TryIncrement()) + if (releaser.TryIncrement(key)) { releaser.IsPooled = true; _pool.PutObject(releaserToAdd); diff --git a/AsyncKeyedLock/AsyncKeyedLockReleaser.cs b/AsyncKeyedLock/AsyncKeyedLockReleaser.cs index 281443d..6917b43 100644 --- a/AsyncKeyedLock/AsyncKeyedLockReleaser.cs +++ b/AsyncKeyedLock/AsyncKeyedLockReleaser.cs @@ -50,11 +50,11 @@ internal AsyncKeyedLockReleaser(TKey key, SemaphoreSlim semaphoreSlim, AsyncKeye } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal bool TryIncrement() + internal bool TryIncrement(TKey key) { if (Monitor.TryEnter(this)) { - if (IsPooled) // rare race condition + if (IsPooled || !_key.Equals(key)) // rare race condition { Monitor.Exit(this); return false;