Esempio n. 1
0
 public AsyncLock()
 {
     _semaphore        = new SemaphoreSlim(1);
     _semaphoreRelease = new SafeSemaphoreRelease(this);
     _lockTaken        = new AsyncLocal <bool>(LockTakenValueChanged);
     _lockTaken.Value  = false;
 }
Esempio n. 2
0
        public Task <IAsyncDisposable> TakeLockAsync()
        {
            if (_intDisposedStatus != 0)
            {
                return(Task.FromException <IAsyncDisposable>(new ObjectDisposedException(nameof(AsyncLock))));
            }
            DebuggableSemaphoreSlim objNextSemaphore    = Utils.SemaphorePool.Get();
            DebuggableSemaphoreSlim objCurrentSemaphore = _objCurrentSemaphore.Value ?? _objTopLevelSemaphore;

            _objCurrentSemaphore.Value = objNextSemaphore;
            SafeSemaphoreRelease objRelease = new SafeSemaphoreRelease(objCurrentSemaphore, objNextSemaphore, this);

            return(TakeLockCoreAsync(objCurrentSemaphore, objRelease));
        }
Esempio n. 3
0
        public Task <IAsyncDisposable> TakeLockAsync()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(AsyncLock));
            }

            _currentSemaphore.Value = _currentSemaphore.Value ?? _topLevelSemaphore;
            SemaphoreSlim currentSem = _currentSemaphore.Value;
            var           nextSem    = s_semaphorePool.Get();

            _currentSemaphore.Value = nextSem;
            var safeRelease = new SafeSemaphoreRelease(currentSem, nextSem, this);

            return(TakeLockCoreAsync(currentSem, safeRelease));
        }
Esempio n. 4
0
 private static async Task <IAsyncDisposable> TakeLockCoreAsync(DebuggableSemaphoreSlim objCurrentSemaphore, SafeSemaphoreRelease objRelease, CancellationToken token)
 {
     try
     {
         await objCurrentSemaphore.WaitAsync(token);
     }
     catch (OperationCanceledException)
     {
         //swallow this because it must be handled as a disposal in the original ExecutionContext
     }
     return(objRelease);
 }
Esempio n. 5
0
        private static async Task <IAsyncDisposable> TakeLockCoreAsync(DebuggableSemaphoreSlim objCurrentSemaphore, SafeSemaphoreRelease objRelease)
        {
            await objCurrentSemaphore.WaitAsync();

            return(objRelease);
        }
Esempio n. 6
0
 public AsyncLock()
 {
     _semaphore        = new SemaphoreSlim(1);
     _semaphoreRelease = new SafeSemaphoreRelease(this);
 }
Esempio n. 7
0
        private async Task <IAsyncDisposable> TakeLockCoreAsync(SemaphoreSlim currentSemaphore, SafeSemaphoreRelease safeSemaphoreRelease)
        {
            await currentSemaphore.WaitAsync();

            return(safeSemaphoreRelease);
        }