find a way to create Thread binded to LogicalCore properly

This commit is contained in:
Youmu Liu
2026-04-14 18:08:22 +08:00
parent b07013a003
commit fdf11c729f
15 changed files with 228 additions and 254 deletions
+4 -5
View File
@@ -6,9 +6,9 @@
/* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */
SN_API void SN_DECL sn_set_thread_affinity(int core_id)
SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id)
{
if (core_id < 0) {
if (core_id < 0 || numa_id < 0) {
/* Query which group the thread currently belongs to */
GROUP_AFFINITY current;
GetThreadGroupAffinity(GetCurrentThread(), &current);
@@ -21,14 +21,13 @@ SN_API void SN_DECL sn_set_thread_affinity(int core_id)
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
return;
}
WORD group = (WORD)(core_id / 64);
int index = core_id % 64;
DWORD count = GetActiveProcessorCount(group);
DWORD count = GetActiveProcessorCount(numa_id);
if ((DWORD)index >= count)
return;
GROUP_AFFINITY ga;
memset(&ga, 0, sizeof(ga));
ga.Group = group;
ga.Group = (WORD)numa_id;
ga.Mask = (KAFFINITY)1 << index;
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
}