Skip to content

coreclr: implement Environment.CurrentManagedThreadId via ManagedThreadId.Current#129954

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/coreclr-use-managedthreadid-current
Draft

coreclr: implement Environment.CurrentManagedThreadId via ManagedThreadId.Current#129954
Copilot wants to merge 2 commits into
mainfrom
copilot/coreclr-use-managedthreadid-current

Conversation

Copilot AI commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

On CoreCLR, Environment.CurrentManagedThreadId was backed by an FCall (JIT_GetCurrentManagedThreadIdGetThread()->GetThreadId()). ManagedThreadId.Current reads t_currentManagedThreadId, a [ThreadStatic] wired by the type system to a special location that is populated with the thread's managed ID before any managed caller can observe it — the thin-lock implementation already depends on this guarantee.

This makes Environment.CurrentManagedThreadId a plain thread-static read on CoreCLR, matching what NativeAOT and Mono already do.

Changes

  • Environment.CoreCLR.cs: replace [MethodImpl(MethodImplOptions.InternalCall)] extern property with => Threading.ManagedThreadId.Current
  • ecalllist.h: remove FCFuncElement("get_CurrentManagedThreadId", JIT_GetCurrentManagedThreadId) from gEnvironmentFuncs
  • JIT_GetCurrentManagedThreadId (jithelpers.cpp / jitinterface.h) is kept: it remains the native entry point for the CORINFO_HELP_GETCURRENTMANAGEDTHREADID JIT helper (used when optimizing Thread.CurrentThread.ManagedThreadId)

Copilot AI requested review from Copilot and removed request for Copilot June 29, 2026 00:58
@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.

….Current on CoreCLR

On CoreCLR, `t_currentManagedThreadId` is set up by the type system to
point to a special location populated with the thread's managed ID before
any managed caller can observe it (the thin-lock implementation already
relies on this guarantee).

This change makes `Environment.CurrentManagedThreadId` simply delegate to
`ManagedThreadId.Current`, turning an FCall into a cheap thread-static
read, aligning CoreCLR with NativeAOT/Mono which already do the same.

- Environment.CoreCLR.cs: replace InternalCall property with
  `=> Threading.ManagedThreadId.Current`
- ecalllist.h: remove the now-unused
  `FCFuncElement("get_CurrentManagedThreadId", JIT_GetCurrentManagedThreadId)`

`JIT_GetCurrentManagedThreadId` is kept in jithelpers.cpp and
jitinterface.h because it is still needed as the native entry point for
the JIT helper `CORINFO_HELP_GETCURRENTMANAGEDTHREADID` (used by the JIT
when optimizing `Thread.CurrentThread.ManagedThreadId`).

Co-authored-by: VSadov <8218165+VSadov@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 29, 2026 01:24
Copilot AI changed the title [WIP] Update Environment.CurrentManagedThreadId to return ManagedThreadId.Current coreclr: implement Environment.CurrentManagedThreadId via ManagedThreadId.Current Jun 29, 2026
Copilot AI requested a review from VSadov June 29, 2026 01:24
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.

2 participants