Skip to content
Merged
Show file tree
Hide file tree
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 the identity hash table to use PyMutex
  • Loading branch information
ngoldbaum committed Jul 24, 2024
commit 571b60d727dbe12fbaed5591242d960adac440d0
9 changes: 8 additions & 1 deletion numpy/_core/src/common/npy_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#endif

#ifdef Py_GIL_DISABLED
#if PY_VERSION_HEX < 0x30d00b3
// TODO: replace with PyMutex when it is public
#define LOCK_TABLE(tb) \
if (!PyThread_acquire_lock(tb->mutex, NOWAIT_LOCK)) { \
Expand All @@ -48,6 +49,12 @@
PyThread_free_lock(tb->mutex); \
}
#else
#define LOCK_TABLE(tb) PyMutex_Lock(&tb->mutex)
#define UNLOCK_TABLE(tb) PyMutex_Unlock(&tb->mutex)
#define INITIALIZE_LOCK(tb) memset(&tb->mutex, 0, sizeof(PyMutex))
#define FREE_LOCK(tb)
#endif
#else
// the GIL serializes access to the table so no need
// for locking if it is enabled
#define LOCK_TABLE(tb)
Expand Down Expand Up @@ -252,7 +259,7 @@ PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb,


NPY_NO_EXPORT PyObject *
PyArrayIdentityHash_GetItem(PyArrayIdentityHash const *tb, PyObject *const *key)
PyArrayIdentityHash_GetItem(PyArrayIdentityHash *tb, PyObject *const *key)
{
LOCK_TABLE(tb);
PyObject *res = find_item(tb, key)[0];
Expand Down
6 changes: 5 additions & 1 deletion numpy/_core/src/common/npy_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ typedef struct {
npy_intp size; /* current size */
npy_intp nelem; /* number of elements */
#ifdef Py_GIL_DISABLED
#if PY_VERSION_HEX < 0x30d00b3
PyThread_type_lock *mutex;
#else
PyMutex mutex;
#endif
#endif
} PyArrayIdentityHash;

Expand All @@ -24,7 +28,7 @@ PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb,
PyObject *const *key, PyObject *value, int replace);

NPY_NO_EXPORT PyObject *
PyArrayIdentityHash_GetItem(PyArrayIdentityHash const *tb, PyObject *const *key);
PyArrayIdentityHash_GetItem(PyArrayIdentityHash *tb, PyObject *const *key);

NPY_NO_EXPORT PyArrayIdentityHash *
PyArrayIdentityHash_New(int key_len);
Expand Down