moel@238: /* moel@238: moel@238: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@238: moel@238: The contents of this file are subject to the Mozilla Public License Version moel@238: 1.1 (the "License"); you may not use this file except in compliance with moel@238: the License. You may obtain a copy of the License at moel@238: moel@238: http://www.mozilla.org/MPL/ moel@238: moel@238: Software distributed under the License is distributed on an "AS IS" basis, moel@238: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@238: for the specific language governing rights and limitations under the License. moel@238: moel@238: The Original Code is the Open Hardware Monitor code. moel@238: moel@238: The Initial Developer of the Original Code is moel@238: Michael Möller . moel@238: Portions created by the Initial Developer are Copyright (C) 2010 moel@238: the Initial Developer. All Rights Reserved. moel@238: moel@238: Contributor(s): moel@238: moel@238: Alternatively, the contents of this file may be used under the terms of moel@238: either the GNU General Public License Version 2 or later (the "GPL"), or moel@238: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@238: in which case the provisions of the GPL or the LGPL are applicable instead moel@238: of those above. If you wish to allow use of your version of this file only moel@238: under the terms of either the GPL or the LGPL, and not to allow others to moel@238: use your version of this file under the terms of the MPL, indicate your moel@238: decision by deleting the provisions above and replace them with the notice moel@238: and other provisions required by the GPL or the LGPL. If you do not delete moel@238: the provisions above, a recipient may use your version of this file under moel@238: the terms of any one of the MPL, the GPL or the LGPL. moel@238: moel@238: */ moel@238: moel@238: using System; moel@238: using System.Runtime.InteropServices; moel@238: moel@238: namespace OpenHardwareMonitor.Hardware { moel@238: moel@238: internal static class ThreadAffinity { moel@238: moel@238: public static ulong Set(ulong mask) { moel@238: if (mask == 0) moel@238: return 0; moel@238: moel@238: int p = (int)Environment.OSVersion.Platform; moel@238: if ((p == 4) || (p == 128)) { // Unix moel@238: ulong result = 0; moel@238: if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), moel@238: ref result) != 0) moel@238: return 0; moel@238: if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), moel@238: ref mask) != 0) moel@238: return 0; moel@238: return result; moel@238: } else { // Windows moel@238: return (ulong)NativeMethods.SetThreadAffinityMask( moel@238: NativeMethods.GetCurrentThread(), (UIntPtr)mask); moel@238: } moel@238: } moel@238: moel@238: private static class NativeMethods { moel@238: private const string KERNEL = "kernel32.dll"; moel@238: moel@238: [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] moel@238: public static extern UIntPtr moel@238: SetThreadAffinityMask(IntPtr handle, UIntPtr mask); moel@238: moel@238: [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] moel@238: public static extern IntPtr GetCurrentThread(); moel@238: moel@238: private const string LIBC = "libc"; moel@238: moel@238: [DllImport(LIBC)] moel@238: public static extern int sched_getaffinity(int pid, IntPtr maskSize, moel@238: ref ulong mask); moel@238: moel@238: [DllImport(LIBC)] moel@238: public static extern int sched_setaffinity(int pid, IntPtr maskSize, moel@238: ref ulong mask); moel@238: } moel@238: } moel@238: } moel@238: