public static async Task <SemaphoreGuard> Async(SemaphoreSlim semaphore) { var guard = new SemaphoreGuard(semaphore, false); await semaphore.WaitAsync(); return(guard); }
public static async ValueTask <SemaphoreGuard> Async(SemaphoreSlim semaphore) { if (semaphore == null) { ThrowArgumnetNullException(nameof(semaphore)); return(null !); } var guard = new SemaphoreGuard(semaphore, false); // try to grab it sync because it's faster if (!semaphore.Wait(0)) { await semaphore.WaitAsync().ConfigureAwait(false); } return(guard); }
private static void ThrowObjectDisposedException(SemaphoreGuard guard) => throw new ObjectDisposedException(guard.ToString());