Hardware/ThreadAffinity.cs
author moel.mich
Thu, 07 Jul 2011 20:41:09 +0000
changeset 308 d882720734bf
child 344 3145aadca3d2
permissions -rwxr-xr-x
Added support for reading the TAMG ACPI table on Gigabyte mainboards. Fixed a small problem with HDD/SSD names (0 chars are trimmed now as well).
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Runtime.InteropServices;
    40 
    41 namespace OpenHardwareMonitor.Hardware {
    42   
    43   internal static class ThreadAffinity {
    44   
    45     public static ulong Set(ulong mask) { 
    46       if (mask == 0)
    47         return 0;
    48         
    49       int p = (int)Environment.OSVersion.Platform;
    50       if ((p == 4) || (p == 128)) { // Unix
    51         ulong result = 0;
    52         if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), 
    53           ref result) != 0)          
    54           return 0;
    55         if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), 
    56           ref mask) != 0)
    57           return 0;
    58         return result;
    59       } else { // Windows      
    60         return (ulong)NativeMethods.SetThreadAffinityMask(
    61           NativeMethods.GetCurrentThread(), (UIntPtr)mask);
    62       }
    63     }
    64   
    65     private static class NativeMethods {      
    66       private const string KERNEL = "kernel32.dll";
    67 
    68       [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    69       public static extern UIntPtr
    70         SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
    71 
    72       [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
    73       public static extern IntPtr GetCurrentThread();       
    74       
    75       private const string LIBC = "libc";
    76       
    77       [DllImport(LIBC)]
    78       public static extern int sched_getaffinity(int pid, IntPtr maskSize,
    79         ref ulong mask);
    80       
    81       [DllImport(LIBC)]
    82       public static extern int sched_setaffinity(int pid, IntPtr maskSize,
    83         ref ulong mask);  
    84     }  
    85   }
    86 }
    87