Class AsyncSharedLock
Represents a lock that can be acquired in exclusive or weak mode.
Implements
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncSharedLock : QueuedSynchronizer, ISynchronizer
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. |
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 SourceAcquire(Boolean, CancellationToken)
Entres the lock asynchronously.
Declaration
public Task Acquire(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. |
Acquire(Boolean, TimeSpan)
Entres the lock asynchronously.
Declaration
public Task Acquire(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 | The task representing lock acquisition operation. |
Downgrade()
Releases the acquired weak lock or downgrade exclusive lock to the weak lock.
Declaration
public void Downgrade()
Release()
Release the acquired lock.
Declaration
public void Release()
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. |
TryAcquire(Boolean, TimeSpan)
Attempts to enter the lock asynchronously, with an optional time-out.
Declaration
public Task<bool> TryAcquire(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. |
TryAcquire(Boolean, TimeSpan, CancellationToken)
Attempts to enter the lock asynchronously, with an optional time-out.
Declaration
public Task<bool> TryAcquire(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. |