From fbaa74de91ef46fc030bdad2b20b874b823c2653 Mon Sep 17 00:00:00 2001 From: sakno Date: Sun, 9 Jun 2024 12:19:29 +0300 Subject: [PATCH] Fixed #241 --- .../Threading/AsyncLockAcquisition.cs | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/DotNext.Threading/Threading/AsyncLockAcquisition.cs b/src/DotNext.Threading/Threading/AsyncLockAcquisition.cs index 3857bea9f..02704f8c0 100644 --- a/src/DotNext.Threading/Threading/AsyncLockAcquisition.cs +++ b/src/DotNext.Threading/Threading/AsyncLockAcquisition.cs @@ -154,7 +154,7 @@ private static AsyncLock GetExclusiveLock(this T obj) /// The acquired lock holder. /// The operation has been canceled. public static ValueTask AcquireWriteLockAsync(this T obj, CancellationToken token = default) - where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(token); + where T : class => AcquireWriteLockAsync(obj, upgrade: false, token); /// /// Acquires reader lock associated with the given object. @@ -164,7 +164,40 @@ private static AsyncLock GetExclusiveLock(this T obj) /// The interval to wait for the lock. /// The token that can be used to abort acquisition operation. /// The acquired lock holder. + /// The lock cannot be acquired during the specified amount of time. /// The operation has been canceled. public static ValueTask AcquireWriteLockAsync(this T obj, TimeSpan timeout, CancellationToken token = default) - where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock()).AcquireAsync(timeout, token); + where T : class => AcquireWriteLockAsync(obj, upgrade: false, timeout, token); + + /// + /// Acquires reader lock associated with the given object. + /// + /// The type of the object to be locked. + /// The object to be locked. + /// + /// to upgrade from read lock; + /// otherwise, . + /// + /// The token that can be used to abort acquisition operation. + /// The acquired lock holder. + /// The operation has been canceled. + public static ValueTask AcquireWriteLockAsync(this T obj, bool upgrade, CancellationToken token = default) + where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock(), upgrade).AcquireAsync(token); + + /// + /// Acquires reader lock associated with the given object. + /// + /// The type of the object to be locked. + /// The object to be locked. + /// + /// to upgrade from read lock; + /// otherwise, . + /// + /// The interval to wait for the lock. + /// The token that can be used to abort acquisition operation. + /// The acquired lock holder. + /// The lock cannot be acquired during the specified amount of time. + /// The operation has been canceled. + public static ValueTask AcquireWriteLockAsync(this T obj, bool upgrade, TimeSpan timeout, CancellationToken token = default) + where T : class => AsyncLock.WriteLock(obj.GetReaderWriterLock(), upgrade).AcquireAsync(timeout, token); } \ No newline at end of file