c930aac925406339681ab3cd63bc89e0b67d1b70
[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 readonly object s_osLock = new object();
11         private static readonly object s_processLock = new object();
12         private static string? s_osPlatformName;
13         private static string? s_osDescription;
14         private static Architecture? s_osArch;
15         private static Architecture? s_processArch;
16
17         public static bool IsOSPlatform(OSPlatform osPlatform)
18         {
19             string name = s_osPlatformName ??= Interop.Sys.GetUnixName();
20             return osPlatform.Equals(name);
21         }
22
23         public static string OSDescription => s_osDescription ??= Interop.Sys.GetUnixVersion();
24
25         public static Architecture OSArchitecture
26         {
27             get
28             {
29                 lock (s_osLock)
30                 {
31                     if (null == s_osArch)
32                     {
33                         Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetOSArchitecture();
34                         switch (arch)
35                         {
36                             case Interop.Sys.ProcessorArchitecture.ARM:
37                                 s_osArch = Architecture.Arm;
38                                 break;
39
40                             case Interop.Sys.ProcessorArchitecture.x64:
41                                 s_osArch = Architecture.X64;
42                                 break;
43
44                             case Interop.Sys.ProcessorArchitecture.x86:
45                                 s_osArch = Architecture.X86;
46                                 break;
47
48                             case Interop.Sys.ProcessorArchitecture.ARM64:
49                                 s_osArch = Architecture.Arm64;
50                                 break;
51                         }
52                     }
53                 }
54
55                 Debug.Assert(s_osArch != null);
56                 return s_osArch.Value;
57             }
58         }
59
60         public static Architecture ProcessArchitecture
61         {
62             get
63             {
64                 lock (s_processLock)
65                 {
66                     if (null == s_processArch)
67                     {
68                         Interop.Sys.ProcessorArchitecture arch = (Interop.Sys.ProcessorArchitecture)Interop.Sys.GetProcessArchitecture();
69                         switch (arch)
70                         {
71                             case Interop.Sys.ProcessorArchitecture.ARM:
72                                 s_processArch = Architecture.Arm;
73                                 break;
74
75                             case Interop.Sys.ProcessorArchitecture.x64:
76                                 s_processArch = Architecture.X64;
77                                 break;
78
79                             case Interop.Sys.ProcessorArchitecture.x86:
80                                 s_processArch = Architecture.X86;
81                                 break;
82
83                             case Interop.Sys.ProcessorArchitecture.ARM64:
84                                 s_processArch = Architecture.Arm64;
85                                 break;
86                         }
87                     }
88                 }
89
90                 Debug.Assert(s_processArch != null);
91                 return s_processArch.Value;
92             }
93         }
94     }
95 }