Add lock to synchronize Dispose/DeviceLost logic

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:11 +02:00
1 親 8b8ca034db
コミット c8c779d827
2ファイル変更
+27 -2

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

+14 -1
ファイルの表示
@@ -61,7 +61,20 @@ unsafe partial class GraphicsDevice
return;
}
HRESULT result = this.d3D12Device.Get()->GetDeviceRemovedReason();
HRESULT result;
// Make sure to guard against concurrent accesses from the dispose logic
lock (this.d3D12DeviceLock)
{
ID3D12Device* d3D12Device = this.d3D12Device.Get();
if (d3D12Device is null)
{
return;
}
result = d3D12Device->GetDeviceRemovedReason();
}
// Only raise the event once, and store the device removed reason to track it
if (result != S.S_OK &&
+13 -1
ファイルの表示
@@ -107,6 +107,13 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// </summary>
private HRESULT deviceRemovedReason;
/// <summary>
/// A lcok object used to synchronize accesses to <see cref="d3D12Device"/> between <see cref="OnDispose"/> and <see cref="QueueRaiseDeviceLostEventIfNeeded"/>.
/// This is needed because the device lost callback might race with a dispose call and end up trying to invoke a method on an invalid object.
/// We can't just check whether it's <see langword="null"/>, as a null condition might still cause a racing thread to access a freed up object.
/// </summary>
private readonly object d3D12DeviceLock = new();
/// <summary>
/// Raised whenever the device is lost.
/// </summary>
@@ -402,7 +409,12 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
{
DeviceHelper.NotifyDisposedDevice(this);
this.d3D12Device.Dispose();
// Guard the dispose call around the native device to avoid device lost races
lock (this.d3D12DeviceLock)
{
this.d3D12Device.Dispose();
}
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();