bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pthread-h: Fix override of pthread_spinlock_t with non-GNU compilers


From: Bruno Haible
Subject: pthread-h: Fix override of pthread_spinlock_t with non-GNU compilers
Date: Thu, 28 Mar 2024 22:07:04 +0100

Compiling a testdir with Oracle Developer Studio 12.6 (SunPRO C) on Linux,
I get this compilation error:

CC -m64 -O -DHAVE_CONFIG_H -DEXEEXT=\"\" -DEXEEXT=\"\" -I. -I../../gltests -I.. 
 -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. 
-I../../gltests/.. -I../gllib -I../../gltests/../gllib 
-I/home/bruno/inst-x86_64-64-suncc/include -Wall  -Wno-error -g -c -o 
test-pthread-c++.o ../../gltests/test-pthread-c++.cc
"../gllib/pthread.h", line 2436: Error: Cannot return extern "C" int(volatile 
int*,int) from a function that should return int(*)(pthread_mutex_t*,int).
"../gllib/pthread.h", line 2463: Error: Cannot return extern "C" int(volatile 
int*) from a function that should return int(*)(pthread_mutex_t*).
"../gllib/pthread.h", line 2490: Error: Cannot return extern "C" int(volatile 
int*) from a function that should return int(*)(pthread_mutex_t*).
"../gllib/pthread.h", line 2517: Error: Cannot return extern "C" int(volatile 
int*) from a function that should return int(*)(pthread_mutex_t*).
"../gllib/pthread.h", line 2544: Error: Cannot return extern "C" int(volatile 
int*) from a function that should return int(*)(pthread_mutex_t*).
5 Error(s) detected.
make[4]: *** [Makefile:25912: test-pthread-c++.o] Error 2

The cause is that the generated pthread.h overrides pthread_spinlock_t,
although it shouldn't: On glibc, the pthread spinlock functions exist and
work fine.

This patch fixes it.


2024-03-28  Bruno Haible  <bruno@clisp.org>

        pthread-h: Fix override of pthread_spinlock_t with non-GNU compilers.
        * m4/pthread-spin.m4 (gl_PTHREAD_SPIN): Define through AC_DEFUN_ONCE.
        Require gl_PTHREAD_H_DEFAULTS, not gl_PTHREAD_H.
        * m4/pthread_h.m4 (gl_PTHREAD_H): Invoke gl_PTHREAD_SPIN.
        * lib/pthread.in.h (pthread_spinlock_t,
        GNULIB_defined_pthread_spin_types): Don't define if
        HAVE_PTHREAD_SPIN_INIT && !REPLACE_PTHREAD_SPIN_INIT.
        * modules/pthread-h (Files): Add m4/pthread-spin.m4.

diff --git a/lib/pthread.in.h b/lib/pthread.in.h
index cd705fbd18..81598616df 100644
--- a/lib/pthread.in.h
+++ b/lib/pthread.in.h
@@ -417,16 +417,21 @@ typedef pthread_mutex_t pthread_spinlock_t;
 #else
 # if @HAVE_PTHREAD_SPINLOCK_T@
 /* <pthread.h> exists and defines pthread_spinlock_t.  */
-#  if !(((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \
-          || __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ 
>= 1)) \
-         || (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) \
-              && !defined __ANDROID__) \
-             || __clang_major__ >= 3)) \
-        && !defined __ibmxl__)
+#  if !@HAVE_PTHREAD_SPIN_INIT@ || @REPLACE_PTHREAD_SPIN_INIT@
+/* If the 'pthread-spin' module is in use, it defines all the pthread_spin*
+   functions.  Prepare for it by overriding pthread_spinlock_t if that might
+   be needed.  */
+#   if !(((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \
+           || __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ 
>= 1)) \
+          || (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) \
+               && !defined __ANDROID__) \
+              || __clang_major__ >= 3)) \
+         && !defined __ibmxl__)
 /* We can't use GCC built-ins.  Approximate spinlocks with mutexes.  */
-#   if !GNULIB_defined_pthread_spin_types
-#    define pthread_spinlock_t pthread_mutex_t
-#    define GNULIB_defined_pthread_spin_types 1
+#    if !GNULIB_defined_pthread_spin_types
+#     define pthread_spinlock_t pthread_mutex_t
+#     define GNULIB_defined_pthread_spin_types 1
+#    endif
 #   endif
 #  endif
 # else
diff --git a/m4/pthread-spin.m4 b/m4/pthread-spin.m4
index 05c38ba3cc..deb463774d 100644
--- a/m4/pthread-spin.m4
+++ b/m4/pthread-spin.m4
@@ -1,12 +1,12 @@
-# pthread-spin.m4 serial 6
+# pthread-spin.m4 serial 7
 dnl Copyright (C) 2019-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-AC_DEFUN([gl_PTHREAD_SPIN],
+AC_DEFUN_ONCE([gl_PTHREAD_SPIN],
 [
-  AC_REQUIRE([gl_PTHREAD_H])
+  AC_REQUIRE([gl_PTHREAD_H_DEFAULTS])
   AC_REQUIRE([AC_CANONICAL_HOST])
 
   if { case "$host_os" in mingw* | windows*) true;; *) false;; esac; } \
diff --git a/m4/pthread_h.m4 b/m4/pthread_h.m4
index c33afa0ce2..9954c81c17 100644
--- a/m4/pthread_h.m4
+++ b/m4/pthread_h.m4
@@ -1,4 +1,4 @@
-# pthread_h.m4 serial 9
+# pthread_h.m4 serial 10
 dnl Copyright (C) 2009-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -41,6 +41,8 @@ AC_DEFUN_ONCE([gl_PTHREAD_H]
   if test $ac_cv_type_pthread_spinlock_t != yes; then
     HAVE_PTHREAD_SPINLOCK_T=0
   fi
+  dnl Set HAVE_PTHREAD_SPIN_INIT, REPLACE_PTHREAD_SPIN_INIT.
+  gl_PTHREAD_SPIN
 
   dnl Constants may be defined as C preprocessor macros or as enum items.
 
diff --git a/modules/pthread-h b/modules/pthread-h
index 6daa6d02a7..241671404c 100644
--- a/modules/pthread-h
+++ b/modules/pthread-h
@@ -5,6 +5,7 @@ Files:
 lib/pthread.in.h
 m4/pthread_h.m4
 m4/threadlib.m4
+m4/pthread-spin.m4
 
 Depends-on:
 extensions






reply via email to

[Prev in Thread] Current Thread [Next in Thread]