mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
gh-111916: Make hashlib related modules thread-safe without the GIL (#111981)
Always use an individual lock on hash objects when in free-threaded builds. Fixes #111916
This commit is contained in:
parent
7218bac8c8
commit
a6465605c1
10 changed files with 188 additions and 137 deletions
|
|
@ -1,5 +1,7 @@
|
|||
/* Common code for use by all hashlib related modules. */
|
||||
|
||||
#include "pycore_lock.h" // PyMutex
|
||||
|
||||
/*
|
||||
* Given a PyObject* obj, fill in the Py_buffer* viewp with the result
|
||||
* of PyObject_GetBuffer. Sets an exception and issues the erraction
|
||||
|
|
@ -48,18 +50,28 @@
|
|||
|
||||
#include "pythread.h"
|
||||
#define ENTER_HASHLIB(obj) \
|
||||
if ((obj)->lock) { \
|
||||
if (!PyThread_acquire_lock((obj)->lock, 0)) { \
|
||||
Py_BEGIN_ALLOW_THREADS \
|
||||
PyThread_acquire_lock((obj)->lock, 1); \
|
||||
Py_END_ALLOW_THREADS \
|
||||
} \
|
||||
if ((obj)->use_mutex) { \
|
||||
PyMutex_Lock(&(obj)->mutex); \
|
||||
}
|
||||
#define LEAVE_HASHLIB(obj) \
|
||||
if ((obj)->lock) { \
|
||||
PyThread_release_lock((obj)->lock); \
|
||||
if ((obj)->use_mutex) { \
|
||||
PyMutex_Unlock(&(obj)->mutex); \
|
||||
}
|
||||
|
||||
#ifdef Py_NOGIL
|
||||
#define HASHLIB_INIT_MUTEX(obj) \
|
||||
do { \
|
||||
(obj)->mutex = (PyMutex){0}; \
|
||||
(obj)->use_mutex = true; \
|
||||
} while (0)
|
||||
#else
|
||||
#define HASHLIB_INIT_MUTEX(obj) \
|
||||
do { \
|
||||
(obj)->mutex = (PyMutex){0}; \
|
||||
(obj)->use_mutex = false; \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
/* TODO(gpshead): We should make this a module or class attribute
|
||||
* to allow the user to optimize based on the platform they're using. */
|
||||
#define HASHLIB_GIL_MINSIZE 2048
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue