Merge pull request #213 from DarthAffe/SDK/Wooting

Wooting - Fixed race condition during SDK load/unload
This commit is contained in:
Robert Beekman authored and GitHub committed 2021-05-13 19:45:33 +02:00
commit faf7535eaf
3 files changed
+43 -22

No files matched your search

@@ -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
@@ -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
}
}
@@ -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