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
| Improve this Doc View SourceAcquireAsync(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. |
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. |
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 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. |
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
| Improve this Doc 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
| Improve this Doc 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. |
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. |
GetHashCode()
Computes hash code of this lock.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 | The hash code of this lock. |
Overrides
| Improve this Doc View SourceReadLock(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. |
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.
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. |
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
| Improve this Doc View SourceTryAcquireAsync(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. |
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 |
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. |
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. |
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 SourceEquality(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. |
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. |