Table of Contents

Class AsyncKeyedMutex<TKey>

Namespace
LfrlAnvil.Async
Assembly
LfrlAnvil.Core.dll

Represents a source of a keyed collection of fair asynchronous mutex primitives.

public sealed class AsyncKeyedMutex<TKey> where TKey : notnull

Type Parameters

TKey

Key's type.

Inheritance
AsyncKeyedMutex<TKey>
Inherited Members
Extension Methods

Remarks

Lock is not reentrant.

Constructors

AsyncKeyedMutex(IEqualityComparer<TKey>?)

Creates a new AsyncKeyedMutex<TKey> instance.

public AsyncKeyedMutex(IEqualityComparer<TKey>? keyComparer = null)

Parameters

keyComparer IEqualityComparer<TKey>

Optional TKey comparer. Equal to null by default.

Properties

ActiveKeys

Returns the collection of all active keys.

public TKey[] ActiveKeys { get; }

Property Value

TKey[]

KeyComparer

Used key comparer.

public IEqualityComparer<TKey> KeyComparer { get; }

Property Value

IEqualityComparer<TKey>

Methods

EnterAsync(TKey, CancellationToken)

Asynchronously acquires an exclusive lock for the specified key from this mutex.

public ValueTask<AsyncKeyedMutexLock<TKey>> EnterAsync(TKey key, CancellationToken cancellationToken = default)

Parameters

key TKey

Key for which to acquire an exclusive lock.

cancellationToken CancellationToken

Optional CancellationToken that can be used to cancel pending mutex acquisition.

Returns

ValueTask<AsyncKeyedMutexLock<TKey>>

New ValueTask<TResult> instance which returns an AsyncKeyedMutexLock<TKey> value.

Exceptions

OperationCanceledException

When provided cancellationToken was cancelled before the lock was acquired.

Participants(TKey)

Returns the total number of lock participants for the provided key, which includes current lock holder and all waiters.

public int Participants(TKey key)

Parameters

key TKey

Key to check.

Returns

int

Total number of lock participants.

TrimExcess()

Attempts to discard unused resources.

public void TrimExcess()