Show / Hide Table of Contents

Class AsyncSharedLock

Represents a lock that can be acquired in exclusive or weak mode.

Inheritance
Object
QueuedSynchronizer
AsyncSharedLock
Implements
ISynchronizer
IAsyncDisposable
Inherited Members
QueuedSynchronizer.ISynchronizer.HasWaiters
QueuedSynchronizer.CancelSuspendedCallers(CancellationToken)
QueuedSynchronizer.Dispose(Boolean)
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncSharedLock : QueuedSynchronizer, ISynchronizer, IAsyncDisposable
Remarks

This lock represents the combination of semaphore and reader-writer lock. The caller can acquire weak locks simultaneously which count is limited by the concurrency level passed into the constructor. However, the only one caller can acquire the lock exclusively.

Constructors

| Improve this Doc View Source

AsyncSharedLock(Int64)

Initializes a new shared lock.

Declaration
public AsyncSharedLock(long concurrencyLevel)
Parameters
Type Name Description
Int64 concurrencyLevel

The number of unique callers that can obtain shared lock simultaneously.

Exceptions
Type Condition
ArgumentOutOfRangeException

concurrencyLevel is less than 1.

Properties

| Improve this Doc View Source

ConcurrencyLevel

Gets the maximum number of locks that can be obtained simultaneously.

Declaration
public long ConcurrencyLevel { get; }
Property Value
Type Description
Int64
| Improve this Doc View Source

IsLockHeld

Indicates that the lock is acquired in exclusive or shared mode.

Declaration
public bool IsLockHeld { get; }
Property Value
Type Description
Boolean
| Improve this Doc View Source

RemainingCount

Gets the number of shared locks that can be acquired.

Declaration
public long RemainingCount { get; }
Property Value
Type Description
Int64

Methods

| Improve this Doc View Source

AcquireAsync(Boolean, CancellationToken)

Entres the lock asynchronously.

Declaration
public Task AcquireAsync(bool strongLock, CancellationToken token)
Parameters
Type Name Description
Boolean strongLock

true to acquire strong(exclusive) lock; false to acquire weak lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
Task

The task representing lock acquisition operation.

Exceptions
Type Condition
ObjectDisposedException

This object has been disposed.

| Improve this Doc View Source

AcquireAsync(Boolean, TimeSpan, CancellationToken)

Entres the lock asynchronously.

Declaration
public Task AcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default(CancellationToken))
Parameters
Type Name Description
Boolean strongLock

true to acquire strong(exclusive) lock; false to acquire weak lock.

TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
Task

The task representing lock acquisition operation.

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

TimeoutException

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

| Improve this Doc View Source

DisposeAsync()

Disposes this lock asynchronously and gracefully.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask

The task representing graceful shutdown of this lock.

Remarks

If this lock is not acquired then the method just completes synchronously. Otherwise, it waits for calling of Release() or Downgrade() method.

| Improve this Doc View Source

Downgrade()

Releases the acquired weak lock or downgrade exclusive lock to the weak lock.

Declaration
public void Downgrade()
Exceptions
Type Condition
SynchronizationLockException

The caller has not entered the lock.

ObjectDisposedException

This object has been disposed.

| Improve this Doc View Source

Release()

Release the acquired lock.

Declaration
public void Release()
Exceptions
Type Condition
SynchronizationLockException

The caller has not entered the lock.

ObjectDisposedException

This object has been disposed.

| Improve this Doc View Source

TryAcquire(Boolean)

Attempts to obtain lock synchronously without blocking caller thread.

Declaration
public bool TryAcquire(bool strongLock)
Parameters
Type Name Description
Boolean strongLock

true to acquire strong(exclusive) lock; false to acquire weak lock.

Returns
Type Description
Boolean

true if the caller entered the lock; otherwise, false.

Exceptions
Type Condition
ObjectDisposedException

This object has been disposed.

| Improve this Doc View Source

TryAcquireAsync(Boolean, TimeSpan)

Attempts to enter the lock asynchronously, with an optional time-out.

Declaration
public Task<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout)
Parameters
Type Name Description
Boolean strongLock

true to acquire strong(exclusive) lock; false to acquire weak lock.

TimeSpan timeout

The interval to wait for the lock.

Returns
Type Description
Task<Boolean>

true if the caller entered the lock; otherwise, false.

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

| Improve this Doc View Source

TryAcquireAsync(Boolean, TimeSpan, CancellationToken)

Attempts to enter the lock asynchronously, with an optional time-out.

Declaration
public Task<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token)
Parameters
Type Name Description
Boolean strongLock

true to acquire strong(exclusive) lock; false to acquire weak lock.

TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to abort lock acquisition.

Returns
Type Description
Task<Boolean>

true if the caller entered the lock; otherwise, false.

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

Implements

ISynchronizer
System.IAsyncDisposable

Extension Methods

AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireUpgradeableReadLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireUpgradeableReadLockAsync<T>(T, CancellationToken)
ObjectExtensions.GetUserData<T>(T)
ObjectExtensions.IsOneOf<T>(T, IEnumerable<T>)
ObjectExtensions.IsOneOf<T>(T, T[])
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, Func<T, TResult1>, Func<T, TResult2>, out TResult1, out TResult2)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, ValueFunc<T, TResult1>, ValueFunc<T, TResult2>, out TResult1, out TResult2)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, Func<T, TResult1>, Func<T, TResult2>)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, ValueFunc<T, TResult1>, ValueFunc<T, TResult2>)
ObjectExtensions.As<T>(T)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX