mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
SemaphoreSlim context
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
<RootNamespace>System</RootNamespace>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<Version>1.6.5</Version>
|
||||
<Version>1.6.6</Version>
|
||||
<Authors>Alexandru Macocian</Authors>
|
||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||
<Description>Extensions for the System namespace</Description>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Extensions;
|
||||
public static class SemaphoreSlimExtensions
|
||||
{
|
||||
public static async Task<SemaphoreSlimContext> Acquire(this SemaphoreSlim semaphore)
|
||||
{
|
||||
return await SemaphoreSlimContext.Create(semaphore);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<RootNamespace>System</RootNamespace>
|
||||
<Version>1.6.5</Version>
|
||||
<Version>1.6.6</Version>
|
||||
<Authors>Alexandru Macocian</Authors>
|
||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||
<Description>Extensions for the System namespace</Description>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Threading;
|
||||
public readonly struct SemaphoreSlimContext : IDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim semaphore;
|
||||
|
||||
private SemaphoreSlimContext(SemaphoreSlim semaphoreSlim)
|
||||
{
|
||||
this.semaphore = semaphoreSlim.ThrowIfNull(nameof(semaphoreSlim));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.semaphore.Release();
|
||||
}
|
||||
|
||||
public static async Task<SemaphoreSlimContext> Create(SemaphoreSlim semaphore)
|
||||
{
|
||||
await semaphore.ThrowIfNull(nameof(semaphore)).WaitAsync();
|
||||
return new SemaphoreSlimContext(semaphore);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user