Skip to content

Unify Thread.CurrentOSThreadId between CoreCLR and NativeAOT - #135144

Merged
jkotas merged 10 commits into
dotnet:mainfrom
EgorBo:unify-current-os-thread-id
Oct 6, 2026
Merged

jkotas merged 10 commits into
dotnet:mainfrom
EgorBo:unify-current-os-thread-id

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 2, 2026

Copy link
Copy Markdown
Member

No description provided.

Implement CurrentOSThreadId in shared Thread.cs on top of existing interop
(Kernel32.GetCurrentThreadId on Windows, SystemNative_GetUInt64OSThreadId elsewhere)
and delete the CoreCLR ThreadNative_GetCurrentOSThreadId QCall and the NativeAOT
RhCurrentOSThreadId FCALL. Mark GetUInt64OSThreadId with SuppressGCTransition, and make
the WASI stub return 1 (single thread) instead of asserting.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac6fb33d-fc74-41c7-a804-75fbf616b155
@EgorBo
EgorBo requested a balanced review from Copilot and removed request for MichalStrehovsky October 2, 2026 23:38
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

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.

Copilot review overview

🔵 Needs a closer look

Cross-runtime interop changes need platform validation and human review, particularly for WASI and NativeAOT fail-fast consumers; builds and tests were not run.

Review effort: Balanced
Findings: None

What changed in this PR

Unifies Thread.CurrentOSThreadId across CoreCLR and NativeAOT through shared platform interop, while preserving Mono’s implementation.

Changes:

  • Routes Windows and Unix thread-ID queries through shared interop.
  • Removes redundant runtime entry points and declarations.
  • Returns thread ID 1 for single-threaded WASI.
File Description
src/​native/​libs/​System.Native/​pal_threading_wasi.c Returns the single-threaded WASI ID.
src/​libraries/​System.Private.CoreLib/​src/​System/​Threading/​Thread.cs Shares platform-specific thread-ID dispatch.
src/​libraries/​Common/​src/​Interop/​Unix/​System.Native/​Interop.Threading.cs Broadens 64-bit interop availability and suppresses GC transitions.
src/​coreclr/​vm/​qcallentrypoints.cpp Removes the obsolete QCall registration.
src/​coreclr/​vm/​comsynchronizable.h Removes the obsolete QCall declaration.
src/​coreclr/​vm/​comsynchronizable.cpp Removes the CoreCLR-specific implementation.
src/​coreclr/​System.Private.CoreLib/​src/​System/​Threading/​Thread.CoreCLR.cs Removes the managed QCall import.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Threading/​Thread.NativeAot.cs Removes the NativeAOT-specific property.
src/​coreclr/​nativeaot/​System.Private.CoreLib/​src/​System/​Runtime/​RuntimeImports.cs Removes the unused runtime import.
src/​coreclr/​nativeaot/​Runtime/​thread.cpp Removes the NativeAOT native entry point.

Comment thread src/libraries/Common/src/Interop/Unix/System.Native/Interop.Threading.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs Outdated
Comment thread src/native/libs/System.Native/pal_threading_wasi.c Outdated
Comment thread src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs Outdated
…lpers

- Move CurrentOSThreadId into Thread.Windows.cs / Thread.Unix.cs and let Mono use it too
  (drop the Mono GetCurrentOSThreadId icall, which returned the same values).
- Rename SystemNative_GetUInt64OSThreadId to SystemNative_GetOSThreadId and delete the unused
  SystemNative_TryGetUInt32OSThreadId.
- Use minipal_get_current_thread_id in the WASI shim.
- Delete unused RhCurrentNativeThreadId.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac6fb33d-fc74-41c7-a804-75fbf616b155
@EgorBo

EgorBo commented Oct 3, 2026

Copy link
Copy Markdown
Member Author

@jkotas thanks for the fb, addressed

Comment thread src/libraries/System.Private.CoreLib/src/System/Threading/Thread.Windows.cs Outdated
Comment thread src/native/libs/System.Native/pal_threading.c Outdated
Comment thread src/native/libs/System.Native/pal_threading.c Outdated
Comment thread src/native/libs/System.Native/pal_threading.c Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8acd7b8c-9618-46e1-8654-0e1b27b4480a
Comment thread src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 1605c6d9-7fb1-4846-bd10-06628c6bf2ea

@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.

Thanks

@jkotas
jkotas enabled auto-merge (squash) October 4, 2026 17:56
@jkotas

jkotas commented Oct 5, 2026

Copy link
Copy Markdown
Member

@lewing Could you please take a look at System.Threading.Tasks failure on Wasm? I am not able to tell whether it was somehow caused by the PR changes (the PR is changing threading, so there may be some connection).

@jkotas

jkotas commented Oct 6, 2026

Copy link
Copy Markdown
Member

@EgorBo I think this may need https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/wasm/generate-coreclr-helpers.cmd to fix the wasm failure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e652426b-8668-4590-b10e-94bbe6edf26c
@jkotas

jkotas commented Oct 6, 2026

Copy link
Copy Markdown
Member

Merge conflicts

@EgorBo

EgorBo commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

Ah, oops.

EgorBo and others added 3 commits October 6, 2026 18:27
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ec60cb09-d94b-427d-886d-cac926a87fbb
@jkotas
jkotas merged commit b08c345 into dotnet:main Oct 6, 2026
165 of 168 checks passed
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.

3 participants