2
0
mirror of https://github.com/vasi/pixz synced 2024-10-30 15:21:41 +00:00

Merge pull request #107 from wsldankers/sched_getaffinity

Only use available CPUs
This commit is contained in:
Dave Vasilevsky 2022-10-30 21:13:11 -04:00 committed by GitHub
commit d2131cc19f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -68,7 +68,7 @@ AC_SYS_LARGEFILE
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_FUNC_STRTOD
AC_CHECK_FUNCS([memchr memmove memset strerror strtol])
AC_CHECK_FUNCS([memchr memmove memset strerror strtol sched_getaffinity])
AC_CHECK_HEADER([sys/endian.h],
[
AC_CHECK_DECLS([htole64, le64toh], [], [], [

View File

@ -1,5 +1,28 @@
#define _GNU_SOURCE
#include <unistd.h>
#include "config.h"
#ifdef HAVE_SCHED_GETAFFINITY
#include <sched.h>
#include <stdio.h>
size_t num_threads(void) {
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
if (sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
return sysconf(_SC_NPROCESSORS_ONLN);
else
return CPU_COUNT(&cpu_set);
}
#else
size_t num_threads(void) {
return sysconf(_SC_NPROCESSORS_ONLN);
}
#endif