Skip to content

macOS: thread affinity handling (no-op guard + QoS performance-core hint) - #3035

Open
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:mac/thread-affinity-guard-stub
Open

macOS: thread affinity handling (no-op guard + QoS performance-core hint)#3035
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:mac/thread-affinity-guard-stub

Conversation

@tomjn

@tomjn tomjn commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

What

macOS thread-affinity handling, in the two places the engine touches affinity. macOS / Apple Silicon has no portable per-thread CPU affinity API — no sched_*/cpu_set_t, and Mach's THREAD_AFFINITY_POLICY is a no-op on Apple Silicon — so neither hard pinning nor save/restore is possible.

1. ThreadAffinityGuard → no-op on macOS
affinitySaved stays false (its constructor default), so the destructor's restore is skipped, and the cpu_set_t/sched_* code is excluded from the macOS compile. Kept deliberately minimal: no placeholder member, no extra includes, and the existing cross-platform comments are left intact.

2. Threading::SetAffinity → QoS "prefer performance cores" hint
Instead of a hard affinity mask, macOS applies pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0), which biases the scheduler toward the performance cores and away from the efficiency cores. The specific coreMask can't be honored, so the affinity is reported as "not set" (~0) and final placement is left to the scheduler.

Scope / intent

This is a prefer, not a force — hard pinning to specific cores doesn't exist on Apple Silicon, so QoS is the strongest available lever, and the scheduler retains the final say. Accurate P/E-core sizing of the worker pool (e.g. via hw.perflevel0.physicalcpu) is a separate, later concern and not needed for this hint.

Cross-platform safety

Purely additive on the macOS path. Windows and Linux are unchanged; FreeBSD/OpenBSD keep their existing affinity no-op. The __APPLE__ branch was split out of the previous __APPLE__ || __FreeBSD__ || __OpenBSD__ group so only macOS pulls in <pthread/qos.h> and the QoS call.

Extracted/adapted from the macOS bring-up (#2991) as a standalone, reviewable piece.

…int)

macOS / Apple Silicon has no portable per-thread CPU affinity API
(no sched_*/cpu_set_t, and Mach's THREAD_AFFINITY_POLICY is a no-op on
Apple Silicon). Handle this in two places:

- ThreadAffinityGuard: no-op on macOS. affinitySaved stays false so the
  destructor's restore is skipped. Kept minimal -- no extra members or
  includes, existing comments preserved.

- Threading::SetAffinity: instead of a hard affinity mask, apply a QoS
  hint (QOS_CLASS_USER_INTERACTIVE) which biases the scheduler toward the
  performance cores and away from the efficiency cores. The specific
  coreMask can't be honored, so the affinity is reported as "not set"
  (~0) and final placement is left to the scheduler.

This is a "prefer performance cores" hint, not a hard pin (which Apple
Silicon does not offer). Cross-platform-safe: Windows/Linux unchanged,
FreeBSD/OpenBSD keep their existing affinity no-op.
@tomjn tomjn changed the title ThreadAffinityGuard: no-op on macOS (no portable affinity API) macOS: thread affinity handling (no-op guard + QoS performance-core hint) Jun 20, 2026
@tomjn
tomjn force-pushed the mac/thread-affinity-guard-stub branch from 8549657 to ad16282 Compare June 20, 2026 00:03

@sprunk sprunk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM but @lostsquirrel1 should probably take a look just in case.

@tomjn

tomjn commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

Some related changes to go with this in #3036

@sprunk sprunk added the MacOS Part of the big push to support Mac. label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

MacOS Part of the big push to support Mac.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants