Class AsyncReaderWriterLock
Represents asynchronous version of
Implements
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncReaderWriterLock : QueuedSynchronizer
Remarks
This lock doesn't support recursion.
Constructors
| Improve this Doc View SourceAsyncReaderWriterLock()
Initializes a new reader/writer lock.
Declaration
public AsyncReaderWriterLock()
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 SourceCurrentReadCount
Gets the total number of unique readers.
Declaration
public long CurrentReadCount { get; }
Property Value
Type | Description |
---|---|
Int64 |
IsReadLockHeld
Gets a value that indicates whether the read lock taken.
Declaration
public bool IsReadLockHeld { get; }
Property Value
Type | Description |
---|---|
Boolean |
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 SourceDowngradeFromWriteLock()
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.
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. |
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. |
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. |
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. |
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.
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |