Show / Hide Table of Contents

Class AsyncParkingLot

Represents a set of wait queues shared by many lightweight asynchronous locks.

Inheritance
object
Disposable
AsyncParkingLot
Implements
IDisposable
Inherited Members
Disposable.CreateException()
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.DisposeAsyncCore()
Disposable.DisposeAsync()
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(params IEnumerable<IAsyncDisposable>)
Disposable.Dispose<TDisposable>(params ReadOnlySpan<TDisposable>)
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.DisposedTask
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncParkingLot : Disposable, IDisposable
Remarks

Each lock is represented by a field of type AsyncParkingLot.State owned by the caller, so the lock itself doesn't require any allocation. The wait queue is allocated only for the contended locks. The lock is identified by its position and optional user data. The positions mapped to the same slot share the wait queue, but they don't block each other.

The ownership is handed off to the first waiter on release, so the waiters are resumed in FIFO order. The lock is not reentrant and doesn't track the owner.

Constructors

View Source

AsyncParkingLot(int, long?)

Initializes a new parking lot.

Declaration
public AsyncParkingLot(int capacity, long? poolSizePerSlot = null)
Parameters
Type Name Description
int capacity

The number of slots. It is rounded up to the nearest power of two.

long? poolSizePerSlot

The maximum number of wait nodes retained by the pool of each slot; or null to retain all of them.

Exceptions
Type Condition
ArgumentOutOfRangeException

capacity is less than or equal to zero; or poolSizePerSlot is less than or equal to zero.

Properties

View Source

Capacity

Gets the number of slots.

Declaration
public int Capacity { get; }
Property Value
Type Description
int
View Source

MeasurementTags

Sets a list of tags to be associated with each measurement.

Declaration
[CLSCompliant(false)]
public TagList MeasurementTags { init; }
Property Value
Type Description
TagList

Methods

View Source

Dispose(bool)

Releases managed and unmanaged resources associated with this object.

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

true if called from Dispose(); false if called from finalizer ~Disposable().

Overrides
Disposable.Dispose(bool)
View Source

ParkAsync(int, ref State, object?, CancellationToken)

Acquires the lock asynchronously.

Declaration
public ValueTask ParkAsync(int position, ref AsyncParkingLot.State state, object? userData = null, CancellationToken token = default)
Parameters
Type Name Description
int position

The position of the lock.

AsyncParkingLot.State state

The state of the lock.

object userData

The object that distinguishes the locks with the same position. It is compared by reference, so Unpark(int, ref State, object?) must be called with the same object.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing lock acquisition.

Remarks

The returned task must be awaited. Otherwise, the wait node of the canceled caller is never removed from the wait queue.

Exceptions
Type Condition
ArgumentOutOfRangeException

position is negative.

OperationCanceledException

The operation has been canceled.

ObjectDisposedException

This object is disposed.

View Source

ParkAsync(int, ref State, object?, TimeSpan, CancellationToken)

Acquires the lock asynchronously, with a time-out.

Declaration
public ValueTask<bool> ParkAsync(int position, ref AsyncParkingLot.State state, object? userData, TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
int position

The position of the lock.

AsyncParkingLot.State state

The state of the lock.

object userData

The object that distinguishes the locks with the same position. It is compared by reference, so Unpark(int, ref State, object?) must be called with the same object.

TimeSpan timeout

The interval to wait for the lock.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<bool>

true if the lock is acquired; false if timed out.

Remarks

The returned task must be awaited. Otherwise, the wait node of the timed out or canceled caller is never removed from the wait queue.

Exceptions
Type Condition
ArgumentOutOfRangeException

position is negative; or timeout is negative.

OperationCanceledException

The operation has been canceled.

ObjectDisposedException

This object is disposed.

View Source

TryPark(ref State)

Attempts to acquire the lock synchronously, without waiting.

Declaration
public static bool TryPark(ref AsyncParkingLot.State state)
Parameters
Type Name Description
AsyncParkingLot.State state

The state of the lock.

Returns
Type Description
bool

true if the lock is acquired; false if the lock is already acquired.

View Source

Unpark(int, ref State, object?)

Releases the lock.

Declaration
public void Unpark(int position, ref AsyncParkingLot.State state, object? userData = null)
Parameters
Type Name Description
int position

The position of the lock.

AsyncParkingLot.State state

The state of the lock.

object userData

The object passed to ParkAsync(int, ref State, object?, CancellationToken).

Remarks

If the lock has waiters, the ownership is handed off to the first of them. The method must be called by the lock owner only, it's not checked.

Exceptions
Type Condition
ArgumentOutOfRangeException

position is negative.

InvalidOperationException

The lock is not acquired.

Implements

IDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾