From 84ebe369755c2c1d06018a0e56a726137a5c7a28 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Jul 2022 22:39:08 +0200 Subject: [PATCH] Make InteropServices APIs thread safe --- .../Graphics/Interop/InteropServices.cs | 42 +++++++++++++++++++ .../Graphics/Interop/NativeObject.cs | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Interop/InteropServices.cs b/src/ComputeSharp/Graphics/Interop/InteropServices.cs index 288679f7..4fa165e2 100644 --- a/src/ComputeSharp/Graphics/Interop/InteropServices.cs +++ b/src/ComputeSharp/Graphics/Interop/InteropServices.cs @@ -30,9 +30,12 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the device interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Device(GraphicsDevice device, Guid* riid, void** ppvObject) { + using var _0 = device.GetReferenceTracker().GetLease(); + device.D3D12Device->QueryInterface(riid, ppvObject).Assert(); } @@ -44,10 +47,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = buffer.GetReferenceTracker().GetLease(); + buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -59,10 +65,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(Texture2D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -74,10 +83,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(Texture3D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -89,10 +101,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(TransferBuffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = buffer.GetReferenceTracker().GetLease(); + buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -104,10 +119,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(TransferTexture2D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -119,10 +137,13 @@ public static unsafe class InteropServices /// The input instance in use. /// A reference to the interface identifier (IID) of the resource interface being queried for. /// The address of a pointer to an interface with the IID specified in . + /// The instance has been disposed. /// Thrown if the IUnknown::QueryInterface call doesn't return S_OK. public static void GetID3D12Resource(TransferTexture3D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert(); } @@ -137,8 +158,11 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Device(GraphicsDevice device, Guid* riid, void** ppvObject) { + using var _0 = device.GetReferenceTracker().GetLease(); + return device.D3D12Device->QueryInterface(riid, ppvObject); } @@ -154,9 +178,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(Buffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = buffer.GetReferenceTracker().GetLease(); + return buffer.D3D12Resource->QueryInterface(riid, ppvObject); } @@ -172,9 +199,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(Texture2D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + return texture.D3D12Resource->QueryInterface(riid, ppvObject); } @@ -190,9 +220,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(Texture3D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + return texture.D3D12Resource->QueryInterface(riid, ppvObject); } @@ -208,9 +241,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(TransferBuffer buffer, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = buffer.GetReferenceTracker().GetLease(); + return buffer.D3D12Resource->QueryInterface(riid, ppvObject); } @@ -226,9 +262,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(TransferTexture2D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + return texture.D3D12Resource->QueryInterface(riid, ppvObject); } @@ -244,9 +283,12 @@ public static unsafe class InteropServices /// S_OK if the interface is supported, and E_NOINTERFACE otherwise. /// If ppvObject (the address) is nullptr, then this method returns E_POINTER. /// + /// The instance has been disposed. public static int TryGetID3D12Resource(TransferTexture3D texture, Guid* riid, void** ppvObject) where T : unmanaged { + using var _0 = texture.GetReferenceTracker().GetLease(); + return texture.D3D12Resource->QueryInterface(riid, ppvObject); } } diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs index 53383848..bfbc2c04 100644 --- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs +++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs @@ -16,7 +16,7 @@ public abstract class NativeObject : IDisposable, ReferenceTracker.ITrackedObjec /// /// Creates a new instance. /// - internal NativeObject() + private protected NativeObject() { this.referenceTracker = new ReferenceTracker(this); }