Skip to content

Use pthread_cpu_number_np for current processor number when sched_getcpu is unavailable (macOS) - #137231

Merged
VSadov merged 4 commits into
mainfrom
copilot/use-pthread-cpu-number-np
Nov 11, 2026
Merged

VSadov merged 4 commits into
mainfrom
copilot/use-pthread-cpu-number-np

Conversation

Copilot AI commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Thread.GetCurrentProcessorId() and the GC's per-processor logic get no processor number on macOS, because sched_getcpu doesn't exist there. pthread_cpu_number_np is available on all supported macOS versions, so use it as the fallback.

  • Detection
    • src/native/libs/configure.cmake (pal_config.h.in): HAVE_PTHREAD_CPU_NUMBER_NP via check_symbol_exists on <pthread.h>.
    • src/coreclr/gc/unix/configure.cmake (config.gc.h.in): the same macro via a compile check.
  • Managed path (SystemNative_SchedGetCpu, pal_threading.c): the sched_getcpu path is unchanged. The new #elif branch calls pthread_cpu_number_np, and the shim returns -1 on failure or when neither API exists, so ProcessorIdCache still falls back to the managed thread ID.
  • GC (gcenv.unix.cpp): GetCurrentProcessorNumber() uses pthread_cpu_number_np when sched_getcpu is unavailable. CanGetCurrentProcessorNumber() now returns true when either API is present, so the GC's processor-number code paths are newly enabled on macOS.
  • Comments: updated in Thread.Unix.cs and ProcessorIdCache.cs to mention both APIs.

Not built or run on macOS. The new path depends on the SDK declaring pthread_cpu_number_np in <pthread.h>. If it doesn't, the check fails quietly and macOS keeps returning -1, so please confirm HAVE_PTHREAD_CPU_NUMBER_NP is set in a macOS build.

@azure-pipelines

azure-pipelines Bot commented Nov 10, 2026 •

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: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Copilot AI changed the title [WIP] Use pthread_cpu_number_np to get current core on OSX Use pthread_cpu_number_np for current processor number when sched_getcpu is unavailable (macOS) Nov 10, 2026
Copilot AI requested a review from VSadov November 10, 2026 01:08
@VSadov
VSadov requested a balanced review from Copilot November 10, 2026 01:23

This comment was marked as resolved.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @anicka-net, @dotnet/gc
See info in area-owners.md if you want to be subscribed.

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

🔵 Needs a closer look

The macOS-only native paths remain unbuilt and untested, leaving CMake feature detection, processor-ID behavior, and server-GC heap selection unverified.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@VSadov
VSadov marked this pull request as ready for review November 10, 2026 23:12

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

LGTM, thank you!

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.

Use pthread_cpu_number_np to get current core on OSX

4 participants