From 16c63870e149ffbcc490e5989b276399ab0ad156 Mon Sep 17 00:00:00 2001 From: Gunnar Liljas Date: Tue, 20 Oct 2026 10:32:01 +0200 Subject: [PATCH 1/2] Observe lock token during composite acquisition --- ...mpositeDistributedSynchronizationHandle.cs | 112 +++++++-- .../DistributedLockLostException.cs | 29 +++ .../DistributedLockProviderExtensions.cs | 84 ++++++- ...butedReaderWriterLockProviderExtensions.cs | 168 +++++++++++++- .../DistributedSemaphoreProviderExtensions.cs | 92 +++++++- .../PublicAPI.Unshipped.txt | 20 ++ .../DistributedLockProviderExtensionsTest.cs | 214 ++++++++++++++++++ .../GenerateProviders.cs | 47 +++- 8 files changed, 740 insertions(+), 26 deletions(-) create mode 100644 src/DistributedLock.Core/DistributedLockLostException.cs diff --git a/src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs b/src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs index d567f0cd..423a3189 100644 --- a/src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs +++ b/src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs @@ -57,6 +57,7 @@ public static async ValueTask TryAcquireAllAsync( IReadOnlyList primitives, Func> acquireFunc, TimeoutValue timeout, + bool observeLockLostToken, CancellationToken cancellationToken) where TPrimitive : class { @@ -68,22 +69,42 @@ public static async ValueTask TryAcquireAllAsync( TimeoutTracker timeoutTracker = new(timeout); List handles = new(primitives.Count); CompositeDistributedSynchronizationHandle? result = null; - + using var cancellationAggregator = observeLockLostToken ? new CancellationAggregator(primitives.Count) : null; + cancellationAggregator?.Add(cancellationToken); try { - foreach (var primitive in primitives) + for (var i = 0; i < primitives.Count; i++) { - var handle = await acquireFunc(primitive, timeoutTracker.Remaining, cancellationToken) - .ConfigureAwait(false); + if (GetLostPrimitiveOrDefault() is { } lostBeforeAcquire) + { + return new(lostBeforeAcquire, isLost: true); + } - if (handle is null) + IDistributedSynchronizationHandle? handle; + try + { + handle = await acquireFunc(primitives[i], timeoutTracker.Remaining, cancellationAggregator?.Token ?? cancellationToken) + .ConfigureAwait(false); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested && GetLostPrimitiveOrDefault() != null) { - return new(primitive); // failure + return new(GetLostPrimitiveOrDefault()!, isLost: true); } + if (handle is null) + { + return new(primitives[i]); // failure + } handles.Add(handle); + cancellationAggregator?.Add(handle.HandleLostToken); } + if (GetLostPrimitiveOrDefault() is { } lostAfterAcquire) + { + return new(lostAfterAcquire, isLost: true); + } + if (cancellationAggregator != null) { cancellationToken.ThrowIfCancellationRequested(); } + result = new(handles); } finally @@ -93,16 +114,29 @@ public static async ValueTask TryAcquireAllAsync( await DisposeHandlesAsync(handles).ConfigureAwait(false); } } - return new(result); + + // handles[i] was acquired from primitives[i] + object? GetLostPrimitiveOrDefault() + { + if (cancellationAggregator is null) { return null; } + for (var j = 0; j < handles.Count; j++) + { + if (handles[j].HandleLostToken.IsCancellationRequested) { return primitives[j]; } + } + return null; + } } - public readonly struct AcquireResult(object handleOrFailedPrimitive) + public readonly struct AcquireResult(object handleOrFailedPrimitive, bool isLost = false) { public IDistributedSynchronizationHandle? GetHandleOrDefault() => handleOrFailedPrimitive as IDistributedSynchronizationHandle; public IDistributedSynchronizationHandle Handle => - this.GetHandleOrDefault() ?? throw new TimeoutException($"Timed out acquiring '{this.GetFailedName()}'"); + this.GetHandleOrDefault() + ?? throw (isLost + ? new DistributedLockLostException($"Lost '{this.GetFailedName()}' while acquiring remaining locks") + : new TimeoutException($"Timed out acquiring '{this.GetFailedName()}'")); private string GetFailedName() => handleOrFailedPrimitive switch { @@ -113,6 +147,54 @@ public readonly struct AcquireResult(object handleOrFailedPrimitive) }; } + private sealed class CancellationAggregator : IDisposable + { + private readonly CancellationTokenSource _cts = new(); + private readonly object _gate = new(); + private readonly List _registrations; + private bool _disposed; + + public CancellationAggregator(int capacity) + { + _registrations = new List(capacity); + } + public CancellationToken Token => _cts.Token; + + public void Add(CancellationToken token) + { + if (!token.CanBeCanceled) { return; } + + lock (_gate) + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(CancellationAggregator)); + } + _registrations.Add( + token.Register(static s => ((CancellationTokenSource)s!).Cancel(), _cts)); + } + } + + public void Dispose() + { + CancellationTokenRegistration[] toDispose; + lock (_gate) + { + if (_disposed) return; + _disposed = true; + toDispose = [.. _registrations]; + _registrations.Clear(); + } + + foreach (var registration in toDispose) + { + registration.Dispose(); + } + + _cts.Dispose(); + } + } + private static CancellationTokenSource? CreateLinkedCancellationTokenSource(IReadOnlyList handles) { var cancellableTokens = handles @@ -179,16 +261,18 @@ public static async ValueTask GetHandleOrTime this IDistributedLockProvider provider, IReadOnlyList names, TimeoutValue timeout, + bool observeLockLostToken, CancellationToken cancellationToken) => CompositeDistributedSynchronizationHandle.TryAcquireAllAsync( CompositeDistributedSynchronizationHandle.FromNames(names, provider ?? throw new ArgumentNullException(nameof(provider)), static (p, n) => p.CreateLock(n)), static (p, t, c) => SyncViaAsync.IsSynchronous ? p.TryAcquire(t, c).AsValueTask() : p.TryAcquireAsync(t, c), - timeout, cancellationToken); + timeout, observeLockLostToken, cancellationToken); public static ValueTask TryAcquireAllReadLocksInternalAsync( this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeoutValue timeout, + bool observeLockLostToken, CancellationToken cancellationToken) => CompositeDistributedSynchronizationHandle.TryAcquireAllAsync( CompositeDistributedSynchronizationHandle.FromNames( @@ -196,12 +280,13 @@ public static async ValueTask GetHandleOrTime provider ?? throw new ArgumentNullException(nameof(provider)), static (p, n) => p.CreateReaderWriterLock(n)), static (p, t, c) => SyncViaAsync.IsSynchronous ? p.TryAcquireReadLock(t, c).AsValueTask() : p.TryAcquireReadLockAsync(t, c), - timeout, cancellationToken); + timeout, observeLockLostToken, cancellationToken); public static ValueTask TryAcquireAllWriteLocksInternalAsync( this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeoutValue timeout, + bool observeLockLostToken, CancellationToken cancellationToken) => CompositeDistributedSynchronizationHandle.TryAcquireAllAsync( CompositeDistributedSynchronizationHandle.FromNames( @@ -209,13 +294,14 @@ public static async ValueTask GetHandleOrTime provider ?? throw new ArgumentNullException(nameof(provider)), static (p, n) => p.CreateReaderWriterLock(n)), static (p, t, c) => SyncViaAsync.IsSynchronous ? p.TryAcquireWriteLock(t, c).AsValueTask() : p.TryAcquireWriteLockAsync(t, c), - timeout, cancellationToken); + timeout, observeLockLostToken, cancellationToken); public static ValueTask TryAcquireAllSemaphoresInternalAsync( this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeoutValue timeout, + bool observeLockLostToken, CancellationToken cancellationToken) => CompositeDistributedSynchronizationHandle.TryAcquireAllAsync( CompositeDistributedSynchronizationHandle.FromNames( @@ -224,5 +310,5 @@ public static async ValueTask GetHandleOrTime static (s, n) => s.provider.CreateSemaphore(n, s.maxCount)), static (p, t, c) => SyncViaAsync.IsSynchronous ? p.TryAcquire(t, c).AsValueTask() : p.TryAcquireAsync(t, c), timeout, - cancellationToken); + observeLockLostToken, cancellationToken); } \ No newline at end of file diff --git a/src/DistributedLock.Core/DistributedLockLostException.cs b/src/DistributedLock.Core/DistributedLockLostException.cs new file mode 100644 index 00000000..78f48967 --- /dev/null +++ b/src/DistributedLock.Core/DistributedLockLostException.cs @@ -0,0 +1,29 @@ +namespace Medallion.Threading; + +/// +/// An exception thrown when acquiring multiple locks/primitives fails because an already-acquired +/// lock was lost before all of them could be acquired +/// +[Serializable] +public sealed class DistributedLockLostException : InvalidOperationException +{ + /// + /// Constructs a new instance of with a default message + /// + public DistributedLockLostException() : this("A lock was lost during acquisition") { } + + /// + /// Constructs an instance of with the given + /// + public DistributedLockLostException(string message) : base(message) { } + + /// + /// Constructs an instance of with the given and + /// + public DistributedLockLostException(string message, Exception innerException) : base(message, innerException) { } + +#if NET8_0_OR_GREATER + [Obsolete] // calls obsolete constructor +#endif + private DistributedLockLostException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } +} diff --git a/src/DistributedLock.Core/DistributedLockProviderExtensions.cs b/src/DistributedLock.Core/DistributedLockProviderExtensions.cs index be307746..2e8846e0 100644 --- a/src/DistributedLock.Core/DistributedLockProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedLockProviderExtensions.cs @@ -47,29 +47,109 @@ public static ValueTask AcquireLockAsync(this /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.TryAcquireAllLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.AcquireAllLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.AcquireAllLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => - provider.TryAcquireAllLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrDefault(); + provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrDefault(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => - provider.TryAcquireAllLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrTimeout(); + provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrTimeout(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each lock. + /// The names of the locks to acquire. + /// The total time to wait for all locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion } \ No newline at end of file diff --git a/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs b/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs index cf8876a9..b489b0f2 100644 --- a/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs @@ -75,57 +75,217 @@ public static ValueTask AcquireWriteLockAsync /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle? TryAcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllReadLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle? TryAcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.TryAcquireAllReadLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.AcquireAllReadLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.AcquireAllReadLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask TryAcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => - provider.TryAcquireAllReadLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrDefault(); + provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrDefault(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask TryAcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask AcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => - provider.TryAcquireAllReadLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrTimeout(); + provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrTimeout(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire read locks. + /// The total time to wait for all read locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask AcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle? TryAcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllWriteLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle? TryAcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.TryAcquireAllWriteLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.AcquireAllWriteLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.AcquireAllWriteLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask TryAcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout = default, CancellationToken cancellationToken = default) => - provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrDefault(); + provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrDefault(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask TryAcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask AcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => - provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrTimeout(); + provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrTimeout(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each reader-writer lock. + /// The names of the reader-writer locks on which to acquire write locks. + /// The total time to wait for all write locks to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask AcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion } \ No newline at end of file diff --git a/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs b/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs index 578348ae..1ff4fe3f 100644 --- a/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs @@ -47,29 +47,117 @@ public static ValueTask AcquireSemaphoreAsync /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle? TryAcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout = default, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.cancellationToken), (provider, names, maxCount, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle? TryAcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.TryAcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, maxCount, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// Specifies a token by which the wait can be canceled. public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => SyncViaAsync.Run(static s => s.provider.AcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.cancellationToken), (provider, names, maxCount, timeout, cancellationToken)); + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + SyncViaAsync.Run(static s => s.provider.AcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, maxCount, timeout, observeLockLostToken, cancellationToken)); + /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask TryAcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout = default, CancellationToken cancellationToken = default) => - provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, cancellationToken).GetHandleOrDefault(); + provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrDefault(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and null is returned. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask TryAcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// /// Equivalent to calling for each name in and then /// on each created instance, combining the results into a composite handle. /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// Specifies a token by which the wait can be canceled. public static ValueTask AcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => - provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, cancellationToken).GetHandleOrTimeout(); + provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken: false, cancellationToken).GetHandleOrTimeout(); + + /// + /// Equivalent to calling for each name in and then + /// on each created instance, combining the results into a composite handle. + /// + /// The provider used to create each semaphore. + /// The names of the semaphores to acquire. + /// The max count of each semaphore. + /// The total time to wait for all semaphores to be acquired. + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and a is thrown. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + /// Specifies a token by which the wait can be canceled. + public static ValueTask AcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion } \ No newline at end of file diff --git a/src/DistributedLock.Core/PublicAPI.Unshipped.txt b/src/DistributedLock.Core/PublicAPI.Unshipped.txt index e69de29b..862cc519 100644 --- a/src/DistributedLock.Core/PublicAPI.Unshipped.txt +++ b/src/DistributedLock.Core/PublicAPI.Unshipped.txt @@ -0,0 +1,20 @@ +Medallion.Threading.DistributedLockLostException +Medallion.Threading.DistributedLockLostException.DistributedLockLostException() -> void +Medallion.Threading.DistributedLockLostException.DistributedLockLostException(string! message) -> void +Medallion.Threading.DistributedLockLostException.DistributedLockLostException(string! message, System.Exception! innerException) -> void +static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask diff --git a/src/DistributedLock.Tests/Tests/Core/DistributedLockProviderExtensionsTest.cs b/src/DistributedLock.Tests/Tests/Core/DistributedLockProviderExtensionsTest.cs index 71799e7c..d1df1ff8 100644 --- a/src/DistributedLock.Tests/Tests/Core/DistributedLockProviderExtensionsTest.cs +++ b/src/DistributedLock.Tests/Tests/Core/DistributedLockProviderExtensionsTest.cs @@ -328,4 +328,218 @@ public async Task TestCompositeHandleLostToken() ctsA.Dispose(); ctsB.Dispose(); } + + [Test, Combinatorial] + public async Task TestCompositeObserveLockLostTokenPassesLinkedTokenToSubsequentAcquires([Values] bool observe, [Values] bool isAsync) + { + using var ctsA = new CancellationTokenSource(); + var handleA = CreateHandle(ctsA.Token); + var handleB = CreateHandle(CancellationToken.None); + bool? tokenBCanceledByLoss = null; + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, c) => + { + ctsA.Cancel(); + tokenBCanceledByLoss = c.IsCancellationRequested; + return handleB.Object; + })); + + await using var result = await AcquireAllAsync(provider, ["A", "B"], isTry: true, isAsync, observe, default); + + Assert.That(tokenBCanceledByLoss, Is.EqualTo(observe)); + Assert.That(result, observe ? Is.Null : Is.Not.Null); + } + + [Test, Combinatorial] + public async Task TestCompositeLockLostDuringAcquisition([Values] bool isTry, [Values] bool isAsync) + { + using var ctsA = new CancellationTokenSource(); + var handleA = CreateHandle(ctsA.Token); + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, c) => + { + ctsA.Cancel(); + c.ThrowIfCancellationRequested(); + throw new AssertionException("should have been canceled"); + })); + + await AssertLostAsync(provider, ["A", "B"], isTry, isAsync); + VerifyDisposedOnce(handleA, isAsync); + } + + [Test, Combinatorial] + public async Task TestCompositeLockLostAfterLastAcquireReleasesHandles([Values] bool isTry, [Values] bool isAsync) + { + using var ctsA = new CancellationTokenSource(); + var handleA = CreateHandle(ctsA.Token); + var handleB = CreateHandle(CancellationToken.None); + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, _) => { ctsA.Cancel(); return handleB.Object; })); + + await AssertLostAsync(provider, ["A", "B"], isTry, isAsync); + VerifyDisposedOnce(handleA, isAsync); + VerifyDisposedOnce(handleB, isAsync); + } + + [Test, Combinatorial] + public void TestCompositeCallerCancellationStillThrowsOperationCanceled([Values] bool isTry, [Values] bool isAsync) + { + using var callerCts = new CancellationTokenSource(); + var handleA = CreateHandle(CancellationToken.None); + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, c) => + { + callerCts.Cancel(); + c.ThrowIfCancellationRequested(); + throw new AssertionException("should have been canceled"); + })); + + var ex = Assert.CatchAsync( + () => AcquireAllAsync(provider, ["A", "B"], isTry, isAsync, observeLockLostToken: true, callerCts.Token).AsTask()); + Assert.That(ex!.CancellationToken.IsCancellationRequested, Is.True); + VerifyDisposedOnce(handleA, isAsync); + } + + [Test, Combinatorial] + public async Task TestCompositeLockLostWhenAlreadyCancelledHandleToken([Values] bool isTry, [Values] bool isAsync) + { + var handleA = CreateHandle(new CancellationToken(canceled: true)); + var bInvoked = false; + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, _) => { bInvoked = true; return CreateHandle(CancellationToken.None).Object; })); + + await AssertLostAsync(provider, ["A", "B"], isTry, isAsync); + Assert.That(bInvoked, Is.False); + VerifyDisposedOnce(handleA, isAsync); + } + + [Test, Combinatorial] + public async Task TestCompositeObserveLockLostTokenIgnoresNonCancelableTokens([Values] bool isTry, [Values] bool isAsync) + { + var handleA = CreateHandle(CancellationToken.None); + var handleB = CreateHandle(CancellationToken.None); + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, _) => handleB.Object)); + + await using var result = await AcquireAllAsync(provider, ["A", "B"], isTry, isAsync, observeLockLostToken: true, default); + Assert.That(result, Is.Not.Null); + } + + [Test, Combinatorial] + public async Task TestCompositeObserveLockLostTokenSingleName([Values] bool isAsync) + { + using var callerCts = new CancellationTokenSource(); + var handleA = CreateHandle(CancellationToken.None); + CancellationToken tokenA = default; + var provider = CreateProvider(isAsync, ("A", (_, c) => { tokenA = c; return handleA.Object; })); + + var result = await AcquireAllAsync(provider, ["A"], isTry: true, isAsync, observeLockLostToken: true, callerCts.Token); + + Assert.That(result, Is.SameAs(handleA.Object)); + Assert.That(tokenA, Is.EqualTo(callerCts.Token)); + } + + [Test, Combinatorial] + public async Task TestCompositeObserveLockLostTokenNotObservedWhenDisabled([Values] bool isTry, [Values] bool isAsync) + { + using var ctsA = new CancellationTokenSource(); + var handleA = CreateHandle(ctsA.Token); + var handleB = CreateHandle(CancellationToken.None); + var provider = CreateProvider( + isAsync, + ("A", (_, _) => handleA.Object), + ("B", (_, c) => + { + ctsA.Cancel(); + c.ThrowIfCancellationRequested(); + return handleB.Object; + })); + + await using var result = await AcquireAllAsync(provider, ["A", "B"], isTry, isAsync, observeLockLostToken: false, default); + Assert.That(result, Is.Not.Null); + handleA.Verify(h => h.Dispose(), Times.Never); + handleA.Verify(h => h.DisposeAsync(), Times.Never); + } + + private static Mock CreateHandle(CancellationToken handleLostToken) + { + var handle = new Mock(); + handle.Setup(h => h.HandleLostToken).Returns(handleLostToken); + return handle; + } + + private static IDistributedLockProvider CreateProvider( + bool isAsync, + params (string Name, Func Acquire)[] locks) + { + var provider = new Mock(MockBehavior.Strict); + foreach (var (name, acquire) in locks) + { + var @lock = new Mock(MockBehavior.Strict); + @lock.Setup(l => l.Name).Returns(name); + if (isAsync) + { + @lock.Setup(l => l.TryAcquireAsync(It.IsAny(), It.IsAny())) + .Returns((TimeSpan t, CancellationToken c) => + { + try { return new ValueTask(acquire(t, c)); } + catch (Exception ex) { return new ValueTask(Task.FromException(ex)); } + }); + } + else + { + @lock.Setup(l => l.TryAcquire(It.IsAny(), It.IsAny())) + .Returns((TimeSpan t, CancellationToken c) => acquire(t, c)); + } + provider.Setup(p => p.CreateLock(name)).Returns(@lock.Object); + } + return provider.Object; + } + + private static async ValueTask AcquireAllAsync( + IDistributedLockProvider provider, + IReadOnlyList names, + bool isTry, + bool isAsync, + bool observeLockLostToken, + CancellationToken cancellationToken) => + (isTry, isAsync) switch + { + (true, true) => await provider.TryAcquireAllLocksAsync(names, TimeSpan.FromSeconds(10), observeLockLostToken, cancellationToken), + (true, false) => provider.TryAcquireAllLocks(names, TimeSpan.FromSeconds(10), observeLockLostToken, cancellationToken), + (false, true) => await provider.AcquireAllLocksAsync(names, TimeSpan.FromSeconds(10), observeLockLostToken, cancellationToken), + (false, false) => provider.AcquireAllLocks(names, TimeSpan.FromSeconds(10), observeLockLostToken, cancellationToken), + }; + + private static async Task AssertLostAsync(IDistributedLockProvider provider, IReadOnlyList names, bool isTry, bool isAsync) + { + if (isTry) + { + Assert.That(await AcquireAllAsync(provider, names, isTry, isAsync, observeLockLostToken: true, default), Is.Null); + } + else + { + var ex = Assert.ThrowsAsync( + () => AcquireAllAsync(provider, names, isTry, isAsync, observeLockLostToken: true, default).AsTask()); + Assert.That(ex!.Message, Does.Contain("'A'")); + } + } + + private static void VerifyDisposedOnce(Mock handle, bool isAsync) + { + if (isAsync) { handle.Verify(h => h.DisposeAsync(), Times.Once); } + else { handle.Verify(h => h.Dispose(), Times.Once); } + } } diff --git a/src/DistributedLockCodeGen/GenerateProviders.cs b/src/DistributedLockCodeGen/GenerateProviders.cs index 5258ff67..8fa4c48d 100644 --- a/src/DistributedLockCodeGen/GenerateProviders.cs +++ b/src/DistributedLockCodeGen/GenerateProviders.cs @@ -1,4 +1,4 @@ -using NUnit.Framework; +using NUnit.Framework; using System; using System.Collections.Generic; using System.IO; @@ -86,19 +86,56 @@ public interface {{providerInterfaceName}}{{(interfaceName == "IDistributedUpgra + "s" + (isAsync ? "Async" : ""); var isSemaphore = interfaceName.Contains("Semaphore"); + var isReaderWriterLock = interfaceName.Contains("ReaderWriterLock"); + var primitiveNoun = isSemaphore ? "semaphore" : isReaderWriterLock ? "reader-writer lock" : "lock"; + var acquiredNoun = isReaderWriterLock ? (m.Groups["name"].Value.Contains("Read") ? "read locks" : "write locks") : primitiveNoun + "s"; string MaxCountArg(string prefix = "") => isSemaphore ? prefix + "maxCount, " : ""; - - return $""" + var internalMethodName = $"Try{extensionMethodName.Replace("Try", "").Replace("Async", "InternalAsync")}"; + var getHandleMethodName = $"GetHandleOr{(isTry ? "Default" : "Timeout")}"; + var timeoutType = m.Groups["parameterType"].Captures[0].Value; + var summary = $""" /// /// Equivalent to calling for each name in and then /// c.Value))})" /> on each created instance, combining the results into a composite handle. /// + """; + var leadingParamDocs = $""" + /// The provider used to create each {primitiveNoun}. + /// {(isReaderWriterLock ? $"The names of the reader-writer locks on which to acquire {acquiredNoun}." : $"The names of the {acquiredNoun} to acquire.")}{(isSemaphore ? """ + + /// The max count of each semaphore. + """ : string.Empty)} + /// The total time to wait for all {acquiredNoun} to be acquired. + """; + var cancellationTokenParamDoc = """ + /// Specifies a token by which the wait can be canceled. + """; + + return $""" + {summary} + {leadingParamDocs} + {cancellationTokenParamDoc} public static {m.Groups["returnType"].Value} {extensionMethodName}(this {providerInterfaceName} provider, IReadOnlyList names{(isSemaphore ? ", int maxCount" : string.Empty)}, {m.Groups["parameters"].Value}) => {( isAsync - ? $"provider.Try{extensionMethodName.Replace("Try", "").Replace("Async", "InternalAsync")}(names, {MaxCountArg()}timeout, cancellationToken).GetHandleOr{(isTry ? "Default" : "Timeout")}();" + ? $"provider.{internalMethodName}(names, {MaxCountArg()}timeout, observeLockLostToken: false, cancellationToken).{getHandleMethodName}();" : $"SyncViaAsync.Run(static s => s.provider.{extensionMethodName}Async(s.names, {MaxCountArg("s.")}s.timeout, s.cancellationToken), (provider, names, {MaxCountArg()}timeout, cancellationToken));" )} + + {summary} + {leadingParamDocs} + /// + /// Whether to observe the of already-acquired handles while acquiring the remaining ones. + /// If a handle is lost mid-acquisition, all acquired handles are released and {(isTry ? "null is returned" : "a is thrown")}. + /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. + /// + {cancellationTokenParamDoc} + public static {m.Groups["returnType"].Value} {extensionMethodName}(this {providerInterfaceName} provider, IReadOnlyList names{(isSemaphore ? ", int maxCount" : string.Empty)}, {timeoutType} timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + {( + isAsync + ? $"provider.{internalMethodName}(names, {MaxCountArg()}timeout, observeLockLostToken, cancellationToken).{getHandleMethodName}();" + : $"SyncViaAsync.Run(static s => s.provider.{extensionMethodName}Async(s.names, {MaxCountArg("s.")}s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, {MaxCountArg()}timeout, observeLockLostToken, cancellationToken));" + )} """; } ); @@ -152,4 +189,4 @@ string GetExtensionMethodName(string interfaceMethodName) => + (interfaceMethodName.EndsWith("Async") ? "Async" : string.Empty) : interfaceMethodName; } -} \ No newline at end of file +} From 61f82a217b49ce4aa4ab0d127386023ee71b50b1 Mon Sep 17 00:00:00 2001 From: Gunnar Liljas Date: Tue, 20 Oct 2026 15:49:11 +0200 Subject: [PATCH 2/2] Make cancellationToken required on the new overloads. --- .../DistributedLockProviderExtensions.cs | 8 ++--- ...butedReaderWriterLockProviderExtensions.cs | 16 +++++----- .../DistributedSemaphoreProviderExtensions.cs | 8 ++--- .../PublicAPI.Unshipped.txt | 32 +++++++++---------- .../GenerateProviders.cs | 2 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/DistributedLock.Core/DistributedLockProviderExtensions.cs b/src/DistributedLock.Core/DistributedLockProviderExtensions.cs index 2e8846e0..a8dfb394 100644 --- a/src/DistributedLock.Core/DistributedLockProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedLockProviderExtensions.cs @@ -67,7 +67,7 @@ public static ValueTask AcquireLockAsync(this /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -94,7 +94,7 @@ public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistribute /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.AcquireAllLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -121,7 +121,7 @@ public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistribute /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// @@ -148,7 +148,7 @@ public static ValueTask AcquireAllLocksAsync( /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion diff --git a/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs b/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs index b489b0f2..28551044 100644 --- a/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs @@ -95,7 +95,7 @@ public static ValueTask AcquireWriteLockAsync /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle? TryAcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle? TryAcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllReadLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -122,7 +122,7 @@ public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistri /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.AcquireAllReadLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -149,7 +149,7 @@ public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistri /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask TryAcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask TryAcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// @@ -176,7 +176,7 @@ public static ValueTask AcquireAllReadLocksAs /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask AcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask AcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllReadLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); /// @@ -203,7 +203,7 @@ public static ValueTask AcquireAllReadLocksAs /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle? TryAcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle? TryAcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllWriteLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -230,7 +230,7 @@ public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistr /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.AcquireAllWriteLocksAsync(s.names, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, timeout, observeLockLostToken, cancellationToken)); /// @@ -257,7 +257,7 @@ public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistr /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask TryAcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask TryAcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// @@ -284,7 +284,7 @@ public static ValueTask AcquireAllWriteLocksA /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask AcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask AcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList names, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllWriteLocksInternalAsync(names, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion diff --git a/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs b/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs index 1ff4fe3f..eb1c5e49 100644 --- a/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs +++ b/src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs @@ -69,7 +69,7 @@ public static ValueTask AcquireSemaphoreAsync /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle? TryAcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle? TryAcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.TryAcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, maxCount, timeout, observeLockLostToken, cancellationToken)); /// @@ -98,7 +98,7 @@ public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistr /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => SyncViaAsync.Run(static s => s.provider.AcquireAllSemaphoresAsync(s.names, s.maxCount, s.timeout, s.observeLockLostToken, s.cancellationToken), (provider, names, maxCount, timeout, observeLockLostToken, cancellationToken)); /// @@ -127,7 +127,7 @@ public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistr /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask TryAcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask TryAcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken, cancellationToken).GetHandleOrDefault(); /// @@ -156,7 +156,7 @@ public static ValueTask AcquireAllSemaphoresA /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// /// Specifies a token by which the wait can be canceled. - public static ValueTask AcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static ValueTask AcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList names, int maxCount, TimeSpan? timeout, bool observeLockLostToken, CancellationToken cancellationToken) => provider.TryAcquireAllSemaphoresInternalAsync(names, maxCount, timeout, observeLockLostToken, cancellationToken).GetHandleOrTimeout(); # endregion diff --git a/src/DistributedLock.Core/PublicAPI.Unshipped.txt b/src/DistributedLock.Core/PublicAPI.Unshipped.txt index 862cc519..35d613a1 100644 --- a/src/DistributedLock.Core/PublicAPI.Unshipped.txt +++ b/src/DistributedLock.Core/PublicAPI.Unshipped.txt @@ -2,19 +2,19 @@ Medallion.Threading.DistributedLockLostException Medallion.Threading.DistributedLockLostException.DistributedLockLostException() -> void Medallion.Threading.DistributedLockLostException.DistributedLockLostException(string! message) -> void Medallion.Threading.DistributedLockLostException.DistributedLockLostException(string! message, System.Exception! innerException) -> void -static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! -static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? -static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle! -static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Medallion.Threading.IDistributedSynchronizationHandle? -static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocksAsync(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedLockProviderExtensions.AcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedLockProviderExtensions.TryAcquireAllLocks(this Medallion.Threading.IDistributedLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.AcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllReadLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocks(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedReaderWriterLockProviderExtensions.TryAcquireAllWriteLocksAsync(this Medallion.Threading.IDistributedReaderWriterLockProvider! provider, System.Collections.Generic.IReadOnlyList! names, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle! +static Medallion.Threading.DistributedSemaphoreProviderExtensions.AcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan? timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask +static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphores(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> Medallion.Threading.IDistributedSynchronizationHandle? +static Medallion.Threading.DistributedSemaphoreProviderExtensions.TryAcquireAllSemaphoresAsync(this Medallion.Threading.IDistributedSemaphoreProvider! provider, System.Collections.Generic.IReadOnlyList! names, int maxCount, System.TimeSpan timeout, bool observeLockLostToken, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask diff --git a/src/DistributedLockCodeGen/GenerateProviders.cs b/src/DistributedLockCodeGen/GenerateProviders.cs index 8fa4c48d..709d2911 100644 --- a/src/DistributedLockCodeGen/GenerateProviders.cs +++ b/src/DistributedLockCodeGen/GenerateProviders.cs @@ -130,7 +130,7 @@ public interface {{providerInterfaceName}}{{(interfaceName == "IDistributedUpgra /// Has no effect when only one name is given. Note that accessing the token may start background monitoring for some providers. /// {cancellationTokenParamDoc} - public static {m.Groups["returnType"].Value} {extensionMethodName}(this {providerInterfaceName} provider, IReadOnlyList names{(isSemaphore ? ", int maxCount" : string.Empty)}, {timeoutType} timeout, bool observeLockLostToken, CancellationToken cancellationToken = default) => + public static {m.Groups["returnType"].Value} {extensionMethodName}(this {providerInterfaceName} provider, IReadOnlyList names{(isSemaphore ? ", int maxCount" : string.Empty)}, {timeoutType} timeout, bool observeLockLostToken, CancellationToken cancellationToken) => {( isAsync ? $"provider.{internalMethodName}(names, {MaxCountArg()}timeout, observeLockLostToken, cancellationToken).{getHandleMethodName}();"