>       * m4/pthread-once.m4 (gl_PTHREAD_ONCE): Use $LIBPMULTITHREAD also on
>       glibc systems.

I'm not satisfied with a manually determined list of platforms here.
Better use a configure test in the spirit of Autoconf.


2025-09-21  Bruno Haible  <[email protected]>

        pthread-once: Improve configuration.
        * m4/pthread-once.m4 (gl_PTHREAD_ONCE): Use a configure test to
        determine whether linking with $(LIBPMULTITHREAD) is necessary.

diff --git a/m4/pthread-once.m4 b/m4/pthread-once.m4
index f384df49a0..85549254fa 100644
--- a/m4/pthread-once.m4
+++ b/m4/pthread-once.m4
@@ -1,5 +1,5 @@
 # pthread-once.m4
-# serial 5
+# serial 6
 dnl Copyright (C) 2019-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -35,17 +35,48 @@ AC_DEFUN([gl_PTHREAD_ONCE]
         *yes) ;;
         *) REPLACE_PTHREAD_ONCE=1 ;;
       esac
-      dnl Among the platforms where $(LIBPTHREAD) is empty and
-      dnl $(LIBPMULTITHREAD) is non-empty, namely
-      dnl   glibc < 2.34, musl libc, macOS, FreeBSD, NetBSD, Solaris, Cygwin,
-      dnl   Haiku, Android,
-      dnl $(LIBPMULTITHREAD) is necessary only on glibc and FreeBSD.
-      case "$host_os" in
-        *-gnu* | gnu* | freebsd* | dragonfly* | midnightbsd*)
-          PTHREAD_ONCE_LIB="$LIBPMULTITHREAD" ;;
-        *)
-          PTHREAD_ONCE_LIB="$LIBPTHREAD" ;;
-      esac
+      dnl Determine whether linking requires $(LIBPMULTITHREAD) or only
+      dnl $(LIBPTHREAD).
+      if test -z "$LIBPTHREAD" && test -n "$LIBPMULTITHREAD"; then
+        AC_CACHE_CHECK([whether pthread_once can be used without linking with 
libpthread],
+          [gl_cv_func_pthread_once_no_lib],
+          [AC_RUN_IFELSE(
+             [AC_LANG_PROGRAM(
+                [[#include <pthread.h>
+                  static pthread_once_t a_once = PTHREAD_ONCE_INIT;
+                  static int a;
+                  static void a_init (void) { a = 8647; }
+                ]],
+                [[if (pthread_once (&a_once, a_init)) return 1;
+                  if (a != 8647) return 2;
+                  return 0;
+                ]])],
+             [gl_cv_func_pthread_once_no_lib=yes],
+             [gl_cv_func_pthread_once_no_lib=no],
+             [case "$host_os" in
+                # Guess no on glibc.
+                *-gnu* | gnu*)
+                  gl_cv_func_pthread_once_no_lib="guessing no" ;;
+                # Guess no on FreeBSD.
+                freebsd* | dragonfly* | midnightbsd*)
+                  gl_cv_func_pthread_once_no_lib="guessing no" ;;
+                # Guess yes otherwise.
+                *)
+                  gl_cv_func_pthread_once_no_lib="guessing yes" ;;
+              esac
+             ])
+          ])
+        case "$gl_cv_func_pthread_once_no_lib" in
+          *yes) PTHREAD_ONCE_LIB="$LIBPTHREAD" ;;
+          *)    PTHREAD_ONCE_LIB="$LIBPMULTITHREAD" ;;
+        esac
+        dnl Expected result:
+        dnl PTHREAD_ONCE_LIB is $(LIBPMULTITHREAD) on glibc < 2.34, FreeBSD.
+        dnl PTHREAD_ONCE_LIB is $(LIBPTHREAD) in particular on
+        dnl   musl libc, macOS, NetBSD, Solaris, Cygwin, Haiku, Android.
+      else
+        PTHREAD_ONCE_LIB="$LIBPTHREAD"
+      fi
     fi
   fi
   AC_SUBST([PTHREAD_ONCE_LIB])




Reply via email to