Skip to content

Commit fbaa74d

Browse files
committed
Fixed #241
1 parent 5e3f92e commit fbaa74d

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/DotNext.Threading/Threading/AsyncLockAcquisition.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
154154
/// <returns>The acquired lock holder.</returns>
155155
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
156156
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, CancellationToken token = default)
157-
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(token);
157+
where T : class => AcquireWriteLockAsync(obj, upgrade: false, token);
158158

159159
/// <summary>
160160
/// Acquires reader lock associated with the given object.
@@ -164,7 +164,40 @@ private static AsyncLock GetExclusiveLock<T>(this T obj)
164164
/// <param name="timeout">The interval to wait for the lock.</param>
165165
/// <param name="token">The token that can be used to abort acquisition operation.</param>
166166
/// <returns>The acquired lock holder.</returns>
167+
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
167168
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
168169
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);
170+
where T : class => AcquireWriteLockAsync(obj, upgrade: false, timeout, token);
171+
172+
/// <summary>
173+
/// Acquires reader lock associated with the given object.
174+
/// </summary>
175+
/// <typeparam name="T">The type of the object to be locked.</typeparam>
176+
/// <param name="obj">The object to be locked.</param>
177+
/// <param name="upgrade">
178+
/// <see langword="true"/> to upgrade from read lock;
179+
/// otherwise, <see langword="false"/>.
180+
/// </param>
181+
/// <param name="token">The token that can be used to abort acquisition operation.</param>
182+
/// <returns>The acquired lock holder.</returns>
183+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
184+
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, bool upgrade, CancellationToken token = default)
185+
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock(), upgrade).AcquireAsync(token);
186+
187+
/// <summary>
188+
/// Acquires reader lock associated with the given object.
189+
/// </summary>
190+
/// <typeparam name="T">The type of the object to be locked.</typeparam>
191+
/// <param name="obj">The object to be locked.</param>
192+
/// <param name="upgrade">
193+
/// <see langword="true"/> to upgrade from read lock;
194+
/// otherwise, <see langword="false"/>.
195+
/// </param>
196+
/// <param name="timeout">The interval to wait for the lock.</param>
197+
/// <param name="token">The token that can be used to abort acquisition operation.</param>
198+
/// <returns>The acquired lock holder.</returns>
199+
/// <exception cref="TimeoutException">The lock cannot be acquired during the specified amount of time.</exception>
200+
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
201+
public static ValueTask<AsyncLock.Holder> AcquireWriteLockAsync<T>(this T obj, bool upgrade, TimeSpan timeout, CancellationToken token = default)
202+
where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock(), upgrade).AcquireAsync(timeout, token);
170203
}

0 commit comments

Comments
 (0)