Class ManualResetValueTaskSource<T>
- Namespace
- CryptoHives.Foundation.Threading.Pools
- Assembly
- CryptoHives.Foundation.Threading.dll
An abstract implementation of IValueTaskSource<TResult>.
public abstract class ManualResetValueTaskSource<T> : IValueTaskSource<T>, IValueTaskSource, IResettable
Type Parameters
T
- Inheritance
-
ManualResetValueTaskSource<T>
- Implements
- Derived
- Inherited Members
Remarks
This class is a sealed implementation of IValueTaskSource<TResult> and provides methods to manage the lifecycle of a task-like operation. It allows resetting and signaling the completion of the operation, and supports querying the status and retrieving the result. In addition, an implementation can set the owner instance to return the instance to the pool when it is no longer needed. This class is designed for direct use with CryptoHives.Foundation.Threading.Pools.WaiterQueue<T> to store the derived waiters allocation free. The IResettable interface is implemented to allow resetting the state of the instance for reuse by an implementation of an ObjectPool that uses the DefaultObjectPool<T> implementation.
Properties
CancellationToken
Gets the CancellationToken associated with the current operation.
public abstract CancellationToken CancellationToken { get; set; }
Property Value
Remarks
This token may be cancelled during long-running operations.
CancellationTokenRegistration
Gets the CancellationTokenRegistration associated with the current operation.
public abstract CancellationTokenRegistration CancellationTokenRegistration { get; set; }
Property Value
Remarks
Use this token to unregister from cancellation requests.
Owner
Gets or sets the owner associated with this instance.
public abstract object? Owner { get; }
Property Value
RunContinuationsAsynchronously
Gets or sets whether to force continuations to run asynchronously.
public abstract bool RunContinuationsAsynchronously { get; set; }
Property Value
Remarks
Continuations may run asynchronously if this is false, but they'll never run synchronously if this is true.
TimeoutTimer
Gets or sets the TimeoutTimer used to enforce a timeout on the wait operation.
public abstract ITimer? TimeoutTimer { get; set; }
Property Value
- ITimer
Remarks
Set to a non-null value when the waiter is created with a timeout. The timer is disposed and cleared by TryReset() when the waiter is recycled.
Assign this after leaving the owner's lock, not inside it. Creating a timer takes the runtime's process-global timer queue lock and allocates, which is the one cost in those critical sections that the library cannot bound - and the primitives' spin lock is tuned on the premise that everything it guards is a few pointer writes.
This is safe for the same reason CancellationTokenRegistration is already assigned outside the lock. A waiter is recycled only by TryReset(), which runs from GetResult(short), which is reachable only through the ValueTask the enqueueing method has not returned yet. So between releasing the lock and returning that ValueTask, the waiter is owned exclusively by the enqueueing thread: another thread may SetResult(T) it, but cannot reset it, so Version is stable and the timer cannot be disposed underneath the assignment.
A waiter completed inside that window still gets a timer, which fires and finds either a version mismatch or a waiter no longer in the queue, and does nothing - the stale-callback case CryptoHives.Foundation.Threading.Pools.TimeoutState<T> already exists to handle.
Version
Gets the version number of the current instance.
public abstract short Version { get; }
Property Value
Methods
GetResult(short)
Gets the result of the IValueTaskSource<TResult>.
public abstract T GetResult(short token)
Parameters
Returns
- T
GetStatus(short)
Gets the status of the current operation.
public abstract ValueTaskSourceStatus GetStatus(short token)
Parameters
Returns
OnCompleted(Action<object?>, object?, short, ValueTaskSourceOnCompletedFlags)
Schedules the continuation action for this IValueTaskSource<TResult>.
public abstract void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
Parameters
continuationAction<object>The continuation to invoke when the operation has completed.
stateobjectThe state object to pass to
continuationwhen it's invoked.tokenshortOpaque value that was provided to the ValueTask's constructor.
flagsValueTaskSourceOnCompletedFlagsThe flags describing the behavior of the continuation.
SetChainResult(T)
Signals the completion of an operation, setting the result to T for a chain of operations.
public abstract void SetChainResult(T result)
Parameters
resultT
Remarks
This method is typically used to indicate that a chain of asynchronous operations has completed successfully.
SetException(Exception)
Sets the specified exception to be associated with the current operation.
public abstract void SetException(Exception ex)
Parameters
exException
SetResult(T)
Signals the completion of an operation, setting the result to T.
public abstract void SetResult(T result)
Parameters
resultT
Remarks
This method is typically used to indicate that an asynchronous operation has completed successfully.
TryReset()
Reset the object to a neutral state, semantically similar to when the object was first constructed.
public abstract bool TryReset()
Returns
Remarks
In general, this method is not expected to be thread-safe.