Show / Hide Table of Contents

Class AsyncReaderWriterLock

Represents asynchronous version of .

Inheritance
Object
Disposable
QueuedSynchronizer
AsyncReaderWriterLock
Implements
IAsyncDisposable
Inherited Members
QueuedSynchronizer.TrackSuspendedCallers(Nullable<Func<Object>>)
QueuedSynchronizer.SetCallerInformation(Object)
QueuedSynchronizer.GetSuspendedCallers()
QueuedSynchronizer.LockContentionCounter
QueuedSynchronizer.LockDurationCounter
QueuedSynchronizer.CancelSuspendedCallers(CancellationToken)
QueuedSynchronizer.Dispose(Boolean)
QueuedSynchronizer.Dispose(Nullable<Exception>)
QueuedSynchronizer.DisposeAsyncCore()
QueuedSynchronizer.DisposeAsync()
Disposable.IsDisposed
Disposable.ThrowIfDisposed()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.Dispose()
Disposable.Dispose(IEnumerable<Nullable<IDisposable>>)
Disposable.DisposeAsync(IEnumerable<Nullable<IAsyncDisposable>>)
Disposable.Dispose(Nullable<IDisposable>[])
Disposable.DisposeAsync(Nullable<IAsyncDisposable>[])
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncReaderWriterLock : QueuedSynchronizer
Remarks

This lock doesn't support recursion.

Constructors

| Improve this Doc View Source

AsyncReaderWriterLock()

Initializes a new reader/writer lock.

Declaration
public AsyncReaderWriterLock()
| Improve this Doc View Source

AsyncReaderWriterLock(Int32)

Initializes a new reader/writer lock.

Declaration
public AsyncReaderWriterLock(int concurrencyLevel)
Parameters
Type Name Description
Int32 concurrencyLevel

The expected number of concurrent flows.

Properties

| Improve this Doc View Source

CurrentReadCount

Gets the total number of unique readers.

Declaration
public long CurrentReadCount { get; }
Property Value
Type Description
Int64
| Improve this Doc View Source

IsReadLockHeld

Gets a value that indicates whether the read lock taken.

Declaration
public bool IsReadLockHeld { get; }
Property Value
Type Description
Boolean
| Improve this Doc View Source

IsWriteLockHeld

Gets a value that indicates whether the write lock taken.

Declaration
public bool IsWriteLockHeld { get; }
Property Value
Type Description
Boolean

Methods

| Improve this Doc View Source

DowngradeFromWriteLock()

Downgrades the write lock to the read lock.

Declaration
public void DowngradeFromWriteLock()
Remarks

Exiting from the lock is synchronous non-blocking operation. Lock acquisition is an asynchronous operation.

| Improve this Doc View Source

EnterReadLockAsync(CancellationToken)

Enters the lock in read mode asynchronously.

Declaration
public ValueTask EnterReadLockAsync(CancellationToken token = null)
Parameters
Type Name Description
CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing acquisition operation.

| Improve this Doc View Source

EnterReadLockAsync(TimeSpan, CancellationToken)

Enters the lock in read mode asynchronously.

Declaration
public ValueTask EnterReadLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing asynchronous result.

| Improve this Doc View Source

EnterWriteLockAsync(CancellationToken)

Enters the lock in write mode asynchronously.

Declaration
public ValueTask EnterWriteLockAsync(CancellationToken token = null)
Parameters
Type Name Description
CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing lock acquisition operation.

| Improve this Doc View Source

EnterWriteLockAsync(TimeSpan, CancellationToken)

Enters the lock in write mode asynchronously.

Declaration
public ValueTask EnterWriteLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing lock acquisition operation.

| Improve this Doc View Source

Release()

Exits previously acquired mode.

Declaration
public void Release()
Remarks

Exiting from the lock is synchronous non-blocking operation. Lock acquisition is an asynchronous operation.

| Improve this Doc View Source

TryEnterReadLock()

Attempts to obtain reader lock synchronously without blocking caller thread.

Declaration
public bool TryEnterReadLock()
Returns
Type Description
Boolean

true if lock is taken successfuly; otherwise, false.

| Improve this Doc View Source

TryEnterReadLockAsync(TimeSpan, CancellationToken)

Tries to enter the lock in read mode asynchronously, with an optional time-out.

Declaration
public ValueTask<bool> TryEnterReadLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask<Boolean>

true if the caller entered read mode; otherwise, false.

| Improve this Doc View Source

TryEnterWriteLock()

Attempts to obtain writer lock synchronously without blocking caller thread.

Declaration
public bool TryEnterWriteLock()
Returns
Type Description
Boolean

true if lock is taken successfuly; otherwise, false.

| Improve this Doc View Source

TryEnterWriteLock(in AsyncReaderWriterLock.LockStamp)

Attempts to acquire write lock without blocking.

Declaration
public bool TryEnterWriteLock(in AsyncReaderWriterLock.LockStamp stamp)
Parameters
Type Name Description
AsyncReaderWriterLock.LockStamp stamp

The stamp of the read lock.

Returns
Type Description
Boolean

true if lock is acquired successfully; otherwise, false.

| Improve this Doc View Source

TryEnterWriteLockAsync(TimeSpan, CancellationToken)

Tries to enter the lock in write mode asynchronously, with an optional time-out.

Declaration
public ValueTask<bool> TryEnterWriteLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask<Boolean>

true if the caller entered write mode; otherwise, false.

| Improve this Doc View Source

TryOptimisticRead()

Returns a stamp that can be validated later.

Declaration
public AsyncReaderWriterLock.LockStamp TryOptimisticRead()
Returns
Type Description
AsyncReaderWriterLock.LockStamp

Optimistic read stamp. May be invalid.

| Improve this Doc View Source

TryUpgradeToWriteLock()

Tries to upgrade the read lock to the write lock synchronously without blocking of the caller.

Declaration
public bool TryUpgradeToWriteLock()
Returns
Type Description
Boolean

true if lock is taken successfuly; otherwise, false.

| Improve this Doc View Source

TryUpgradeToWriteLockAsync(TimeSpan, CancellationToken)

Tries to upgrade the read lock to the write lock asynchronously.

Declaration
public ValueTask<bool> TryUpgradeToWriteLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask<Boolean>

true if the caller entered upgradeable mode; otherwise, false.

| Improve this Doc View Source

UpgradeToWriteLockAsync(CancellationToken)

Upgrades the read lock to the write lock asynchronously.

Declaration
public ValueTask UpgradeToWriteLockAsync(CancellationToken token = null)
Parameters
Type Name Description
CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing lock acquisition operation.

| Improve this Doc View Source

UpgradeToWriteLockAsync(TimeSpan, CancellationToken)

Upgrades the read lock to the write lock asynchronously.

Declaration
public ValueTask UpgradeToWriteLockAsync(TimeSpan timeout, CancellationToken token = null)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
ValueTask

The task representing lock acquisition operation.

| Improve this Doc View Source

Validate(in AsyncReaderWriterLock.LockStamp)

Returns true if the lock has not been exclusively acquired since issuance of the given stamp.

Declaration
public bool Validate(in AsyncReaderWriterLock.LockStamp stamp)
Parameters
Type Name Description
AsyncReaderWriterLock.LockStamp stamp

A stamp to check.

Returns
Type Description
Boolean

true if the lock has not been exclusively acquired since issuance of the given stamp; else false.

Implements

IAsyncDisposable

Extension Methods

AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
ObjectExtensions.GetUserData<T>(T)
ObjectExtensions.IsOneOf<T>(T, IEnumerable<T>)
ObjectExtensions.IsOneOf<T>(T, T[])
ObjectExtensions.As<T>(T)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
☀
☾
In This Article
Back to top
Supported by the .NET Foundation
☀
☾