1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Wooting - Fixed race condition during SDK load/unload

This commit is contained in:
Robert 2021-05-06 21:18:34 +02:00
parent 63498eb8c3
commit 41f9d82edc
3 changed files with 43 additions and 22 deletions

View File

@ -28,13 +28,16 @@ namespace RGB.NET.Devices.Wooting.Generic
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
foreach ((object key, Color color) in dataSet)
lock (_WootingSDK.SdkLock)
{
(int row, int column) = ((int, int))key;
_WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB());
}
foreach ((object key, Color color) in dataSet)
{
(int row, int column) = ((int, int))key;
_WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB());
}
_WootingSDK.ArrayUpdateKeyboard();
_WootingSDK.ArrayUpdateKeyboard();
}
}
#endregion

View File

@ -16,6 +16,7 @@ namespace RGB.NET.Devices.Wooting.Native
#region Library management
private static IntPtr _dllHandle = IntPtr.Zero;
internal static object SdkLock = new();
/// <summary>
/// Reloads the SDK.
@ -52,6 +53,16 @@ namespace RGB.NET.Devices.Wooting.Native
{
if (_dllHandle == IntPtr.Zero) return;
Reset();
Close();
_getDeviceInfoPointer = null;
_keyboardConnectedPointer = null;
_arrayUpdateKeyboardPointer = null;
_arraySetSinglePointer = null;
_resetPointer = null;
_closePointer = null;
// ReSharper disable once EmptyEmbeddedStatement - DarthAffe 20.02.2016: We might need to reduce the internal reference counter more than once to set the library free
while (FreeLibrary(_dllHandle)) ;
_dllHandle = IntPtr.Zero;
@ -116,3 +127,4 @@ namespace RGB.NET.Devices.Wooting.Native
#endregion
}
}

View File

@ -55,25 +55,31 @@ namespace RGB.NET.Devices.Wooting
protected override void InitializeSDK()
{
_WootingSDK.Reload();
lock (_WootingSDK.SdkLock)
{
_WootingSDK.Reload();
}
}
protected override IEnumerable<IRGBDevice> LoadDevices()
{
if (_WootingSDK.KeyboardConnected())
lock (_WootingSDK.SdkLock)
{
_WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!;
IWootingRGBDevice? device = nativeDeviceInfo.Model switch
if (_WootingSDK.KeyboardConnected())
{
"Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()),
"Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()),
_ => null
};
_WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!;
IWootingRGBDevice? device = nativeDeviceInfo.Model switch
{
"Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()),
"Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()),
_ => null
};
if (device == null)
Throw(new RGBDeviceException("No supported Wooting keyboard connected"));
else
yield return device;
if (device == null)
Throw(new RGBDeviceException("No supported Wooting keyboard connected"));
else
yield return device;
}
}
}
@ -82,11 +88,11 @@ namespace RGB.NET.Devices.Wooting
{
base.Dispose();
try { _WootingSDK.Close(); }
catch { /* Unlucky.. */ }
try { _WootingSDK.UnloadWootingSDK(); }
catch { /* at least we tried */ }
lock (_WootingSDK.SdkLock)
{
try { _WootingSDK.UnloadWootingSDK(); }
catch { /* at least we tried */ }
}
}
#endregion