From c4ec54e1cf75f177d6673bf0b3aec7c13464119a Mon Sep 17 00:00:00 2001 From: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com> Date: Mon, 7 Nov 2022 15:00:44 +0100 Subject: [PATCH] Thread affinity Don't set thread affinity in IsValid. Remove SetThreadAffinityMask fallback as SetThreadGroupAffinity is available in W7 and up. --- .../Hardware/ThreadAffinity.cs | 45 +++++++------------ LibreHardwareMonitorLib/Interop/Kernel32.cs | 6 +-- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs b/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs index 340af2c..eebe967 100644 --- a/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs +++ b/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs @@ -1,7 +1,7 @@ // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // Copyright (C) LibreHardwareMonitor and Contributors. -// Partial Copyright (C) Michael Möller and Contributors. +// Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. using System; @@ -34,24 +34,25 @@ internal static class ThreadAffinity /// true if the specified affinity is valid; otherwise, false. public static bool IsValid(GroupAffinity affinity) { - if (Software.OperatingSystem.IsUnix && affinity.Group > 0) + if (Software.OperatingSystem.IsUnix) { - return false; - } - - try - { - GroupAffinity previousAffinity = Set(affinity); - if (previousAffinity == GroupAffinity.Undefined) + if (affinity.Group > 0) return false; - Set(previousAffinity); return true; } - catch + + UIntPtr uIntPtrMask; + try + { + uIntPtrMask = (UIntPtr)affinity.Mask; + } + catch (OverflowException) { return false; } + + return true; } /// @@ -94,25 +95,13 @@ internal static class ThreadAffinity IntPtr currentThread = Kernel32.GetCurrentThread(); - try + if (Kernel32.SetThreadGroupAffinity(currentThread, + ref groupAffinity, + out Kernel32.GROUP_AFFINITY previousGroupAffinity)) { - if (Kernel32.SetThreadGroupAffinity(currentThread, - ref groupAffinity, - out Kernel32.GROUP_AFFINITY previousGroupAffinity)) - { - return new GroupAffinity(previousGroupAffinity.Group, (ulong)previousGroupAffinity.Mask); - } - - return GroupAffinity.Undefined; + return new GroupAffinity(previousGroupAffinity.Group, (ulong)previousGroupAffinity.Mask); } - catch (EntryPointNotFoundException) - { - if (affinity.Group > 0) - throw new ArgumentOutOfRangeException(nameof(affinity)); - ulong previous = (ulong)Kernel32.SetThreadAffinityMask(currentThread, uIntPtrMask); - - return new GroupAffinity(0, previous); - } + return GroupAffinity.Undefined; } } \ No newline at end of file diff --git a/LibreHardwareMonitorLib/Interop/Kernel32.cs b/LibreHardwareMonitorLib/Interop/Kernel32.cs index 64d2c38..5d9faba 100644 --- a/LibreHardwareMonitorLib/Interop/Kernel32.cs +++ b/LibreHardwareMonitorLib/Interop/Kernel32.cs @@ -317,10 +317,6 @@ public class Kernel32 [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool FreeLibrary(IntPtr module); - [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - internal static extern UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask); - [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr GetCurrentThread(); @@ -1558,4 +1554,4 @@ public class Kernel32 FIRM = (byte)'F' << 24 | (byte)'I' << 16 | (byte)'R' << 8 | (byte)'M', RSMB = (byte)'R' << 24 | (byte)'S' << 16 | (byte)'M' << 8 | (byte)'B' } -} \ No newline at end of file +}