Table of Contents

Class AsyncKeyedReaderWriterLock<TKey>

Namespace
LfrlAnvil.Async
Assembly
LfrlAnvil.Core.dll

Represents a source of a keyed collection of fair asynchronous reader-writer locks.

public sealed class AsyncKeyedReaderWriterLock<TKey> where TKey : notnull

Type Parameters

TKey

Key's type.

Inheritance
AsyncKeyedReaderWriterLock<TKey>
Inherited Members
Extension Methods

Remarks

Lock is not reentrant.

Constructors

AsyncKeyedReaderWriterLock(IEqualityComparer<TKey>?)

Creates a new AsyncKeyedReaderWriterLock<TKey> instance.

public AsyncKeyedReaderWriterLock(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

EnterReadAsync(TKey, CancellationToken)

Asynchronously acquires a read lock for the specified key from this reader-writer lock.

public ValueTask<AsyncKeyedReaderWriterLockReadToken<TKey>> EnterReadAsync(TKey key, CancellationToken cancellationToken = default)

Parameters

key TKey

Key for which to acquire a read lock.

cancellationToken CancellationToken

Optional CancellationToken that can be used to cancel pending read lock acquisition.

Returns

ValueTask<AsyncKeyedReaderWriterLockReadToken<TKey>>

New ValueTask instance which returns an AsyncKeyedReaderWriterLockReadToken<TKey> value.

Exceptions

OperationCanceledException

When provided cancellationToken was cancelled before the read lock was acquired.

EnterUpgradeableReadAsync(TKey, CancellationToken)

Asynchronously acquires an upgradeable read lock for the specified key from this reader-writer lock.

public ValueTask<AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey>> EnterUpgradeableReadAsync(TKey key, CancellationToken cancellationToken = default)

Parameters

key TKey

Key for which to acquire an upgradeable lock.

cancellationToken CancellationToken

Optional CancellationToken that can be used to cancel pending upgradeable read lock acquisition.

Returns

ValueTask<AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey>>

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

Exceptions

OperationCanceledException

When provided cancellationToken was cancelled before the upgradeable read lock was acquired.

EnterWriteAsync(TKey, CancellationToken)

Asynchronously acquires a write lock for the specified key from this reader-writer lock.

public ValueTask<AsyncKeyedReaderWriterLockWriteToken<TKey>> EnterWriteAsync(TKey key, CancellationToken cancellationToken = default)

Parameters

key TKey

Key for which to acquire a write lock.

cancellationToken CancellationToken

Optional CancellationToken that can be used to cancel pending write lock acquisition.

Returns

ValueTask<AsyncKeyedReaderWriterLockWriteToken<TKey>>

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

Exceptions

OperationCanceledException

When provided cancellationToken was cancelled before the write lock was acquired.

Participants(TKey)

Returns the total number of lock participants for the provided key, which includes current lock holders 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()

TryEnterRead(TKey, out bool)

Attempts to synchronously acquire a read lock for the specified key from this reader-writer lock.

public AsyncKeyedReaderWriterLockReadToken<TKey> TryEnterRead(TKey key, out bool entered)

Parameters

key TKey

Key for which to acquire a read lock.

entered bool

out parameter which specifies whether the read lock was acquired.

Returns

AsyncKeyedReaderWriterLockReadToken<TKey>

New AsyncKeyedReaderWriterLockReadToken<TKey> value. When entered is false, then returned instanced will be a default value.

TryEnterUpgradeableRead(TKey, out bool)

Attempts to synchronously acquire an upgradeable read lock for the specified key from this reader-writer lock.

public AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey> TryEnterUpgradeableRead(TKey key, out bool entered)

Parameters

key TKey

Key for which to acquire an upgradeable lock.

entered bool

out parameter which specifies whether the upgradeable read lock was acquired.

Returns

AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey>

New AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey> value. When entered is false, then returned instanced will be a default value.

TryEnterWrite(TKey, out bool)

Attempts to synchronously acquire a write lock for the specified key from this reader-writer lock.

public AsyncKeyedReaderWriterLockWriteToken<TKey> TryEnterWrite(TKey key, out bool entered)

Parameters

key TKey

Key for which to acquire a write lock.

entered bool

out parameter which specifies whether the write lock was acquired.

Returns

AsyncKeyedReaderWriterLockWriteToken<TKey>

New AsyncKeyedReaderWriterLockWriteToken<TKey> value. When entered is false, then returned instanced will be a default value.