Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved performance for SemaphoreSlim locking. #21065

Merged
merged 9 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<PackageVersion Include="System.Text.Encoding.CodePages" Version="9.0.0-rc.2.24473.5" />
<PackageVersion Include="System.Text.Encodings.Web" Version="9.0.0-rc.2.24473.5" />
<PackageVersion Include="System.Text.Json" Version="9.0.0-rc.2.24473.5" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.1.0" />
<PackageVersion Include="TimeZoneConverter" Version="6.1.0" />
<PackageVersion Include="Unidecode.NET" Version="2.1.0" />
Expand Down
3 changes: 3 additions & 0 deletions framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="Nito.AsyncEx.Context" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Threading.Tasks.Extensions" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '2.1')) ">
<PackageReference Include="System.ComponentModel.Annotations" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Volo.Abp.Threading;

public static class SemaphoreSlimExtensions
{
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim)
{
await semaphoreSlim.WaitAsync();
return GetDispose(semaphoreSlim);
}

public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(cancellationToken);
return GetDispose(semaphoreSlim);
}

public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
if (await semaphoreSlim.WaitAsync(millisecondsTimeout))
{
Expand All @@ -28,7 +32,8 @@ public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim
throw new TimeoutException();
}

public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
if (await semaphoreSlim.WaitAsync(millisecondsTimeout, cancellationToken))
{
Expand All @@ -38,7 +43,8 @@ public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim
throw new TimeoutException();
}

public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
if (await semaphoreSlim.WaitAsync(timeout))
{
Expand All @@ -48,7 +54,8 @@ public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim
throw new TimeoutException();
}

public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async static ValueTask<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
if (await semaphoreSlim.WaitAsync(timeout, cancellationToken))
{
Expand All @@ -58,18 +65,21 @@ public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim
throw new TimeoutException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim)
{
semaphoreSlim.Wait();
return GetDispose(semaphoreSlim);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(cancellationToken);
return GetDispose(semaphoreSlim);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
if (semaphoreSlim.Wait(millisecondsTimeout))
Expand All @@ -80,6 +90,7 @@ public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecond
throw new TimeoutException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
if (semaphoreSlim.Wait(millisecondsTimeout, cancellationToken))
Expand All @@ -90,6 +101,7 @@ public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecond
throw new TimeoutException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
if (semaphoreSlim.Wait(timeout))
Expand All @@ -100,6 +112,7 @@ public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeou
throw new TimeoutException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
if (semaphoreSlim.Wait(timeout, cancellationToken))
Expand All @@ -110,6 +123,7 @@ public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeou
throw new TimeoutException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim)
{
return new DisposeAction<SemaphoreSlim>(static (semaphoreSlim) =>
Expand Down
Loading