summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-09-03 17:37:25 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-09-03 17:45:05 -0700
commiteb13ccdbd81fc47ee79741ad9e536f9a7d2163c2 (patch)
tree0e884ec3ae0d583576cb1b83599149c1edd2a2fe
parent76c48cfffbac6cb40103c99eb76c0847b0d1a16a (diff)
til_utils: switch til_get_ncpus() to sysconf
The ad-hoc sys-based probe of cpus works fine on Linux, but it's not really preferable when Linux's sysconf supports _SC_NPROCESSORS_ONLN, and there's a chance non-Linux's will support it too. Supposedly even Emscripten supports this, and will report the number of available WebWorkers through this interface, even with pthreads emulation. I'm curious if that actually works, getting wasm builds of rototiller demos could be fun.
-rw-r--r--src/til_util.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/til_util.c b/src/til_util.c
index b87a2d8..7c44d67 100644
--- a/src/til_util.c
+++ b/src/til_util.c
@@ -28,23 +28,20 @@ unsigned til_get_ncpus(void)
#endif
#ifdef __MACH__
- int count;
- size_t count_len = sizeof(count);
+ int count;
+ size_t count_len = sizeof(count);
if (sysctlbyname("hw.logicalcpu_max", &count, &count_len, NULL, 0) < 0)
return 1;
return MIN(count, TIL_MAXCPUS);
#else
- char path[cstrlen(TIL_SYSFS_CPU "1024") + 1];
- unsigned n;
+ long n;
- for (n = 0; n < TIL_MAXCPUS; n++) {
- snprintf(path, sizeof(path), "%s%u", TIL_SYSFS_CPU, n);
- if (access(path, F_OK) == -1)
- break;
- }
+ n = sysconf(_SC_NPROCESSORS_ONLN);
+ if (n <= 0)
+ return 1;
- return n == 0 ? 1 : n;
+ return MIN(n, TIL_MAXCPUS);
#endif
}
© All Rights Reserved