Skip to content

Add FEATURE_MULTITHREADING ifdefs to a couple places in CoreLib - #137076

Merged
MichalStrehovsky merged 10 commits into
dotnet:mainfrom
MichalStrehovsky:singlethreadcore
Nov 18, 2026
Merged

MichalStrehovsky merged 10 commits into
dotnet:mainfrom
MichalStrehovsky:singlethreadcore

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Each of these is a small saving. Cannot run rt-sz on this because we still don't have runtime packs.

MichalStrehovsky and others added 3 commits November 6, 2026 19:44
Exclude constructor locks and cross-thread deadlock tracking when FEATURE_MULTITHREADING is disabled, while preserving recursion and cached failures. Remove the obsolete WASM initialization fallback in favor of startup initialization.

Local size-optimized, invariant WASI Hello World: 838510 -> 834900 bytes (-3610 bytes).

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9bc3dfee-1040-4648-bdad-86922a749613
Use a fixed executing-thread ID without TLS, ID-dispenser allocation, or finalization when FEATURE_MULTITHREADING is disabled. Preserve distinct IDs for unstarted Thread objects with a checked counter.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9bc3dfee-1040-4648-bdad-86922a749613
Compile foreground accounting, shutdown waits, exit callbacks, and private Thread locking only with FEATURE_MULTITHREADING. Preserve thread properties, validation, and the background state observed by ProcessExit handlers.

Validated WASI Release/Checked shutdown and priority probes plus Preinitialization, and compiled Windows x64 Debug CoreLib. WASI Release Hello World shrinks from 832265 to 825570 bytes.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9bc3dfee-1040-4648-bdad-86922a749613
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@am11

am11 commented Nov 6, 2026

Copy link
Copy Markdown
Member

because we still don't have runtime packs.

That's why build.sh --cross --os .. --arch .. --bootstrap was invented. 🙂

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

because we still don't have runtime packs.

That's why build.sh --cross --os .. --arch .. --bootstrap was invented. 🙂

rt-sz takes shortcuts and uses daily builds of the .NET SDK with overriden IlcToolsPath etc. It would have to be rewritten to work different and likely build more of the repo. It already takes half an hour to get results and that feels like too much already.

@jkotas
jkotas requested a review from jkoritzinsky November 6, 2026 18:07

@jkoritzinsky jkoritzinsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jan's already commented on my concerns. LGTM otherwise

MichalStrehovsky and others added 2 commits November 7, 2026 13:48
Split ManagedThreadId into conditionally included multithreading and single-threading files in NativeAOT and CoreCLR. Move NativeAOT thread lifecycle members into the multithreading partial and remove threading feature guards from Windows-specific code.

Validated Windows Debug and WASI Checked CoreCLR/NativeAOT CoreLib builds, Windows NativeAOT smoke tests, and WASI identity and normal/explicit shutdown probes. CoreCLR execution was not tested.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: d2be6eb7-4bbd-4c48-9031-40db58562aeb
Move the managed thread ID field and accessor back below s_idDispenser to avoid an unrelated diff against main. Declaration placement only; no behavior change.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: d2be6eb7-4bbd-4c48-9031-40db58562aeb

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new single-threaded NativeAOT ID allocator lacks coverage verifying that unstarted threads receive distinct IDs greater than 1 before first current-thread access, while both current-thread ID APIs remain 1.

1 open issue
What changed in this PR

This PR reduces code included in single-threaded CoreLib builds by separating thread-specific implementations and excluding multithreading infrastructure.

Changes:

  • Split managed thread ID implementations by threading configuration in CoreCLR and NativeAOT.
  • Move NativeAOT thread lifecycle bookkeeping and synchronization into the multithreaded implementation.
  • Exclude class-constructor locks and deadlock tracking from single-threaded builds.

Builds and tests were not run.

File Description
src/​coreclr/​System.Private.CoreLib/​System.Private.CoreLib.csproj Selects the appropriate managed thread ID implementation.
src/​coreclr/​System.Private.CoreLib/​src/​System/​Threading/​ManagedThreadId.CoreCLR.SingleThreading.cs Provides constant current-thread IDs for single-threaded CoreCLR.
src/​coreclr/​System.Private.CoreLib/​src/​System/​Threading/​ManagedThreadId.CoreCLR.Multithreading.cs Retains the thread-local CoreCLR implementation.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.Windows.cs Removes conditional guards around Windows thread creation.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.Unix.SingleThreading.cs Removes the single-threaded Unix thread-exit callback.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.SingleThreading.cs Preserves background-state handling during shutdown.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.Multithreading.cs Houses thread locks, exit registration, and foreground-thread bookkeeping.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.cs Gates shared thread operations on multithreading support.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​ManagedThreadId.SingleThreading.cs Adds a simple allocator for unstarted-thread IDs.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​ManagedThreadId.Multithreading.cs Preserves the existing thread ID allocation and recycling implementation.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Runtime/​CompilerServices/​ClassConstructorRunner.cs Conditionally excludes synchronization and removes the WebAssembly initialization fallback.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System.Private.CoreLib.csproj Selects the NativeAOT thread ID implementation by configuration.

Review effort: Balanced

@am11

am11 commented Nov 7, 2026

Copy link
Copy Markdown
Member

It already takes half an hour to get results and that feels like too much already.

GHA job timeout is 6h, so it's well within the range. We are doing full VMR linux-riscv64 build (which uses --bootstrap at runtime stage) and the whole job takes about 80-90 mins (https://github.com/filipnavara/dotnet-riscv/actions/workflows/build.yml). For rt-sz, it could be used sparingly only for the targets without the runtime packs.

MichalStrehovsky and others added 3 commits November 17, 2026 11:29
Restore the existing Lock-based class constructor runner on single-threaded targets instead of conditionally compiling out its locks. Leave the managed thread ID and thread lifecycle specializations unchanged.

Addresses dotnet#137076 (comment).

Co-authored-by: Copilot <[email protected]>
Copilot-Session: a4983540-34d2-4b99-8e53-4b7796fc9228
Correct the overly broad class constructor rollback: keep DeadlockAwareAcquire, BlockingRecord, and Cctor.Count conditional on FEATURE_MULTITHREADING, and keep the obsolete WASM lazy initialization removed.

Retain unconditional Lock usage, using a recursion check and TryEnter for single-threaded class constructor acquisition.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: a4983540-34d2-4b99-8e53-4b7796fc9228

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last iteration looks reasonable to me

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g osx leg got stuck and timed out

@MichalStrehovsky
MichalStrehovsky merged commit 30d28af into dotnet:main Nov 18, 2026
98 of 101 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the singlethreadcore branch November 18, 2026 00:43
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants