Files
stgsharp/StgSharp.Native/sn-src/thread.linux.c
T
2026-04-02 18:13:32 +08:00

40 lines
1.1 KiB
C

#include "sn_target.h"
#include "StgSharpNative.h"
#if SN_IS_OS(SN_OS_LINUX)
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
/* 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)
{
long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
cpu_set_t *set = CPU_ALLOC(ncpus);
size_t size = CPU_ALLOC_SIZE(ncpus);
CPU_ZERO_S(size, set);
if (core_id < 0) {
for (long i = 0; i < ncpus; i++)
CPU_SET_S((int)i, size, set);
} else {
CPU_SET_S(core_id, size, set);
}
pthread_setaffinity_np(pthread_self(), size, set);
CPU_FREE(set);
}
SN_API void SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_numa)
{
}
SN_API void SN_DECL sn_get_numa_each(int numa_id, int *in_numa_cpuid, int *global_cpuid)
{
}
#endif