working on per-PoolThread context loading and global context loading
This commit is contained in:
@@ -5,27 +5,51 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
/* Pin the calling thread to a single logical processor.
|
||||
* core_id: zero-based logical processor index.
|
||||
* Silently ignored if core_id is out of range or the OS call fails. */
|
||||
* 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)
|
||||
{
|
||||
if (core_id < 0)
|
||||
return;
|
||||
|
||||
#if SN_IS_OS(SN_OS_WINDOWS)
|
||||
/* windows.h already included via sn_internal.h */
|
||||
if (core_id >= 64)
|
||||
if (core_id < 0) {
|
||||
/* Query which group the thread currently belongs to */
|
||||
GROUP_AFFINITY current;
|
||||
GetThreadGroupAffinity(GetCurrentThread(), ¤t);
|
||||
/* Build a full mask for that group's actual core count */
|
||||
DWORD count = GetActiveProcessorCount(current.Group);
|
||||
GROUP_AFFINITY ga;
|
||||
memset(&ga, 0, sizeof(ga));
|
||||
ga.Group = current.Group;
|
||||
ga.Mask = (count >= 64) ? ~(KAFFINITY)0
|
||||
: ((KAFFINITY)1 << count) - 1;
|
||||
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
|
||||
return;
|
||||
DWORD_PTR mask = (DWORD_PTR)1 << core_id;
|
||||
SetThreadAffinityMask(GetCurrentThread(), mask);
|
||||
}
|
||||
WORD group = (WORD)(core_id / 64);
|
||||
int index = core_id % 64;
|
||||
DWORD count = GetActiveProcessorCount(group);
|
||||
if ((DWORD)index >= count)
|
||||
return;
|
||||
GROUP_AFFINITY ga;
|
||||
memset(&ga, 0, sizeof(ga));
|
||||
ga.Group = group;
|
||||
ga.Mask = (KAFFINITY)1 << index;
|
||||
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
|
||||
|
||||
#elif SN_IS_OS(SN_OS_LINUX)
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
CPU_SET(core_id, &set);
|
||||
if (core_id < 0) {
|
||||
CPU_ZERO(&set);
|
||||
long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
for (long i = 0; i < ncpus; i++)
|
||||
CPU_SET((int)i, &set);
|
||||
} else {
|
||||
CPU_ZERO(&set);
|
||||
CPU_SET(core_id, &set);
|
||||
}
|
||||
pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user