Skip to content

Commit

Permalink
Minor optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Jan 2, 2023
1 parent 945e28e commit cd6d9f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
8 changes: 4 additions & 4 deletions AsyncKeyedLock/AsyncKeyedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock</PackageProjectUrl>
<Copyright>MIT</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>6.0.4</Version>
<Version>6.0.5</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Optimizations and fixing of more rare race conditions.</PackageReleaseNotes>
<Description>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.</Description>
<Copyright2022 Mark Cilia Vincenti</Copyright>
<Copyright2023 Mark Cilia Vincenti</Copyright>
<PackageTags>async,lock,key,keyed,semaphore,dictionary,pooling,duplicate,synchronization</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>6.0.4.0</AssemblyVersion>
<FileVersion>6.0.4.0</FileVersion>
<AssemblyVersion>6.0.4.1</AssemblyVersion>
<FileVersion>6.0.4.1</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<IsTrimmable>true</IsTrimmable>
Expand Down
27 changes: 9 additions & 18 deletions AsyncKeyedLock/AsyncKeyedLockDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public AsyncKeyedLockReleaser<TKey> GetOrAdd(TKey key)
}
if (releaser.TryIncrement(key))
{
releaser.IsPooled = true;
releaser.IsNotInUse = true;
_pool.PutObject(releaserToAdd);
return releaser;
}
Expand Down Expand Up @@ -135,24 +135,20 @@ public void Release(AsyncKeyedLockReleaser<TKey> releaser)
{
Monitor.Enter(releaser);

if (--releaser.ReferenceCount == 0)
if (releaser.ReferenceCount == 1)
{
TryRemove(releaser.Key, out _);
releaser.IsNotInUse = true;
Monitor.Exit(releaser);
if (PoolingEnabled)
{
releaser.IsPooled = true;
Monitor.Exit(releaser);
releaser.ReferenceCount = 1;
_pool.PutObject(releaser);
}
else
{
Monitor.Exit(releaser);
}
releaser.SemaphoreSlim.Release();
return;
}

--releaser.ReferenceCount;
Monitor.Exit(releaser);
releaser.SemaphoreSlim.Release();
}
Expand All @@ -162,23 +158,18 @@ public void ReleaseWithoutSemaphoreRelease(AsyncKeyedLockReleaser<TKey> releaser
{
Monitor.Enter(releaser);

if (--releaser.ReferenceCount == 0)
if (releaser.ReferenceCount == 1)
{
TryRemove(releaser.Key, out _);
releaser.IsNotInUse = true;
Monitor.Exit(releaser);
if (PoolingEnabled)
{
releaser.IsPooled = true;
Monitor.Exit(releaser);
releaser.ReferenceCount = 1;
_pool.PutObject(releaser);
}
else
{
Monitor.Exit(releaser);
}
return;
}

--releaser.ReferenceCount;
Monitor.Exit(releaser);
}
}
Expand Down
6 changes: 3 additions & 3 deletions AsyncKeyedLock/AsyncKeyedLockPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public AsyncKeyedLockPool(Func<TKey, AsyncKeyedLockReleaser<TKey>> objectGenerat
for (int i = 0; i < capacity; ++i)
{
var releaser = _objectGenerator(default);
releaser.IsPooled = true;
releaser.IsNotInUse = true;
_objects.Add(releaser);
}
}
Expand All @@ -28,7 +28,7 @@ public AsyncKeyedLockPool(Func<TKey, AsyncKeyedLockReleaser<TKey>> objectGenerat
for (int i = 0; i < initialFill; ++i)
{
var releaser = _objectGenerator(default);
releaser.IsPooled = true;
releaser.IsNotInUse = true;
_objects.Add(releaser);
}
}
Expand All @@ -40,7 +40,7 @@ public AsyncKeyedLockReleaser<TKey> GetObject(TKey key)
if (_objects.TryTake(out var item))
{
item.Key = key;
item.IsPooled = false;
item.IsNotInUse = false;
return item;
}
return _objectGenerator(key);
Expand Down
8 changes: 5 additions & 3 deletions AsyncKeyedLock/AsyncKeyedLockReleaser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AsyncKeyedLock
/// </summary>
public sealed class AsyncKeyedLockReleaser<TKey> : IDisposable
{
internal bool IsPooled { get; set; } = false;
internal bool IsNotInUse { get; set; } = false;

private TKey _key;

Expand Down Expand Up @@ -54,7 +54,7 @@ internal bool TryIncrement(TKey key)
{
if (Monitor.TryEnter(this))
{
if (IsPooled || !_key.Equals(key)) // rare race condition
if (IsNotInUse || !_key.Equals(key)) // rare race condition
{
Monitor.Exit(this);
return false;
Expand All @@ -71,7 +71,7 @@ internal bool TryIncrementNoPooling()
{
if (Monitor.TryEnter(this))
{
if (_referenceCount == 0) // rare race condition
if (IsNotInUse) // rare race condition
{
Monitor.Exit(this);
return false;
Expand All @@ -86,11 +86,13 @@ internal bool TryIncrementNoPooling()
/// <summary>
/// Releases the <see cref="SemaphoreSlim"/> object once.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
_dictionary.Release(this);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Dispose(bool enteredSemaphore)
{
if (enteredSemaphore)
Expand Down

0 comments on commit cd6d9f9

Please sign in to comment.