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 /> /// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) 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; foreach ((object key, Color color) in dataSet)
_WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB()); {
} (int row, int column) = ((int, int))key;
_WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB());
}
_WootingSDK.ArrayUpdateKeyboard(); _WootingSDK.ArrayUpdateKeyboard();
}
} }
#endregion #endregion
@@ -16,6 +16,7 @@ namespace RGB.NET.Devices.Wooting.Native
#region Library management #region Library management
private static IntPtr _dllHandle = IntPtr.Zero; private static IntPtr _dllHandle = IntPtr.Zero;
internal static object SdkLock = new();
/// <summary> /// <summary>
/// Reloads the SDK. /// Reloads the SDK.
@@ -52,6 +53,16 @@ namespace RGB.NET.Devices.Wooting.Native
{ {
if (_dllHandle == IntPtr.Zero) return; 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 // 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)) ; while (FreeLibrary(_dllHandle)) ;
_dllHandle = IntPtr.Zero; _dllHandle = IntPtr.Zero;
@@ -116,3 +127,4 @@ namespace RGB.NET.Devices.Wooting.Native
#endregion #endregion
} }
} }
@@ -55,25 +55,31 @@ namespace RGB.NET.Devices.Wooting
protected override void InitializeSDK() protected override void InitializeSDK()
{ {
_WootingSDK.Reload(); lock (_WootingSDK.SdkLock)
{
_WootingSDK.Reload();
}
} }
protected override IEnumerable<IRGBDevice> LoadDevices() protected override IEnumerable<IRGBDevice> LoadDevices()
{ {
if (_WootingSDK.KeyboardConnected()) lock (_WootingSDK.SdkLock)
{ {
_WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!; if (_WootingSDK.KeyboardConnected())
IWootingRGBDevice? device = nativeDeviceInfo.Model switch
{ {
"Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()), _WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!;
"Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()), IWootingRGBDevice? device = nativeDeviceInfo.Model switch
_ => null {
}; "Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()),
"Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()),
_ => null
};
if (device == null) if (device == null)
Throw(new RGBDeviceException("No supported Wooting keyboard connected")); Throw(new RGBDeviceException("No supported Wooting keyboard connected"));
else else
yield return device; yield return device;
}
} }
} }
@@ -82,11 +88,11 @@ namespace RGB.NET.Devices.Wooting
{ {
base.Dispose(); base.Dispose();
try { _WootingSDK.Close(); } lock (_WootingSDK.SdkLock)
catch { /* Unlucky.. */ } {
try { _WootingSDK.UnloadWootingSDK(); }
try { _WootingSDK.UnloadWootingSDK(); } catch { /* at least we tried */ }
catch { /* at least we tried */ } }
} }
#endregion #endregion