[work-in-progress][API] RuntimeFeature.IsMultithreadingSupported#124603
[work-in-progress][API] RuntimeFeature.IsMultithreadingSupported#124603pavelsavara wants to merge 10 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request introduces a new public API RuntimeFeature.IsMultithreadingSupported to replace the internal Thread.IsSingleThreaded property. This is part of API proposal #77541 to provide a public feature flag for determining if multithreading is supported on the current platform (particularly relevant for single-threaded WebAssembly environments).
Changes:
- Introduces public
RuntimeFeature.IsMultithreadingSupportedproperty and internalThrowIfMultithreadingIsNotSupported()method - Removes internal
Thread.IsSingleThreadedandThread.ThrowIfSingleThreaded() - Renames
PlatformDetection.IsThreadingSupported→IsMultithreadingSupportedandIsNotWasmThreadingSupported→IsNotMultithreadingSupported - Updates hundreds of test files to use the new naming convention
- Configures the feature switch in MSBuild targets for WebAssembly
Reviewed changes
Copilot reviewed 272 out of 272 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| System.Runtime.cs | Adds public API surface for IsMultithreadingSupported |
| RuntimeFeature.cs | Implements new property and internal throw helper |
| Thread.cs | Removes old IsSingleThreaded API |
| PlatformDetection.cs (multiple) | Updates to use RuntimeFeature instead of environment variable |
| Environment.cs | Uses new API for ProcessorCount logic |
| Microsoft.NET.Sdk.WebAssembly.Browser.targets | Configures feature switch for WASM |
| tests.browser.targets | Removes IsBrowserThreadingSupported environment variable |
| 200+ test files | Updates ConditionalFact/Theory attributes to use new naming |
Can you do 1 boring prep PR that has just this rename and nothing else, and do the rest in this PR? It is hard to find any interesting changes in 250+ changed files. |
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.cs
Show resolved
Hide resolved
For now this is a draft on which I'm trying to make it compile. There is change of namespace and the new field doesn't exist in Net10/11.0.0-preview.1.26104.118 or lower (for which we build some tests). Once I have passing build, I would try to make it easier to review. Perhaps this could be rebased as 3 commits ? |
26ff2f4 to
7b0c965
Compare
src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.cs
Outdated
Show resolved
Hide resolved
...nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoNames.cs
Outdated
Show resolved
Hide resolved
...ging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/SimpleConsoleFormatterTests.cs
Show resolved
Hide resolved
8904240 to
9b8972d
Compare
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.cs
Outdated
Show resolved
Hide resolved
| /// <summary> | ||
| /// Gets a value that indicates whether the runtime supports multithreading, including | ||
| /// creating threads and using blocking synchronization primitives. This property | ||
| /// returns <see langword="false"/> on platforms that do not support multithreading, | ||
| /// such as browser and WASI. | ||
| /// </summary> | ||
| [UnsupportedOSPlatformGuard("browser")] | ||
| [UnsupportedOSPlatformGuard("wasi")] | ||
| [FeatureSwitchDefinition("System.Runtime.CompilerServices.RuntimeFeature.IsMultithreadingSupported")] | ||
| public static bool IsMultithreadingSupported { get; } | ||
| = AppContext.TryGetSwitch("System.Runtime.CompilerServices.RuntimeFeature.IsMultithreadingSupported", out bool isMultithreadingSupported) ? isMultithreadingSupported : true; | ||
|
|
||
| internal static void ThrowIfMultithreadingIsNotSupported() | ||
| { | ||
| if (!IsMultithreadingSupported) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| } |
There was a problem hiding this comment.
IsMultithreadingSupported defaults to true when the AppContext switch isn't set, but the XML doc and platform guards state it returns false on unsupported platforms like browser/WASI. I couldn't find any non-browser wiring for this switch (only Browser.targets sets it), so on WASI (and any single-threaded host missing the config) this will incorrectly report true and may allow thread/blocking paths to run. Consider defaulting based on platform/feature defines (e.g., #if FEATURE_SINGLE_THREADED => false, otherwise check OperatingSystem.IsBrowser()/IsWasi() or ensure the host sets the switch for WASI as well).
| [SkipOnPlatform(TestPlatforms.Browser, "Requires threads")] | ||
| [Fact] |
There was a problem hiding this comment.
This test creates/joins threads; if WASI is also single-threaded (as implied by the new RuntimeFeature guards elsewhere), it should be skipped there as well. Consider skipping on TestPlatforms.Wasi (or using a multithreading-supported condition) to avoid runtime failures on WASI test runs.
| public static bool IsMultithreadingSupported => (!IsWasi && !IsBrowser) || IsWasmThreadingSupported; | ||
|
|
||
| // TODO-WASM: https://github.com/dotnet/runtime/issues/124748 | ||
| // this is compiled with 11.0.0-preview.1.26104.118\ref |
There was a problem hiding this comment.
Why is this compiled against stale references?
I would expect that the libraries tests are always built against the live references. Otherwise, we would not be able to add new public APIs and their tests in the same PR.
There was a problem hiding this comment.
This file compiles for both live .NET and .NET Standard. Maybe that's the problem?
Other places in this file deal with it by #if NET and implementing fallback for .NET Standard under #if !NET.
There was a problem hiding this comment.
I saw nuget path in the binlog, I think it was not .NET Standard problem. I can try again ...
src/libraries/System.Private.CoreLib/src/System/Threading/SpinWait.cs
Outdated
Show resolved
Hide resolved
…Wait.cs Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Fixes #77541
Continuation of #123329