using AyCode.Utils.Wrappers;

namespace AyCode.Utils.Extensions
{
    public static class LockExtensions
    {
        public static IDisposable UseWaitOne(this Mutex mutex)
        {
            mutex.WaitOne();

            return new ReleaseWrapperMutex(mutex);
        }
        
        public static IDisposable UseWait(this SemaphoreSlim semaphore, CancellationToken cancelToken = default)
        {
            semaphore.Wait(cancelToken);

            return new ReleaseWrapperSemaphore(semaphore);
        }

        public static async Task<IDisposable> UseWaitAsync(this SemaphoreSlim semaphore, CancellationToken cancelToken = default)
        {
            await semaphore.WaitAsync(cancelToken).ConfigureAwait(false);

            return new ReleaseWrapperSemaphore(semaphore);
        }
    }
}
