summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@gnugeneration.com>2016-11-23 19:19:20 -0800
committerVito Caputo <vcaputo@gnugeneration.com>2016-11-23 19:19:20 -0800
commit5de227689402736235f6bdb932fa9ad5f939fcb8 (patch)
tree8356f205b7f698c42774cd47cc73f29044829427 /util.c
parente3d3c06aa5b47648c0ed145279d7950df42ad80d (diff)
util: put convenience helpers into util.[ch]
Also introduces get_ncpus(), in preparation for threaded rendering.
Diffstat (limited to 'util.c')
-rw-r--r--util.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..c07589e
--- /dev/null
+++ b/util.c
@@ -0,0 +1,22 @@
+#include <limits.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "util.h"
+
+#define SYSFS_CPU "/sys/devices/system/cpu/cpu"
+#define MAXCPUS 1024
+
+unsigned get_ncpus(void)
+{
+ char path[cstrlen(SYSFS_CPU "1024") + 1];
+ unsigned n;
+
+ for (n = 0; n < MAXCPUS; n++) {
+ snprintf(path, sizeof(path), "%s%u", SYSFS_CPU, n);
+ if (access(path, F_OK) == -1)
+ break;
+ }
+
+ return n == 0 ? 1 : n;
+}
© All Rights Reserved