Struct AsyncLock
Unified representation of asynchronous exclusive lock, semaphore lock, read lock, write lock or upgradeable read lock.
Inherited Members
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 SourceAcquireAsync(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. |
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. |
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.
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.
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. |
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
| Edit this page View SourceExclusive()
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
| Edit this page View SourceExclusive(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 |
|
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 |
|
GetHashCode()
Computes hash code of this lock.
Declaration
public override readonly int GetHashCode()
Returns
Type | Description |
---|---|
int | The hash code of this lock. |
Overrides
| Edit this page View SourceReadLock(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 |
|
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.
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 |
|
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
| Edit this page View SourceTryAcquireAsync(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. |
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 |
|
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 |
Returns
Type | Description |
---|---|
AsyncLock | Write-only lock. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Operators
| Edit this page View Sourceoperator ==(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 |
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 |