AsyncSemaphore.Analyzers
2.0.0
dotnet add package AsyncSemaphore.Analyzers --version 2.0.0
NuGet\Install-Package AsyncSemaphore.Analyzers -Version 2.0.0
<PackageReference Include="AsyncSemaphore.Analyzers" Version="2.0.0" />
<PackageVersion Include="AsyncSemaphore.Analyzers" Version="2.0.0" />
<PackageReference Include="AsyncSemaphore.Analyzers" />
paket add AsyncSemaphore.Analyzers --version 2.0.0
#r "nuget: AsyncSemaphore.Analyzers, 2.0.0"
#:package AsyncSemaphore.Analyzers@2.0.0
#addin nuget:?package=AsyncSemaphore.Analyzers&version=2.0.0
#tool nuget:?package=AsyncSemaphore.Analyzers&version=2.0.0
AsyncSemaphore
A fast, allocation-free async semaphore featuring:
- Automatic releasing without try/finally blocks by utilising the IDisposable
usingpattern - Guarantee that release can only be called once per
WaitAsynccall - A custom lock-free core that outperforms
SemaphoreSlim(~2x faster uncontended, ~27% faster async handoff) and allocates zero bytes even for contended async waits (pooledIValueTaskSourcewaiters) - Analyzers to help you implement the desired pattern
- An
IAsyncSemaphoreinterface for if you need to mock
Install
dotnet add package AsyncSemaphore
Usage
private readonly AsyncSemaphore _asyncSemaphore = new AsyncSemaphore(1);
public async Task MyMethod()
{
// Just assign the `IDisposable` returned from `WaitAsync` to a variable and use the using statement with it
using var lockHandle = await _asyncSemaphore.WaitAsync();
// Do whatever you want - Even if we throw exceptions, we'll release the semaphore once we leave this method's scope
await DoSomethingInsideLock();
}
or scoped:
private readonly AsyncSemaphore _asyncSemaphore = new AsyncSemaphore(1);
public async Task MyMethod()
{
// or create your own scope with {} braces - And after you leave that scope, your lock will be released
using (await _asyncSemaphore.WaitAsync())
{
await DoSomethingInsideLock();
}
await DoSomethingAfterLockReleased();
}
Performance
AsyncSemaphore no longer wraps SemaphoreSlim — it has its own lock-free core:
- Uncontended waits are a single interlocked operation (no monitor lock): ~2x faster than
SemaphoreSlim. - Contended async waits use pooled (including thread-local cached)
IValueTaskSourcewaiter nodes: ~27% faster handoff and zero bytes allocated per wait, versus 88 B per async waiter forSemaphoreSlim.
BenchmarkDotNet v0.15.8, Windows 11
12th Gen Intel Core i7-12700K 3.60GHz, 1 CPU, 20 logical and 12 physical cores
.NET SDK 10.0.400
[Host] : .NET 10.0.11, X64 RyuJIT x86-64-v3
DefaultJob : .NET 10.0.11, X64 RyuJIT x86-64-v3
| Method | Categories | Mean | Ratio | Allocated | Alloc Ratio |
|---|---|---|---|---|---|
| SemaphoreSlim | Uncontended | 29.73 ns | 1.00 | - | NA |
| AsyncSemaphore | Uncontended | 15.16 ns | 0.51 | - | NA |
| SemaphoreSlim_AsyncHandoff | AsyncHandoff | 47.60 ns | 1.00 | 88 B | 1.00 |
| AsyncSemaphore_AsyncHandoff | AsyncHandoff | 34.98 ns | 0.73 | - | 0.00 |
| SemaphoreSlim_Parallel | Parallel | 710.74 ns | 1.00 | 89 B | 1.00 |
| AsyncSemaphore_Parallel | Parallel | 747.84 ns | 1.05 | - | 0.01 |
Uncontended is a wait/release cycle with the permit available. AsyncHandoff measures handing the permit to a queued async waiter. Parallel is 4 workers hammering a single-permit semaphore with an await Task.Yield() inside the lock — AsyncSemaphore trades ~5% mean time there for fully allocation-free waits.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AsyncSemaphore.Analyzers:
| Package | Downloads |
|---|---|
|
AsyncSemaphore
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.