Hardware/ThreadAffinity.cs
author moel.mich
Sat, 31 Dec 2011 17:31:04 +0000
changeset 324 c6ee430d6995
child 344 3145aadca3d2
permissions -rwxr-xr-x
Modified and extended version of the patch v4 by Roland Reinl (see Issue 256). Main differences to the original patch: DeviceIoControl refactorings removed, SmartAttribute is now descriptive only and does not hold any state, report is written as one 80 columns table, sensors are created only for meaningful values and without duplicates (remaining life, temperatures, host writes and reads). Also the current implementation should really preserve all the functionality of the old system. Additionally there is now a simple SMART devices emulation class (DebugSmart) that can be used in place of WindowsSmart for testing with reported data.
moel@238
     1
/*
moel@238
     2
  
moel@238
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@238
     4
moel@238
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@238
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@238
     7
  the License. You may obtain a copy of the License at
moel@238
     8
 
moel@238
     9
  http://www.mozilla.org/MPL/
moel@238
    10
moel@238
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@238
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@238
    13
  for the specific language governing rights and limitations under the License.
moel@238
    14
moel@238
    15
  The Original Code is the Open Hardware Monitor code.
moel@238
    16
moel@238
    17
  The Initial Developer of the Original Code is 
moel@238
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@238
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@238
    20
  the Initial Developer. All Rights Reserved.
moel@238
    21
moel@238
    22
  Contributor(s):
moel@238
    23
moel@238
    24
  Alternatively, the contents of this file may be used under the terms of
moel@238
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@238
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@238
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@238
    28
  of those above. If you wish to allow use of your version of this file only
moel@238
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@238
    30
  use your version of this file under the terms of the MPL, indicate your
moel@238
    31
  decision by deleting the provisions above and replace them with the notice
moel@238
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@238
    33
  the provisions above, a recipient may use your version of this file under
moel@238
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@238
    35
 
moel@238
    36
*/
moel@238
    37
moel@238
    38
using System;
moel@238
    39
using System.Runtime.InteropServices;
moel@238
    40
moel@238
    41
namespace OpenHardwareMonitor.Hardware {
moel@238
    42
  
moel@238
    43
  internal static class ThreadAffinity {
moel@238
    44
  
moel@238
    45
    public static ulong Set(ulong mask) { 
moel@238
    46
      if (mask == 0)
moel@238
    47
        return 0;
moel@238
    48
        
moel@238
    49
      int p = (int)Environment.OSVersion.Platform;
moel@238
    50
      if ((p == 4) || (p == 128)) { // Unix
moel@238
    51
        ulong result = 0;
moel@238
    52
        if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), 
moel@238
    53
          ref result) != 0)          
moel@238
    54
          return 0;
moel@238
    55
        if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), 
moel@238
    56
          ref mask) != 0)
moel@238
    57
          return 0;
moel@238
    58
        return result;
moel@238
    59
      } else { // Windows      
moel@238
    60
        return (ulong)NativeMethods.SetThreadAffinityMask(
moel@238
    61
          NativeMethods.GetCurrentThread(), (UIntPtr)mask);
moel@238
    62
      }
moel@238
    63
    }
moel@238
    64
  
moel@238
    65
    private static class NativeMethods {      
moel@238
    66
      private const string KERNEL = "kernel32.dll";
moel@238
    67
moel@238
    68
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@238
    69
      public static extern UIntPtr
moel@238
    70
        SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
moel@238
    71
moel@238
    72
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@238
    73
      public static extern IntPtr GetCurrentThread();       
moel@238
    74
      
moel@238
    75
      private const string LIBC = "libc";
moel@238
    76
      
moel@238
    77
      [DllImport(LIBC)]
moel@238
    78
      public static extern int sched_getaffinity(int pid, IntPtr maskSize,
moel@238
    79
        ref ulong mask);
moel@238
    80
      
moel@238
    81
      [DllImport(LIBC)]
moel@238
    82
      public static extern int sched_setaffinity(int pid, IntPtr maskSize,
moel@238
    83
        ref ulong mask);  
moel@238
    84
    }  
moel@238
    85
  }
moel@238
    86
}
moel@238
    87