When I added the pthread-once module in 2019, I set the
link dependencies to
  $(LIBPMULTITHREAD)
because with just $(LIBPTHREAD), there would be a link error
in the test program 'test-pthread-once1' on FreeBSD.

But $(LIBPMULTITHREAD) = '-lpthread' is overkill on some
other platforms:
  musl libc, macOS, NetBSD, Solaris, Cygwin, Haiku, Android.

This patch reduces the link dependencies of this module
for these 7 platforms.


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

        pthread-once: Reduce link dependencies.
        Linking with -lpthread is not needed on musl libc, macOS, NetBSD,
        Solaris, Cygwin, Haiku, Android.
        Reported by Michael Osipov <[email protected]> and
        Tijl Coosemans <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gettext/2025-09/msg00019.html>.
        * m4/pthread-once.m4 (gl_PTHREAD_ONCE): Require gl_PTHREADLIB. Set
        PTHREAD_ONCE_LIB.
        * modules/pthread-once (Link): Link with $(PTHREAD_ONCE_LIB) instead of
        $(LIBPMULTITHREAD).
        * modules/pthread-once-tests (Makefile.am): Update accordingly.

diff --git a/m4/pthread-once.m4 b/m4/pthread-once.m4
index 97484129ee..6978bcb563 100644
--- a/m4/pthread-once.m4
+++ b/m4/pthread-once.m4
@@ -1,5 +1,5 @@
 # pthread-once.m4
-# serial 3
+# serial 4
 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,
@@ -10,15 +10,18 @@ AC_DEFUN([gl_PTHREAD_ONCE]
 [
   AC_REQUIRE([gl_PTHREAD_H])
   AC_REQUIRE([AC_CANONICAL_HOST])
+  AC_REQUIRE([gl_PTHREADLIB])
 
   if { case "$host_os" in mingw* | windows*) true;; *) false;; esac; } \
      && test $gl_threads_api = windows; then
     dnl Choose function names that don't conflict with the mingw-w64 
winpthreads
     dnl library.
     REPLACE_PTHREAD_ONCE=1
+    PTHREAD_ONCE_LIB=
   else
     if test $HAVE_PTHREAD_H = 0; then
       HAVE_PTHREAD_ONCE=0
+      PTHREAD_ONCE_LIB=
     else
       dnl Work around Cygwin 3.5.3 bug.
       AC_CACHE_CHECK([whether pthread_once works],
@@ -32,6 +35,17 @@ 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   musl libc, macOS, FreeBSD, NetBSD, Solaris, Cygwin, Haiku, Android,
+      dnl $(LIBPMULTITHREAD) is necessary only on FreeBSD.
+      case "$host_os" in
+        freebsd* | dragonfly* | midnightbsd*)
+          PTHREAD_ONCE_LIB="$LIBPMULTITHREAD" ;;
+        *)
+          PTHREAD_ONCE_LIB="$LIBPTHREAD" ;;
+      esac
     fi
   fi
+  AC_SUBST([PTHREAD_ONCE_LIB])
 ])
diff --git a/modules/pthread-once b/modules/pthread-once
index 3e6ab5f93b..03d102ef48 100644
--- a/modules/pthread-once
+++ b/modules/pthread-once
@@ -24,7 +24,7 @@ Include:
 <pthread.h>
 
 Link:
-$(LIBPMULTITHREAD)
+$(PTHREAD_ONCE_LIB)
 
 License:
 LGPLv2+
diff --git a/modules/pthread-once-tests b/modules/pthread-once-tests
index 8ec7030ab8..b15703a330 100644
--- a/modules/pthread-once-tests
+++ b/modules/pthread-once-tests
@@ -15,5 +15,5 @@ AC_CHECK_DECLS_ONCE([alarm])
 Makefile.am:
 TESTS += test-pthread-once1 test-pthread-once2
 check_PROGRAMS += test-pthread-once1 test-pthread-once2
-test_pthread_once1_LDADD = $(LDADD) @LIBPMULTITHREAD@
+test_pthread_once1_LDADD = $(LDADD) @PTHREAD_ONCE_LIB@
 test_pthread_once2_LDADD = $(LDADD) @LIBPMULTITHREAD@ @SCHED_YIELD_LIB@




Reply via email to