[corlib] Update public api
[mono.git] / mcs / class / corlib / ReferenceSources / PlatformHelper.cs
1 using System.Diagnostics.CodeAnalysis;
2
3 namespace System.Threading
4 {
5     /// <summary>
6     /// A helper class to get the number of processors, it updates the numbers of processors every sampling interval.
7     /// </summary>
8     internal static class PlatformHelper
9     {
10         private const int PROCESSOR_COUNT_REFRESH_INTERVAL_MS = 30000; // How often to refresh the count, in milliseconds.
11         private static volatile int s_processorCount; // The last count seen.
12         private static volatile int s_lastProcessorCountRefreshTicks; // The last time we refreshed.
13
14         /// <summary>
15         /// Gets the number of available processors
16         /// </summary>
17         [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
18         internal static int ProcessorCount
19         {
20             get
21             {
22                 int now = Environment.TickCount;
23                 int procCount = s_processorCount;
24                 if (procCount == 0 || (now - s_lastProcessorCountRefreshTicks) >= PROCESSOR_COUNT_REFRESH_INTERVAL_MS)
25                 {
26                     s_processorCount = procCount = Environment.ProcessorCount;
27                     s_lastProcessorCountRefreshTicks = now;
28                 }
29
30                 return procCount;
31             }
32         }
33
34         /// <summary>
35         /// Gets whether the current machine has only a single processor.
36         /// </summary>
37         internal static bool IsSingleProcessor
38         {
39             get { return ProcessorCount == 1; }
40         }
41     }
42 }