Table of Contents

Class DisposalUtilities

Namespace
DotCompute.Abstractions
Assembly
DotCompute.Abstractions.dll

Common disposal patterns and utilities for accelerator implementations.

public static class DisposalUtilities
Inheritance
DisposalUtilities
Inherited Members

Methods

DisposeWithSynchronization(Action, IEnumerable<object?>, ILogger?, string)

Common disposal pattern for components with synchronization requirements (synchronous).

public static void DisposeWithSynchronization(Action synchronizeAction, IEnumerable<object?> disposables, ILogger? logger, string componentName = "component")

Parameters

synchronizeAction Action

Action to synchronize the component

disposables IEnumerable<object>

Objects to dispose after synchronization

logger ILogger

Logger for recording events

componentName string

Name of the component for logging

DisposeWithSynchronizationAsync(Func<ValueTask>, IEnumerable<object?>, ILogger?, string)

Common disposal pattern for components with synchronization requirements.

public static ValueTask DisposeWithSynchronizationAsync(Func<ValueTask> synchronizeFunc, IEnumerable<object?> disposables, ILogger? logger, string componentName = "component")

Parameters

synchronizeFunc Func<ValueTask>

Function to synchronize the component

disposables IEnumerable<object>

Objects to dispose after synchronization

logger ILogger

Logger for recording events

componentName string

Name of the component for logging

Returns

ValueTask

ReleaseNativeResources<T>(IDictionary<string, T>, Action<T>, ILogger?, string)

Common pattern for components that need to free native resources.

public static void ReleaseNativeResources<T>(IDictionary<string, T> nativeHandles, Action<T> releaseFunc, ILogger? logger, string componentName = "component")

Parameters

nativeHandles IDictionary<string, T>

Dictionary of native handles to release

releaseFunc Action<T>

Function to release a native handle

logger ILogger

Logger for recording events

componentName string

Name of the component for logging

Type Parameters

T

SafeDispose(object?, ILogger?, string)

Safely disposes an object synchronously with logging.

public static void SafeDispose(object? disposable, ILogger? logger, string objectName = "object")

Parameters

disposable object

The object to dispose

logger ILogger

Logger for recording disposal events

objectName string

Name of the object being disposed for logging

SafeDisposeAll(IEnumerable<object?>, ILogger?, IEnumerable<string>?)

Safely disposes multiple objects in sequence synchronously.

public static void SafeDisposeAll(IEnumerable<object?> disposables, ILogger? logger, IEnumerable<string>? objectNames = null)

Parameters

disposables IEnumerable<object>

The objects to dispose

logger ILogger

Logger for recording disposal events

objectNames IEnumerable<string>

Names of the objects being disposed (optional)

SafeDisposeAllAsync(IEnumerable<object?>, ILogger?, IEnumerable<string>?)

Safely disposes multiple objects in sequence.

public static ValueTask SafeDisposeAllAsync(IEnumerable<object?> disposables, ILogger? logger, IEnumerable<string>? objectNames = null)

Parameters

disposables IEnumerable<object>

The objects to dispose

logger ILogger

Logger for recording disposal events

objectNames IEnumerable<string>

Names of the objects being disposed (optional)

Returns

ValueTask

SafeDisposeAsync(object?, ILogger?, string)

Safely disposes an object with logging, handling both sync and async disposal patterns.

public static ValueTask SafeDisposeAsync(object? disposable, ILogger? logger, string objectName = "object")

Parameters

disposable object

The object to dispose

logger ILogger

Logger for recording disposal events

objectName string

Name of the object being disposed for logging

Returns

ValueTask

ThrowIfDisposed(bool, object)

Validates that an object is not disposed and throws ObjectDisposedException if it is.

public static void ThrowIfDisposed(bool disposed, object objectInstance)

Parameters

disposed bool

The disposed flag

objectInstance object

The object instance to validate

ThrowIfDisposedInterlocked(int, object)

Validates that an object is not disposed using Interlocked and throws ObjectDisposedException if it is.

public static void ThrowIfDisposedInterlocked(int disposed, object objectInstance)

Parameters

disposed int

The disposed flag (as int)

objectInstance object

The object instance to validate

TrySetDisposed(ref bool, object)

Thread-safe disposal state management helper.

public static bool TrySetDisposed(ref bool disposed, object lockObject)

Parameters

disposed bool

Reference to the disposed flag

lockObject object

Lock object for thread safety

Returns

bool

True if disposal should proceed, false if already disposed

TrySetDisposedInterlocked(ref int)

Thread-safe disposal state management helper using Interlocked.

public static bool TrySetDisposedInterlocked(ref int disposed)

Parameters

disposed int

Reference to the disposed flag (as int, 0=not disposed, 1=disposed)

Returns

bool

True if disposal should proceed, false if already disposed