Skip to content

Commit

Permalink
Fixed #240
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jun 6, 2024
1 parent 3dc7c55 commit 8776bca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/DotNext.Threading/Threading/AsyncLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ public readonly async ValueTask<Holder> AcquireAsync(TimeSpan timeout, Cancellat
task = As<AsyncReaderWriterLock>(lockedObject).UpgradeToWriteLockAsync(timeout, token);
break;
case Type.Semaphore:
task = CheckOnTimeoutAsync(As<SemaphoreSlim>(lockedObject).WaitAsync(timeout, token));
task = timeout == InfiniteTimeSpan
? new(As<SemaphoreSlim>(lockedObject).WaitAsync(token))
: CheckOnTimeoutAsync(As<SemaphoreSlim>(lockedObject).WaitAsync(timeout, token));
break;
case Type.Strong:
task = As<AsyncSharedLock>(lockedObject).AcquireAsync(true, timeout, token);
Expand Down
62 changes: 53 additions & 9 deletions src/DotNext.Threading/Threading/AsyncLockAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="timeout">The interval to wait for the lock.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, TimeSpan timeout)
where T : class => obj.GetExclusiveLock().AcquireAsync(timeout);
[Obsolete("Use AcquireLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(T obj, TimeSpan timeout)
where T : class => AcquireLockAsync(obj, timeout, CancellationToken.None);

/// <summary>
/// Acquires exclusive lock associated with the given object.
Expand All @@ -79,9 +80,23 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="obj">The object to be locked.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, CancellationToken token)
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, CancellationToken token = default)
where T : class => obj.GetExclusiveLock().AcquireAsync(token);

/// <summary>
/// Acquires exclusive lock associated with the given object.
/// </summary>
/// <typeparam name="T">The type of the object to be locked.</typeparam>
/// <param name="obj">The object to be locked.</param>
/// <param name="timeout">The interval to wait for the lock.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
where T : class => obj.GetExclusiveLock().AcquireAsync(timeout, token);

/// <summary>
/// Acquires reader lock associated with the given object.
/// </summary>
Expand All @@ -90,8 +105,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="timeout">The interval to wait for the lock.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, TimeSpan timeout)
where T : class => AsyncLock.ReadLock(obj.GetReaderWriterLock()).AcquireAsync(timeout);
[Obsolete("Use AcquireReadLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(T obj, TimeSpan timeout)
where T : class => AcquireReadLockAsync(obj, timeout, CancellationToken.None);

/// <summary>
/// Acquires reader lock associated with the given object.
Expand All @@ -100,9 +116,23 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="obj">The object to be locked.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, CancellationToken token)
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, CancellationToken token = default)
where T : class => AsyncLock.ReadLock(obj.GetReaderWriterLock()).AcquireAsync(token);

/// <summary>
/// Acquires reader lock associated with the given object.
/// </summary>
/// <typeparam name="T">The type of the object to be locked.</typeparam>
/// <param name="obj">The object to be locked.</param>
/// <param name="timeout">The interval to wait for the lock.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
where T : class => AsyncLock.ReadLock(obj.GetReaderWriterLock()).AcquireAsync(timeout, token);

/// <summary>
/// Acquires writer lock associated with the given object.
/// </summary>
Expand All @@ -111,8 +141,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="timeout">The interval to wait for the lock.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, TimeSpan timeout)
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(timeout);
[Obsolete("Use AcquireWriteLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(T obj, TimeSpan timeout)
where T : class => AcquireWriteLockAsync(obj, timeout, CancellationToken.None);

/// <summary>
/// Acquires reader lock associated with the given object.
Expand All @@ -121,6 +152,19 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
/// <param name="obj">The object to be locked.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, CancellationToken token)
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, CancellationToken token = default)
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(token);

/// <summary>
/// Acquires reader lock associated with the given object.
/// </summary>
/// <typeparam name="T">The type of the object to be locked.</typeparam>
/// <param name="obj">The object to be locked.</param>
/// <param name="timeout">The interval to wait for the lock.</param>
/// <param name="token">The token that can be used to abort acquisition operation.</param>
/// <returns>The acquired lock holder.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(timeout, token);
}

0 comments on commit 8776bca

Please sign in to comment.