Skip to content

Add Windows thread affinity support - #338

Merged
GagaLP merged 1 commit into
masterfrom
thread-affinity-windows
Aug 10, 2026
Merged

Add Windows thread affinity support#338
GagaLP merged 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.

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

Instead of duplicating the win.cc / unix.cc, I think the win version could just be reduced to a include of the helper header plus a include of the unix.cc, with an explanation (of the windows helper introducing compat with the unix impl).

Comment thread test/affinity_tests.cc Outdated
Comment on lines 65 to 69
#ifdef _WIN32
#define SKIP_UNSUPPORTED() SKIP("Affinity is not supported on Windows");
#define SKIP_UNSUPPORTED() // SKIP("Affinity is not supported on Windows");
#else
#define SKIP_UNSUPPORTED()
#endif

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.

This whole code block (and the invocation sites) can just be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -0,0 +1,105 @@
#pragma once

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.

This file should have a comment at the start explaining its purpose (i.e. implementing unix-like affinity using windows APIs).

maybe the name should also be something like affinity_win32_adapter?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@GagaLP
GagaLP force-pushed the thread-affinity-windows branch from 1aacd2d to 90901f4 Compare August 5, 2026 13:55

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (1/5)

Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (2/5)

Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (3/5)

Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (4/5)

Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread src/platform_specific/affinity.win.cc
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (5/5)

Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
Comment thread include/platform_specific/affinity_win32_adapter.h
@coveralls

coveralls commented Aug 5, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 31384404925

Coverage remained the same at 95.07%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 7264
Covered Lines: 7159
Line Coverage: 98.55%
Relevant Branches: 3690
Covered Branches: 3255
Branch Coverage: 88.21%
Branches in Coverage %: Yes
Coverage Strength: 1928675.45 hits per line

💛 - Coveralls

@GagaLP
GagaLP force-pushed the thread-affinity-windows branch from 90901f4 to 889d6db Compare August 6, 2026 13:43
@GagaLP
GagaLP force-pushed the thread-affinity-windows branch from 889d6db to 8e719b5 Compare August 10, 2026 11:38
@GagaLP
GagaLP merged commit 47b60b7 into master Aug 10, 2026
17 checks passed
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.

3 participants