Skip to content

Commit

Permalink
5.0.2-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Nov 26, 2022
1 parent cf067fb commit ccd55b8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
6 changes: 3 additions & 3 deletions AsyncKeyedLock/AsyncKeyedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<RepositoryUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>5.0.1</Version>
<Version>5.0.2-rc</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Performance improvements and allowing for object pooling.</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>
<Copyright>© 2022 Mark Cilia Vincenti</Copyright>
<PackageTags>async,lock,key,semaphore,dictionary,pooling,duplicate</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>5.0.1.0</AssemblyVersion>
<FileVersion>5.0.1.0</FileVersion>
<AssemblyVersion>5.0.1.9</AssemblyVersion>
<FileVersion>5.0.1.9</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<IsTrimmable>true</IsTrimmable>
Expand Down
10 changes: 3 additions & 7 deletions AsyncKeyedLock/AsyncKeyedLockOptions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AsyncKeyedLock
namespace AsyncKeyedLock
{
/// <summary>
/// Options for the <see cref="AsyncKeyedLocker"/> constructors
/// </summary>
public class AsyncKeyedLockOptions
public sealed class AsyncKeyedLockOptions
{
/// <summary>
/// The maximum number of requests for the semaphore that can be granted concurrently. Defaults to 1.
Expand All @@ -30,4 +26,4 @@ public AsyncKeyedLockOptions(int maxCount = 1, int poolSize = 0)
PoolSize = poolSize;
}
}
}
}
6 changes: 2 additions & 4 deletions AsyncKeyedLock/AsyncKeyedLockPool.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace AsyncKeyedLock
{
internal class AsyncKeyedLockPool<TKey>
internal sealed class AsyncKeyedLockPool<TKey>
{
private readonly BlockingCollection<AsyncKeyedLockReleaser<TKey>> _objects;
private readonly Func<TKey, AsyncKeyedLockReleaser<TKey>> _objectGenerator;
Expand Down Expand Up @@ -35,4 +33,4 @@ public void PutObject(AsyncKeyedLockReleaser<TKey> item)
_objects.TryAdd(item);
}
}
}
}
2 changes: 1 addition & 1 deletion AsyncKeyedLock/AsyncKeyedLockReleaser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AsyncKeyedLock
/// <summary>
/// Represents an <see cref="IDisposable"/> for AsyncKeyedLock.
/// </summary>
public class AsyncKeyedLockReleaser<TKey> : IDisposable
public sealed class AsyncKeyedLockReleaser<TKey> : IDisposable
{
private TKey _key;

Expand Down
1 change: 0 additions & 1 deletion AsyncKeyedLock/AsyncKeyedLocker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down
50 changes: 27 additions & 23 deletions AsyncKeyedLock/AsyncKeyedLockerDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

Expand Down Expand Up @@ -67,45 +66,47 @@ public AsyncKeyedLockerDictionary(AsyncKeyedLockOptions options, int concurrency
}
}

private AsyncKeyedLockReleaser<TKey> GetReleaser(TKey key)
public AsyncKeyedLockReleaser<TKey> GetOrAdd(TKey key)
{
if (_poolingEnabled)
if (TryGetValue(key, out var referenceCounter) && referenceCounter.TryIncrement())
{
return _pool.GetObject(key);
return referenceCounter;
}
return new AsyncKeyedLockReleaser<TKey>(key, new SemaphoreSlim(MaxCount), this);
}

private void AddToPool(AsyncKeyedLockReleaser<TKey> item)
{
if (_poolingEnabled)
{
_pool.PutObject((AsyncKeyedLockReleaser<TKey>)item);
}
}
var toAddReferenceCounter = _pool.GetObject(key);
if (TryAdd(key, toAddReferenceCounter))
{
return toAddReferenceCounter;
}

public AsyncKeyedLockReleaser<TKey> GetOrAdd(TKey key)
{
if (TryGetValue(key, out var referenceCounter) && referenceCounter.TryIncrement())
{
while (!(TryGetValue(key, out referenceCounter) && referenceCounter.TryIncrement()))
{
if (TryAdd(key, toAddReferenceCounter))
{
return toAddReferenceCounter;
}
}

_pool.PutObject(toAddReferenceCounter);
return referenceCounter;
}

var toAddReferenceCounter = GetReleaser(key);
if (TryAdd(key, toAddReferenceCounter))
var toAddReferenceCounterNoPooling = new AsyncKeyedLockReleaser<TKey>(key, new SemaphoreSlim(MaxCount), this);
if (TryAdd(key, toAddReferenceCounterNoPooling))
{
return toAddReferenceCounter;
return toAddReferenceCounterNoPooling;
}

while (!(TryGetValue(key, out referenceCounter) && referenceCounter.TryIncrement()))
{
if (TryAdd(key, toAddReferenceCounter))
if (TryAdd(key, toAddReferenceCounterNoPooling))
{
return toAddReferenceCounter;
return toAddReferenceCounterNoPooling;
}
}

AddToPool(toAddReferenceCounter);
return referenceCounter;
}

Expand All @@ -117,7 +118,10 @@ public void Release(AsyncKeyedLockReleaser<TKey> referenceCounter)
{
TryRemove(referenceCounter.Key, out _);
Monitor.Exit(referenceCounter);
AddToPool(referenceCounter);
if (_poolingEnabled)
{
_pool.PutObject(referenceCounter);
}
referenceCounter.SemaphoreSlim.Release();
return;
}
Expand Down

0 comments on commit ccd55b8

Please sign in to comment.