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, ISynchronizer, 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
| Improve this Doc View SourceAsyncSharedLock(Int64)
Initializes a new shared lock.
Declaration
public AsyncSharedLock(long concurrencyLevel)
Parameters
Type | Name | Description |
---|---|---|
Int64 | concurrencyLevel | The number of unique callers that can obtain shared lock simultaneously. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
Properties
| Improve this Doc View SourceConcurrencyLevel
Gets the maximum number of locks that can be obtained simultaneously.
Declaration
public long ConcurrencyLevel { get; }
Property Value
Type | Description |
---|---|
Int64 |
IsLockHeld
Indicates that the lock is acquired in exclusive or shared mode.
Declaration
public bool IsLockHeld { get; }
Property Value
Type | Description |
---|---|
Boolean |
RemainingCount
Gets the number of shared locks that can be acquired.
Declaration
public long RemainingCount { get; }
Property Value
Type | Description |
---|---|
Int64 |
Methods
| Improve this Doc View SourceAcquireAsync(Boolean, CancellationToken)
Entres the lock asynchronously.
Declaration
public Task AcquireAsync(bool strongLock, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
Boolean | 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 |
---|---|
Task | The task representing lock acquisition operation. |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | This object has been disposed. |
AcquireAsync(Boolean, TimeSpan, CancellationToken)
Entres the lock asynchronously.
Declaration
public Task AcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Boolean | 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 |
---|---|
Task | 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. |
DisposeAsync()
Disposes this lock asynchronously and gracefully.
Declaration
public ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask | The task representing graceful shutdown of this lock. |
Remarks
If this lock is not acquired then the method just completes synchronously. Otherwise, it waits for calling of Release() or Downgrade() method.
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()
Release 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(Boolean)
Attempts to obtain lock synchronously without blocking caller thread.
Declaration
public bool TryAcquire(bool strongLock)
Parameters
Type | Name | Description |
---|---|---|
Boolean | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
Returns
Type | Description |
---|---|
Boolean | true if the caller entered the lock; otherwise, false. |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | This object has been disposed. |
TryAcquireAsync(Boolean, TimeSpan)
Attempts to enter the lock asynchronously, with an optional time-out.
Declaration
public Task<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
Boolean | strongLock | true to acquire strong(exclusive) lock; false to acquire weak lock. |
TimeSpan | timeout | The interval to wait for the lock. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the caller entered the lock; otherwise, false. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
TryAcquireAsync(Boolean, TimeSpan, CancellationToken)
Attempts to enter the lock asynchronously, with an optional time-out.
Declaration
public Task<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
Boolean | 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 |
---|---|
Task<Boolean> | true if the caller entered the lock; otherwise, false. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |