Class AsyncExclusiveLock
Represents asynchronous mutually exclusive lock.
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncExclusiveLock : QueuedSynchronizer, IDisposable, IAsyncDisposable
Constructors
| Edit this page View SourceAsyncExclusiveLock()
Initializes a new asynchronous exclusive lock.
Declaration
public AsyncExclusiveLock()
AsyncExclusiveLock(int)
Initializes a new asynchronous exclusive lock.
Declaration
public AsyncExclusiveLock(int concurrencyLevel)
Parameters
Type | Name | Description |
---|---|---|
int | concurrencyLevel | The expected number of concurrent flows. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
Properties
| Edit this page View SourceIsLockHeld
Indicates that exclusive lock taken.
Declaration
public bool IsLockHeld { get; }
Property Value
Type | Description |
---|---|
bool |
Methods
| Edit this page View SourceAcquireAsync(CancellationToken)
Enters the lock in exclusive mode asynchronously.
Declaration
public ValueTask AcquireAsync(CancellationToken token = default)
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. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |
PendingTaskInterruptedException | The operation has been interrupted manually. |
AcquireAsync(TimeSpan, CancellationToken)
Enters the lock in exclusive mode asynchronously.
Declaration
public ValueTask AcquireAsync(TimeSpan timeout, CancellationToken token = default)
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. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
ObjectDisposedException | This object has been disposed. |
TimeoutException | The lock cannot be acquired during the specified amount of time. |
OperationCanceledException | The operation has been canceled. |
PendingTaskInterruptedException | The operation has been interrupted manually. |
Release()
Releases previously acquired exclusive lock.
Declaration
public void Release()
Exceptions
Type | Condition |
---|---|
SynchronizationLockException | The caller has not entered the lock. |
ObjectDisposedException | This object has been disposed. |
StealAsync(object?, CancellationToken)
Interrupts all pending callers in the queue and acquires the lock.
Declaration
public ValueTask StealAsync(object? reason = null, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
object | reason | The reason for lock steal. |
CancellationToken | token | The token that can be used to abort lock acquisition. |
Returns
Type | Description |
---|---|
ValueTask | The task representing lock acquisition operation. |
Remarks
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |
See Also
| Edit this page View SourceStealAsync(object?, TimeSpan, CancellationToken)
Interrupts all pending callers in the queue and acquires the lock.
Declaration
public ValueTask StealAsync(object? reason, TimeSpan timeout, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
object | reason | The reason for lock steal. |
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. |
Remarks
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. |
See Also
| Edit this page View SourceTryAcquire()
Attempts to obtain exclusive lock synchronously without blocking caller thread.
Declaration
public bool TryAcquire()
Returns
Type | Description |
---|---|
bool |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | This object has been disposed. |
TryAcquire(TimeSpan)
Tries to acquire the lock synchronously.
Declaration
public bool TryAcquire(TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | The interval to wait for the lock. |
Returns
Type | Description |
---|---|
bool | true if the lock is acquired in timely manner; otherwise, false. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
ObjectDisposedException | This object has been disposed. |
TryAcquireAsync(TimeSpan, CancellationToken)
Tries to enter the lock in exclusive mode asynchronously, with an optional time-out.
Declaration
public ValueTask<bool> TryAcquireAsync(TimeSpan timeout, CancellationToken token = default)
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<bool> | true if the caller entered exclusive mode; otherwise, false. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |
PendingTaskInterruptedException | The operation has been interrupted manually. |
TryStealAsync(object?, TimeSpan, CancellationToken)
Interrupts all pending callers in the queue and acquires the lock.
Declaration
public ValueTask<bool> TryStealAsync(object? reason, TimeSpan timeout, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
object | reason | The reason for lock steal. |
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> | true if the caller entered exclusive mode; otherwise, false. |
Remarks
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Time-out value is negative. |
ObjectDisposedException | This object has been disposed. |
OperationCanceledException | The operation has been canceled. |