Show / Hide Table of Contents

Class AsyncExclusiveLock

Represents asynchronous mutually exclusive lock.

Inheritance
object
Disposable
QueuedSynchronizer
AsyncExclusiveLock
Implements
IDisposable
IAsyncDisposable
Inherited Members
QueuedSynchronizer.MeasurementTags
QueuedSynchronizer.TrackSuspendedCallers(Func<object>)
QueuedSynchronizer.SetCallerInformation(object)
QueuedSynchronizer.GetSuspendedCallers()
QueuedSynchronizer.CancelSuspendedCallers(CancellationToken)
QueuedSynchronizer.Dispose(bool)
QueuedSynchronizer.Dispose(Exception)
QueuedSynchronizer.DisposeAsyncCore()
QueuedSynchronizer.DisposeAsync()
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncExclusiveLock : QueuedSynchronizer, IDisposable, IAsyncDisposable

Constructors

| Edit this page View Source

AsyncExclusiveLock()

Initializes a new asynchronous exclusive lock.

Declaration
public AsyncExclusiveLock()
| Edit this page View Source

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

concurrencyLevel is less than or equal to zero.

Properties

| Edit this page View Source

IsLockHeld

Indicates that exclusive lock taken.

Declaration
public bool IsLockHeld { get; }
Property Value
Type Description
bool

Methods

| Edit this page View Source

AcquireAsync(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.

| Edit this page View Source

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

timeout 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.

PendingTaskInterruptedException

The operation has been interrupted manually.

| Edit this page View Source

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.

| Edit this page View Source

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

will be thrown for each suspended caller in the queue. The method cannot interrupt the caller that has already acquired the lock. If there is no suspended callers in the queue, this method is equivalent to TryAcquireAsync(TimeSpan, CancellationToken).

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

OperationCanceledException

The operation has been canceled.

See Also
PendingTaskInterruptedException
| Edit this page View Source

StealAsync(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

will be thrown for each suspended caller in the queue. The method cannot interrupt the caller that has already acquired the lock. If there is no suspended callers in the queue, this method is equivalent to TryAcquireAsync(TimeSpan, CancellationToken).

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
PendingTaskInterruptedException
| Edit this page View Source

TryAcquire()

Attempts to obtain exclusive lock synchronously without blocking caller thread.

Declaration
public bool TryAcquire()
Returns
Type Description
bool

true if lock is taken successfully; otherwise, false.

Exceptions
Type Condition
ObjectDisposedException

This object has been disposed.

| Edit this page View Source

TryAcquire(TimeSpan, CancellationToken)

Tries to acquire the lock synchronously.

Declaration
public bool TryAcquire(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 cancel the operation.

Returns
Type Description
bool

true if the lock is acquired in timely manner; false if canceled or timed out.

Exceptions
Type Condition
ArgumentOutOfRangeException

timeout is negative.

ObjectDisposedException

This object has been disposed.

LockRecursionException

The lock is already acquired by the current thread.

| Edit this page View Source

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

timeout is negative.

ObjectDisposedException

This object has been disposed.

OperationCanceledException

The operation has been canceled.

PendingTaskInterruptedException

The operation has been interrupted manually.

| Edit this page View Source

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

will be thrown for each suspended caller in the queue. The method cannot interrupt the caller that has already acquired the lock. If there is no suspended callers in the queue, this method is equivalent to TryAcquireAsync(TimeSpan, CancellationToken).

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

OperationCanceledException

The operation has been canceled.

See Also
PendingTaskInterruptedException

Implements

IDisposable
IAsyncDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾