Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
505 changes: 505 additions & 0 deletions src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions src/DistributedLock.Core/DistributedLockProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Medallion.Threading;
/// </summary>
public static class DistributedLockProviderExtensions
{
# region Single Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> and then
/// <see cref="IDistributedLock.TryAcquire(TimeSpan, CancellationToken)" />.
Expand Down Expand Up @@ -34,4 +36,50 @@ public static IDistributedSynchronizationHandle AcquireLock(this IDistributedLoc
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireLockAsync(this IDistributedLockProvider provider, string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateLock(name).AcquireAsync(timeout, cancellationToken);

# endregion

# region Composite Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.TryAcquire(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAll(
provider,
static (p, n, t, c) => p.TryAcquireLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.Acquire(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAll(
provider,
static (p, n, t, c) => p.AcquireLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.TryAcquireAsync(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAllAsync(
provider,
static (p, n, t, c) => p.TryAcquireLockAsync(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.AcquireAsync(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAllAsync(
provider,
static (p, n, t, c) => p.AcquireLockAsync(n, t, c),
names, timeout, cancellationToken);

# endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Medallion.Threading;
/// </summary>
public static class DistributedReaderWriterLockProviderExtensions
{
# region Single Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> and then
/// <see cref="IDistributedReaderWriterLock.TryAcquireReadLock(TimeSpan, CancellationToken)" />.
Expand Down Expand Up @@ -62,4 +64,90 @@ public static IDistributedSynchronizationHandle AcquireWriteLock(this IDistribut
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireWriteLockAsync(this IDistributedReaderWriterLockProvider provider, string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateReaderWriterLock(name).AcquireWriteLockAsync(timeout, cancellationToken);

# endregion

# region Composite Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.TryAcquireReadLock(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAll(
provider,
static (p, n, t, c) => p.TryAcquireReadLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.AcquireReadLock(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle AcquireAllReadLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAll(
provider,
static (p, n, t, c) => p.AcquireReadLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.TryAcquireReadLockAsync(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAllAsync(
provider,
static (p, n, t, c) => p.TryAcquireReadLockAsync(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.AcquireReadLockAsync(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireAllReadLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAllAsync(
provider,
static (p, n, t, c) => p.AcquireReadLockAsync(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.TryAcquireWriteLock(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAll(
provider,
static (p, n, t, c) => p.TryAcquireWriteLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.AcquireWriteLock(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle AcquireAllWriteLocks(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAll(
provider,
static (p, n, t, c) => p.AcquireWriteLock(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.TryAcquireWriteLockAsync(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAllAsync(
provider,
static (p, n, t, c) => p.TryAcquireWriteLockAsync(n, t, c),
names, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedReaderWriterLockProvider.CreateReaderWriterLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedReaderWriterLock.AcquireWriteLockAsync(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireAllWriteLocksAsync(this IDistributedReaderWriterLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAllAsync(
provider,
static (p, n, t, c) => p.AcquireWriteLockAsync(n, t, c),
names, timeout, cancellationToken);

# endregion
}
48 changes: 48 additions & 0 deletions src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Medallion.Threading;
/// </summary>
public static class DistributedSemaphoreProviderExtensions
{
# region Single Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedSemaphoreProvider.CreateSemaphore(string, int)" /> and then
/// <see cref="IDistributedSemaphore.TryAcquire(TimeSpan, CancellationToken)" />.
Expand Down Expand Up @@ -34,4 +36,50 @@ public static IDistributedSynchronizationHandle AcquireSemaphore(this IDistribut
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireSemaphoreAsync(this IDistributedSemaphoreProvider provider, string name, int maxCount, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateSemaphore(name, maxCount).AcquireAsync(timeout, cancellationToken);

# endregion

# region Composite Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedSemaphoreProvider.CreateSemaphore(string, int)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedSemaphore.TryAcquire(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList<string> names, int maxCount, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAll(
provider,
static (p, n, mc, t, c) => p.TryAcquireSemaphore(n, mc, t, c),
names, maxCount, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedSemaphoreProvider.CreateSemaphore(string, int)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedSemaphore.Acquire(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle AcquireAllSemaphores(this IDistributedSemaphoreProvider provider, IReadOnlyList<string> names, int maxCount, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAll(
provider,
static (p, n, mc, t, c) => p.AcquireSemaphore(n, mc, t, c),
names, maxCount, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedSemaphoreProvider.CreateSemaphore(string, int)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedSemaphore.TryAcquireAsync(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList<string> names, int maxCount, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.TryAcquireAllAsync(
provider,
static (p, n, mc, t, c) => p.TryAcquireSemaphoreAsync(n, mc, t, c),
names, maxCount, timeout, cancellationToken);

/// <summary>
/// Equivalent to calling <see cref="IDistributedSemaphoreProvider.CreateSemaphore(string, int)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedSemaphore.AcquireAsync(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireAllSemaphoresAsync(this IDistributedSemaphoreProvider provider, IReadOnlyList<string> names, int maxCount, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
CompositeDistributedSynchronizationHandle.AcquireAllAsync(
provider,
static (p, n, mc, t, c) => p.AcquireSemaphoreAsync(n, mc, t, c),
names, maxCount, timeout, cancellationToken);

# endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Medallion.Threading;
/// </summary>
public static class DistributedUpgradeableReaderWriterLockProviderExtensions
{
# region Single Lock Methods

/// <summary>
/// Equivalent to calling <see cref="IDistributedUpgradeableReaderWriterLockProvider.CreateUpgradeableReaderWriterLock(string)" /> and then
/// <see cref="IDistributedUpgradeableReaderWriterLock.TryAcquireUpgradeableReadLock(TimeSpan, CancellationToken)" />.
Expand Down Expand Up @@ -34,4 +36,13 @@ public static IDistributedLockUpgradeableHandle AcquireUpgradeableReadLock(this
/// </summary>
public static ValueTask<IDistributedLockUpgradeableHandle> AcquireUpgradeableReadLockAsync(this IDistributedUpgradeableReaderWriterLockProvider provider, string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateUpgradeableReaderWriterLock(name).AcquireUpgradeableReadLockAsync(timeout, cancellationToken);

# endregion

# region Composite Lock Methods

// Composite methods are not supported for IDistributedUpgradeableReaderWriterLock
// because a composite acquire operation must be able to roll back and upgrade does not support that.

# endregion
}
Loading