Skip to content

Add Windows thread affinity support#338

Open
GagaLP wants to merge 1 commit into
masterfrom
thread-affinity-windows
Open

Add Windows thread affinity support#338
GagaLP wants to merge 1 commit into
masterfrom
thread-affinity-windows

Conversation

@GagaLP

@GagaLP GagaLP commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Thread pinning was previously a no-op on Windows: thread_pinner only emitted a warning ("Thread pinning is currently not supported on Windows.") and did not apply any affinity settings, regardless of configuration.

This PR adds a real implementation by introducing a small pthread/cpu_set_t compatibility layer (platform_specific/affinity_win32.h/.cc). The layer maps the POSIX affinity APIs already used by Celerity (sched_getaffinity/sched_setaffinity, pthread_self, pthread_get/setaffinity_np, CPU_SET/CPU_ISSET/CPU_COUNT, etc.) to their Windows equivalents (GetActiveProcessorGroupCount, GROUP_AFFINITY, SetThreadGroupAffinity).

With this compatibility layer in place, affinity_win.cc no longer requires separate pinning logic and now closely mirrors the Linux implementation. It initializes and tears down the pinning plan in the same way and pins threads to sequential cores using the same approach.

One limitation to note: Windows organizes logical processors into groups of up to 64 processors, and a single GROUP_AFFINITY mask cannot span multiple groups. If a requested core set crosses a group boundary, the layer currently emits a warning and skips pinning instead of attempting cross-group affinity. This should not affect the common case of pinning a small number of sequential cores, but remains a limitation on systems with more than 64 logical processors.

Added corresponding tests in affinity_tests.cc.

@GagaLP GagaLP requested a review from PeterTh July 14, 2026 16:17
@github-actions

Copy link
Copy Markdown

Check-perf-impact results: (ae6918621b46271c2f10d6eb978fe95d)

❓ No new benchmark data submitted. ❓
Please re-run the microbenchmarks and include the results if your commit could potentially affect performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant