summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac57
-rw-r--r--libdimension/platform.c40
2 files changed, 88 insertions, 9 deletions
diff --git a/configure.ac b/configure.ac
index ced49a6..9ce9b2a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -68,8 +68,61 @@ PKG_CHECK_MODULES([libsandglass], [libsandglass >= 0.2],
AC_SUBST(libsandglass_CFLAGS)
AC_SUBST(libsandglass_LIBS)
-dnl Ensure compilation in C99 mode
-AC_DEFINE([_XOPEN_SOURCE], [600])
+dnl Platform feature tests
+
+AC_MSG_CHECKING([for sched_getaffinity()])
+AC_LINK_IFELSE(
+ AC_LANG_PROGRAM(
+ [
+ #define _GNU_SOURCE
+ #include <sched.h>
+ ],
+ [
+ cpu_set_t cpuset;
+ sched_getaffinity(0, sizeof(cpuset), &cpuset);
+ ]
+ ),
+ [AC_DEFINE([DMNSN_SCHED_GETAFFINITY], [1])
+ AC_MSG_RESULT([yes])],
+ [AC_DEFINE([DMNSN_SCHED_GETAFFINITY], [0])
+ AC_MSG_RESULT([no])]
+)
+
+AC_MSG_CHECKING([for sysconf(_SC_NPROCESSORS_ONLN)])
+AC_COMPILE_IFELSE(
+ AC_LANG_PROGRAM(
+ [ #include <unistd.h> ],
+ [ sysconf(_SC_NPROCESSORS_ONLN); ]
+ ),
+ [AC_DEFINE([DMNSN_SC_NPROCESSORS_ONLN], [1])
+ AC_MSG_RESULT([yes])],
+ [AC_DEFINE([DMNSN_SC_NPROCESSORS_ONLN], [0])
+ AC_MSG_RESULT([no])]
+)
+
+AC_MSG_CHECKING([for backtrace()])
+AC_LINK_IFELSE(
+ AC_LANG_PROGRAM(
+ [ #include <execinfo.h> ],
+ [ backtrace(0, 0); ]
+ ),
+ [AC_DEFINE([DMNSN_BACKTRACE], [1])
+ AC_MSG_RESULT([yes])],
+ [AC_DEFINE([DMNSN_BACKTRACE], [0])
+ AC_MSG_RESULT([no])]
+)
+
+AC_MSG_CHECKING([for gettid()])
+AC_COMPILE_IFELSE(
+ AC_LANG_PROGRAM(
+ [ #include <sys/syscall.h> ],
+ [ syscall(SYS_gettid); ]
+ ),
+ [AC_DEFINE([DMNSN_GETTID], [1])
+ AC_MSG_RESULT([yes])],
+ [AC_DEFINE([DMNSN_GETTID], [0])
+ AC_MSG_RESULT([no])]
+)
dnl Generate Makefiles
AC_CONFIG_MACRO_DIR([m4])
diff --git a/libdimension/platform.c b/libdimension/platform.c
index be7e7a8..372dbb5 100644
--- a/libdimension/platform.c
+++ b/libdimension/platform.c
@@ -19,26 +19,35 @@
*************************************************************************/
#include "dimension_impl.h"
-#include <unistd.h> /* For sysconf() */
-#include <arpa/inet.h> /* For htonl() */
-#include <execinfo.h> /* For backtrace() etc. */
-#include <sys/syscall.h> /* For gettid() where supported */
-#include <sched.h> /* For sched_getaffinity() */
+#if HAVE_UNISTD_H
+ #include <unistd.h>
+#endif
+#if DMNSN_BACKTRACE
+ #include <execinfo.h> /* For backtrace() etc. */
+#endif
+#if DMNSN_GETTID
+ #include <sys/syscall.h> /* For gettid() where supported */
+#endif
+#if DMNSN_SCHED_GETAFFINITY
+ #include <sched.h> /* For sched_getaffinity() */
+#endif
void
dmnsn_backtrace(FILE *file)
{
+#if DMNSN_BACKTRACE
const size_t size = 128;
void *buffer[size];
int nptrs = backtrace(buffer, size);
backtrace_symbols_fd(buffer, nptrs, fileno(file));
+#endif
}
bool
dmnsn_is_main_thread()
{
-#ifdef SYS_gettid
+#if DMNSN_GETTID
return getpid() == syscall(SYS_gettid);
#else
return true;
@@ -48,12 +57,18 @@ dmnsn_is_main_thread()
bool
dmnsn_is_little_endian()
{
- return htonl(1) != 1;
+ /* Undefined behaviour, but quite portable */
+ union {
+ unsigned int i;
+ unsigned char c;
+ } u = { .i = 1 };
+ return u.c == 1;
}
size_t
dmnsn_ncpus()
{
+#if DMNSN_SCHED_GETAFFINITY
cpu_set_t cpuset;
if (sched_getaffinity(0, sizeof(cpuset), &cpuset) == 0) {
return CPU_COUNT(&cpuset);
@@ -61,4 +76,15 @@ dmnsn_ncpus()
dmnsn_error(DMNSN_SEVERITY_MEDIUM, "sched_getaffinity() failed.");
return 1;
}
+#elif DMNSN_SC_NPROCESSORS_ONLN
+ long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
+ if (nprocs > 0) {
+ return nprocs;
+ } else {
+ dmnsn_error(DMNSN_SEVERITY_MEDIUM, "sysconf(_SC_NPROCESSORS_ONLN) failed.");
+ return 1;
+ }
+#else
+ return 1;
+#endif
}