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

| Improve this Doc View Source

AcquireAsync(CancellationToken)

Acquires the lock asynchronously.

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

The token that can be used to abort acquisition operation.

Returns
Type Description
Task<AsyncLock.Holder>

The task returning the acquired lock holder.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Improve this Doc View Source

AcquireAsync(TimeSpan, CancellationToken)

Acquires the lock asynchronously.

Declaration
public Task<AsyncLock.Holder> AcquireAsync(TimeSpan timeout, CancellationToken token = default(CancellationToken))
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
Task<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.

| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc View Source

Equals(AsyncLock)

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

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

Other lock to compare.

Returns
Type Description
Boolean

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

| Improve this Doc View Source

Equals(Object)

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

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

Other lock to compare.

Returns
Type Description
Boolean

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

Overrides
ValueType.Equals(Object)
| Improve this Doc 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
| Improve this Doc 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.

| Improve this Doc 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.

| Improve this Doc View Source

GetHashCode()

Computes hash code of this lock.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

The hash code of this lock.

Overrides
ValueType.GetHashCode()
| Improve this Doc View Source

ReadLock(AsyncReaderWriterLock, Boolean)

Creates read lock but doesn't acquire it.

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

Read/write lock source.

Boolean upgradeable

true to create upgradeable read lock wrapper.

Returns
Type Description
AsyncLock

Reader lock.

| Improve this Doc View Source

Semaphore(Int32, Int32)

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

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

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

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

| Improve this Doc 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.

| Improve this Doc View Source

ToString()

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

Declaration
public override string ToString()
Returns
Type Description
String

The actual type of this lock.

Overrides
ValueType.ToString()
| Improve this Doc View Source

TryAcquireAsync(CancellationToken)

Tries to acquire lock asynchronously.

Declaration
public Task<AsyncLock.Holder> TryAcquireAsync(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token that can be used to abort acquisition operation.

Returns
Type Description
Task<AsyncLock.Holder>

The task returning the acquired lock holder; or empty lock holder if operation was canceled.

| Improve this Doc View Source

TryAcquireAsync(TimeSpan, Boolean, CancellationToken)

Tries to acquire the lock asynchronously.

Declaration
public Task<AsyncLock.Holder> TryAcquireAsync(TimeSpan timeout, bool suppressCancellation, CancellationToken token)
Parameters
Type Name Description
TimeSpan timeout

The interval to wait for the lock.

Boolean suppressCancellation

true to return empty lock holder instead of throwing OperationCanceledException.

CancellationToken token

The token that can be used to abort acquisition operation.

Returns
Type Description
Task<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 and suppressCancellation is false.

| Improve this Doc View Source

TryAcquireAsync(TimeSpan, CancellationToken)

Tries to acquire the lock asynchronously.

Declaration
public Task<AsyncLock.Holder> TryAcquireAsync(TimeSpan timeout, CancellationToken token = default(CancellationToken))
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
Task<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.

| Improve this Doc 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.

| Improve this Doc View Source

WriteLock(AsyncReaderWriterLock)

Creates write lock but doesn't acquire it.

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

Read/write lock source.

Returns
Type Description
AsyncLock

Write-only lock.

Operators

| Improve this Doc View Source

Equality(AsyncLock, 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
Boolean

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

| Improve this Doc View Source

Inequality(AsyncLock, 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
Boolean

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

Implements

System.IDisposable
System.IEquatable<T>
System.IAsyncDisposable

Extension Methods

ValueTypeExtensions.IsOneOf<T>(T, IEnumerable<T>)
ValueTypeExtensions.IsOneOf<T>(T, T[])
Sequence.Skip<TEnumerator, T>(ref TEnumerator, Int32)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX