From 64f70e26ac0b61d89683746c912f522d7cb02f78 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Tue, 18 Oct 2022 11:37:57 +0200 Subject: [PATCH] ForceRelease now releases everything in the semaphore before removing the key from the dictionary. --- AsyncKeyedLock.Tests/AsyncKeyedLock.Tests.csproj | 4 ++-- AsyncKeyedLock/AsyncKeyedLock.csproj | 8 ++++---- AsyncKeyedLock/AsyncKeyedLocker.cs | 7 ++++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/AsyncKeyedLock.Tests/AsyncKeyedLock.Tests.csproj b/AsyncKeyedLock.Tests/AsyncKeyedLock.Tests.csproj index 8de83ee..27f5886 100644 --- a/AsyncKeyedLock.Tests/AsyncKeyedLock.Tests.csproj +++ b/AsyncKeyedLock.Tests/AsyncKeyedLock.Tests.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -9,7 +9,7 @@ - + diff --git a/AsyncKeyedLock/AsyncKeyedLock.csproj b/AsyncKeyedLock/AsyncKeyedLock.csproj index a15a0cf..0e75c69 100644 --- a/AsyncKeyedLock/AsyncKeyedLock.csproj +++ b/AsyncKeyedLock/AsyncKeyedLock.csproj @@ -6,16 +6,16 @@ https://github.com/MarkCiliaVincenti/AsyncKeyedLock.git https://github.com/MarkCiliaVincenti/AsyncKeyedLock LICENSE - 3.0.0 + 3.0.1 logo.png - Fixed serious issue with concurrent threads having been limited to 1 irrespective of setting in the constructor. Also added a few features and tests. + ForceRelease now releases everything in the semaphore before removing the key from the dictionary. An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), only allowing a defined number of concurrent threads that share the same key. © 2022 Mark Cilia Vincenti async,lock,key,semaphore,dictionary git false - 3.0.0.0 - 3.0.0.0 + 3.0.1.0 + 3.0.1.0 README.md true true diff --git a/AsyncKeyedLock/AsyncKeyedLocker.cs b/AsyncKeyedLock/AsyncKeyedLocker.cs index 90ce07f..453ab49 100644 --- a/AsyncKeyedLock/AsyncKeyedLocker.cs +++ b/AsyncKeyedLock/AsyncKeyedLocker.cs @@ -128,7 +128,12 @@ public bool ForceRelease(object key) { lock (SemaphoreSlims) { - return SemaphoreSlims.Remove(key); + if (SemaphoreSlims.TryGetValue(key, out var referenceCounter)) + { + referenceCounter.Value.Release(referenceCounter.ReferenceCount); + return SemaphoreSlims.Remove(key); + } + return false; } } }