Class QueuedSynchronizer<TContext>
Provides low-level infrastructure for writing custom synchronization primitives.
Implements
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public abstract class QueuedSynchronizer<TContext> : QueuedSynchronizer, IDisposable
Type Parameters
| Name | Description |
|---|---|
| TContext | The context to be associated with each suspended caller. |
Constructors
View SourceQueuedSynchronizer()
Initializes a new synchronization primitive.
Declaration
protected QueuedSynchronizer()
Properties
View SourceIsFair
Gets or initializes a value indicating whether the suspended callers are resumed in FIFO order.
Declaration
protected bool IsFair { get; init; }
Property Value
| Type | Description |
|---|---|
| bool | The default value is true. |
Remarks
If true, the release stops at the first suspended caller that cannot acquire the synchronization primitive. Otherwise, all suspended callers in the queue are examined on every release, which costs O(n) per release.
Methods
View SourceAcquireAsync(TContext, CancellationToken)
Implements acquire semantics: attempts to move this object to acquired state asynchronously.
Declaration
protected ValueTask AcquireAsync(TContext context, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context to be passed to CanAcquire(TContext). |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this method. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | This object has been disposed. |
AcquireAsync(TContext, TimeSpan, CancellationToken)
Implements acquire semantics: attempts to move this object to acquired state asynchronously.
Declaration
protected ValueTask AcquireAsync(TContext context, TimeSpan timeout, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context to be passed to CanAcquire(TContext). |
| TimeSpan | timeout | The time to wait for the acquisition. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask | true if acquisition is successful; false if timeout occurred. |
Exceptions
| Type | Condition |
|---|---|
| TimeoutException | The operation cannot be completed within the specified amount of time. |
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | This object has been disposed. |
AcquireCore(TContext)
Modifies the internal state according to acquisition semantics.
Declaration
protected virtual void AcquireCore(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context associated with the suspended caller or supplied externally. |
Remarks
By default, this method does nothing.
CanAcquire(TContext)
Tests whether the lock acquisition can be done successfully before calling AcquireCore(TContext).
Declaration
protected abstract bool CanAcquire(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context associated with the suspended caller or supplied externally. |
Returns
| Type | Description |
|---|---|
| bool |
Exceptions
| Type | Condition |
|---|---|
| Exception | A custom exception is thrown. |
GetAcquisitionException(TContext)
Returns an optional exception if CanAcquire(TContext) returns false.
Declaration
protected virtual QueuedSynchronizer<TContext>.ExceptionFactory? GetAcquisitionException(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The acquisition context. |
Returns
| Type | Description |
|---|---|
| QueuedSynchronizer<TContext>.ExceptionFactory | The exception; or null. |
Release()
Implements release semantics: attempts to resume the suspended callers.
Declaration
protected void Release()
Remarks
This method doesn't invoke ReleaseCore(TContext) method and trying to resume suspended callers.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This object has been disposed. |
Release(TContext)
Implements release semantics: attempts to resume the suspended callers.
Declaration
protected void Release(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The argument to be passed to ReleaseCore(TContext). |
Remarks
This method invokes
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This object has been disposed. |
ReleaseCore(TContext)
Modifies the internal state according to release semantics.
Declaration
protected virtual void ReleaseCore(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context associated with the suspended caller or supplied externally. |
Remarks
This method is called by Release(TContext) method.
TryAcquire(TContext)
Implements acquire semantics: attempts to move this object to acquired state synchronously.
Declaration
protected bool TryAcquire(TContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context to be passed to CanAcquire(TContext). |
Returns
| Type | Description |
|---|---|
| bool | true if this primitive is in acquired state; otherwise, false. |
Remarks
This method invokes CanAcquire(TContext), and if it returns true, invokes AcquireCore(TContext) to modify the internal state.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This object has been disposed. |
TryAcquireAsync(TContext, TimeSpan, CancellationToken)
Implements acquire semantics: attempts to move this object to acquired state asynchronously.
Declaration
protected ValueTask<bool> TryAcquireAsync(TContext context, TimeSpan timeout, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context | The context to be passed to CanAcquire(TContext). |
| TimeSpan | timeout | The time to wait for the acquisition. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> | true if acquisition is successful; false if timeout occurred. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | This object has been disposed. |