[PATCH] Factor out thread model detection with `GCC_AC_THREAD_MODEL`
John Ericson
John.Ericson@Obsidian.Systems
Sun Jul 13 16:31:17 GMT 2025
From: John Ericson <git@JohnEricson.me>
This macro deduplicates the
$CC -v 2>&1 | sed -n 's/^Thread model: //p'
check that was occurring in various runtime libs.
Additionally, as a bit of an Easter egg, this also allows overriding
what the compiler would return by setting the
`gcc_cv_target_thread_file` cache variable first. I admit that it is in
fact this Easter egg that led me to write the patch. The use-case for it
is for making multilib builds where the library sets do not all share
the same thread model easier. See also `THREAD_MODEL_SPEC` for more
about the varying thread models use-case.
Arguably one could could try to define on `THREAD_MODEL_SPEC` on more
platforms (besides e.g. AIX) but the ramifications of this are a bit
unclear. Setting `gcc_cv_target_thread_file` directly is a "low tech"
solution that will work for now for sure. Of course, since setting a
cache variable like this a hacky trick, I will not expect this to be at
all stable/guaranteed to work, going forward.
Thanks to Arsen who on IRC discussed these things with me, including in
particular making it a cache var not `--with-model` flag, to not
prematurely foster expectations that this is stable.
Suggested-by: Arsen Arsenović <arsen@aarsen.me>
---
config/gthr.m4 | 23 +++++++++++++++++++++++
libatomic/configure.ac | 4 +---
libgcc/configure.ac | 4 +---
libphobos/m4/druntime/os.m4 | 2 +-
libstdc++-v3/acinclude.m4 | 8 +++-----
5 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/config/gthr.m4 b/config/gthr.m4
index 11996247f15..e8fac4a5721 100644
--- a/config/gthr.m4
+++ b/config/gthr.m4
@@ -5,6 +5,26 @@ dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
+dnl Define thread model
+
+dnl usage: GCC_AC_THREAD_MODEL
+AC_DEFUN([GCC_AC_THREAD_MODEL],
+[
+# Specify the threading model for this GCC runtime library
+# Pass with no value to take from compiler's metadata
+# Pass with a value to specify a thread package
+# 'single' means single threaded -- without threads.
+AC_CACHE_CHECK([for the threading model used by GCC], [gcc_cv_target_thread_file], [
+ # Set new cache variable
+ gcc_cv_target_thread_file=`$CC -v 2>&1 | sed -n 's/^Thread model: //p'`
+])
+# Set variable name (not prefixed enough to be a good cache variable
+# name) traditionally used for this purpose, to avoid having to change
+# a bunch of configure scripts.
+target_thread_file="$gcc_cv_target_thread_file"
+])
+
+
dnl Define header location by thread model
dnl usage: GCC_AC_THREAD_HEADER([thread_model])
@@ -23,6 +43,9 @@ case $1 in
vxworks) thread_header=config/gthr-vxworks.h ;;
win32) thread_header=config/i386/gthr-win32.h ;;
mcf) thread_header=config/i386/gthr-mcf.h ;;
+ *)
+ AC_MSG_ERROR([No known header for threading model '$1'.])
+ ;;
esac
AC_SUBST(thread_header)
])
diff --git a/libatomic/configure.ac b/libatomic/configure.ac
index aafae71028d..a1aa3bdf69f 100644
--- a/libatomic/configure.ac
+++ b/libatomic/configure.ac
@@ -162,9 +162,7 @@ libtool_VERSION=3:0:2
AC_SUBST(libtool_VERSION)
# Check for used threading-model
-AC_MSG_CHECKING([for thread model used by GCC])
-target_thread_file=`$CC -v 2>&1 | sed -n 's/^Thread model: //p'`
-AC_MSG_RESULT([$target_thread_file])
+GCC_AC_THREAD_MODEL
case "$target" in
*aarch64*)
diff --git a/libgcc/configure.ac b/libgcc/configure.ac
index 85e4f1bc48b..d44493f9653 100644
--- a/libgcc/configure.ac
+++ b/libgcc/configure.ac
@@ -305,9 +305,7 @@ AC_SUBST([use_tm_clone_registry])
AC_LIB_PROG_LD_GNU
-AC_MSG_CHECKING([for thread model used by GCC])
-target_thread_file=`$CC -v 2>&1 | sed -n 's/^Thread model: //p'`
-AC_MSG_RESULT([$target_thread_file])
+GCC_AC_THREAD_MODEL
# Check for assembler CFI support.
AC_CACHE_CHECK([whether assembler supports CFI directives], [libgcc_cv_cfi],
diff --git a/libphobos/m4/druntime/os.m4 b/libphobos/m4/druntime/os.m4
index ef8ca434407..7bb91362dbe 100644
--- a/libphobos/m4/druntime/os.m4
+++ b/libphobos/m4/druntime/os.m4
@@ -32,7 +32,7 @@ case $1 in
# TODO: These targets need porting.
dce|mipssde|rtems|tpf|vxworks)
DCFG_THREAD_MODEL="Single" ;;
- *) as_fn_error "Thread implementation '$1' not recognised" "$LINENO" 5 ;;
+ *) AC_MSG_ERROR([Thread implementation '$1' not recognised]) ;;
esac
AC_SUBST(DCFG_THREAD_MODEL)
])
diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index a0094c2dd95..66fc3abe4fd 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -4345,9 +4345,7 @@ dnl Substs:
dnl thread_header
dnl
AC_DEFUN([GLIBCXX_ENABLE_THREADS], [
- AC_MSG_CHECKING([for thread model used by GCC])
- target_thread_file=`$CXX -v 2>&1 | sed -n 's/^Thread model: //p'`
- AC_MSG_RESULT([$target_thread_file])
+ GCC_AC_THREAD_MODEL
GCC_AC_THREAD_HEADER([$target_thread_file])
])
@@ -4357,7 +4355,8 @@ dnl Check if gthread implementation defines the types and functions
dnl required by the c++0x thread library. Conforming gthread
dnl implementations can define __GTHREADS_CXX0X to enable use with c++0x.
dnl
-dnl GLIBCXX_ENABLE_SYMVERS must be done before this.
+dnl GLIBCXX_ENABLE_SYMVERS and GLIBCXX_ENABLE_THREADS must be done
+dnl before this.
dnl
AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
GLIBCXX_ENABLE(libstdcxx-threads,auto,,[enable C++11 threads support])
@@ -4372,7 +4371,6 @@ AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
CXXFLAGS="$CXXFLAGS -fno-exceptions \
-I${toplevel_srcdir}/libgcc -I${toplevel_builddir}/libgcc"
- target_thread_file=`$CXX -v 2>&1 | sed -n 's/^Thread model: //p'`
case $target_thread_file in
posix)
CXXFLAGS="$CXXFLAGS -DSUPPORTS_WEAK -DGTHREAD_USE_WEAK -D_PTHREADS"
--
2.47.2
More information about the Gcc-patches
mailing list