summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2022-06-27 21:55:09 +0000
committerYann Ylavic <ylavic@apache.org>2022-06-27 21:55:09 +0000
commita34b5d4de58e009006a5128194dc64ec8327c571 (patch)
tree7760137c064cbf7af70fa2622fdd09cf1628ba6d
parent40dfb114cf029090d49879f9cb0d2a5e3d1bfd88 (diff)
downloadapr-a34b5d4de58e009006a5128194dc64ec8327c571.tar.gz
atomic: Detect 32bit and 64bit atomic builtins separately.
Some 32bit platforms provide 32bit but not 64bit atomics, detect and distinctly set/use HAVE_ATOMIC_BUILTINS/HAVE_ATOMIC_BUILTINS64 and USE_ATOMICS_BUILTINS/USE_ATOMICS_BUILTINS64. These platforms NEED_ATOMICS_GENERIC64 as 64bit implementation. * configure.in(): Use stdint.h's uint{32,64}_t as atomic type for testing atomic builtins when available, otherwise "unsigned int" for 32bit testing and "unsigned long long" as 64bit. * configure.in(): AC_TRY_RUN each 32bit and 64bit type separately, the former determining HAVE_ATOMIC_BUILTINS and the latter HAVE_ATOMIC_BUILTINS64. * include/arch/unix/apr_arch_atomic.h(): Define USE_ATOMICS_BUILTINS64 when HAVE_ATOMIC_BUILTINS64, otherise define NEED_ATOMICS_GENERIC64 * include/arch/unix/apr_arch_atomic.h(): Check defined(__powerpc__) for USE_ATOMICS_PPC. * atomic/unix/builtins64.c(): Implement for USE_ATOMICS_BUILTINS64 only. BZ 63566. Merge r1902266 from trunk. Submitted by: ylavic git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.7.x@1902290 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--atomic/unix/builtins64.c4
-rw-r--r--configure.in101
-rw-r--r--include/arch/unix/apr_arch_atomic.h9
3 files changed, 105 insertions, 9 deletions
diff --git a/atomic/unix/builtins64.c b/atomic/unix/builtins64.c
index 0f6edffbb..7d8422548 100644
--- a/atomic/unix/builtins64.c
+++ b/atomic/unix/builtins64.c
@@ -16,7 +16,7 @@
#include "apr_arch_atomic.h"
-#ifdef USE_ATOMICS_BUILTINS
+#ifdef USE_ATOMICS_BUILTINS64
#if defined(__arm__) || defined(__powerpc__) || defined(__powerpc64__)
#define WEAK_MEMORY_ORDERING 1
@@ -106,4 +106,4 @@ APR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint
#endif
}
-#endif /* USE_ATOMICS_BUILTINS */
+#endif /* USE_ATOMICS_BUILTINS64 */
diff --git a/configure.in b/configure.in
index 2fbd0a131..1cdc4653a 100644
--- a/configure.in
+++ b/configure.in
@@ -465,11 +465,18 @@ case "$host:$CC" in
;;
esac
-AC_CACHE_CHECK([whether the compiler provides atomic builtins], [ap_cv_atomic_builtins],
+AC_CACHE_CHECK([whether the compiler provides 32bit atomic builtins], [ap_cv_atomic_builtins],
[AC_TRY_RUN([
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
int main(int argc, const char *const *argv)
{
- unsigned long val = 1010, tmp, *mem = &val;
+#if HAVE_STDINT_H
+ uint32_t val = 1010, tmp, *mem = &val;
+#else
+ unsigned int val = 1010, tmp, *mem = &val;
+#endif
if (__sync_fetch_and_add(&val, 1010) != 1010 || val != 2020)
return 1;
@@ -495,11 +502,18 @@ int main(int argc, const char *const *argv)
return 0;
}], [ap_cv_atomic_builtins=yes], [ap_cv_atomic_builtins=no], [ap_cv_atomic_builtins=no])])
-AC_CACHE_CHECK([whether the compiler provides __atomic builtins], [ap_cv__atomic_builtins],
+AC_CACHE_CHECK([whether the compiler provides 32bit __atomic builtins], [ap_cv__atomic_builtins],
[AC_TRY_RUN([
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
int main(int argc, const char *const *argv)
{
- unsigned long val = 1010, tmp, *mem = &val, *ptmp;
+#if HAVE_STDINT_H
+ uint32_t val = 1010, tmp, *mem = &val, *ptmp;
+#else
+ unsigned int val = 1010, tmp, *mem = &val, *ptmp;
+#endif
if (__atomic_fetch_add(&val, 1010, __ATOMIC_SEQ_CST) != 1010 || val != 2020)
return 1;
@@ -528,9 +542,84 @@ int main(int argc, const char *const *argv)
}], [ap_cv__atomic_builtins=yes], [ap_cv__atomic_builtins=no], [ap_cv__atomic_builtins=no])])
if test "$ap_cv_atomic_builtins" = "yes" -o "$ap_cv__atomic_builtins" = "yes"; then
- AC_DEFINE(HAVE_ATOMIC_BUILTINS, 1, [Define if compiler provides atomic builtins])
+ AC_DEFINE(HAVE_ATOMIC_BUILTINS, 1, [Define if compiler provides 32bit atomic builtins])
if test "$ap_cv__atomic_builtins" = "yes"; then
- AC_DEFINE(HAVE__ATOMIC_BUILTINS, 1, [Define if compiler provides __atomic builtins])
+ AC_DEFINE(HAVE__ATOMIC_BUILTINS, 1, [Define if compiler provides 32bit __atomic builtins])
+ fi
+fi
+
+AC_CACHE_CHECK([whether the compiler provides 64bit atomic builtins], [ap_cv_atomic_builtins64],
+[AC_TRY_RUN([
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
+int main(int argc, const char *const *argv)
+{
+#if HAVE_STDINT_H
+ uint64_t val = 1010, tmp, *mem = &val;
+#else
+ unsigned long long val = 1010, tmp, *mem = &val;
+#endif
+
+ if (__sync_fetch_and_add(&val, 1010) != 1010 || val != 2020)
+ return 1;
+
+ tmp = val;
+ if (__sync_fetch_and_sub(mem, 1010) != tmp || val != 1010)
+ return 1;
+
+ if (__sync_sub_and_fetch(&val, 1010) != 0 || val != 0)
+ return 1;
+
+ tmp = 3030;
+ if (__sync_val_compare_and_swap(mem, 0, tmp) != 0 || val != tmp)
+ return 1;
+
+ __sync_synchronize();
+ if (__sync_lock_test_and_set(&val, 4040) != 3030)
+ return 1;
+
+ return 0;
+}], [ap_cv_atomic_builtins64=yes], [ap_cv_atomic_builtins64=no], [ap_cv_atomic_builtins64=no])])
+
+AC_CACHE_CHECK([whether the compiler provides 64bit __atomic builtins], [ap_cv__atomic_builtins64],
+[AC_TRY_RUN([
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
+int main(int argc, const char *const *argv)
+{
+#if HAVE_STDINT_H
+ uint64_t val = 1010, tmp, *mem = &val;
+#else
+ unsigned long long val = 1010, tmp, *mem = &val;
+#endif
+
+ if (__atomic_fetch_add(&val, 1010, __ATOMIC_SEQ_CST) != 1010 || val != 2020)
+ return 1;
+
+ tmp = val;
+ if (__atomic_fetch_sub(mem, 1010, __ATOMIC_SEQ_CST) != tmp || val != 1010)
+ return 1;
+
+ if (__atomic_sub_fetch(&val, 1010, __ATOMIC_SEQ_CST) != 0 || val != 0)
+ return 1;
+
+ tmp = val;
+ if (!__atomic_compare_exchange_n(mem, &tmp, 3030, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
+ || tmp != 0)
+ return 1;
+
+ if (__atomic_exchange_n(&val, 4040, __ATOMIC_SEQ_CST) != 3030)
+ return 1;
+
+ return 0;
+}], [ap_cv__atomic_builtins64=yes], [ap_cv__atomic_builtins64=no], [ap_cv__atomic_builtins64=no])])
+
+if test "$ap_cv_atomic_builtins64" = "yes" -o "$ap_cv__atomic_builtins64" = "yes"; then
+ AC_DEFINE(HAVE_ATOMIC_BUILTINS64, 1, [Define if compiler provides 64bit atomic builtins])
+ if test "$ap_cv__atomic_builtins64" = "yes"; then
+ AC_DEFINE(HAVE__ATOMIC_BUILTINS64, 1, [Define if compiler provides 64bit __atomic builtins])
fi
fi
diff --git a/include/arch/unix/apr_arch_atomic.h b/include/arch/unix/apr_arch_atomic.h
index 559257deb..ce771ad9c 100644
--- a/include/arch/unix/apr_arch_atomic.h
+++ b/include/arch/unix/apr_arch_atomic.h
@@ -26,6 +26,11 @@
/* noop */
#elif HAVE_ATOMIC_BUILTINS
# define USE_ATOMICS_BUILTINS
+# if HAVE_ATOMIC_BUILTINS64
+# define USE_ATOMICS_BUILTINS64
+# else
+# define NEED_ATOMICS_GENERIC64
+# endif
#elif defined(SOLARIS2) && SOLARIS2 >= 10
# define USE_ATOMICS_SOLARIS
# define NEED_ATOMICS_GENERIC64
@@ -36,7 +41,9 @@
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
# define USE_ATOMICS_IA32
# define NEED_ATOMICS_GENERIC64
-#elif defined(__GNUC__) && (defined(__PPC__) || defined(__ppc__))
+#elif defined(__GNUC__) && (defined(__powerpc__) \
+ || defined(__PPC__) \
+ || defined(__ppc__))
# define USE_ATOMICS_PPC
# define NEED_ATOMICS_GENERIC64
#elif defined(__GNUC__) && (defined(__s390__) || defined(__s390x__))