Skip to content

Commit 667564f

Browse files
committedNov 11, 2024·
Add AsyncLockSet
1 parent 177837a commit 667564f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
 

‎AsyncKeyedLockBenchmarks/AsyncKeyedLockBenchmarks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageReference Include="ListShuffle" Version="2.1.0" />
1919
<PackageReference Include="NeoSmart.Synchronization" Version="2.0.0" />
2020
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.3" />
21+
<PackageReference Include="Stl.Fusion" Version="6.8.11" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

‎AsyncKeyedLockBenchmarks/Benchmarks.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using KeyedSemaphores;
99
using ListShuffle;
1010
using NeoSmart.Synchronization;
11+
using Stl.Locking;
1112

1213
namespace AsyncKeyedLockBenchmarks
1314
{
@@ -574,6 +575,46 @@ public async Task DaoIndividualLock()
574575
await RunTests(DaoIndividualLockTasks).ConfigureAwait(false);
575576
#pragma warning restore CS8604 // Possible null reference argument.
576577
}
577-
#endregion SimpleHelpers.NamedLock
578+
#endregion Dao.IndividualLock
579+
580+
#region Stl.Fusion.AsyncLockSet
581+
public AsyncLockSet<string> AsyncLockSetLocks { get; set; }
582+
public ParallelQuery<Task>? AsyncLockSetTasks { get; set; }
583+
584+
[IterationSetup(Target = nameof(StlFusionAsyncLockSet))]
585+
public void SetupAsyncLockSet()
586+
{
587+
if (NumberOfLocks != Contention)
588+
{
589+
AsyncLockSetLocks = new AsyncLockSet<string>(LockReentryMode.Unchecked, Environment.ProcessorCount, NumberOfLocks);
590+
AsyncLockSetTasks = ShuffledIntegers
591+
.Select(async i =>
592+
{
593+
var key = (i % NumberOfLocks).ToString();
594+
595+
using (var myLock = await AsyncLockSetLocks.Lock(key).ConfigureAwait(false))
596+
{
597+
Operation();
598+
}
599+
600+
await Task.Yield();
601+
}).AsParallel();
602+
}
603+
}
604+
605+
[IterationCleanup(Target = nameof(StlFusionAsyncLockSet))]
606+
public void CleanupAsyncLockSet()
607+
{
608+
AsyncLockSetTasks = null;
609+
}
610+
611+
[Benchmark(Description = "Stl.Fusion.AsyncLockSet")]
612+
public async Task StlFusionAsyncLockSet()
613+
{
614+
#pragma warning disable CS8604 // Possible null reference argument.
615+
await RunTests(AsyncLockSetTasks).ConfigureAwait(false);
616+
#pragma warning restore CS8604 // Possible null reference argument.
617+
}
618+
#endregion Stl.Fusion.AsyncLockSet
578619
}
579620
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ When looking at the benchmarks, please ignore all values for 10k locks with 10k
2020
12. StripedAsyncLock from [AsyncUtilities](https://github.com/i3arnon/AsyncUtilities), similar to StripedAsyncKeyedLocker
2121
13. [NeoSmart.Synchronization](https://github.com/neosmart/synchronization)
2222
14. [Dao.IndividualLock](https://github.com/OscarKoo/IndividualLock)
23+
15. AsyncLockSet from [Stl.Fusion](https://github.com/servicetitan/Stl.Fusion)
2324

2425
## Results
2526
The [benchmark results](https://github.com/MarkCiliaVincenti/AsyncKeyedLockBenchmarks/actions/workflows/dotnet.yml) can be found in our actions as they run in Github Actions, in a fully transparent fashion.

0 commit comments

Comments
 (0)
Please sign in to comment.