0ccc9deb93004744b128e03b77252a9228e67bff
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3
4 using System.Diagnostics;
5
6 namespace System.Runtime.InteropServices
7 {
8     public static partial class RuntimeInformation
9     {
10         private static string? s_osDescription;
11         private static readonly object s_osLock = new object();
12         private static readonly object s_processLock = new object();
13         private static Architecture? s_osArch;
14         private static Architecture? s_processArch;
15
16         public static bool IsOSPlatform(OSPlatform osPlatform)
17         {
18             return OSPlatform.Windows == osPlatform;
19         }
20
21         public static string OSDescription
22         {
23             get
24             {
25                 string? osDescription = s_osDescription;
26                 if (osDescription is null)
27                 {
28                     OperatingSystem os = Environment.OSVersion;
29                     Version v = os.Version;
30
31                     const string Version = "Microsoft Windows";
32                     s_osDescription = osDescription = string.IsNullOrEmpty(os.ServicePack) ?
33                         $"{Version} {(uint)v.Major}.{(uint)v.Minor}.{(uint)v.Build}" :
34                         $"{Version} {(uint)v.Major}.{(uint)v.Minor}.{(uint)v.Build} {os.ServicePack}";
35                 }
36
37                 return osDescription;
38             }
39         }
40
41         public static Architecture OSArchitecture
42         {
43             get
44             {
45                 lock (s_osLock)
46                 {
47                     if (null == s_osArch)
48                     {
49                         Interop.Kernel32.SYSTEM_INFO sysInfo;
50                         Interop.Kernel32.GetNativeSystemInfo(out sysInfo);
51
52                         switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
53                         {
54                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
55                                 s_osArch = Architecture.Arm64;
56                                 break;
57                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
58                                 s_osArch = Architecture.Arm;
59                                 break;
60                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
61                                 s_osArch = Architecture.X64;
62                                 break;
63                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
64                                 s_osArch = Architecture.X86;
65                                 break;
66                         }
67
68                     }
69                 }
70
71                 Debug.Assert(s_osArch != null);
72                 return s_osArch.Value;
73             }
74         }
75
76         public static Architecture ProcessArchitecture
77         {
78             get
79             {
80                 lock (s_processLock)
81                 {
82                     if (null == s_processArch)
83                     {
84                         Interop.Kernel32.SYSTEM_INFO sysInfo;
85                         Interop.Kernel32.GetSystemInfo(out sysInfo);
86
87                         switch ((Interop.Kernel32.ProcessorArchitecture)sysInfo.wProcessorArchitecture)
88                         {
89                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM64:
90                                 s_processArch = Architecture.Arm64;
91                                 break;
92                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_ARM:
93                                 s_processArch = Architecture.Arm;
94                                 break;
95                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_AMD64:
96                                 s_processArch = Architecture.X64;
97                                 break;
98                             case Interop.Kernel32.ProcessorArchitecture.Processor_Architecture_INTEL:
99                                 s_processArch = Architecture.X86;
100                                 break;
101                         }
102                     }
103                 }
104
105                 Debug.Assert(s_processArch != null);
106                 return s_processArch.Value;
107             }
108         }
109     }
110 }