Improved concurrency support for native resource disposal

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-18 14:24:21 +02:00
1 親 df970a5e40
コミット bee668f2f8
11ファイル変更
+34 -58

検索条件に一致するファイルはありません

+1 -3
ファイルの表示
@@ -40,12 +40,10 @@ namespace ComputeSharp.Graphics.Commands
public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12RootSignature.Dispose();
this.d3D12PipelineState.Dispose();
return true;
}
}
}
+2 -4
ファイルの表示
@@ -381,11 +381,11 @@ namespace ComputeSharp
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
if (DeviceHelper.GetDefaultDeviceLuid() == Luid)
{
return false;
return;
}
DeviceHelper.NotifyDisposedDevice(this);
@@ -400,8 +400,6 @@ namespace ComputeSharp
this.shaderResourceViewDescriptorAllocator.Dispose();
this.pool.Dispose();
this.allocator.Dispose();
return true;
}
/// <inheritdoc/>
+1 -3
ファイルの表示
@@ -203,11 +203,9 @@ namespace ComputeSharp.Graphics.Helpers
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.dxgiFactory4.Dispose();
return true;
}
}
}
+21 -19
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace ComputeSharp.Interop
{
@@ -8,51 +9,52 @@ namespace ComputeSharp.Interop
/// </summary>
public abstract class NativeObject : IDisposable
{
/// <summary>
/// Indicates whether or not the current instance has been disposed.
/// </summary>
private volatile int isDisposed;
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations.
/// </summary>
~NativeObject()
{
_ = CheckAndDispose();
CheckAndDispose();
}
/// <summary>
/// Gets whether or not the current instance has already been disposed.
/// </summary>
internal bool IsDisposed { get; private set; }
internal bool IsDisposed
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.isDisposed != 0;
}
/// <summary>
/// Releases all the native resources for the current instance.
/// </summary>
/// <inheritdoc/>
public void Dispose()
{
if (CheckAndDispose())
{
GC.SuppressFinalize(this);
}
CheckAndDispose();
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
private bool CheckAndDispose()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CheckAndDispose()
{
if (!IsDisposed)
if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0)
{
return IsDisposed = OnDispose();
OnDispose();
}
return true;
}
/// <summary>
/// Releases unmanaged and (optionally) managed resources.
/// </summary>
/// <returns>
/// Whether or not the dispose has actually been executed. This is done in order to allow derived types to
/// optionally cancel a dispose operation, by not releasing resources and returning <see langword="false"/>.
/// </returns>
protected abstract bool OnDispose();
protected abstract void OnDispose();
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> if the current instance has been disposed.
+1 -3
ファイルの表示
@@ -168,7 +168,7 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
@@ -179,8 +179,6 @@ namespace ComputeSharp.Resources
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
return true;
}
/// <summary>
+2 -7
ファイルの表示
@@ -432,19 +432,14 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
GraphicsDevice.UnregisterAllocatedResource();
if (GraphicsDevice?.IsDisposed == false)
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
return true;
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
/// <summary>
+2 -7
ファイルの表示
@@ -475,19 +475,14 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
GraphicsDevice.UnregisterAllocatedResource();
if (GraphicsDevice?.IsDisposed == false)
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
return true;
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
/// <summary>
+1 -3
ファイルの表示
@@ -106,14 +106,12 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
GraphicsDevice.UnregisterAllocatedResource();
return true;
}
/// <summary>
+1 -3
ファイルの表示
@@ -116,14 +116,12 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
GraphicsDevice.UnregisterAllocatedResource();
return true;
}
/// <summary>
+1 -3
ファイルの表示
@@ -123,14 +123,12 @@ namespace ComputeSharp.Resources
}
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
GraphicsDevice.UnregisterAllocatedResource();
return true;
}
/// <summary>
+1 -3
ファイルの表示
@@ -35,11 +35,9 @@ namespace ComputeSharp.Shaders.Translation.Models
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get());
/// <inheritdoc/>
protected override bool OnDispose()
protected override void OnDispose()
{
this.dxcBlobBytecode.Dispose();
return true;
}
}
}