Class AsyncKeyedReaderWriterLock<TKey>
Represents a source of a keyed collection of fair asynchronous reader-writer locks.
public sealed class AsyncKeyedReaderWriterLock<TKey> where TKey : notnull
Type Parameters
TKeyKey'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
keyComparerIEqualityComparer<TKey>Optional
TKeycomparer. 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
keyTKeyKey for which to acquire a read lock.
cancellationTokenCancellationTokenOptional 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
cancellationTokenwas 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
keyTKeyKey for which to acquire an upgradeable lock.
cancellationTokenCancellationTokenOptional 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
cancellationTokenwas 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
keyTKeyKey for which to acquire a write lock.
cancellationTokenCancellationTokenOptional 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
cancellationTokenwas 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
keyTKeyKey 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
keyTKeyKey for which to acquire a read lock.
enteredboolout parameter which specifies whether the read lock was acquired.
Returns
- AsyncKeyedReaderWriterLockReadToken<TKey>
New AsyncKeyedReaderWriterLockReadToken<TKey> value. When
enteredis 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
keyTKeyKey for which to acquire an upgradeable lock.
enteredboolout parameter which specifies whether the upgradeable read lock was acquired.
Returns
- AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey>
New AsyncKeyedReaderWriterLockUpgradeableReadToken<TKey> value. When
enteredis 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
keyTKeyKey for which to acquire a write lock.
enteredboolout parameter which specifies whether the write lock was acquired.
Returns
- AsyncKeyedReaderWriterLockWriteToken<TKey>
New AsyncKeyedReaderWriterLockWriteToken<TKey> value. When
enteredis false, then returned instanced will be a default value.