Show / Hide Table of Contents

Class AsyncSharedLock

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

Inheritance
object
Disposable
QueuedSynchronizer
AsyncSharedLock
Implements
IDisposable
IAsyncDisposable
Inherited Members
QueuedSynchronizer.MeasurementTags
QueuedSynchronizer.TrackSuspendedCallers(Func<object>)
QueuedSynchronizer.SetCallerInformation(object)
QueuedSynchronizer.GetSuspendedCallers()
QueuedSynchronizer.CancelSuspendedCallers(CancellationToken)
QueuedSynchronizer.Dispose(bool)
QueuedSynchronizer.Dispose(Exception)
QueuedSynchronizer.DisposeAsyncCore()
QueuedSynchronizer.DisposeAsync()
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncSharedLock : QueuedSynchronizer, IDisposable, 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

| Edit this page View Source

AsyncSharedLock(long, bool)

Initializes a new shared lock.

Declaration
public AsyncSharedLock(long concurrencyLevel, bool limitedConcurrency = true)
Parameters
Type Name Description
long concurrencyLevel

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

bool limitedConcurrency

true if the potential number of concurrent flows will not be greater than concurrencyLevel; otherwise, false.

Exceptions
Type Condition
ArgumentOutOfRangeException

concurrencyLevel is less than 1.

Properties

| Edit this page View Source

ConcurrencyLevel

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

Declaration
public long ConcurrencyLevel { get; }
Property Value
Type Description
long
| Edit this page View Source

IsLockHeld

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

Declaration
public bool IsLockHeld { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsStrongLockHeld

Indicates that the lock is acquired in exclusive mode.

Declaration
public bool IsStrongLockHeld { get; }
Property Value
Type Description
bool
| Edit this page View Source

RemainingCount

Gets the number of shared locks that can be acquired.

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

Methods

| Edit this page View Source

AcquireAsync(bool, CancellationToken)

Enters the lock asynchronously.

Declaration
public ValueTask AcquireAsync(bool strongLock, CancellationToken token = default)
Parameters
Type Name Description
bool 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
ValueTask

The task representing lock acquisition operation.

Exceptions
Type Condition
ObjectDisposedException

This object has been disposed.

OperationCanceledException

The operation has been canceled.

| Edit this page View Source

AcquireAsync(bool, TimeSpan, CancellationToken)

Enters the lock asynchronously.

Declaration
public ValueTask AcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
bool 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
ValueTask

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.

OperationCanceledException

The operation has been canceled.

| Edit this page 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.

| Edit this page View Source

Release()

Releases the acquired lock.

Declaration
public void Release()
Exceptions
Type Condition
SynchronizationLockException

The caller has not entered the lock.

ObjectDisposedException

This object has been disposed.

| Edit this page View Source

TryAcquire(bool)

Attempts to obtain lock synchronously without blocking caller thread.

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

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

Returns
Type Description
bool

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

Exceptions
Type Condition
ObjectDisposedException

This object has been disposed.

| Edit this page View Source

TryAcquireAsync(bool, TimeSpan, CancellationToken)

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

Declaration
public ValueTask<bool> TryAcquireAsync(bool strongLock, TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
bool 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
ValueTask<bool>

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

Exceptions
Type Condition
ArgumentOutOfRangeException

Time-out value is negative.

ObjectDisposedException

This object has been disposed.

OperationCanceledException

The operation has been canceled.

Implements

IDisposable
IAsyncDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾