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

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