Class AsyncParkingLot
Represents a set of wait queues shared by many lightweight asynchronous locks.
Implements
Inherited Members
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 SourceAsyncParkingLot(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 |
|
Properties
View SourceCapacity
Gets the number of slots.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
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 SourceDispose(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
View SourceParkAsync(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 |
|
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | This object is disposed. |
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> |
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 |
|
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | This object is disposed. |
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. |
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 |
|
| InvalidOperationException | The lock is not acquired. |