Skip to content

Commit

Permalink
Implemented IDisposable.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Apr 10, 2023
1 parent 449ee6a commit 89891f0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 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.2.0</Version>
<Version>6.2.1</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Created an alternate method for locking using striped locks. For a list of pros and cons of both techniques, please visit the wiki at https://github.com/MarkCiliaVincenti/AsyncKeyedLock/wiki</PackageReleaseNotes>
<PackageReleaseNotes>Implemented IDisposable.</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, with optional pooling for reducing memory allocations.</Description>
<Copyright>© 2023 Mark Cilia Vincenti</Copyright>
<PackageTags>async,lock,key,keyed,semaphore,striped,dictionary,concurrentdictionary,pooling,duplicate,synchronization</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<FileVersion>6.2.0.0</FileVersion>
<AssemblyVersion>6.2.1.0</AssemblyVersion>
<FileVersion>6.2.1.0</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<IsTrimmable>true</IsTrimmable>
Expand Down
23 changes: 22 additions & 1 deletion AsyncKeyedLock/AsyncKeyedLockDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

namespace AsyncKeyedLock
{
internal sealed class AsyncKeyedLockDictionary<TKey> : ConcurrentDictionary<TKey, AsyncKeyedLockReleaser<TKey>>
internal sealed class AsyncKeyedLockDictionary<TKey> : ConcurrentDictionary<TKey, AsyncKeyedLockReleaser<TKey>>, IDisposable
{
public int MaxCount { get; private set; } = 1;
private readonly AsyncKeyedLockPool<TKey> _pool;
Expand Down Expand Up @@ -172,5 +173,25 @@ public void ReleaseWithoutSemaphoreRelease(AsyncKeyedLockReleaser<TKey> releaser
--releaser.ReferenceCount;
Monitor.Exit(releaser);
}

public void Dispose()
{
foreach (var semaphore in Values)
{
try
{
semaphore.Dispose();
}
catch
{
// do nothing
}
}
Clear();
if (PoolingEnabled)
{
_pool.Dispose();
}
}
}
}
7 changes: 6 additions & 1 deletion AsyncKeyedLock/AsyncKeyedLockPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AsyncKeyedLock
{
internal sealed class AsyncKeyedLockPool<TKey>
internal sealed class AsyncKeyedLockPool<TKey> : IDisposable
{
private readonly BlockingCollection<AsyncKeyedLockReleaser<TKey>> _objects;
private readonly Func<TKey, AsyncKeyedLockReleaser<TKey>> _objectGenerator;
Expand Down Expand Up @@ -34,6 +34,11 @@ public AsyncKeyedLockPool(Func<TKey, AsyncKeyedLockReleaser<TKey>> objectGenerat
}
}

public void Dispose()
{
_objects.Dispose();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public AsyncKeyedLockReleaser<TKey> GetObject(TKey key)
{
Expand Down
10 changes: 9 additions & 1 deletion AsyncKeyedLock/AsyncKeyedLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public AsyncKeyedLocker(Action<AsyncKeyedLockOptions> options, int concurrencyLe
/// <summary>
/// Represents a thread-safe keyed locker that allows you to lock based on a key (keyed semaphores), only allowing a specified number of concurrent threads that share the same key.
/// </summary>
public class AsyncKeyedLocker<TKey> where TKey : notnull
public class AsyncKeyedLocker<TKey> : IDisposable where TKey : notnull
{
private readonly AsyncKeyedLockDictionary<TKey> _dictionary;
/// <summary>
Expand Down Expand Up @@ -1013,5 +1013,13 @@ public int GetCurrentCount(TKey key)
{
return MaxCount - GetRemainingCount(key);
}

/// <summary>
/// Disposes the AsyncKeyedLocker.
/// </summary>
public void Dispose()
{
_dictionary.Dispose();
}
}
}

0 comments on commit 89891f0

Please sign in to comment.