@@ -33,28 +33,41 @@ typedef struct {
3333
3434
3535/* Use a free list, since we should normally only need one at a time */
36- #ifndef Py_GIL_DISABLED
3736#define NPY_LOOP_DATA_CACHE_SIZE 5
3837static int loop_data_num_cached = 0 ;
39- static legacy_array_method_auxdata * loop_data_cache [NPY_LOOP_DATA_CACHE_SIZE ];
38+ static legacy_array_method_auxdata * loop_data_cache [NPY_LOOP_DATA_CACHE_SIZE ];
39+
40+ /* Thread-safe cache access using mutex */
41+ #if PY_VERSION_HEX < 0x30d00b3
42+ static PyThread_type_lock loop_data_cache_mutex ;
43+ #define LOCK_LOOP_DATA_CACHE \
44+ if (loop_data_cache_mutex) PyThread_acquire_lock(loop_data_cache_mutex, WAIT_LOCK)
45+ #define UNLOCK_LOOP_DATA_CACHE \
46+ if (loop_data_cache_mutex) PyThread_release_lock(loop_data_cache_mutex)
4047#else
41- #define NPY_LOOP_DATA_CACHE_SIZE 0
48+ static PyMutex loop_data_cache_mutex = {0 };
49+ #define LOCK_LOOP_DATA_CACHE PyMutex_Lock(&loop_data_cache_mutex)
50+ #define UNLOCK_LOOP_DATA_CACHE PyMutex_Unlock(&loop_data_cache_mutex)
4251#endif
4352
4453static void
4554legacy_array_method_auxdata_free (NpyAuxData * data )
4655{
4756#if NPY_LOOP_DATA_CACHE_SIZE > 0
57+ LOCK_LOOP_DATA_CACHE ;
4858 if (loop_data_num_cached < NPY_LOOP_DATA_CACHE_SIZE ) {
4959 loop_data_cache [loop_data_num_cached ] = (
5060 (legacy_array_method_auxdata * )data );
5161 loop_data_num_cached ++ ;
62+ UNLOCK_LOOP_DATA_CACHE ;
5263 }
53- else
54- #endif
55- {
64+ else {
65+ UNLOCK_LOOP_DATA_CACHE ;
5666 PyMem_Free (data );
5767 }
68+ #else
69+ PyMem_Free (data );
70+ #endif
5871}
5972
6073NpyAuxData *
@@ -63,20 +76,29 @@ get_new_loop_data(
6376{
6477 legacy_array_method_auxdata * data ;
6578#if NPY_LOOP_DATA_CACHE_SIZE > 0
79+ LOCK_LOOP_DATA_CACHE ;
6680 if (NPY_LIKELY (loop_data_num_cached > 0 )) {
6781 loop_data_num_cached -- ;
6882 data = loop_data_cache [loop_data_num_cached ];
83+ UNLOCK_LOOP_DATA_CACHE ;
6984 }
70- else
71- #endif
72- {
85+ else {
86+ UNLOCK_LOOP_DATA_CACHE ;
7387 data = PyMem_Malloc (sizeof (legacy_array_method_auxdata ));
7488 if (data == NULL ) {
7589 return NULL ;
7690 }
7791 data -> base .free = legacy_array_method_auxdata_free ;
7892 data -> base .clone = NULL ; /* no need for cloning (at least for now) */
7993 }
94+ #else
95+ data = PyMem_Malloc (sizeof (legacy_array_method_auxdata ));
96+ if (data == NULL ) {
97+ return NULL ;
98+ }
99+ data -> base .free = legacy_array_method_auxdata_free ;
100+ data -> base .clone = NULL ; /* no need for cloning (at least for now) */
101+ #endif
80102 data -> loop = loop ;
81103 data -> user_data = user_data ;
82104 data -> pyerr_check = pyerr_check ;
@@ -85,6 +107,18 @@ get_new_loop_data(
85107
86108#undef NPY_LOOP_DATA_CACHE_SIZE
87109
110+ NPY_NO_EXPORT int
111+ init_loop_data_cache_mutex (void ) {
112+ #if PY_VERSION_HEX < 0x30d00b3
113+ loop_data_cache_mutex = PyThread_allocate_lock ();
114+ if (loop_data_cache_mutex == NULL ) {
115+ PyErr_NoMemory ();
116+ return -1 ;
117+ }
118+ #endif
119+ return 0 ;
120+ }
121+
88122/*
89123 * This is a thin wrapper around the legacy loop signature.
90124 */
0 commit comments