Hardware/ThreadAffinity.cs
author moel.mich
Mon, 01 Oct 2012 17:10:09 +0000
changeset 381 ed5d5fc482ed
parent 238 bddc6e01840a
child 432 7b859a06eecb
permissions -rwxr-xr-x
Changed a few types to internal (instead of public) in the Open Hardware Monitor library.
moel@344
     1
/*
moel@344
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@344
     6
 
moel@344
     7
  Copyright (C) 2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@238
     9
*/
moel@238
    10
moel@238
    11
using System;
moel@238
    12
using System.Runtime.InteropServices;
moel@238
    13
moel@238
    14
namespace OpenHardwareMonitor.Hardware {
moel@238
    15
  
moel@238
    16
  internal static class ThreadAffinity {
moel@238
    17
  
moel@238
    18
    public static ulong Set(ulong mask) { 
moel@238
    19
      if (mask == 0)
moel@238
    20
        return 0;
moel@238
    21
        
moel@238
    22
      int p = (int)Environment.OSVersion.Platform;
moel@238
    23
      if ((p == 4) || (p == 128)) { // Unix
moel@238
    24
        ulong result = 0;
moel@238
    25
        if (NativeMethods.sched_getaffinity(0, (IntPtr)Marshal.SizeOf(result), 
moel@238
    26
          ref result) != 0)          
moel@238
    27
          return 0;
moel@238
    28
        if (NativeMethods.sched_setaffinity(0, (IntPtr)Marshal.SizeOf(mask), 
moel@238
    29
          ref mask) != 0)
moel@238
    30
          return 0;
moel@238
    31
        return result;
moel@238
    32
      } else { // Windows      
moel@238
    33
        return (ulong)NativeMethods.SetThreadAffinityMask(
moel@238
    34
          NativeMethods.GetCurrentThread(), (UIntPtr)mask);
moel@238
    35
      }
moel@238
    36
    }
moel@238
    37
  
moel@238
    38
    private static class NativeMethods {      
moel@238
    39
      private const string KERNEL = "kernel32.dll";
moel@238
    40
moel@238
    41
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@238
    42
      public static extern UIntPtr
moel@238
    43
        SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
moel@238
    44
moel@238
    45
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@238
    46
      public static extern IntPtr GetCurrentThread();       
moel@238
    47
      
moel@238
    48
      private const string LIBC = "libc";
moel@238
    49
      
moel@238
    50
      [DllImport(LIBC)]
moel@238
    51
      public static extern int sched_getaffinity(int pid, IntPtr maskSize,
moel@238
    52
        ref ulong mask);
moel@238
    53
      
moel@238
    54
      [DllImport(LIBC)]
moel@238
    55
      public static extern int sched_setaffinity(int pid, IntPtr maskSize,
moel@238
    56
        ref ulong mask);  
moel@238
    57
    }  
moel@238
    58
  }
moel@238
    59
}
moel@238
    60