Skip to content

Commit fd55435

Browse files
authored
Merge pull request #26016 from rgommers/fix-thread-handling
MAINT: handle `NPY_ALLOW_THREADS` and related build option better
2 parents 7ff72c6 + 80871f5 commit fd55435

File tree

6 files changed

+12
-22
lines changed

6 files changed

+12
-22
lines changed

doc/source/reference/c-api/array.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,15 +4032,12 @@ variables), the GIL should be released so that other Python threads
40324032
can run while the time-consuming calculations are performed. This can
40334033
be accomplished using two groups of macros. Typically, if one macro in
40344034
a group is used in a code block, all of them must be used in the same
4035-
code block. Currently, :c:data:`NPY_ALLOW_THREADS` is defined to the
4036-
python-defined :c:data:`WITH_THREADS` constant unless the environment
4037-
variable ``NPY_NOSMP`` is set in which case
4038-
:c:data:`NPY_ALLOW_THREADS` is defined to be 0.
4035+
code block. :c:data:`NPY_ALLOW_THREADS` is true (defined as ``1``) unless the
4036+
build option ``-Ddisable-threading`` is set to ``true`` - in which case
4037+
:c:data:`NPY_ALLOW_THREADS` is false (``0``).
40394038
40404039
.. c:macro:: NPY_ALLOW_THREADS
40414040
4042-
.. c:macro:: WITH_THREADS
4043-
40444041
Group 1
40454042
^^^^^^^
40464043

numpy/_core/include/numpy/_numpyconfig.h.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#mesondefine NPY_SIZEOF_PY_LONG_LONG
1818
#mesondefine NPY_SIZEOF_LONGLONG
1919

20+
/*
21+
* Defined to 1 or 0. Note that Pyodide hardcodes NPY_NO_SMP (and other defines
22+
* in this header) for better cross-compilation, so don't rename them without a
23+
* good reason.
24+
*/
2025
#mesondefine NPY_NO_SMP
2126

2227
#mesondefine NPY_VISIBILITY_HIDDEN

numpy/_core/include/numpy/ndarraytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#define NPY_NO_EXPORT NPY_VISIBILITY_HIDDEN
1010

11-
/* Only use thread if configured in config and python supports it */
12-
#if defined WITH_THREAD && !NPY_NO_SMP
11+
/* Always allow threading unless it was explicitly disabled at build time */
12+
#if !NPY_NO_SMP
1313
#define NPY_ALLOW_THREADS 1
1414
#else
1515
#define NPY_ALLOW_THREADS 0

numpy/_core/meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ endif
491491
if cc.has_header('sys/endian.h')
492492
cdata.set10('NPY_HAVE_SYS_ENDIAN_H', true)
493493
endif
494-
# Command-line switch; distutils build checked for `NPY_NOSMP` env var instead
495-
# TODO: document this (search for NPY_NOSMP in C API docs)
494+
# Build-time option to disable threading is stored and exposed in numpyconfig.h
495+
# Note: SMP is an old acronym for threading (Symmetric/Shared-memory MultiProcessing)
496496
cdata.set10('NPY_NO_SMP', get_option('disable-threading'))
497497

498498
visibility_hidden = ''

numpy/_core/src/common/python_xerbla.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,16 @@ CBLAS_INT BLAS_FUNC(xerbla)(char *srname, CBLAS_INT *info)
2828
char buf[sizeof(format) + 6 + 4]; /* 6 for name, 4 for param. num. */
2929

3030
int len = 0; /* length of subroutine name*/
31-
#ifdef WITH_THREAD
3231
PyGILState_STATE save;
33-
#endif
3432

3533
while( len<6 && srname[len]!='\0' )
3634
len++;
3735
while( len && srname[len-1]==' ' )
3836
len--;
39-
#ifdef WITH_THREAD
4037
save = PyGILState_Ensure();
41-
#endif
4238
PyOS_snprintf(buf, sizeof(buf), format, len, srname, (int)*info);
4339
PyErr_SetString(PyExc_ValueError, buf);
44-
#ifdef WITH_THREAD
4540
PyGILState_Release(save);
46-
#endif
4741

4842
return 0;
4943
}

numpy/linalg/lapack_lite/python_xerbla.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,16 @@ CBLAS_INT BLAS_FUNC(xerbla)(char *srname, CBLAS_INT *info)
2828
char buf[sizeof(format) + 6 + 4]; /* 6 for name, 4 for param. num. */
2929

3030
int len = 0; /* length of subroutine name*/
31-
#ifdef WITH_THREAD
3231
PyGILState_STATE save;
33-
#endif
3432

3533
while( len<6 && srname[len]!='\0' )
3634
len++;
3735
while( len && srname[len-1]==' ' )
3836
len--;
39-
#ifdef WITH_THREAD
4037
save = PyGILState_Ensure();
41-
#endif
4238
PyOS_snprintf(buf, sizeof(buf), format, len, srname, (int)*info);
4339
PyErr_SetString(PyExc_ValueError, buf);
44-
#ifdef WITH_THREAD
4540
PyGILState_Release(save);
46-
#endif
4741

4842
return 0;
4943
}

0 commit comments

Comments
 (0)