Added locks around device provider instance usages

This commit is contained in:
DarthAffe committed 2023-06-18 19:54:57 +02:00
1 parent fdc69fdac5
commit 0609e49571
14 files changed
+336 -108

No files matched your search

@@ -17,11 +17,21 @@ public sealed class CorsairDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly object _lock = new();
private static CorsairDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="CorsairDeviceProvider"/> instance.
/// </summary>
public static CorsairDeviceProvider Instance => _instance ?? new CorsairDeviceProvider();
public static CorsairDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new CorsairDeviceProvider();
}
}
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
@@ -80,8 +90,11 @@ public sealed class CorsairDeviceProvider : AbstractRGBDeviceProvider
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public CorsairDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CorsairDeviceProvider)}");
_instance = this;
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CorsairDeviceProvider)}");
_instance = this;
}
}
#endregion
@@ -302,12 +315,18 @@ public sealed class CorsairDeviceProvider : AbstractRGBDeviceProvider
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
lock (_lock)
{
base.Dispose(disposing);
try { _CUESDK.CorsairDisconnect(); } catch { /* at least we tried */ }
try { _CUESDK.UnloadCUESDK(); } catch { /* at least we tried */ }
try { _CUESDK.CorsairDisconnect(); }
catch { /* at least we tried */ }
_instance = null;
try { _CUESDK.UnloadCUESDK(); }
catch { /* at least we tried */ }
_instance = null;
}
}
#endregion