Change CoreFX to use SetThreadErrorMode (dotnet/corefx#19801)

* Change CoreFX to use SetThreadErrorMode

See dotnet/corefx#19738. SetErrorMode isn't thread safe- we use SetThreadErrorMode
in NetFX.

* Add P/Invoke exceptions


Commit migrated from https://github.com/dotnet/corefx/commit/ac3d3ba3b4097a5626055447b39535e4ee67d9cb
This commit is contained in:
Jeremy Kuhne authored and Stephen Toub committed 2017-05-16 11:20:39 -04:00
1 parent 0441dc3e9f
commit 6624d0c80d
9 files changed
+51 -30

No files matched your search

@@ -1,5 +1,5 @@
kernel32.dll!GetDriveTypeW
kernel32.dll!GetLogicalDrives
kernel32.dll!GetVolumeInformationW
kernel32.dll!SetErrorMode
kernel32.dll!SetThreadErrorMode
kernel32.dll!SetVolumeLabelW
@@ -49,8 +49,8 @@
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetVolumeLabel.cs">
<Link>Common\Interop\Windows\Interop.SetVolumeLabel.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetErrorMode.cs">
<Link>Common\Interop\Windows\Interop.SetErrorMode.cs</Link>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetThreadErrorMode.cs">
<Link>Common\Interop\Windows\Interop.SetThreadErrorMode.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SecurityOptions.cs">
<Link>Common\Interop\Windows\Interop.SecurityOptions.cs</Link>
@@ -67,7 +67,8 @@ namespace System.IO
StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen);
int serialNumber, maxFileNameLen, fileSystemFlags;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen);
@@ -78,7 +79,8 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
if (success)
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
return fileSystemName.ToString();
}
@@ -90,7 +92,8 @@ namespace System.IO
get
{
long userBytes, totalBytes, freeBytes;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
@@ -99,7 +102,8 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
if (success)
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
return userBytes;
}
@@ -111,7 +115,8 @@ namespace System.IO
get
{
long userBytes, totalBytes, freeBytes;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
@@ -120,7 +125,8 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
if (success)
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
return freeBytes;
}
@@ -134,7 +140,8 @@ namespace System.IO
// Don't cache this, to handle variable sized floppy drives
// or other various removable media drives.
long userBytes, totalBytes, freeBytes;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
@@ -143,7 +150,7 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
return totalBytes;
}
@@ -174,7 +181,8 @@ namespace System.IO
StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen);
int serialNumber, maxFileNameLen, fileSystemFlags;
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen);
@@ -190,14 +198,16 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
if (success)
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
return volumeName.ToString();
}
[System.Security.SecuritySafeCritical] // auto-generated
set
{
uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS);
uint oldMode;
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
try
{
bool r = Interop.Kernel32.SetVolumeLabel(Name, value);
@@ -212,7 +222,8 @@ namespace System.IO
}
finally
{
Interop.Kernel32.SetErrorMode(oldMode);
if (success)
Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
}
}
}