@@ -278,21 +278,15 @@ get_hashlib_state(PyObject *module)
278278}
279279
280280typedef struct {
281- PyObject_HEAD
281+ HASHLIB_OBJECT_HEAD
282282 EVP_MD_CTX * ctx ; /* OpenSSL message digest context */
283- // Prevents undefined behavior via multiple threads entering the C API.
284- bool use_mutex ;
285- PyMutex mutex ; /* OpenSSL context lock */
286283} HASHobject ;
287284
288285#define HASHobject_CAST (op ) ((HASHobject *)(op))
289286
290287typedef struct {
291- PyObject_HEAD
288+ HASHLIB_OBJECT_HEAD
292289 HMAC_CTX * ctx ; /* OpenSSL hmac context */
293- // Prevents undefined behavior via multiple threads entering the C API.
294- bool use_mutex ;
295- PyMutex mutex ; /* HMAC context lock */
296290} HMACobject ;
297291
298292#define HMACobject_CAST (op ) ((HMACobject *)(op))
@@ -700,9 +694,9 @@ static int
700694_hashlib_HASH_copy_locked (HASHobject * self , EVP_MD_CTX * new_ctx_p )
701695{
702696 int result ;
703- ENTER_HASHLIB (self );
697+ HASHLIB_ACQUIRE_LOCK (self );
704698 result = EVP_MD_CTX_copy (new_ctx_p , self -> ctx );
705- LEAVE_HASHLIB (self );
699+ HASHLIB_RELEASE_LOCK (self );
706700 if (result == 0 ) {
707701 notify_smart_ssl_error_occurred_in (Py_STRINGIFY (EVP_MD_CTX_copy ));
708702 return -1 ;
@@ -802,27 +796,13 @@ _hashlib_HASH_update_impl(HASHobject *self, PyObject *obj)
802796{
803797 int result ;
804798 Py_buffer view ;
805-
806799 GET_BUFFER_VIEW_OR_ERROUT (obj , & view );
807-
808- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
809- self -> use_mutex = true;
810- }
811- if (self -> use_mutex ) {
812- Py_BEGIN_ALLOW_THREADS
813- PyMutex_Lock (& self -> mutex );
814- result = _hashlib_HASH_hash (self , view .buf , view .len );
815- PyMutex_Unlock (& self -> mutex );
816- Py_END_ALLOW_THREADS
817- } else {
818- result = _hashlib_HASH_hash (self , view .buf , view .len );
819- }
820-
800+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED (
801+ self , HASHLIB_GIL_MINSIZE ,
802+ result = _hashlib_HASH_hash (self , view .buf , view .len )
803+ );
821804 PyBuffer_Release (& view );
822-
823- if (result == -1 )
824- return NULL ;
825- Py_RETURN_NONE ;
805+ return result < 0 ? NULL : Py_None ;
826806}
827807
828808static PyMethodDef HASH_methods [] = {
@@ -1125,15 +1105,12 @@ _hashlib_HASH(PyObject *module, const char *digestname, PyObject *data_obj,
11251105 }
11261106
11271107 if (view .buf && view .len ) {
1128- if (view .len >= HASHLIB_GIL_MINSIZE ) {
1129- /* We do not initialize self->lock here as this is the constructor
1130- * where it is not yet possible to have concurrent access. */
1131- Py_BEGIN_ALLOW_THREADS
1132- result = _hashlib_HASH_hash (self , view .buf , view .len );
1133- Py_END_ALLOW_THREADS
1134- } else {
1135- result = _hashlib_HASH_hash (self , view .buf , view .len );
1136- }
1108+ /* Do not use self->mutex here as this is the constructor
1109+ * where it is not yet possible to have concurrent access. */
1110+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED (
1111+ view .len ,
1112+ result = _hashlib_HASH_hash (self , view .buf , view .len )
1113+ );
11371114 if (result == -1 ) {
11381115 assert (PyErr_Occurred ());
11391116 Py_CLEAR (self );
@@ -1794,9 +1771,9 @@ static int
17941771locked_HMAC_CTX_copy (HMAC_CTX * new_ctx_p , HMACobject * self )
17951772{
17961773 int result ;
1797- ENTER_HASHLIB (self );
1774+ HASHLIB_ACQUIRE_LOCK (self );
17981775 result = HMAC_CTX_copy (new_ctx_p , self -> ctx );
1799- LEAVE_HASHLIB (self );
1776+ HASHLIB_RELEASE_LOCK (self );
18001777 if (result == 0 ) {
18011778 notify_smart_ssl_error_occurred_in (Py_STRINGIFY (HMAC_CTX_copy ));
18021779 return -1 ;
@@ -1827,24 +1804,12 @@ _hmac_update(HMACobject *self, PyObject *obj)
18271804 Py_buffer view = {0 };
18281805
18291806 GET_BUFFER_VIEW_OR_ERROR (obj , & view , return 0 );
1830-
1831- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
1832- self -> use_mutex = true;
1833- }
1834- if (self -> use_mutex ) {
1835- Py_BEGIN_ALLOW_THREADS
1836- PyMutex_Lock (& self -> mutex );
1837- r = HMAC_Update (self -> ctx ,
1838- (const unsigned char * )view .buf ,
1839- (size_t )view .len );
1840- PyMutex_Unlock (& self -> mutex );
1841- Py_END_ALLOW_THREADS
1842- } else {
1843- r = HMAC_Update (self -> ctx ,
1844- (const unsigned char * )view .buf ,
1845- (size_t )view .len );
1846- }
1847-
1807+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED (
1808+ self , view .len ,
1809+ r = HMAC_Update (
1810+ self -> ctx , (const unsigned char * )view .buf , (size_t )view .len
1811+ )
1812+ );
18481813 PyBuffer_Release (& view );
18491814
18501815 if (r == 0 ) {
0 commit comments