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: use PyMutex in the argparse cache
  • Loading branch information
ngoldbaum committed Jul 24, 2024
commit dec1f8de33969dac845c98f47b60caf2f012893d
18 changes: 15 additions & 3 deletions numpy/_core/src/common/npy_argparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@

#include "arrayfunction_override.h"

#if PY_VERSION_HEX < 0x30d00b3
static PyThread_type_lock argparse_mutex;
#define LOCK_ARGPARSE_MUTEX \
PyThread_acquire_lock(argparse_mutex, WAIT_LOCK)
#define UNLOCK_ARGPARSE_MUTEX \
PyThread_release_lock(argparse_mutex)
#else
static PyMutex argparse_mutex = {0};
#define LOCK_ARGPARSE_MUTEX PyMutex_Lock(&argparse_mutex)
#define UNLOCK_ARGPARSE_MUTEX PyMutex_Unlock(&argparse_mutex)
#endif

NPY_NO_EXPORT int
init_argparse_mutex(void) {
#if PY_VERSION_HEX < 0x30d00b3
argparse_mutex = PyThread_allocate_lock();
if (argparse_mutex == NULL) {
PyErr_NoMemory();
return -1;
}
#endif
return 0;
}

Expand Down Expand Up @@ -286,19 +298,19 @@ _npy_parse_arguments(const char *funcname,
...)
{
if (!npy_atomic_load_uint8(&cache->initialized)) {
PyThread_acquire_lock(argparse_mutex, WAIT_LOCK);
LOCK_ARGPARSE_MUTEX;
if (!npy_atomic_load_uint8(&cache->initialized)) {
va_list va;
va_start(va, kwnames);
int res = initialize_keywords(funcname, cache, va);
va_end(va);
if (res < 0) {
PyThread_release_lock(argparse_mutex);
UNLOCK_ARGPARSE_MUTEX;
return -1;
}
npy_atomic_store_uint8(&cache->initialized, 1);
}
PyThread_release_lock(argparse_mutex);
UNLOCK_ARGPARSE_MUTEX;
}

if (NPY_UNLIKELY(len_args > cache->npositional)) {
Expand Down