Skip to content

Commit

Permalink
Skip redundant dispose checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Oct 15, 2024
1 parent 0252f0f commit a8e9ede
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/DotNext.Threading/Threading/AsyncExclusiveLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/// <summary>
Expand Down
24 changes: 17 additions & 7 deletions src/DotNext.Threading/Threading/AsyncReaderWriterLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ public LockStamp TryOptimisticRead()
/// </summary>
/// <returns><see langword="true"/> if lock is taken successfully; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ObjectDisposedException">This object has been disposed.</exception>
public bool TryEnterReadLock() => TryEnter<ReadLockManager>();

public bool TryEnterReadLock()
{
ObjectDisposedException.ThrowIf(IsDisposed, this);
return TryEnter<ReadLockManager>();
}

/// <summary>
/// Tries to obtain reader lock synchronously.
/// </summary>
Expand Down Expand Up @@ -365,8 +369,12 @@ public bool TryEnterWriteLock(in LockStamp stamp)
/// </summary>
/// <returns><see langword="true"/> if lock is taken successfully; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ObjectDisposedException">This object has been disposed.</exception>
public bool TryEnterWriteLock() => TryEnter<WriteLockManager>();

public bool TryEnterWriteLock()
{
ObjectDisposedException.ThrowIf(IsDisposed, this);
return TryEnter<WriteLockManager>();
}

/// <summary>
/// Tries to obtain writer lock synchronously.
/// </summary>
Expand Down Expand Up @@ -425,13 +433,15 @@ public ValueTask EnterWriteLockAsync(TimeSpan timeout, CancellationToken token =
/// </summary>
/// <returns><see langword="true"/> if lock is taken successfully; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ObjectDisposedException">This object has been disposed.</exception>
public bool TryUpgradeToWriteLock() => TryEnter<UpgradeManager>();
public bool TryUpgradeToWriteLock()
{
ObjectDisposedException.ThrowIf(IsDisposed, this);
return TryEnter<UpgradeManager>();
}

private bool TryEnter<TLockManager>()
where TLockManager : struct, ILockManager<WaitNode>
{
ObjectDisposedException.ThrowIf(IsDisposed, this);

Monitor.Enter(SyncRoot);
var result = TryAcquire(ref GetLockManager<TLockManager>());
Monitor.Exit(SyncRoot);
Expand Down

0 comments on commit a8e9ede

Please sign in to comment.