From a8e9eded60e4d33c4d9e91d06505ef175b0a620f Mon Sep 17 00:00:00 2001 From: sakno Date: Tue, 15 Oct 2024 18:56:49 +0300 Subject: [PATCH] Skip redundant dispose checks --- .../Threading/AsyncExclusiveLock.cs | 7 +++++- .../Threading/AsyncReaderWriterLock.cs | 24 +++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/DotNext.Threading/Threading/AsyncExclusiveLock.cs b/src/DotNext.Threading/Threading/AsyncExclusiveLock.cs index a28a93d72..a272c06d9 100644 --- a/src/DotNext.Threading/Threading/AsyncExclusiveLock.cs +++ b/src/DotNext.Threading/Threading/AsyncExclusiveLock.cs @@ -86,6 +86,11 @@ private void OnCompleted(DefaultWaitNode node) public bool TryAcquire() { ObjectDisposedException.ThrowIf(IsDisposed, this); + return TryAcquireCore(); + } + + private bool TryAcquireCore() + { Monitor.Enter(SyncRoot); var result = TryAcquire(ref manager); Monitor.Exit(SyncRoot); @@ -104,7 +109,7 @@ public bool TryAcquire() public bool TryAcquire(TimeSpan timeout) { ObjectDisposedException.ThrowIf(IsDisposed, this); - return timeout == TimeSpan.Zero ? TryAcquire() : TryAcquire(new Timeout(timeout), ref manager); + return timeout == TimeSpan.Zero ? TryAcquireCore() : TryAcquire(new Timeout(timeout), ref manager); } /// diff --git a/src/DotNext.Threading/Threading/AsyncReaderWriterLock.cs b/src/DotNext.Threading/Threading/AsyncReaderWriterLock.cs index a0154a1b1..b73939370 100644 --- a/src/DotNext.Threading/Threading/AsyncReaderWriterLock.cs +++ b/src/DotNext.Threading/Threading/AsyncReaderWriterLock.cs @@ -284,8 +284,12 @@ public LockStamp TryOptimisticRead() /// /// if lock is taken successfully; otherwise, . /// This object has been disposed. - public bool TryEnterReadLock() => TryEnter(); - + public bool TryEnterReadLock() + { + ObjectDisposedException.ThrowIf(IsDisposed, this); + return TryEnter(); + } + /// /// Tries to obtain reader lock synchronously. /// @@ -365,8 +369,12 @@ public bool TryEnterWriteLock(in LockStamp stamp) /// /// if lock is taken successfully; otherwise, . /// This object has been disposed. - public bool TryEnterWriteLock() => TryEnter(); - + public bool TryEnterWriteLock() + { + ObjectDisposedException.ThrowIf(IsDisposed, this); + return TryEnter(); + } + /// /// Tries to obtain writer lock synchronously. /// @@ -425,13 +433,15 @@ public ValueTask EnterWriteLockAsync(TimeSpan timeout, CancellationToken token = /// /// if lock is taken successfully; otherwise, . /// This object has been disposed. - public bool TryUpgradeToWriteLock() => TryEnter(); + public bool TryUpgradeToWriteLock() + { + ObjectDisposedException.ThrowIf(IsDisposed, this); + return TryEnter(); + } private bool TryEnter() where TLockManager : struct, ILockManager { - ObjectDisposedException.ThrowIf(IsDisposed, this); - Monitor.Enter(SyncRoot); var result = TryAcquire(ref GetLockManager()); Monitor.Exit(SyncRoot);