Skip to content

Commit 8776bca

Browse files
committed
Fixed #240
1 parent 3dc7c55 commit 8776bca

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/DotNext.Threading/Threading/AsyncLock.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ public readonly async ValueTask<Holder> AcquireAsync(TimeSpan timeout, Cancellat
256256
task = As<AsyncReaderWriterLock>(lockedObject).UpgradeToWriteLockAsync(timeout, token);
257257
break;
258258
case Type.Semaphore:
259-
task = CheckOnTimeoutAsync(As<SemaphoreSlim>(lockedObject).WaitAsync(timeout, token));
259+
task = timeout == InfiniteTimeSpan
260+
? new(As<SemaphoreSlim>(lockedObject).WaitAsync(token))
261+
: CheckOnTimeoutAsync(As<SemaphoreSlim>(lockedObject).WaitAsync(timeout, token));
260262
break;
261263
case Type.Strong:
262264
task = As<AsyncSharedLock>(lockedObject).AcquireAsync(true, timeout, token);

src/DotNext.Threading/Threading/AsyncLockAcquisition.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
6969
/// <param name="timeout">The interval to wait for the lock.</param>
7070
/// <returns>The acquired lock holder.</returns>
7171
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
72-
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, TimeSpan timeout)
73-
where T : class => obj.GetExclusiveLock().AcquireAsync(timeout);
72+
[Obsolete("Use AcquireLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
73+
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(T obj, TimeSpan timeout)
74+
where T : class => AcquireLockAsync(obj, timeout, CancellationToken.None);
7475

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

87+
/// <summary>
88+
/// Acquires exclusive lock associated with the given object.
89+
/// </summary>
90+
/// <typeparam name="T">The type of the object to be locked.</typeparam>
91+
/// <param name="obj">The object to be locked.</param>
92+
/// <param name="timeout">The interval to wait for the lock.</param>
93+
/// <param name="token">The token that can be used to abort acquisition operation.</param>
94+
/// <returns>The acquired lock holder.</returns>
95+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
96+
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
97+
public static ValueTask<AsyncLock.Holder> AcquireLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
98+
where T : class => obj.GetExclusiveLock().AcquireAsync(timeout, token);
99+
85100
/// <summary>
86101
/// Acquires reader lock associated with the given object.
87102
/// </summary>
@@ -90,8 +105,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
90105
/// <param name="timeout">The interval to wait for the lock.</param>
91106
/// <returns>The acquired lock holder.</returns>
92107
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
93-
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, TimeSpan timeout)
94-
where T : class => AsyncLock.ReadLock(obj.GetReaderWriterLock()).AcquireAsync(timeout);
108+
[Obsolete("Use AcquireReadLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
109+
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(T obj, TimeSpan timeout)
110+
where T : class => AcquireReadLockAsync(obj, timeout, CancellationToken.None);
95111

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

123+
/// <summary>
124+
/// Acquires reader lock associated with the given object.
125+
/// </summary>
126+
/// <typeparam name="T">The type of the object to be locked.</typeparam>
127+
/// <param name="obj">The object to be locked.</param>
128+
/// <param name="timeout">The interval to wait for the lock.</param>
129+
/// <param name="token">The token that can be used to abort acquisition operation.</param>
130+
/// <returns>The acquired lock holder.</returns>
131+
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
132+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
133+
public static ValueTask<AsyncLock.Holder> AcquireReadLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
134+
where T : class => AsyncLock.ReadLock(obj.GetReaderWriterLock()).AcquireAsync(timeout, token);
135+
106136
/// <summary>
107137
/// Acquires writer lock associated with the given object.
108138
/// </summary>
@@ -111,8 +141,9 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
111141
/// <param name="timeout">The interval to wait for the lock.</param>
112142
/// <returns>The acquired lock holder.</returns>
113143
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
114-
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, TimeSpan timeout)
115-
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(timeout);
144+
[Obsolete("Use AcquireWriteLockAsync(T, TimeSpan, CancellationToken) overload instead.", error: true)]
145+
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(T obj, TimeSpan timeout)
146+
where T : class => AcquireWriteLockAsync(obj, timeout, CancellationToken.None);
116147

117148
/// <summary>
118149
/// Acquires reader lock associated with the given object.
@@ -121,6 +152,19 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
121152
/// <param name="obj">The object to be locked.</param>
122153
/// <param name="token">The token that can be used to abort acquisition operation.</param>
123154
/// <returns>The acquired lock holder.</returns>
124-
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, CancellationToken token)
155+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
156+
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, CancellationToken token = default)
125157
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(token);
158+
159+
/// <summary>
160+
/// Acquires reader lock associated with the given object.
161+
/// </summary>
162+
/// <typeparam name="T">The type of the object to be locked.</typeparam>
163+
/// <param name="obj">The object to be locked.</param>
164+
/// <param name="timeout">The interval to wait for the lock.</param>
165+
/// <param name="token">The token that can be used to abort acquisition operation.</param>
166+
/// <returns>The acquired lock holder.</returns>
167+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
168+
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, TimeSpan timeout, CancellationToken token = default)
169+
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(timeout, token);
126170
}

0 commit comments

Comments
 (0)