Prevent cpu_set_t overflow even if there are more than 63 cores

Do not use `pthread_attr_setaffinity_np` if `sched_getcpu()` exceeds
`CPU_SETSIZE`. (Using `CPU_ALLOC()` would be more appropriate.)
This commit is contained in:
Yusuke Endoh 2023-11-07 04:11:35 +09:00
parent deb6dd76e1
commit 49b6dc8f07

View File

@ -483,8 +483,11 @@ start:
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) #if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set; cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set); CPU_ZERO(&tmp_cpu_set);
CPU_SET(sched_getcpu(), &tmp_cpu_set); int cpu = sched_getcpu();
if (cpu < CPU_SETSIZE) {
CPU_SET(cpu, &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
}
#endif #endif
pthread_t th; pthread_t th;
@ -707,8 +710,11 @@ start:
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) #if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set; cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set); CPU_ZERO(&tmp_cpu_set);
CPU_SET(sched_getcpu(), &tmp_cpu_set); int cpu = sched_getcpu();
if (cpu < CPU_SETSIZE) {
CPU_SET(cpu, &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
}
#endif #endif
pthread_t th; pthread_t th;