Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT: refactor StringDType to use PyMutex
  • Loading branch information
ngoldbaum committed Jul 24, 2024
commit d7713e66617b8f6fdef843749f4e3bfca9636986
20 changes: 19 additions & 1 deletion numpy/_core/src/multiarray/stringdtype/static_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ struct npy_string_allocator {
npy_string_free_func free;
npy_string_realloc_func realloc;
npy_string_arena arena;
#if PY_VERSION_HEX < 0x30d00b3
PyThread_type_lock *allocator_lock;
#else
PyMutex allocator_lock;
#endif
};

static void
Expand Down Expand Up @@ -245,18 +249,22 @@ NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f,
if (allocator == NULL) {
return NULL;
}
#if PY_VERSION_HEX < 0x30d00b3
PyThread_type_lock *allocator_lock = PyThread_allocate_lock();
if (allocator_lock == NULL) {
f(allocator);
PyErr_SetString(PyExc_MemoryError, "Unable to allocate thread lock");
return NULL;
}
allocator->allocator_lock = allocator_lock;
#else
memset(&allocator->allocator_lock, 0, sizeof(PyMutex));
#endif
allocator->malloc = m;
allocator->free = f;
allocator->realloc = r;
// arena buffer gets allocated in arena_malloc
allocator->arena = NEW_ARENA;
allocator->allocator_lock = allocator_lock;

return allocator;
}
Expand All @@ -269,9 +277,11 @@ NpyString_free_allocator(npy_string_allocator *allocator)
if (allocator->arena.buffer != NULL) {
f(allocator->arena.buffer);
}
#if PY_VERSION_HEX < 0x30d00b3
if (allocator->allocator_lock != NULL) {
PyThread_free_lock(allocator->allocator_lock);
}
#endif

f(allocator);
}
Expand All @@ -288,9 +298,13 @@ NpyString_free_allocator(npy_string_allocator *allocator)
NPY_NO_EXPORT npy_string_allocator *
NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)
{
#if PY_VERSION_HEX < 0x30d00b3
if (!PyThread_acquire_lock(descr->allocator->allocator_lock, NOWAIT_LOCK)) {
PyThread_acquire_lock(descr->allocator->allocator_lock, WAIT_LOCK);
}
#else
PyMutex_Lock(&descr->allocator->allocator_lock);
#endif
return descr->allocator;
}

Expand Down Expand Up @@ -358,7 +372,11 @@ NpyString_acquire_allocators(size_t n_descriptors,
NPY_NO_EXPORT void
NpyString_release_allocator(npy_string_allocator *allocator)
{
#if PY_VERSION_HEX < 0x30d00b3
PyThread_release_lock(allocator->allocator_lock);
#else
PyMutex_Unlock(&allocator->allocator_lock);
#endif
}

/*NUMPY_API
Expand Down