Skip to content

Commit 75c1a3f

Browse files
committed
BUG: add compile-time checks for thread-local storage syntax
`f2py` has a macro for TLS that has broken multiple times now. It's more reliable to override the f2py define with the result of a compile-time check for what syntax compiles. Logic adapted from NumPy's `NPY_TLS` in `npy_common.h`.
1 parent c45b3ba commit 75c1a3f

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

scipy/meson.build

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,51 @@ fortranobject_dep = declare_dependency(
136136
link_with: fortranobject_lib,
137137
include_directories: [inc_np, inc_f2py],
138138
)
139-
# Workaround for numpy#24761 on numpy<1.26.1 (see also gh-20515)
140-
_f2py_c_args = []
141-
if is_mingw
142-
_f2py_c_args = '-DNPY_OS_MINGW'
139+
140+
cdata = configuration_data()
141+
142+
# Test variable attribute to use for thread-local storage;
143+
# Adapted from `numpy/_core/meson.build`.
144+
check_tls_attrs = [
145+
['thread_local', 'HAVE_THREAD_LOCAL'], # C23
146+
['_Thread_local', 'HAVE__THREAD_LOCAL'], # C11/C17
147+
['__thread', 'HAVE__THREAD'],
148+
]
149+
if is_windows and not is_mingw
150+
check_tls_attrs += ['__declspec(thread)', 'HAVE___DECLSPEC_THREAD_']
143151
endif
152+
f2py_tls_define = ''
153+
foreach tls_attrs: check_tls_attrs
154+
attr = tls_attrs[0]
155+
code = f'''
156+
#pragma GCC diagnostic error "-Wattributes"
157+
#pragma clang diagnostic error "-Wattributes"
158+
159+
int @attr@ foo;
160+
'''
161+
code += '''
162+
int
163+
main()
164+
{
165+
return 0;
166+
}
167+
'''
168+
if cc.compiles(code, name: tls_attrs[0])
169+
cdata.set10(tls_attrs[1], true)
170+
f2py_tls_define = tls_attrs[0]
171+
endif
172+
endforeach
173+
174+
# Contains only TLS check results for now - name chosen for when more compiler
175+
# checks need adding in the future.
176+
scipy_config_h = configure_file(
177+
input: 'scipy_config.h.in',
178+
output: 'scipy_config.h',
179+
configuration: cdata,
180+
install: false
181+
)
182+
183+
_f2py_c_args = [f'-DF2PY_THREAD_LOCAL_DECL=@f2py_tls_define@']
144184
fortranobject_dep = declare_dependency(
145185
dependencies: fortranobject_dep,
146186
compile_args: _f2py_c_args,

scipy/scipy_config.h.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Thread-local storage */
2+
#mesondefine HAVE_THREAD_LOCAL
3+
#mesondefine HAVE__THREAD_LOCAL
4+
#mesondefine HAVE__THREAD
5+
#mesondefine HAVE___DECLSPEC_THREAD_
6+
7+
#ifdef __cplusplus
8+
#define SCIPY_TLS thread_local
9+
#elif defined(HAVE_THREAD_LOCAL)
10+
#define SCIPY_TLS thread_local
11+
#elif defined(HAVE__THREAD_LOCAL)
12+
#define SCIPY_TLS _Thread_local
13+
#elif defined(HAVE___THREAD)
14+
#define SCIPY_TLS __thread
15+
#elif defined(HAVE___DECLSPEC_THREAD_)
16+
#define SCIPY_TLS __declspec(thread)
17+
#else
18+
#define SCIPY_TLS
19+
#endif

0 commit comments

Comments
 (0)