Thread affinity

Don't set thread affinity in IsValid.
Remove SetThreadAffinityMask fallback as SetThreadGroupAffinity is available in W7 and up.
This commit is contained in:
PhyxionNL
2022-11-07 15:00:44 +01:00
parent 93fa695429
commit c4ec54e1cf
2 changed files with 18 additions and 33 deletions
@@ -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 <mmoeller@openhardwaremonitor.org> and Contributors.
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
// All Rights Reserved.
using System;
@@ -34,24 +34,25 @@ internal static class ThreadAffinity
/// <returns><c>true</c> if the specified affinity is valid; otherwise, <c>false</c>.</returns>
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;
}
/// <summary>
@@ -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;
}
}
+1 -5
View File
@@ -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'
}
}
}