Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't always aquite lock when calling RuntimeInformation.OSArchitecture and OSArchitecture.ProcessArchitecture #39196

Merged
merged 8 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public static partial class RuntimeInformation
private static readonly object s_processLock = new object();
private static string? s_osPlatformName;
private static string? s_osDescription;
private static Architecture? s_osArch;
private static Architecture? s_processArch;
private static int s_osArch = -1;
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
private static int s_processArch = -1;

public static bool IsOSPlatform(OSPlatform osPlatform)
{
Expand All @@ -26,69 +26,79 @@ public static Architecture OSArchitecture
{
get
{
lock (s_osLock)
Debug.Assert(sizeof(Architecture) == sizeof(int));

if (s_osArch == -1)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
if (null == s_osArch)
lock (s_osLock)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetOSArchitecture();
switch (arch)
if (s_osArch == -1)
{
case Interop.Sys.ProcessorArchitecture.ARM:
s_osArch = Architecture.Arm;
break;
Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetOSArchitecture();
switch (arch)
{
case Interop.Sys.ProcessorArchitecture.ARM:
s_osArch = (int)Architecture.Arm;
break;

case Interop.Sys.ProcessorArchitecture.x64:
s_osArch = Architecture.X64;
break;
case Interop.Sys.ProcessorArchitecture.x64:
s_osArch = (int)Architecture.X64;
break;

case Interop.Sys.ProcessorArchitecture.x86:
s_osArch = Architecture.X86;
break;
case Interop.Sys.ProcessorArchitecture.x86:
s_osArch = (int)Architecture.X86;
break;

case Interop.Sys.ProcessorArchitecture.ARM64:
s_osArch = Architecture.Arm64;
break;
case Interop.Sys.ProcessorArchitecture.ARM64:
s_osArch = (int)Architecture.Arm64;
break;
}
}
}
}

Debug.Assert(s_osArch != null);
return s_osArch.Value;
Debug.Assert(s_osArch != -1);
return (Architecture)s_osArch;
}
}

public static Architecture ProcessArchitecture
{
get
{
lock (s_processLock)
Debug.Assert(sizeof(Architecture) == sizeof(int));

if (s_processArch == -1)
{
if (null == s_processArch)
lock (s_processLock)
{
Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetProcessArchitecture();
switch (arch)
if (s_processArch == -1)
{
case Interop.Sys.ProcessorArchitecture.ARM:
s_processArch = Architecture.Arm;
break;
Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetProcessArchitecture();
switch (arch)
{
case Interop.Sys.ProcessorArchitecture.ARM:
s_processArch = (int)Architecture.Arm;
break;

case Interop.Sys.ProcessorArchitecture.x64:
s_processArch = Architecture.X64;
break;
case Interop.Sys.ProcessorArchitecture.x64:
s_processArch = (int)Architecture.X64;
break;

case Interop.Sys.ProcessorArchitecture.x86:
s_processArch = Architecture.X86;
break;
case Interop.Sys.ProcessorArchitecture.x86:
s_processArch = (int)Architecture.X86;
break;

case Interop.Sys.ProcessorArchitecture.ARM64:
s_processArch = Architecture.Arm64;
break;
case Interop.Sys.ProcessorArchitecture.ARM64:
s_processArch = (int)Architecture.Arm64;
break;
}
}
}
}
}

Debug.Assert(s_processArch != null);
return s_processArch.Value;
Debug.Assert(s_processArch != -1);
return (Architecture)s_processArch;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public static partial class RuntimeInformation
private static string? s_osDescription;
private static readonly object s_osLock = new object();
private static readonly object s_processLock = new object();
private static Architecture? s_osArch;
private static Architecture? s_processArch;
private static int s_osArch = -1;
private static int s_processArch = -1;

public static bool IsOSPlatform(OSPlatform osPlatform)
{
Expand Down Expand Up @@ -42,68 +42,77 @@ public static Architecture OSArchitecture
{
get
{
lock (s_osLock)
Debug.Assert(sizeof(Architecture) == sizeof(int));

if (s_osArch == -1)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
if (null == s_osArch)
lock (s_osLock)
{
Interop.Kernel32.SYSTEM_INFO sysInfo;
Interop.Kernel32.GetNativeSystemInfo(out sysInfo);

switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
if (s_osArch == -1)
{
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
s_osArch = Architecture.Arm64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
s_osArch = Architecture.Arm;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
s_osArch = Architecture.X64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
s_osArch = Architecture.X86;
break;
}
Interop.Kernel32.SYSTEM_INFO sysInfo;
Interop.Kernel32.GetNativeSystemInfo(out sysInfo);

switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
{
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
s_osArch = (int)Architecture.Arm64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
s_osArch = (int)Architecture.Arm;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
s_osArch = (int)Architecture.X64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
s_osArch = (int)Architecture.X86;
break;
}
}
}
}

Debug.Assert(s_osArch != null);
return s_osArch.Value;
Debug.Assert(s_osArch != -1);
return (Architecture)s_osArch;
}
}

public static Architecture ProcessArchitecture
{
get
{
lock (s_processLock)
Debug.Assert(sizeof(Architecture) == sizeof(int));

if (s_processArch == -1)
{
if (null == s_processArch)
lock (s_processLock)
{
Interop.Kernel32.SYSTEM_INFO sysInfo;
Interop.Kernel32.GetSystemInfo(out sysInfo);

switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
if (s_processArch == -1)
{
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
s_processArch = Architecture.Arm64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
s_processArch = Architecture.Arm;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
s_processArch = Architecture.X64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
s_processArch = Architecture.X86;
break;
Interop.Kernel32.SYSTEM_INFO sysInfo;
Interop.Kernel32.GetSystemInfo(out sysInfo);

switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
s_processArch = (int)Architecture.Arm64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
s_processArch = (int)Architecture.Arm;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
s_processArch = (int)Architecture.X64;
break;
case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
s_processArch = (int)Architecture.X86;
break;
}
}
}
}

Debug.Assert(s_processArch != null);
return s_processArch.Value;
Debug.Assert(s_processArch != -1);
return (Architecture)s_processArch;
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down