Class AsyncSharedLock
Represents a lock that can be acquired in exclusive or weak mode.
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncSharedLock : QueuedSynchronizer, IDisposable, IAsyncDisposable
Remarks
This lock represents the combination of semaphore and reader-writer lock. The caller can acquire weak locks simultaneously which count is limited by the concurrency level passed into the constructor. However, the only one caller can acquire the lock exclusively.
Constructors
| Edit this page View SourceAsyncSharedLock(long, bool)
Initializes a new shared lock.
Declaration
public AsyncSharedLock(long concurrencyLevel, bool limitedConcurrency = true)
Parameters
Type | Name | Description |
---|---|---|
long | concurrencyLevel | The number of unique callers that can obtain shared lock simultaneously. |
bool | limitedConcurrency | true if the potential number of concurrent flows will not be greater than |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
Properties
| Edit this page View SourceConcurrencyLevel
Gets the maximum number of locks that can be obtained simultaneously.
Declaration
public long ConcurrencyLevel { get; }
Property Value
Type | Description |
---|---|
long |
IsLockHeld
Indicates that the lock is acquired in exclusive or shared mode.
Declaration
public bool IsLockHeld { get; }
Property Value
Type | Description |
---|---|
bool |
IsStrongLockHeld
Indicates that the lock is acquired in exclusive mode.
Declaration
public bool IsStrongLockHeld { get; }
Property Value
Type | Description |
---|---|
bool |
RemainingCount
Gets the number of shared locks that can be acquired.
Declaration
public long RemainingCount { get; }
Property Value
Type | Description |
---|---|
long |
Methods
| Edit this page View SourceAcquireAsync(bool, CancellationToken)
Enters the lock asynchronously.
Declaration
public ValueTask AcquireAsync(bool strongLock, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
bool | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
CancellationToken | token | The token that can be used to abort lock acquisition. |
Returns
Type | Description |
---|---|
ValueTask | The task representing lock acquisition operation. |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |
AcquireAsync(bool, TimeSpan, CancellationToken)
Enters the lock asynchronously.
Declaration
public ValueTask AcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
bool | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
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. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
TimeoutException | The lock cannot be acquired during the specified amount of time. |
OperationCanceledException | The operation has been canceled. |
Downgrade()
Releases the acquired weak lock or downgrade exclusive lock to the weak lock.
Declaration
public void Downgrade()
Exceptions
Type | Condition |
---|---|
SynchronizationLockException | The caller has not entered the lock. |
ObjectDisposedException | This object has been disposed. |
Release()
Releases the acquired lock.
Declaration
public void Release()
Exceptions
Type | Condition |
---|---|
SynchronizationLockException | The caller has not entered the lock. |
ObjectDisposedException | This object has been disposed. |
TryAcquire(bool)
Attempts to obtain lock synchronously without blocking caller thread.
Declaration
public bool TryAcquire(bool strongLock)
Parameters
Type | Name | Description |
---|---|---|
bool | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
Returns
Type | Description |
---|---|
bool |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | This object has been disposed. |
TryAcquireAsync(bool, TimeSpan, CancellationToken)
Attempts to enter the lock asynchronously, with an optional time-out.
Declaration
public ValueTask<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
bool | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
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<bool> |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |