10000 MNT: move some thread-unsafe state in thread-unsafe state struct · numpy/numpy@c35d7a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c35d7a0

Browse files
committed
MNT: move some thread-unsafe state in thread-unsafe state struct
1 parent 66f6ab7 commit c35d7a0

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

numpy/_core/src/multiarray/alloc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "numpy/npy_common.h"
1212
#include "npy_config.h"
1313
#include "alloc.h"
14+
#include "multiarraymodule.h"
1415

1516
#include <assert.h>
1617
#ifdef NPY_OS_LINUX
@@ -35,21 +36,19 @@ typedef struct {
3536
static cache_bucket datacache[NBUCKETS];
3637
static cache_bucket dimcache[NBUCKETS_DIM];
3738

38-
static int _madvise_hugepage = 1;
39-
4039

4140
/*
4241
* This function tells whether NumPy attempts to call `madvise` with
4342
* `MADV_HUGEPAGE`. `madvise` is only ever used on linux, so the value
44-
* of `_madvise_hugepage` may be ignored.
43+
* of `madvise_hugepage` may be ignored.
4544
*
4645
* It is exposed to Python as `np._core.multiarray._get_madvise_hugepage`.
4746
*/
4847
NPY_NO_EXPORT PyObject *
4948
_get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
5049
{
5150
#ifdef NPY_OS_LINUX
52-
if (_madvise_hugepage) {
51+
if (npy_ma_thread_unsafe_state->madvise_hugepage) {
5352
Py_RETURN_TRUE;
5453
}
5554
#endif
@@ -59,20 +58,20 @@ _get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
5958

6059
/*
6160
* This function enables or disables the use of `MADV_HUGEPAGE` on Linux
62-
* by modifying the global static `_madvise_hugepage`.
63-
* It returns the previous value of `_madvise_hugepage`.
61+
* by modifying the global static `madvise_hugepage`.
62+
* It returns the previous value of `madvise_hugepage`.
6463
*
6564
* It is exposed to Python as `np._core.multiarray._set_madvise_hugepage`.
6665
*/
6766
NPY_NO_EXPORT PyObject *
6867
_set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj)
6968
{
70-
int was_enabled = _madvise_hugepage;
69+
int was_enabled = npy_ma_thread_unsafe_state->madvise_hugepage;
7170
int enabled = PyObject_IsTrue(enabled_obj);
7271
if (enabled < 0) {
7372
return NULL;
7473
}
75-
_madvise_hugepage = enabled;
74+
npy_ma_thread_unsafe_state->madvise_hugepage = enabled;
7675
if (was_enabled) {
7776
Py_RETURN_TRUE;
7877
}
@@ -110,7 +109,8 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
110109
#endif
111110
#ifdef NPY_OS_LINUX
112111
/* allow kernel allocating huge pages for large arrays */
113-
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) && _madvise_hugepage) {
112+
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) &&
113+
npy_ma_thread_unsafe_state->madvise_hugepage) {
114114
npy_uintp offset = 4096u - (npy_uintp)p % (4096u);
115115
npy_uintp length = nelem * esz - offset;
116116
/**

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4366,8 +4366,6 @@ _set_numpy_warn_if_no_mem_policy(PyObject *NPY_UNUSED(self), PyObject *arg)
43664366

43674367
static PyObject *
43684368
_reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) {
4369-
static int initialized = 0;
4370-
43714369
#if !defined(PYPY_VERSION)
43724370
if (PyThreadState_Get()->interp != PyInterpreterState_Main()) {
43734371
if (PyErr_WarnEx(PyExc_UserWarning,
@@ -4383,19 +4381,19 @@ _reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) {
43834381
return NULL;
43844382
}
43854383
/* No need to give the other warning in a sub-interpreter as well... */
4386-
initialized = 1;
4384+
npy_ma_thread_unsafe_state->reload_guard_initialized = 1;
43874385
Py_RETURN_NONE;
43884386
}
43894387
#endif
4390-
if (initialized) {
4388+
if (npy_ma_thread_unsafe_state->reload_guard_initialized) {
43914389
if (PyErr_WarnEx(PyExc_UserWarning,
43924390
"The NumPy module was reloaded (imported a second time). "
43934391
"This can in some cases result in small but subtle issues "
43944392
"and is discouraged.", 2) < 0) {
43954393
return NULL;
43964394
}
43974395
}
4398-
initialized = 1;
4396+
npy_ma_thread_unsafe_state->reload_guard_initialized = 1;
43994397
Py_RETURN_NONE;
44004398
}
44014399

@@ -4925,6 +4923,7 @@ initialize_static_globals(void)
49254923
// this is module-level global heap allocation, it is currently
49264924
// never freed
49274925
npy_ma_static_data = PyMem_Calloc(1, sizeof(npy_ma_static_data_struct));
4926+
npy_ma_thread_unsafe_state = PyMem_Calloc(1, sizeof(npy_ma_thread_unsafe_state_struct));
49284927

49294928
// cached reference to objects defined in python
49304929

numpy/_core/src/multiarray/multiarraymodule.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ typedef struct npy_ma_thread_unsafe_state_struct {
202202
*/
203203
PyObject *PyArray_StrFunction;
204204
PyObject *PyArray_ReprFunction;
205+
206+
/*
207+
* Used to test the internal-only scaled float test dtype
208+
*/
209+
npy_bool get_sfloat_dtype_initialized;
210+
211+
/*
212+
* controls the global madvise hugepage setting
213+
*/
214+
int madvise_hugepage;
215+
216+
/*
217+
* used to detect module reloading in the reload guard
218+
*/
219+
int reload_guard_initialized;
205220
} npy_ma_thread_unsafe_state_struct;
206221

207222
NPY_VISIBILITY_HIDDEN extern npy_ma_str_struct *npy_ma_str;

numpy/_core/src/umath/_scaled_float_dtype.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,7 @@ sfloat_init_ufuncs(void) {
867867
NPY_NO_EXPORT PyObject *
868868
get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args))
869869
{
870-
/* Allow calling the function multiple times. */
871-
static npy_bool initialized = NPY_FALSE;
872-
873-
if (initialized) {
870+
if (npy_ma_thread_unsafe_state->get_sfloat_dtype_initialized) {
874871
Py_INCREF(&PyArray_SFloatDType);
875872
return (PyObject *)&PyArray_SFloatDType;
876873
}
@@ -899,6 +896,6 @@ get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args))
899896
return NULL;
900897
}
901898

902-
initialized = NPY_TRUE;
899+
npy_ma_thread_unsafe_state->get_sfloat_dtype_initialized = NPY_TRUE;
903900
return (PyObject *)&PyArray_SFloatDType;
904901
}

0 commit comments

Comments
 (0)
0