Struct AsyncLock
Unified representation of asynchronous exclusive lock, semaphore lock, read lock, write lock or upgradeable read lock.
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public struct AsyncLock : IDisposable, IEquatable<AsyncLock>
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 SourceAcquire(CancellationToken)
Acquires the lock asynchronously.
Declaration
public Task<AsyncLock.Holder> Acquire(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. |
Acquire(TimeSpan)
Acquires the lock asynchronously.
Declaration
public Task<AsyncLock.Holder> Acquire(TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | The interval to wait for the lock. |
Returns
| Type | Description |
|---|---|
| Task<AsyncLock.Holder> | The task returning the acquired lock holder. |
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
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. |
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
| 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. |
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. |
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. |
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.
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. |
TryAcquire(CancellationToken)
Tries to acquire lock asynchronously
Declaration
public Task<AsyncLock.Holder> TryAcquire(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. |
TryAcquire(TimeSpan)
Tries to acquire the lock asynchronously.
Declaration
public Task<AsyncLock.Holder> TryAcquire(TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | The interval to wait for the lock. |
Returns
| Type | Description |
|---|---|
| Task<AsyncLock.Holder> | The task returning the acquired lock holder; or empty lock holder if lock has not been acquired. |
TryAcquire(TimeSpan, CancellationToken, Boolean)
Tries to acquire the lock asynchronously.
Declaration
public Task<AsyncLock.Holder> TryAcquire(TimeSpan timeout, CancellationToken token, bool suppressCancellation = false)
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. |
| Boolean | suppressCancellation | true to return empty lock holder instead of throwing |
Returns
| Type | Description |
|---|---|
| Task<AsyncLock.Holder> | The task returning the acquired lock holder; or empty lock holder if lock has not been acquired. |
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. |