public AsyncLock() { _semaphore = new SemaphoreSlim(1); _semaphoreRelease = new SafeSemaphoreRelease(this); _lockTaken = new AsyncLocal <bool>(LockTakenValueChanged); _lockTaken.Value = false; }
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)); }
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)); }
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); }
private static async Task <IAsyncDisposable> TakeLockCoreAsync(DebuggableSemaphoreSlim objCurrentSemaphore, SafeSemaphoreRelease objRelease) { await objCurrentSemaphore.WaitAsync(); return(objRelease); }
public AsyncLock() { _semaphore = new SemaphoreSlim(1); _semaphoreRelease = new SafeSemaphoreRelease(this); }
private async Task <IAsyncDisposable> TakeLockCoreAsync(SemaphoreSlim currentSemaphore, SafeSemaphoreRelease safeSemaphoreRelease) { await currentSemaphore.WaitAsync(); return(safeSemaphoreRelease); }