Table of Contents

Class AsyncReaderWriterLock

Namespace
LfrlAnvil.Async
Assembly
LfrlAnvil.Core.dll

Represents a source of a fair asynchronous reader-writer lock.

public sealed class AsyncReaderWriterLock
Inheritance
AsyncReaderWriterLock
Inherited Members
Extension Methods

Remarks

Lock is not reentrant.

Constructors

AsyncReaderWriterLock()

Creates a new AsyncReaderWriterLock instance.

public AsyncReaderWriterLock()

Properties

Participants

Returns the total number of lock participants, which includes current lock holders and all waiters.

public int Participants { get; }

Property Value

int

Methods

EnterReadAsync(CancellationToken)

Asynchronously acquires a read lock from this reader-writer lock.

public ValueTask<AsyncReaderWriterLockReadToken> EnterReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

ValueTask<AsyncReaderWriterLockReadToken>

New ValueTask<TResult> instance which returns an AsyncReaderWriterLockReadToken value.

Exceptions

OperationCanceledException

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

EnterUpgradeableReadAsync(CancellationToken)

Asynchronously acquires an upgradeable read lock from this reader-writer lock.

public ValueTask<AsyncReaderWriterLockUpgradeableReadToken> EnterUpgradeableReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

ValueTask<AsyncReaderWriterLockUpgradeableReadToken>

New ValueTask<TResult> instance which returns an AsyncReaderWriterLockUpgradeableReadToken value.

Exceptions

OperationCanceledException

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

EnterWriteAsync(CancellationToken)

Asynchronously acquires a write lock from this reader-writer lock.

public ValueTask<AsyncReaderWriterLockWriteToken> EnterWriteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

ValueTask<AsyncReaderWriterLockWriteToken>

New ValueTask<TResult> instance which returns an AsyncReaderWriterLockWriteToken value.

Exceptions

OperationCanceledException

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

TrimExcess()

Attempts to discard unused resources.

public void TrimExcess()

TryEnterRead(out bool)

Attempts to synchronously acquire a read lock from this reader-writer lock.

public AsyncReaderWriterLockReadToken TryEnterRead(out bool entered)

Parameters

entered bool

out parameter which specifies whether the read lock was acquired.

Returns

AsyncReaderWriterLockReadToken

New AsyncReaderWriterLockReadToken value. When entered is false, then returned instanced will be a default value.

TryEnterUpgradeableRead(out bool)

Attempts to synchronously acquire an upgradeable read lock from this reader-writer lock.

public AsyncReaderWriterLockUpgradeableReadToken TryEnterUpgradeableRead(out bool entered)

Parameters

entered bool

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

Returns

AsyncReaderWriterLockUpgradeableReadToken

New AsyncReaderWriterLockUpgradeableReadToken value. When entered is false, then returned instanced will be a default value.

TryEnterWrite(out bool)

Attempts to synchronously acquire a write lock from this reader-writer lock.

public AsyncReaderWriterLockWriteToken TryEnterWrite(out bool entered)

Parameters

entered bool

out parameter which specifies whether the write lock was acquired.

Returns

AsyncReaderWriterLockWriteToken

New AsyncReaderWriterLockWriteToken value. When entered is false, then returned instanced will be a default value.