Skip to content

Commit 6861ee4

Browse files
committed
.NET 9
1 parent 498b971 commit 6861ee4

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

src/AsyncKeyLock.Benchmarks/AsyncKeyLock.Benchmarks.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>disable</Nullable>
77
<OutputType>Exe</OutputType>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="AsyncKeyedLock" Version="6.3.4" />
12-
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
11+
<PackageReference Include="AsyncKeyedLock" Version="7.1.4" />
12+
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
1313
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
1414
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
15-
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.1" />
15+
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.3" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

src/AsyncKeyLock.Tests/AsyncKeyLock.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
11-
<PackageReference Include="xunit" Version="2.7.0" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
11+
<PackageReference Include="xunit" Version="2.9.3" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1515
</PackageReference>

src/AsyncKeyLock/AsyncKeyLock.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<Authors>usercode</Authors>
@@ -34,7 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37-
<PackageReference Include="MinVer" Version="5.0.0">
37+
<PackageReference Include="MinVer" Version="6.0.0">
3838
<PrivateAssets>all</PrivateAssets>
3939
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4040
</PackageReference>

src/AsyncKeyLock/AsyncLock.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ namespace AsyncKeyLock;
1010
public sealed class AsyncLock
1111
{
1212
public AsyncLock()
13+
: this(new Lock())
1314
{
14-
SyncObj = _waitingWriters;
15-
16-
_readerReleaserTask = Task.FromResult(new ReaderReleaser(this, true));
17-
_writerReleaserTask = Task.FromResult(new WriterReleaser(this, true));
1815
}
1916

20-
internal AsyncLock(object syncObject)
21-
: this()
17+
internal AsyncLock(Lock syncObject)
2218
{
2319
SyncObj = syncObject;
20+
21+
_readerReleaserTask = Task.FromResult(new ReaderReleaser(this, true));
22+
_writerReleaserTask = Task.FromResult(new WriterReleaser(this, true));
2423
}
2524

26-
internal readonly object SyncObj;
25+
internal readonly Lock SyncObj;
2726

2827
private readonly Queue<TaskCompletionSource<ReaderReleaser>> _waitingReaders = new();
2928
private readonly Queue<TaskCompletionSource<WriterReleaser>> _waitingWriters = new();

src/AsyncKeyLock/AsyncLock~.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// https://github.com/usercode/AsyncKeyLock
33
// MIT License
44

5+
using System.Runtime.InteropServices;
6+
57
namespace AsyncKeyLock;
68

79
/// <summary>
@@ -15,44 +17,46 @@ public AsyncLock(int maxPoolSize = 64)
1517
_pool = new Pool<AsyncLock>(maxPoolSize);
1618
}
1719

18-
private readonly Dictionary<TKey, AsyncLock> _locks = new();
20+
private readonly Dictionary<TKey, AsyncLock> _activeLocks = new();
1921
private readonly Pool<AsyncLock> _pool;
22+
private readonly Lock _lock = new Lock();
2023

2124
/// <summary>
2225
/// GetAsyncLock
2326
/// </summary>
2427
private AsyncLock GetAsyncLock(TKey key)
2528
{
26-
if (_locks.TryGetValue(key, out AsyncLock? asyncLock) == false)
29+
ref AsyncLock? dictionaryValue = ref CollectionsMarshal.GetValueRefOrAddDefault(_activeLocks, key, out bool exists);
30+
31+
if (exists)
2732
{
28-
//create new AsyncLock
29-
_pool.GetOrCreate(out asyncLock,
30-
() =>
31-
{
32-
AsyncLock a = new AsyncLock(_locks);
33-
a.Released += x =>
34-
{
35-
_locks.Remove(key);
33+
return dictionaryValue!;
34+
}
3635

37-
//add idle AsynLock to pool
38-
_pool.Add(x);
39-
};
36+
_pool.GetOrCreate(out dictionaryValue,
37+
() =>
38+
{
39+
AsyncLock a = new AsyncLock(_lock);
40+
a.Released += x =>
41+
{
42+
_activeLocks.Remove(key);
4043

41-
return a;
42-
});
44+
//add idle AsynLock to pool
45+
_pool.Add(x);
46+
};
4347

44-
_locks.Add(key, asyncLock);
45-
}
48+
return a;
49+
});
4650

47-
return asyncLock;
51+
return dictionaryValue;
4852
}
4953

5054
/// <summary>
5155
/// ReaderLockAsync
5256
/// </summary>
5357
public Task<ReaderReleaser> ReaderLockAsync(TKey key, CancellationToken cancellation = default)
5458
{
55-
lock (_locks)
59+
lock (_lock)
5660
{
5761
return GetAsyncLock(key).ReaderLockAsync(cancellation);
5862
}
@@ -63,7 +67,7 @@ public Task<ReaderReleaser> ReaderLockAsync(TKey key, CancellationToken cancella
6367
/// </summary>
6468
public Task<WriterReleaser> WriterLockAsync(TKey key, CancellationToken cancellation = default)
6569
{
66-
lock (_locks)
70+
lock (_lock)
6771
{
6872
return GetAsyncLock(key).WriterLockAsync(cancellation);
6973
}

0 commit comments

Comments
 (0)