← All source files

V1 / 8f6c47b125e8

SemaphoreExtensions.cs

Lilith/Core/Extensions/SemaphoreExtensions.cs

Changed in this version
CS
namespace Lilith.Core.Extensions;

public static class SemaphoreExtensions
{
    public static IDisposable EnterOrThrow(this SemaphoreSlim gate, string message)
    {
        if (!gate.Wait(0)) throw new InvalidOperationException(message);
        return new SemaphoreLease(gate);
    }

    public static async Task<IDisposable> EnterAsync(this SemaphoreSlim gate, CancellationToken ct = default)
    {
        await gate.WaitAsync(ct);
        return new SemaphoreLease(gate);
    }
}