From 54070e1954dd508f92ab9d33d7dca992b421cb56 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 30 Nov 2023 22:39:52 +0900 Subject: [PATCH] gh-112535: Update _Py_ThreadId() to support PowerPC, IBM Z, etc --- Include/object.h | 2 ++ configure | 41 +++++++++++++++++++++++++++++++++++++++++ configure.ac | 17 +++++++++++++++++ pyconfig.h.in | 3 +++ 4 files changed, 63 insertions(+) diff --git a/Include/object.h b/Include/object.h index 6b70a494844476..61d16cb0804b79 100644 --- a/Include/object.h +++ b/Include/object.h @@ -261,6 +261,8 @@ _Py_ThreadId(void) __asm__ ("mrs %0, tpidrro_el0" : "=r" (tid)); #elif defined(__aarch64__) __asm__ ("mrs %0, tpidr_el0" : "=r" (tid)); +#elif define(HAVE_BUILTIN_THREAD_POINTER) + tid = (uintptr_t)__builtin_thread_pointer(); #else # error "define _Py_ThreadId for this platform" #endif diff --git a/configure b/configure index 319009537f461c..8053230e2e378e 100755 --- a/configure +++ b/configure @@ -16864,6 +16864,47 @@ then : printf "%s\n" "#define HAVE_BUILTIN_ATOMIC 1" >>confdefs.h +fi + +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for builtin __builtin_thread_pointer" >&5 +printf %s "checking for builtin __builtin_thread_pointer... " >&6; } +if test ${ac_cv_builtin_thread_pointer+y} +then : + printf %s "(cached) " >&6 +else $as_nop + +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + + void *tp; + int main() { + tp = __builtin_thread_pointer(); + return 0; + } + + +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + ac_cv_builtin_thread_pointer=yes +else $as_nop + ac_cv_builtin_thread_pointer=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_builtin_thread_pointer" >&5 +printf "%s\n" "$ac_cv_builtin_thread_pointer" >&6; } + +if test "x$ac_cv_builtin_thread_pointer" = xyes +then : + + +printf "%s\n" "#define HAVE_BUILTIN_THREAD_POINTER 1" >>confdefs.h + + fi # --with-mimalloc diff --git a/configure.ac b/configure.ac index b78472e04846b7..ca133ef48f1b1f 100644 --- a/configure.ac +++ b/configure.ac @@ -4541,6 +4541,23 @@ AS_VAR_IF([ac_cv_builtin_atomic], [yes], [ AC_DEFINE(HAVE_BUILTIN_ATOMIC, 1, [Has builtin __atomic_load_n() and __atomic_store_n() functions]) ]) +AC_CACHE_CHECK([for builtin __builtin_thread_pointer], [ac_cv_builtin_thread_pointer], [ +AC_LINK_IFELSE( +[ + AC_LANG_SOURCE([[ + void *tp; + int main() { + tp = __builtin_thread_pointer(); + return 0; + } + ]]) +],[ac_cv_builtin_thread_pointer=yes],[ac_cv_builtin_thread_pointer=no]) +]) + +AS_VAR_IF([ac_cv_builtin_thread_pointer], [yes], [ + AC_DEFINE(HAVE_BUILTIN_THREAD_POINTER, 1, [Has builtin __builtin_thread_pointer]) +]) + # --with-mimalloc AC_MSG_CHECKING([for --with-mimalloc]) AC_ARG_WITH([mimalloc], diff --git a/pyconfig.h.in b/pyconfig.h.in index bf708926e22c43..480f7bf5c35b7d 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -127,6 +127,9 @@ /* Has builtin __atomic_load_n() and __atomic_store_n() functions */ #undef HAVE_BUILTIN_ATOMIC +/* Has builtin __builtin_thread_pointer */ +#undef HAVE_BUILTIN_THREAD_POINTER + /* Define to 1 if you have the header file. */ #undef HAVE_BZLIB_H