Show / Hide Table of Contents

Struct AsyncLock

Unified representation of asynchronous exclusive lock, semaphore lock, read lock, write lock or upgradeable read lock.

Implements
IDisposable
IEquatable<AsyncLock>
IAsyncDisposable
Inherited Members
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public struct AsyncLock : IDisposable, IEquatable<AsyncLock>, IAsyncDisposable
Remarks

Lock acquisition is asynchronous operation. Note that non-blocking asynchronous locks are not intersected with their blocking alternatives except semaphore. It means that exclusive lock obtained in blocking manner doesn't prevent acquisition of asynchronous lock obtained in non-blocking manner.

Methods

| Edit this page View Source

AcquireAsync(CancellationToken)

Acquires the lock asynchronously.

Declaration
public readonly ValueTask<AsyncLock.Holder> AcquireAsync(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token that can be used to abort acquisition operation.

Returns
Type Description
ValueTask<AsyncLock.Holder>

The task returning the acquired lock holder.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page View Source

AcquireAsync(TimeSpan, CancellationToken)

Acquires the lock asynchronously.

Declaration
public readonly ValueTask<AsyncLock.Holder> 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 acquisition operation.

Returns
Type Description
ValueTask<AsyncLock.Holder>

The task returning the acquired lock holder.

Exceptions
Type Condition
TimeoutException

The lock cannot be acquired during the specified amount of time.

OperationCanceledException

The operation has been canceled.

| Edit this page View Source

Dispose()

Destroy this lock and dispose underlying lock object if it is owned by the given lock.

Declaration
public void Dispose()
Remarks

If the given lock is an owner of the underlying lock object then this method will call Dispose() on it; otherwise, the underlying lock object will not be destroyed. As a result, this lock is not usable after calling of this method.

| Edit this page View Source

DisposeAsync()

Destroy this lock and asynchronously dispose underlying lock object if it is owned by the given lock.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Remarks

If the given lock is an owner of the underlying lock object then this method will call DisposeAsync() or Dispose() on it; otherwise, the underlying lock object will not be destroyed. As a result, this lock is not usable after calling of this method.

| Edit this page View Source

Equals(AsyncLock)

Determines whether this lock object is the same as other lock.

Declaration
public readonly bool Equals(AsyncLock other)
Parameters
Type Name Description
AsyncLock other

Other lock to compare.

Returns
Type Description
bool

true if this lock is the same as the specified lock; otherwise, false.

| Edit this page View Source

Equals(object?)

Determines whether this lock object is the same as other lock.

Declaration
public override readonly bool Equals(object? other)
Parameters
Type Name Description
object other

Other lock to compare.

Returns
Type Description
bool

true if this lock is the same as the specified lock; otherwise, false.

Overrides
ValueType.Equals(object)
| Edit this page View Source

Exclusive()

Creates exclusive asynchronous lock but doesn't acquire it.

Declaration
public static AsyncLock Exclusive()
Returns
Type Description
AsyncLock

Exclusive asynchronous lock.

Remarks

Constructed lock owns the exclusive lock instance.

See Also
AsyncExclusiveLock
| Edit this page View Source

Exclusive(AsyncExclusiveLock)

Wraps exclusive lock into the unified representation of asynchronous lock.

Declaration
public static AsyncLock Exclusive(AsyncExclusiveLock @lock)
Parameters
Type Name Description
AsyncExclusiveLock lock

The lock object to be wrapped.

Returns
Type Description
AsyncLock

Exclusive asynchronous lock.

Exceptions
Type Condition
ArgumentNullException

lock is null.

| Edit this page View Source

Exclusive(AsyncSharedLock)

Creates strong (exclusive) lock but doesn't acquire it.

Declaration
public static AsyncLock Exclusive(AsyncSharedLock @lock)
Parameters
Type Name Description
AsyncSharedLock lock

The shared lock instance.

Returns
Type Description
AsyncLock

Exclusive lock.

Exceptions
Type Condition
ArgumentNullException

lock is null.

| Edit this page View Source

GetHashCode()

Computes hash code of this lock.

Declaration
public override readonly int GetHashCode()
Returns
Type Description
int

The hash code of this lock.

Overrides
ValueType.GetHashCode()
| Edit this page View Source

ReadLock(AsyncReaderWriterLock)

Creates read lock but doesn't acquire it.

Declaration
public static AsyncLock ReadLock(AsyncReaderWriterLock rwLock)
Parameters
Type Name Description
AsyncReaderWriterLock rwLock

Read/write lock source.

Returns
Type Description
AsyncLock

Reader lock.

Exceptions
Type Condition
ArgumentNullException

rwLock is null.

| Edit this page View Source

Semaphore(int, int)

Creates semaphore-based lock but doesn't acquire the lock.

Declaration
public static AsyncLock Semaphore(int initialCount, int maxCount)
Parameters
Type Name Description
int initialCount

The initial number of requests for the semaphore that can be granted concurrently.

int maxCount

The maximum number of requests for the semaphore that can be granted concurrently.

Returns
Type Description
AsyncLock

The lock representing semaphore.

Remarks

Constructed lock owns the semaphore instance.

| Edit this page View Source

Semaphore(SemaphoreSlim)

Wraps semaphore instance into the unified representation of the lock.

Declaration
public static AsyncLock Semaphore(SemaphoreSlim semaphore)
Parameters
Type Name Description
SemaphoreSlim semaphore

The semaphore to wrap into lock object.

Returns
Type Description
AsyncLock

The lock representing semaphore.

Exceptions
Type Condition
ArgumentNullException

semaphore is null.

| Edit this page View Source

ToString()

Returns actual type of this lock in the form of the string.

Declaration
public override readonly string ToString()
Returns
Type Description
string

The actual type of this lock.

Overrides
ValueType.ToString()
| Edit this page View Source

TryAcquireAsync(TimeSpan, CancellationToken)

Tries to acquire the lock asynchronously.

Declaration
public readonly ValueTask<AsyncLock.Holder> 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 acquisition operation.

Returns
Type Description
ValueTask<AsyncLock.Holder>

The task returning the acquired lock holder; or empty lock holder if lock has not been acquired.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page View Source

Weak(AsyncSharedLock)

Creates weak lock but doesn't acquire it.

Declaration
public static AsyncLock Weak(AsyncSharedLock @lock)
Parameters
Type Name Description
AsyncSharedLock lock

The shared lock instance.

Returns
Type Description
AsyncLock

Weak lock.

Exceptions
Type Condition
ArgumentNullException

lock is null.

| Edit this page View Source

WriteLock(AsyncReaderWriterLock, bool)

Creates write lock but doesn't acquire it.

Declaration
public static AsyncLock WriteLock(AsyncReaderWriterLock rwLock, bool upgrade = false)
Parameters
Type Name Description
AsyncReaderWriterLock rwLock

Read/write lock source.

bool upgrade

true to upgrade from read lock; otherwise, false.

Returns
Type Description
AsyncLock

Write-only lock.

Exceptions
Type Condition
ArgumentNullException

rwLock is null.

Operators

| Edit this page View Source

operator ==(in AsyncLock, in AsyncLock)

Determines whether two locks are the same.

Declaration
public static bool operator ==(in AsyncLock first, in AsyncLock second)
Parameters
Type Name Description
AsyncLock first

The first lock to compare.

AsyncLock second

The second lock to compare.

Returns
Type Description
bool

true, if both are the same; otherwise, false.

| Edit this page View Source

operator !=(in AsyncLock, in AsyncLock)

Determines whether two locks are not the same.

Declaration
public static bool operator !=(in AsyncLock first, in AsyncLock second)
Parameters
Type Name Description
AsyncLock first

The first lock to compare.

AsyncLock second

The second lock to compare.

Returns
Type Description
bool

true, if both are not the same; otherwise, false.

Implements

IDisposable
IEquatable<T>
IAsyncDisposable

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
Collection.ToAsyncEnumerator<TEnumerator, T>(TEnumerator, CancellationToken)
Collection.ToClassicEnumerator<TEnumerator, T>(TEnumerator)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
ExpressionBuilder.Const<T>(T)

See Also

Lock
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾