--- numpy/f2py/cfuncs.py 2024-02-06 05:17:48.000000000 +0800 +++ numpy/f2py/cfuncs.py 2024-11-13 10:02:16.000000000 +0800 @@ -540,24 +540,31 @@ #error You need to install NumPy version 0.13 or higher. See https://scipy.org/install.html #endif """ + +# Defining the correct value to indicate thread-local storage in C without +# running a compile-time check (which we have no control over in generated +# code used outside of NumPy) is hard. Therefore we support overriding this +# via an external define - the f2py-using package can them use the same +# compile-time checks as we use for `NPY_TLS` when building NumPy. +# +# __STDC_NO_THREADS__ should not be coupled to the availability of _Thread_local. +# In case we get a bug report, guard it with __STDC_NO_THREADS__ after all. +# +# `thread_local` has become a keyword in C23, but don't try to use that yet +# (too new, doing so while C23 support is preliminary will likely cause more +# problems than it solves). +# +# Note: do not try to use `threads.h`, its availability is very low +# *and* threads.h isn't actually used where `F2PY_THREAD_LOCAL_DECL` is +# in the generated code. See gh-27718 for more details. cppmacros["F2PY_THREAD_LOCAL_DECL"] = """ #ifndef F2PY_THREAD_LOCAL_DECL #if defined(_MSC_VER) #define F2PY_THREAD_LOCAL_DECL __declspec(thread) #elif defined(NPY_OS_MINGW) #define F2PY_THREAD_LOCAL_DECL __thread -#elif defined(__STDC_VERSION__) \\ - && (__STDC_VERSION__ >= 201112L) \\ - && !defined(__STDC_NO_THREADS__) \\ - && (!defined(__GLIBC__) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 12)) \\ - && !defined(NPY_OS_OPENBSD) && !defined(NPY_OS_HAIKU) -/* __STDC_NO_THREADS__ was first defined in a maintenance release of glibc 2.12, - see https://lists.gnu.org/archive/html/commit-hurd/2012-07/msg00180.html, - so `!defined(__STDC_NO_THREADS__)` may give false positive for the existence - of `threads.h` when using an older release of glibc 2.12 - See gh-19437 for details on OpenBSD */ -#include -#define F2PY_THREAD_LOCAL_DECL thread_local +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +#define F2PY_THREAD_LOCAL_DECL _Thread_local #elif defined(__GNUC__) \\ && (__GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 4))) #define F2PY_THREAD_LOCAL_DECL __thread