From e2ccda0144db28a5fa4155fe22a29524d03ed98b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 13 Apr 2020 15:19:16 +0200 Subject: [PATCH] Made Gpu.Default property thread-safe --- src/ComputeSharp/Gpu.cs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 75507597..26ae6399 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -21,6 +21,11 @@ namespace ComputeSharp { Devices = DeviceHelper.QueryAllSupportedDevices(); IsSupported = Devices.Any(); + + if (IsSupported) + { + _Default = new GraphicsDevice(Devices[0].Device, Devices[0].Description); + } } /// @@ -28,25 +33,17 @@ namespace ComputeSharp /// private static readonly IReadOnlyList<(ID3D12Device Device, AdapterDescription Description)> Devices; - /// - /// Gets whether or not the APIs can be used on the current machine (ie. if there is at least a supported GPU device) - /// - public static bool IsSupported { get; } - - private static GraphicsDevice? _Default; + private static readonly GraphicsDevice? _Default; /// /// Gets the default instance for the current machine /// - public static GraphicsDevice Default - { - get - { - if (!IsSupported) throw new NotSupportedException("There isn't a supported GPU device on the current machine"); + public static GraphicsDevice Default => _Default ?? throw new NotSupportedException("There isn't a supported GPU device on the current machine"); - return _Default ??= new GraphicsDevice(Devices[0].Device, Devices[0].Description); - } - } + /// + /// Gets whether or not the APIs can be used on the current machine (ie. if there is at least a supported GPU device) + /// + public static bool IsSupported { get; } /// /// Enumerates all the available objects on the current machine