diff --git a/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj b/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj index b0f0035..8238e34 100644 --- a/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj +++ b/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj @@ -8,7 +8,7 @@ System LICENSE true - 1.6.5 + 1.6.6 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Extensions for the System namespace diff --git a/SystemExtensions.NetStandard/Extensions/SemaphoreSlimExtensions.cs b/SystemExtensions.NetStandard/Extensions/SemaphoreSlimExtensions.cs new file mode 100644 index 0000000..0e6509f --- /dev/null +++ b/SystemExtensions.NetStandard/Extensions/SemaphoreSlimExtensions.cs @@ -0,0 +1,11 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace System.Extensions; +public static class SemaphoreSlimExtensions +{ + public static async Task Acquire(this SemaphoreSlim semaphore) + { + return await SemaphoreSlimContext.Create(semaphore); + } +} diff --git a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj index 2a48bc6..40dd059 100644 --- a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj +++ b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj @@ -7,7 +7,7 @@ LICENSE true System - 1.6.5 + 1.6.6 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Extensions for the System namespace diff --git a/SystemExtensions.NetStandard/Threading/SemaphoreSlimContext.cs b/SystemExtensions.NetStandard/Threading/SemaphoreSlimContext.cs new file mode 100644 index 0000000..fca40d7 --- /dev/null +++ b/SystemExtensions.NetStandard/Threading/SemaphoreSlimContext.cs @@ -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 Create(SemaphoreSlim semaphore) + { + await semaphore.ThrowIfNull(nameof(semaphore)).WaitAsync(); + return new SemaphoreSlimContext(semaphore); + } +}