|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
@ -6,21 +7,24 @@ 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(); |
|
|
|
await semaphoreSlim.WaitAsync().ConfigureAwait(false); |
|
|
|
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); |
|
|
|
await semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false); |
|
|
|
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)) |
|
|
|
if (await semaphoreSlim.WaitAsync(millisecondsTimeout).ConfigureAwait(false)) |
|
|
|
{ |
|
|
|
return GetDispose(semaphoreSlim); |
|
|
|
} |
|
|
|
@ -28,9 +32,10 @@ public static class SemaphoreSlimExtensions |
|
|
|
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)) |
|
|
|
if (await semaphoreSlim.WaitAsync(millisecondsTimeout, cancellationToken).ConfigureAwait(false)) |
|
|
|
{ |
|
|
|
return GetDispose(semaphoreSlim); |
|
|
|
} |
|
|
|
@ -38,9 +43,10 @@ public static class SemaphoreSlimExtensions |
|
|
|
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)) |
|
|
|
if (await semaphoreSlim.WaitAsync(timeout).ConfigureAwait(false)) |
|
|
|
{ |
|
|
|
return GetDispose(semaphoreSlim); |
|
|
|
} |
|
|
|
@ -48,9 +54,10 @@ public static class SemaphoreSlimExtensions |
|
|
|
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)) |
|
|
|
if (await semaphoreSlim.WaitAsync(timeout, cancellationToken).ConfigureAwait(false)) |
|
|
|
{ |
|
|
|
return GetDispose(semaphoreSlim); |
|
|
|
} |
|
|
|
@ -58,18 +65,21 @@ public static class SemaphoreSlimExtensions |
|
|
|
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)) |
|
|
|
@ -80,6 +90,7 @@ public static class SemaphoreSlimExtensions |
|
|
|
throw new TimeoutException(); |
|
|
|
} |
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
if (semaphoreSlim.Wait(millisecondsTimeout, cancellationToken)) |
|
|
|
@ -90,6 +101,7 @@ public static class SemaphoreSlimExtensions |
|
|
|
throw new TimeoutException(); |
|
|
|
} |
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout) |
|
|
|
{ |
|
|
|
if (semaphoreSlim.Wait(timeout)) |
|
|
|
@ -100,6 +112,7 @@ public static class SemaphoreSlimExtensions |
|
|
|
throw new TimeoutException(); |
|
|
|
} |
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
if (semaphoreSlim.Wait(timeout, cancellationToken)) |
|
|
|
@ -110,6 +123,7 @@ public static class SemaphoreSlimExtensions |
|
|
|
throw new TimeoutException(); |
|
|
|
} |
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim) |
|
|
|
{ |
|
|
|
return new DisposeAction<SemaphoreSlim>(static (semaphoreSlim) => |
|
|
|
|