From: Ben Adams Date: Mon, 31 Dec 2018 05:22:08 +0000 (+0100) Subject: Remove Environment static constructor (#21715) X-Git-Tag: accepted/tizen/unified/20190422.045933~256 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6a989431e5a8c5d9113b6d03a4aa9a15b6653655;p=platform%2Fupstream%2Fcoreclr.git Remove Environment static constructor (#21715) --- diff --git a/src/System.Private.CoreLib/src/System/Environment.cs b/src/System.Private.CoreLib/src/System/Environment.cs index e94f62c..488da3b 100644 --- a/src/System.Private.CoreLib/src/System/Environment.cs +++ b/src/System.Private.CoreLib/src/System/Environment.cs @@ -186,7 +186,7 @@ namespace System [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern string[] GetCommandLineArgsNative(); - private static string[] s_CommandLineArgs = null; + private static string[] s_CommandLineArgs; private static void SetCommandLineArgs(string[] cmdLineArgs) { s_CommandLineArgs = cmdLineArgs; @@ -272,39 +272,52 @@ namespace System } #if !FEATURE_PAL - private static Lazy s_IsWindows8OrAbove = new Lazy(() => + internal static bool IsWindows8OrAbove => WindowsVersion.IsWindows8OrAbove; + + // Seperate type so a .cctor is not created for Enviroment which then would be triggered during startup + private static class WindowsVersion { - unsafe - { - ulong conditionMask = Win32Native.VerSetConditionMask(0, Win32Native.VER_MAJORVERSION, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_MINORVERSION, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMAJOR, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMINOR, Win32Native.VER_GREATER_EQUAL); + // Cache the value in readonly static that can be optimized out by the JIT + internal readonly static bool IsWindows8OrAbove = GetIsWindows8OrAbove(); - // Windows 8 version is 6.2 - var version = new Win32Native.OSVERSIONINFOEX(); - version.dwOSVersionInfoSize = sizeof(Win32Native.OSVERSIONINFOEX); - version.dwMajorVersion = 6; - version.dwMinorVersion = 2; - version.wServicePackMajor = 0; - version.wServicePackMinor = 0; + private static bool GetIsWindows8OrAbove() + { + bool isWindows8OrAbove; + unsafe + { + ulong conditionMask = Win32Native.VerSetConditionMask(0, Win32Native.VER_MAJORVERSION, Win32Native.VER_GREATER_EQUAL); + conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_MINORVERSION, Win32Native.VER_GREATER_EQUAL); + conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMAJOR, Win32Native.VER_GREATER_EQUAL); + conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMINOR, Win32Native.VER_GREATER_EQUAL); + + // Windows 8 version is 6.2 + var version = new Win32Native.OSVERSIONINFOEX(); + version.dwOSVersionInfoSize = sizeof(Win32Native.OSVERSIONINFOEX); + version.dwMajorVersion = 6; + version.dwMinorVersion = 2; + version.wServicePackMajor = 0; + version.wServicePackMinor = 0; + + isWindows8OrAbove = Win32Native.VerifyVersionInfoW(ref version, + Win32Native.VER_MAJORVERSION | Win32Native.VER_MINORVERSION | Win32Native.VER_SERVICEPACKMAJOR | Win32Native.VER_SERVICEPACKMINOR, + conditionMask); + } - return Win32Native.VerifyVersionInfoW(ref version, - Win32Native.VER_MAJORVERSION | Win32Native.VER_MINORVERSION | Win32Native.VER_SERVICEPACKMAJOR | Win32Native.VER_SERVICEPACKMINOR, - conditionMask); + return isWindows8OrAbove; } - }); - internal static bool IsWindows8OrAbove => s_IsWindows8OrAbove.Value; + } #endif - + #if FEATURE_COMINTEROP - // Does the current version of Windows have Windows Runtime suppport? - private static Lazy s_IsWinRTSupported = new Lazy(() => + // Seperate type so a .cctor is not created for Enviroment which then would be triggered during startup + private static class WinRT { - return WinRTSupported(); - }); + // Cache the value in readonly static that can be optimized out by the JIT + public readonly static bool IsSupported = WinRTSupported(); + } - internal static bool IsWinRTSupported => s_IsWinRTSupported.Value; + // Does the current version of Windows have Windows Runtime suppport? + internal static bool IsWinRTSupported => WinRT.IsSupported; [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)]