The option --disable-threads from the 'threadlib' module was only meant to
have an effect on the gnulib modules that use the Gnulib multithreading API
internally. But from the 'configure --help' output, David Edelsohn got the
impression that this option should apply also to the gnulib code that tests
HAVE_PTHREAD_API. Since the ability to build a library without dependency
on -lpthread can be seen as a useful feature, I'm implementing it here.


2023-11-17  Bruno Haible  <br...@clisp.org>

        mbrtowc, mbrtoc32, nl_langinfo, setlocale-null: Obey --disable-threads.
        Reported by David Edelsohn <dje....@gmail.com> in
        <https://gcc.gnu.org/pipermail/gcc-patches/2023-November/636830.html>,
        <https://gcc.gnu.org/pipermail/gcc-patches/2023-November/636843.html>.
        * m4/threadlib.m4 (gl_THREADLIB_BODY): If --disable-threads was
        specified, define AVOID_ANY_THREADS to 1.
        * lib/mbtowc-lock.c: Omit all code if --disable-threads was specified.
        * lib/nl_langinfo-lock.c: Likewise.
        * lib/setlocale-lock.c: Likewise.
        * lib/mbtowc-lock.h: Omit locking code if --disable-threads was
        specified.
        * lib/nl_langinfo.c: Likewise.
        * lib/setlocale_null.c: Likewise.
        * lib/mbrtowc.c: Don't include any multithreading headers if
        --disable-threads was specified.
        * lib/mbrtoc32.c: Likewise.

diff --git a/lib/mbrtoc32.c b/lib/mbrtoc32.c
index 0933437a4b..755d092e05 100644
--- a/lib/mbrtoc32.c
+++ b/lib/mbrtoc32.c
@@ -41,7 +41,11 @@
      branch below.
    They are equivalent.  */
 
-# if defined _WIN32 && !defined __CYGWIN__
+# if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+
+# elif defined _WIN32 && !defined __CYGWIN__
 
 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
 #  include <windows.h>
diff --git a/lib/mbrtowc.c b/lib/mbrtowc.c
index c1a689a270..0a061906d7 100644
--- a/lib/mbrtowc.c
+++ b/lib/mbrtowc.c
@@ -28,7 +28,11 @@
 # include <stdint.h>
 # include <stdlib.h>
 
-# if defined _WIN32 && !defined __CYGWIN__
+# if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+
+# elif defined _WIN32 && !defined __CYGWIN__
 
 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
 #  include <windows.h>
diff --git a/lib/mbtowc-lock.c b/lib/mbtowc-lock.c
index c4d0b088f6..9d16068263 100644
--- a/lib/mbtowc-lock.c
+++ b/lib/mbtowc-lock.c
@@ -18,9 +18,10 @@
 
 #include <config.h>
 
+/* The option '--disable-threads' explicitly requests no locking.  */
 /* When it is known that the gl_get_mbtowc_lock function is defined
    by a dependency library, it should not be defined here.  */
-#if OMIT_MBTOWC_LOCK
+#if AVOID_ANY_THREADS || OMIT_MBTOWC_LOCK
 
 /* This declaration is solely to ensure that after preprocessing
    this file is never empty.  */
diff --git a/lib/mbtowc-lock.h b/lib/mbtowc-lock.h
index beb37722f5..b0cc80c6a6 100644
--- a/lib/mbtowc-lock.h
+++ b/lib/mbtowc-lock.h
@@ -32,7 +32,7 @@ mbtowc_unlocked (wchar_t *pwc, const char *p, size_t m)
 /* Prohibit renaming this symbol.  */
 #undef gl_get_mbtowc_lock
 
-#if GNULIB_MBRTOWC_SINGLE_THREAD
+#if AVOID_ANY_THREADS || GNULIB_MBRTOWC_SINGLE_THREAD
 
 /* All uses of this function are in a single thread.  No locking needed.  */
 
diff --git a/lib/nl_langinfo-lock.c b/lib/nl_langinfo-lock.c
index fb12299959..f4cea0c290 100644
--- a/lib/nl_langinfo-lock.c
+++ b/lib/nl_langinfo-lock.c
@@ -18,9 +18,10 @@
 
 #include <config.h>
 
+/* The option '--disable-threads' explicitly requests no locking.  */
 /* When it is known that the gl_get_nl_langinfo_lock function is defined
    by a dependency library, it should not be defined here.  */
-#if OMIT_NL_LANGINFO_LOCK
+#if AVOID_ANY_THREADS || OMIT_NL_LANGINFO_LOCK
 
 /* This declaration is solely to ensure that after preprocessing
    this file is never empty.  */
diff --git a/lib/nl_langinfo.c b/lib/nl_langinfo.c
index f872c6d5e2..3be275510e 100644
--- a/lib/nl_langinfo.c
+++ b/lib/nl_langinfo.c
@@ -30,7 +30,12 @@
 #endif
 
 #if REPLACE_NL_LANGINFO && !NL_LANGINFO_MTSAFE
-# if defined _WIN32 && !defined __CYGWIN__
+
+# if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+
+# elif defined _WIN32 && !defined __CYGWIN__
 
 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
 #  include <windows.h>
@@ -51,6 +56,7 @@
 #  include <threads.h>
 
 # endif
+
 #endif
 
 /* nl_langinfo() must be multithread-safe.  To achieve this without using
@@ -186,7 +192,12 @@ nl_langinfo_unlocked (nl_item item)
 /* Prohibit renaming this symbol.  */
 #  undef gl_get_nl_langinfo_lock
 
-#  if defined _WIN32 && !defined __CYGWIN__
+#  if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+#   define nl_langinfo_with_lock nl_langinfo_unlocked
+
+#  elif defined _WIN32 && !defined __CYGWIN__
 
 extern __declspec(dllimport) CRITICAL_SECTION *gl_get_nl_langinfo_lock (void);
 
diff --git a/lib/setlocale-lock.c b/lib/setlocale-lock.c
index 593f63711b..090f824abe 100644
--- a/lib/setlocale-lock.c
+++ b/lib/setlocale-lock.c
@@ -18,9 +18,10 @@
 
 #include <config.h>
 
+/* The option '--disable-threads' explicitly requests no locking.  */
 /* When it is known that the gl_get_setlocale_null_lock function is defined
    by a dependency library, it should not be defined here.  */
-#if OMIT_SETLOCALE_LOCK
+#if AVOID_ANY_THREADS || OMIT_SETLOCALE_LOCK
 
 /* This declaration is solely to ensure that after preprocessing
    this file is never empty.  */
diff --git a/lib/setlocale_null.c b/lib/setlocale_null.c
index 89c8a06598..9d7cb785f7 100644
--- a/lib/setlocale_null.c
+++ b/lib/setlocale_null.c
@@ -30,7 +30,12 @@
 #endif
 
 #if !(SETLOCALE_NULL_ALL_MTSAFE && SETLOCALE_NULL_ONE_MTSAFE)
-# if defined _WIN32 && !defined __CYGWIN__
+
+# if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+
+# elif defined _WIN32 && !defined __CYGWIN__
 
 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
 #  include <windows.h>
@@ -51,6 +56,7 @@
 #  include <threads.h>
 
 # endif
+
 #endif
 
 /* Use the system's setlocale() function, not the gnulib override, here.  */
@@ -181,7 +187,12 @@ setlocale_null_unlocked (int category, char *buf, size_t 
bufsize)
 /* Prohibit renaming this symbol.  */
 # undef gl_get_setlocale_null_lock
 
-# if defined _WIN32 && !defined __CYGWIN__
+# if AVOID_ANY_THREADS
+
+/* The option '--disable-threads' explicitly requests no locking.  */
+#  define setlocale_null_with_lock setlocale_null_unlocked
+
+# elif defined _WIN32 && !defined __CYGWIN__
 
 extern __declspec(dllimport) CRITICAL_SECTION *gl_get_setlocale_null_lock 
(void);
 
diff --git a/m4/threadlib.m4 b/m4/threadlib.m4
index 855e563d88..b35ad53fd8 100644
--- a/m4/threadlib.m4
+++ b/m4/threadlib.m4
@@ -1,4 +1,4 @@
-# threadlib.m4 serial 40
+# threadlib.m4 serial 41
 dnl Copyright (C) 2005-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -584,6 +584,10 @@ AC_DEFUN([gl_THREADLIB_BODY]
           ;;
       esac
     fi
+  else
+    dnl "$gl_use_threads" is "no".
+    AC_DEFINE([AVOID_ANY_THREADS], [1],
+      [Define if no multithread safety and no multithreading is desired.])
   fi
   AC_MSG_CHECKING([for multithread API to use])
   AC_MSG_RESULT([$gl_threads_api])




Reply via email to