bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20136: 25.0.50; [PATCH] Fix broken libpthread detection


From: Wolfgang Jenkner
Subject: bug#20136: 25.0.50; [PATCH] Fix broken libpthread detection
Date: Wed, 18 Mar 2015 16:42:04 +0100
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (berkeley-unix)

On FreeBSD, configure emacs (trunk) with, say,

./configure --without-all --with-x --with-x-toolkit=no

and look at what configure thinks about pthread support:

checking for library containing pthread_atfork... none required
[...]
checking whether pthread_sigmask works without -lpthread... no
checking whether pthread_sigmask returns error numbers... yes
checking whether pthread_sigmask unblocks signals correctly... no

The correct results are `-lpthread' instead of `none required' (that is,
if the test were pertinent at all, see below) and `yes' instead of the
last `no'.  The bug is due to 93ca4887 and is not present in emacs-24.

The reason is that while pthread_atfork and some other pthread_ stubs
are indeed present in libc, -lpthread or something equivalent is still
needed for correct behaviour.

The issue is not peculiar to FreeBSD, as GNU libc also contains some
pthread_ symbols, but not those that configure tests, so things happen
to work there.  Allegedly, older versions of Solaris libc contain stubs
for (almost) all pthread_ functions whereas in newer versions those are
not stubs but the real stuff...

Next, the pthread_ functions can really be macros (at least in theory).

Finally, it would be nice (I imagine) to use -pthread instead
of -lpthread if possible.

So I wonder if it wouldn't be better to use the ax_pthread.m4 module
from the autoconf archive, which tackles those problems, see

https://www.gnu.org/software/autoconf-archive/ax_pthread.html

It contains some historical cruft, like tests for linuxthreads or
FreeBSD kthreads, but that could easily be trimmed.

Also, guile uses this module, so I guess that copyright assignment to
the FSF either has already been dealt with or is not an issue here.

In any case, I've written the necessary glue to integrate it into
configure.ac (to test it, you have also to download the latest version
of the module from the link above, put it in m4/ and run ./autogen.sh
after the new file and the patch below are in place):

The subtle art of writing tests which can be compiled and linked but
would break at run time is new to me, though (but ax_pthread.m4 does
this, so why not imitate it).

-- >8 --
Subject: [PATCH] configure.ac: Integrate m4/ax_pthread.m4.

---
 configure.ac | 59 ++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 32 insertions(+), 27 deletions(-)

diff --git a/configure.ac b/configure.ac
index 85e04e9..6ce0c6a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2182,33 +2182,38 @@ LIB_PTHREAD=
 if test "$opsys" != "mingw32"; then
 AC_CHECK_HEADERS_ONCE(pthread.h)
 if test "$ac_cv_header_pthread_h"; then
-  dnl gmalloc.c uses pthread_atfork, which is not available on older-style
-  dnl hosts such as MirBSD 10, so test for pthread_atfork instead of merely
-  dnl testing for pthread_kill if Emacs uses gmalloc.c.
-  if test "$GMALLOC_OBJ" = gmalloc.o; then
-    emacs_pthread_function=pthread_atfork
-  else
-    emacs_pthread_function=pthread_kill
-  fi
-  OLD_LIBS=$LIBS
-  AC_SEARCH_LIBS([$emacs_pthread_function], [pthread],
-    [AC_DEFINE([HAVE_PTHREAD], [1],
-       [Define to 1 if you have pthread (-lpthread).])
-     # Some systems optimize for single-threaded programs by default, and
-     # need special flags to disable these optimizations. For example, the
-     # definition of 'errno' in <errno.h>.
-     case $opsys in
-       sol*)
-         AC_DEFINE([_REENTRANT], 1,
-       [Define to 1 if your system requires this in multithreaded code.]);;
-       aix4-2)
-         AC_DEFINE([_THREAD_SAFE], 1,
-       [Define to 1 if your system requires this in multithreaded code.]);;
-     esac])
- if test "X$LIBS" != "X$OLD_LIBS"; then
-    eval LIB_PTHREAD=\$ac_cv_search_$emacs_pthread_function
-  fi
-  LIBS=$OLD_LIBS
+  OLD_CC="$CC"
+  OLD_CFLAGS="$CFLAGS"
+  OLD_LDFLAGS="$LDFLAGS"
+  OLD_LIBS="$LIBS"
+  ACX_PTHREAD([CC="$PTHREAD_CC"
+    CFLAGS="$PTHREAD_CFLAGS $CFLAGS"
+    dnl XXX Might not always work?
+    LDFLAGS="$PTHREAD_CFLAGS $LDFLAGS"
+    LIBS="$PTHREAD_LIBS $LIBS"
+    dnl gmalloc.c uses pthread_atfork, which is not available on older-style
+    dnl hosts such as MirBSD 10, so test for pthread_atfork instead of merely
+    dnl testing for pthread_kill if Emacs uses gmalloc.c.
+    if test "$GMALLOC_OBJ" = gmalloc.o; then
+      emacs_pthread_function=pthread_atfork
+      emacs_pthread_args="NULL, NULL, NULL"
+    else
+      emacs_pthread_function=pthread_kill
+      emacs_pthread_args="tid, 0"
+    fi
+    AC_MSG_CHECKING([for $emacs_pthread_function])
+    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>
+pthread_t tid;]],
+      [[pthread_create (&tid, NULL, NULL, NULL);
+$emacs_pthread_function ($emacs_pthread_args)]])],
+      [AC_MSG_RESULT([yes])
+       AC_DEFINE([HAVE_PTHREAD], [1], [Define to 1 if you have pthread.])
+       LIB_PTHREAD="$PTHREAD_LIBS"],
+      [AC_MSG_RESULT([no])
+       CC="$OLD_CC"
+       CFLAGS="$OLD_CFLAGS"
+       LDFLAGS="$OLD_LDFLAGS"])
+    LIBS="$OLD_LIBS"])
 fi
 AC_SUBST([LIB_PTHREAD])
 fi
-- 
2.3.3






reply via email to

[Prev in Thread] Current Thread [Next in Thread]