1.1 --- a/Hardware/WinRing0.cs Thu Aug 12 20:53:27 2010 +0000
1.2 +++ b/Hardware/WinRing0.cs Sun Aug 15 14:46:58 2010 +0000
1.3 @@ -44,6 +44,8 @@
1.4 namespace OpenHardwareMonitor.Hardware {
1.5
1.6 internal class WinRing0 {
1.7 +
1.8 + private WinRing0() { }
1.9
1.10 public enum OlsDllStatus{
1.11 OLS_DLL_NO_ERROR = 0,
1.12 @@ -56,7 +58,7 @@
1.13 }
1.14
1.15 private static bool available = false;
1.16 - public static Mutex isaBusMutex;
1.17 + private static Mutex isaBusMutex;
1.18
1.19 private static string GetDllName() {
1.20 int p = (int)System.Environment.OSVersion.Platform;
1.21 @@ -78,10 +80,7 @@
1.22 private delegate bool InitializeOlsDelegate();
1.23 private delegate void DeinitializeOlsDelegate();
1.24
1.25 - public delegate uint GetDllStatusDelegate();
1.26 public delegate bool IsCpuidDelegate();
1.27 - public delegate bool CpuidDelegate(uint index, uint ecxValue,
1.28 - out uint eax, out uint ebx, out uint ecx, out uint edx);
1.29 public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
1.30 out uint eax, out uint ebx, out uint ecx, out uint edx,
1.31 UIntPtr threadAffinityMask);
1.32 @@ -90,7 +89,6 @@
1.33 UIntPtr threadAffinityMask);
1.34 public delegate byte ReadIoPortByteDelegate(ushort port);
1.35 public delegate void WriteIoPortByteDelegate(ushort port, byte value);
1.36 - public delegate void SetPciMaxBusIndexDelegate(byte max);
1.37 public delegate uint FindPciDeviceByIdDelegate(ushort vendorId,
1.38 ushort deviceId, byte index);
1.39 public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress,
1.40 @@ -101,85 +99,73 @@
1.41 UIntPtr threadAffinityMask);
1.42 public delegate bool RdtscDelegate(out uint eax, out uint edx);
1.43
1.44 - private static InitializeOlsDelegate InitializeOls;
1.45 - private static DeinitializeOlsDelegate DeinitializeOls;
1.46 + private static InitializeOlsDelegate InitializeOls =
1.47 + CreateDelegate<InitializeOlsDelegate>("InitializeOls");
1.48 + private static DeinitializeOlsDelegate DeinitializeOls =
1.49 + CreateDelegate<DeinitializeOlsDelegate>("DeinitializeOls");
1.50
1.51 - public static readonly GetDllStatusDelegate GetDllStatus;
1.52 - public static readonly IsCpuidDelegate IsCpuid;
1.53 - public static readonly CpuidDelegate Cpuid;
1.54 - public static readonly CpuidTxDelegate CpuidTx;
1.55 - public static readonly RdmsrDelegate Rdmsr;
1.56 - public static readonly RdmsrTxDelegate RdmsrTx;
1.57 - public static readonly ReadIoPortByteDelegate ReadIoPortByte;
1.58 - public static readonly WriteIoPortByteDelegate WriteIoPortByte;
1.59 - public static readonly SetPciMaxBusIndexDelegate SetPciMaxBusIndex;
1.60 - public static readonly FindPciDeviceByIdDelegate FindPciDeviceById;
1.61 - public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx;
1.62 - public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx;
1.63 - public static readonly RdtscTxDelegate RdtscTx;
1.64 - public static readonly RdtscDelegate Rdtsc;
1.65 -
1.66 -
1.67 -
1.68 - private static void GetDelegate<T>(string entryPoint, out T newDelegate)
1.69 - where T : class
1.70 - {
1.71 + public static readonly IsCpuidDelegate IsCpuid =
1.72 + CreateDelegate<IsCpuidDelegate>("IsCpuid");
1.73 + public static readonly CpuidTxDelegate CpuidTx =
1.74 + CreateDelegate<CpuidTxDelegate>("CpuidTx");
1.75 + public static readonly RdmsrDelegate Rdmsr =
1.76 + CreateDelegate<RdmsrDelegate>("Rdmsr");
1.77 + public static readonly RdmsrTxDelegate RdmsrTx =
1.78 + CreateDelegate<RdmsrTxDelegate>("RdmsrTx");
1.79 + public static readonly ReadIoPortByteDelegate ReadIoPortByte =
1.80 + CreateDelegate<ReadIoPortByteDelegate>("ReadIoPortByte");
1.81 + public static readonly WriteIoPortByteDelegate WriteIoPortByte =
1.82 + CreateDelegate<WriteIoPortByteDelegate>("WriteIoPortByte");
1.83 + public static readonly FindPciDeviceByIdDelegate FindPciDeviceById =
1.84 + CreateDelegate<FindPciDeviceByIdDelegate>("FindPciDeviceById");
1.85 + public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx =
1.86 + CreateDelegate<ReadPciConfigDwordExDelegate>("ReadPciConfigDwordEx");
1.87 + public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx =
1.88 + CreateDelegate<WritePciConfigDwordExDelegate>("WritePciConfigDwordEx");
1.89 + public static readonly RdtscTxDelegate RdtscTx =
1.90 + CreateDelegate<RdtscTxDelegate>("RdtscTx");
1.91 + public static readonly RdtscDelegate Rdtsc =
1.92 + CreateDelegate<RdtscDelegate>("Rdtsc");
1.93 +
1.94 + private static T CreateDelegate<T>(string entryPoint) where T : class {
1.95 DllImportAttribute attribute = new DllImportAttribute(GetDllName());
1.96 attribute.CallingConvention = CallingConvention.Winapi;
1.97 attribute.PreserveSig = true;
1.98 attribute.EntryPoint = entryPoint;
1.99 attribute.CharSet = CharSet.Auto;
1.100 - PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
1.101 + T result;
1.102 + PInvokeDelegateFactory.CreateDelegate(attribute, out result);
1.103 + return result;
1.104 }
1.105
1.106 - static WinRing0() {
1.107 - GetDelegate("InitializeOls", out InitializeOls);
1.108 - GetDelegate("DeinitializeOls", out DeinitializeOls);
1.109 - GetDelegate("GetDllStatus", out GetDllStatus);
1.110 - GetDelegate("IsCpuid", out IsCpuid);
1.111 - GetDelegate("Cpuid", out Cpuid);
1.112 - GetDelegate("CpuidTx", out CpuidTx);
1.113 - GetDelegate("Rdmsr", out Rdmsr);
1.114 - GetDelegate("RdmsrTx", out RdmsrTx);
1.115 - GetDelegate("ReadIoPortByte", out ReadIoPortByte);
1.116 - GetDelegate("WriteIoPortByte", out WriteIoPortByte);
1.117 - GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex);
1.118 - GetDelegate("FindPciDeviceById", out FindPciDeviceById);
1.119 - GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx);
1.120 - GetDelegate("WritePciConfigDwordEx", out WritePciConfigDwordEx);
1.121 - GetDelegate("RdtscTx", out RdtscTx);
1.122 - GetDelegate("Rdtsc", out Rdtsc);
1.123 -
1.124 + public static void Open() {
1.125 try {
1.126 if (InitializeOls != null && InitializeOls())
1.127 available = true;
1.128 - } catch (DllNotFoundException) { }
1.129 -
1.130 + } catch (DllNotFoundException) { }
1.131 +
1.132 isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
1.133 }
1.134 -
1.135 +
1.136 public static bool IsAvailable {
1.137 get { return available; }
1.138 }
1.139
1.140 + public static void Close() {
1.141 + if (available)
1.142 + DeinitializeOls();
1.143 + isaBusMutex.Close();
1.144 + }
1.145 +
1.146 public static bool WaitIsaBusMutex(int millisecondsTimeout) {
1.147 try {
1.148 - return isaBusMutex.WaitOne(millisecondsTimeout);
1.149 - } catch { return false; }
1.150 + return isaBusMutex.WaitOne(millisecondsTimeout, false);
1.151 + } catch (AbandonedMutexException) { return false; }
1.152 + catch (InvalidOperationException) { return false; }
1.153 }
1.154
1.155 public static void ReleaseIsaBusMutex() {
1.156 isaBusMutex.ReleaseMutex();
1.157 - }
1.158 -
1.159 - private static Deinitializer deinitializer = new Deinitializer();
1.160 - private class Deinitializer {
1.161 - ~Deinitializer() {
1.162 - if (available)
1.163 - DeinitializeOls();
1.164 -
1.165 - isaBusMutex.Close();
1.166 - }
1.167 - }
1.168 + }
1.169 }
1.170 }