[m-rev.] diff: Check for sched.h CPU set macros at configure time.

Peter Wang novalazy at gmail.com
Wed May 23 18:04:21 AEST 2018


This fixes a build problem on Linux distributions with glibc version < 2.7.

configure.ac:
runtime/mercury_conf.h.in:
    Define MR_HAVE_SCHED_CPUSET_MACROS if sched.h includes the
    dynamic-sized CPU set macros (CPU_ALLOC, CPU_*_S, etc.)
    that first appeared in glibc 2.7.

runtime/mercury_conf_param.h:
    Do not define MR_HAVE_THREAD_PINNING if sched_[gs]etaffinity are
    available but not the dynamic-sized CPU set macros.

runtime/mercury_context.c:
    Check for MR_HAVE_SCHED_CPUSET_MACROS instead of CPU_* macros.
---
 configure.ac                 | 28 ++++++++++++++++++++++++++++
 runtime/mercury_conf.h.in    |  5 +++++
 runtime/mercury_conf_param.h |  6 ++++--
 runtime/mercury_context.c    |  5 +----
 4 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/configure.ac b/configure.ac
index 012033607..37ee47542 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2423,20 +2423,48 @@ AC_CACHE_VAL(mercury_cv_can_do_pending_io,
         ],
         [mercury_cv_can_do_pending_io=yes],
         [mercury_cv_can_do_pending_io=no])
 )
 AC_MSG_RESULT($mercury_cv_can_do_pending_io)
 if test "$mercury_cv_can_do_pending_io" = yes; then
     AC_DEFINE(MR_CAN_DO_PENDING_IO)
 fi
 AC_SUBST(MR_CAN_DO_PENDING_IO)
 
+#-----------------------------------------------------------------------------#
+
+AC_MSG_CHECKING(for sched.h cpuset macros)
+AC_CACHE_VAL(mercury_cv_have_sched_cpuset_macros,
+    AC_TRY_COMPILE(
+        [
+            #define _GNU_SOURCE
+            #ifdef MR_HAVE_SCHED_H
+            #include <sched.h>
+            #endif
+        ],
+        [
+            #if defined(CPU_ALLOC) && defined(CPU_COUNT_S)
+            #else
+                /* Deliberate syntax error. */
+                }
+            #endif
+        ],
+        [mercury_cv_have_sched_cpuset_macros=yes],
+        [mercury_cv_have_sched_cpuset_macros=no]
+    )
+)
+AC_MSG_RESULT($mercury_cv_have_sched_cpuset_macros)
+if test "$mercury_cv_have_sched_cpuset_macros" = yes; then
+    AC_DEFINE(MR_HAVE_SCHED_CPUSET_MACROS)
+fi
+AC_SUBST(MR_HAVE_SCHED_CPUSET_MACROS)
+
 #-----------------------------------------------------------------------------#
 # There is a problem on some BSD based systems that FD_ZERO is defined
 # defined in terms of bzero() but the appropriate header file for bzero()
 # is not automatically included. The following deals with this situation
 # on MacOS 10.3. (It appears to be fixed in MacOS 10.4.)
 
 AC_MSG_CHECKING(to see if strings.h is needed for bzero)
 AC_CACHE_VAL(mercury_cv_bzero_needs_strings_header, [
     save_CC="$CC"
 
diff --git a/runtime/mercury_conf.h.in b/runtime/mercury_conf.h.in
index c9e830ab6..5b48b3c61 100644
--- a/runtime/mercury_conf.h.in
+++ b/runtime/mercury_conf.h.in
@@ -423,20 +423,25 @@
 //
 // MR_THREAD_LOCAL_STORAGE is defined if the thread-local storage extension
 // is supported. That is, the compiler extends the C language with the
 // `__thread' specifier.
 //
 // MR_PTHREADS_WIN32 is defined if we are using the pthreads-win32 library.
 
 #undef MR_THREAD_LOCAL_STORAGE
 #undef MR_PTHREADS_WIN32
 
+// MR_HAVE_SCHED_CPUSET_MACROS is defined if sched.h includes all the
+// Linux-specific CPU_* and CPU_*_S macros.
+
+#undef MR_HAVE_SCHED_CPUSET_MACROS
+
 // MR_HAVE_HWLOC is defined if the hwloc library is available.
 
 #undef MR_HAVE_HWLOC
 
 // MR_INT_IS_32_BIT is defined if C's int type is exactly 32-bits.
 //
 #undef MR_INT_IS_32_BIT
 
 // MR_LONG_IS_64_BIT is defined if C's long type is exactly 64-bits.
 //
diff --git a/runtime/mercury_conf_param.h b/runtime/mercury_conf_param.h
index 87d8304a5..7ca02a9bb 100644
--- a/runtime/mercury_conf_param.h
+++ b/runtime/mercury_conf_param.h
@@ -992,18 +992,20 @@
 
 #if defined(_MSC_VER)
   #define MR_MSVC_STRUCTURED_EXCEPTIONS
 #endif
 
 ////////////////////////////////////////////////////////////////////////////
 
 // MR_HAVE_THREAD_PINNING is defined if we can pin threads, either with
 // sched_setaffinity or hwloc.
 
-#if (defined(MR_HAVE_SCHED_SETAFFINITY) &&                              \
-    defined(MR_HAVE_SCHED_GETAFFINITY)) || defined(MR_HAVE_HWLOC)
+#if defined(MR_HAVE_HWLOC) || \
+    (defined(MR_HAVE_SCHED_GETAFFINITY) && \
+      defined(MR_HAVE_SCHED_SETAFFINITY) && \
+      defined(MR_HAVE_SCHED_CPUSET_MACROS))
   #define MR_HAVE_THREAD_PINNING
 #endif
 
 ////////////////////////////////////////////////////////////////////////////
 
 #endif // MERCURY_CONF_PARAM_H
diff --git a/runtime/mercury_context.c b/runtime/mercury_context.c
index bf2a53935..63e8194dc 100644
--- a/runtime/mercury_context.c
+++ b/runtime/mercury_context.c
@@ -40,26 +40,23 @@ ENDINIT
 #endif
 
 #if defined(MR_THREAD_SAFE) && defined(MR_HAVE_HWLOC)
   #include <hwloc.h>
 #endif
 
 #if defined(MR_HAVE_SCHED_H)
   #include <sched.h>
 #endif
 
-// The CPU_* macros first appeared in glibc 2.7.
 #if defined(MR_HAVE_SCHED_GETAFFINITY) && \
     defined(MR_HAVE_SCHED_SETAFFINITY) && \
-    defined(CPU_ALLOC) && defined(CPU_ALLOC_SIZE) && defined(CPU_FREE) && \
-    defined(CPU_ZERO_S) && defined(CPU_SET_S) && defined(CPU_CLR_S) && \
-    defined(CPU_ISSET_S) && defined(CPU_COUNT_S)
+    defined(MR_HAVE_SCHED_CPUSET_MACROS)
   #define MR_HAVE_LINUX_CPU_AFFINITY_API 1
 #endif
 
 #ifdef MR_MINGW
   #include <sys/time.h>     // for gettimeofday()
 #endif
 
 #ifdef MR_WIN32
   #include <sys/timeb.h>    // for _ftime()
 #endif
-- 
2.17.0



More information about the reviews mailing list