Paul Eggert wrote:
> * lib/glthread/thread.c (gl_thread_create):
> Define as a function only if multithreading.
> This is cleaner than my previous patch today that sometimes made
> it _Noreturn, as the function’s definition and declaration now always
> agree on noreturnedness

I agree that adding the '_Noreturn' keyword to a function in some
configurations and not in others is suboptimal, because it can trigger
warnings at the caller sites and thus violates the principle
"Shield the upper layers [here: callers] from the complexity as far as
possible".

But this #if patch has two other problems:

1) It declares a function and fails to define it in some configurations.
   That is, when there is a link error, it will look like a bug in gnulib
   and require in-depth analysis to determine the cause.

2) While it would support programming styles such as

     #if !SINGLE_THREADED
       gl_thread_t = gl_thread_create (...);
       ...
     #else
       ...
     #endif

   your preferred style is to avoid #if inside a function:

     if (!SINGLE_THREADED)
       {
         gl_thread_t = gl_thread_create (...);
         ...
       }
     else
       ...

   With this style, a link error would occur.

Problem 1 could be solved by adding a declaration to the .h file

#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS \
      || USE_WINDOWS_THREADS)
# define gl_thread_create 
gl_thread_create_not_defined_when_threading_is_disabled
#endif

but it would not help against problem 2.

To fix both problems, the best option I see is the following:


2026-04-28  Bruno Haible  <[email protected]>

        thread: Fix possible link error (regression 2026-04-25).
        * lib/glthread/thread.c (gl_thread_create): Define always. Ignore
        -Wsuggest-attribute=noreturn warning.

diff --git a/lib/glthread/thread.c b/lib/glthread/thread.c
index 261806c74b..1970f65710 100644
--- a/lib/glthread/thread.c
+++ b/lib/glthread/thread.c
@@ -204,8 +204,13 @@ const gl_thread_t gl_null_thread /* = { .p = NULL } */;
 
 /* ========================================================================= */
 
-#if (USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS \
-     || USE_WINDOWS_THREADS)
+/* Avoid gcc warning "function might be candidate for attribute 
‘noreturn’".
+   We don't want to mark gl_thread_create as _Noreturn in some configurations
+   and not in others, because that would cause warnings at the caller sites.  
*/
+#if _GL_GNUC_PREREQ (4, 6)
+# pragma GCC diagnostic ignored "-Wsuggest-attribute=noreturn"
+#endif
+
 gl_thread_t
 gl_thread_create (void *(*func) (void *arg), void *arg)
 {
@@ -215,4 +220,3 @@ gl_thread_create (void *(*func) (void *arg), void *arg)
     abort ();
   return thread;
 }
-#endif




Reply via email to