@@ -229,6 +229,37 @@ arena_free(npy_string_arena *arena, _npy_static_string_u *str)
229229
230230static npy_string_arena NEW_ARENA = {0 , 0 , NULL };
231231
232+ static void
233+ acquire_allocator_lock (npy_string_allocator * allocator )
234+ {
235+ #if PY_VERSION_HEX < 0x30d00b3
236+ if (!PyThread_acquire_lock (allocator -> allocator_lock , NOWAIT_LOCK )) {
237+ if (PyGILState_Check ()) {
238+ Py_BEGIN_ALLOW_THREADS
239+ PyThread_acquire_lock (allocator -> allocator_lock , WAIT_LOCK );
240+ Py_END_ALLOW_THREADS
241+ }
242+ else {
243+ PyThread_acquire_lock (allocator -> allocator_lock , WAIT_LOCK );
244+ }
245+ }
246+ #else
247+ PyMutex_Lock (& allocator -> allocator_lock );
248+ #endif
249+ }
250+
251+ static int
252+ allocator_seen (npy_string_allocator * allocator ,
253+ npy_string_allocator * allocators [], size_t end )
254+ {
255+ for (size_t i = 0 ; i < end ; i ++ ) {
256+ if (allocators [i ] == allocator ) {
257+ return 1 ;
258+ }
259+ }
260+ return 0 ;
261+ }
262+
232263NPY_NO_EXPORT npy_string_allocator *
233264NpyString_new_allocator (npy_string_malloc_func m , npy_string_free_func f ,
234265 npy_string_realloc_func r )
@@ -281,18 +312,13 @@ NpyString_free_allocator(npy_string_allocator *allocator)
281312 * by this function exactly once.
282313 *
283314 * Note that functions requiring the GIL should not be called while the
284- * allocator mutex is held, as doing so may cause deadlocks.
315+ * allocator mutex is held and reentrant calls are possible, as doing so may
316+ * cause deadlocks.
285317 */
286318NPY_NO_EXPORT npy_string_allocator *
287319NpyString_acquire_allocator (const PyArray_StringDTypeObject * descr )
288320{
289- #if PY_VERSION_HEX < 0x30d00b3
290- if (!PyThread_acquire_lock (descr -> allocator -> allocator_lock , NOWAIT_LOCK )) {
291- PyThread_acquire_lock (descr -> allocator -> allocator_lock , WAIT_LOCK );
292- }
293- #else
294- PyMutex_Lock (& descr -> allocator -> allocator_lock );
295- #endif
321+ acquire_allocator_lock (descr -> allocator );
296322 return descr -> allocator ;
297323}
298324
@@ -309,42 +335,64 @@ NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)
309335 * ignored. A buffer overflow will happen if the *descrs* array does not
310336 * contain n_descriptors elements.
311337 *
312- * If pointers to the same descriptor are passed multiple times, only acquires
338+ * If pointers to the same allocator are passed multiple times, only acquires
313339 * the allocator mutex once but sets identical allocator pointers appropriately.
340+ * Distinct allocator mutexes are acquired in ascending allocator address order
341+ * to avoid lock-ordering deadlocks when callers pass different descriptor
342+ * orders.
314343 *
315344 * The allocator mutexes must be released after this function returns, see
316345 * NpyString_release_allocators.
317346 *
318347 * Note that functions requiring the GIL should not be called while the
319- * allocator mutex is held, as doing so may cause deadlocks.
348+ * allocator mutex is held and reentrant calls are possible, as doing so may
349+ * cause deadlocks.
320350 */
321351NPY_NO_EXPORT void
322352NpyString_acquire_allocators (size_t n_descriptors ,
323353 PyArray_Descr * const descrs [],
324354 npy_string_allocator * allocators [])
325355{
326- for (size_t i = 0 ; i < n_descriptors ; i ++ ) {
356+ for (size_t i = 0 ; i < n_descriptors ; i ++ ) {
327357 if (NPY_DTYPE (descrs [i ]) != & PyArray_StringDType ) {
328358 allocators [i ] = NULL ;
329359 continue ;
330360 }
331- int allocators_match = 0 ;
332- for (size_t j = 0 ; j < i ; j ++ ) {
333- if (allocators [j ] == NULL ) {
361+ allocators [i ] = ((PyArray_StringDTypeObject * )descrs [i ])-> allocator ;
362+ }
363+
364+ uintptr_t last_locked_addr = 0 ;
365+ int have_last_locked_addr = 0 ;
366+
367+ for (;;) {
368+ npy_string_allocator * next_allocator = NULL ;
369+ uintptr_t next_addr = UINTPTR_MAX ;
370+
371+ for (size_t i = 0 ; i < n_descriptors ; i ++ ) {
372+ npy_string_allocator * allocator = allocators [i ];
373+
374+ if (allocator == NULL || allocator_seen (allocator , allocators , i )) {
334375 continue ;
335376 }
336- if (((PyArray_StringDTypeObject * )descrs [i ])-> allocator ==
337- ((PyArray_StringDTypeObject * )descrs [j ])-> allocator )
338- {
339- allocators [i ] = allocators [j ];
340- allocators_match = 1 ;
341- break ;
377+
378+ uintptr_t addr = (uintptr_t )allocator ;
379+
380+ if (have_last_locked_addr && addr <= last_locked_addr ) {
381+ continue ;
382+ }
383+ if (next_allocator == NULL || addr < next_addr ) {
384+ next_allocator = allocator ;
385+ next_addr = addr ;
342386 }
343387 }
344- if (! allocators_match ) {
345- allocators [ i ] = NpyString_acquire_allocator (
346- ( PyArray_StringDTypeObject * ) descrs [ i ]) ;
388+
389+ if ( next_allocator == NULL ) {
390+ break ;
347391 }
392+
393+ acquire_allocator_lock (next_allocator );
394+ last_locked_addr = next_addr ;
395+ have_last_locked_addr = 1 ;
348396 }
349397}
350398
0 commit comments