Fix race conditions in device removed unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:14 +02:00
1 親 f9c9b5a24b
コミット 22fccc032c
3ファイル変更
+50 -53

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

+31 -33
ファイルの表示
@@ -45,50 +45,48 @@ internal static class GraphicsDeviceHelper
return d3D12Device.Get()->Release();
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
public static unsafe void RemoveDevice(GraphicsDevice graphicsDevice)
{
ID3D12Device5* d3D12Device = default;
Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID;
if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0)
{
Assert.Inconclusive();
}
try
{
d3D12Device->RemoveDevice();
}
finally
{
if (d3D12Device is not null)
{
d3D12Device->Release();
}
}
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance and waits for it to be reported.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
/// <remarks>
/// Removing a device will cause the device removed callback to be invoked on a separate thread, to get a
/// reference tracking lease for the device and then raise the event. If this is not awaited, it means
/// that that callback might race against a thread just calling Dispose(), so that that thread would not
/// actually cause the object to release resources until the device removed callback has also released its
/// own lease. To avoid issues related to this, the event should always be awaited after removing a device.
/// </remarks>
public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
{
TaskCompletionSource<object?> tcs = new();
graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null);
static unsafe void RemoveDevice(GraphicsDevice graphicsDevice)
{
ID3D12Device5* d3D12Device = default;
Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID;
if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0)
{
Assert.Inconclusive();
}
try
{
d3D12Device->RemoveDevice();
}
finally
{
if (d3D12Device is not null)
{
d3D12Device->Release();
}
}
}
RemoveDevice(graphicsDevice);
await Task.WhenAny(tcs.Task, Task.Delay(1000));
if (!tcs.Task.IsCompleted)
{
Assert.Fail();
}
await tcs.Task;
}
}