Skip to content

Commit 6c08f0d

Browse files
committed
address review
1 parent c9044d2 commit 6c08f0d

8 files changed

Lines changed: 44 additions & 29 deletions

File tree

Modules/_hashopenssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ get_hashlib_state(PyObject *module)
278278
}
279279

280280
typedef struct {
281-
PyObject_HASHLIB_HEAD
281+
HASHLIB_OBJECT_HEAD
282282
EVP_MD_CTX *ctx; /* OpenSSL message digest context */
283283
} HASHobject;
284284

285285
#define HASHobject_CAST(op) ((HASHobject *)(op))
286286

287287
typedef struct {
288-
PyObject_HASHLIB_HEAD
288+
HASHLIB_OBJECT_HEAD
289289
HMAC_CTX *ctx; /* OpenSSL hmac context */
290290
} HMACobject;
291291

Modules/blake2module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ type_to_impl(PyTypeObject *type)
352352
}
353353

354354
typedef struct {
355-
PyObject_HASHLIB_HEAD
355+
HASHLIB_OBJECT_HEAD
356356
union {
357357
Hacl_Hash_Blake2s_state_t *blake2s_state;
358358
Hacl_Hash_Blake2b_state_t *blake2b_state;

Modules/hashlib.h

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include "pycore_lock.h" // PyMutex
44

55
/*
6-
* Given a PyObject* obj, fill in the Py_buffer* viewp with the result
7-
* of PyObject_GetBuffer. Sets an exception and issues the erraction
8-
* on any errors, e.g. 'return NULL' or 'goto error'.
6+
* Given a PyObject* 'obj', fill in the Py_buffer* 'viewp' with the result
7+
* of PyObject_GetBuffer. Sets an exception and issues the 'erraction'
8+
* on any errors, e.g., 'return NULL' or 'goto error'.
99
*/
1010
#define GET_BUFFER_VIEW_OR_ERROR(obj, viewp, erraction) do { \
1111
if (PyUnicode_Check((obj))) { \
@@ -34,36 +34,46 @@
3434

3535
/*
3636
* Helper code to synchronize access to the hash object when the GIL is
37-
* released around a CPU consuming hashlib operation. All code paths that
38-
* access a mutable part of obj must be enclosed in an ENTER_HASHLIB /
39-
* LEAVE_HASHLIB block or explicitly acquire and release the lock inside
40-
* a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for
41-
* an operation.
37+
* released around a CPU consuming hashlib operation.
4238
*
43-
* These only drop the GIL if the lock acquisition itself is likely to
44-
* block. Thus the non-blocking acquire gating the GIL release for a
45-
* blocking lock acquisition. The intent of these macros is to surround
46-
* the assumed always "fast" operations that you aren't releasing the
47-
* GIL around. Otherwise use code similar to what you see in hash
48-
* function update() methods.
39+
* Code accessing a mutable part of the hash object must be enclosed in
40+
* an HASHLIB_{ACQUIRE,RELEASE}_LOCK block or explicitly acquire and release
41+
* the mutex inside a Py_BEGIN_ALLOW_THREADS -- Py_END_ALLOW_THREADS block if
42+
* they wish to release the GIL for an operation.
4943
*/
5044

51-
#include "pythread.h"
52-
53-
#define PyObject_HASHLIB_HEAD \
45+
#define HASHLIB_OBJECT_HEAD \
5446
PyObject_HEAD \
5547
/* Guard against race conditions during incremental update(). */ \
5648
PyMutex mutex;
5749

58-
#define HASHLIB_ACQUIRE_LOCK(OBJ) PyMutex_Lock(&(OBJ)->mutex)
59-
#define HASHLIB_RELEASE_LOCK(OBJ) PyMutex_Unlock(&(OBJ)->mutex)
60-
6150
#define HASHLIB_INIT_MUTEX(OBJ) \
6251
do { \
6352
(OBJ)->mutex = (PyMutex){0}; \
6453
} while (0)
6554

55+
#define HASHLIB_ACQUIRE_LOCK(OBJ) PyMutex_Lock(&(OBJ)->mutex)
56+
#define HASHLIB_RELEASE_LOCK(OBJ) PyMutex_Unlock(&(OBJ)->mutex)
57+
58+
/*
59+
* Message length above which the GIL is to be released
60+
* when performing hashing operations.
61+
*/
6662
#define HASHLIB_GIL_MINSIZE 2048
63+
64+
// Macros for executing code while conditionally holding the GIL.
65+
//
66+
// These only drop the GIL if the lock acquisition itself is likely to
67+
// block. Thus the non-blocking acquire gating the GIL release for a
68+
// blocking lock acquisition. The intent of these macros is to surround
69+
// the assumed always "fast" operations that you aren't releasing the
70+
// GIL around.
71+
72+
/*
73+
* Execute a suite of C statements 'STATEMENTS'.
74+
*
75+
* The GIL is held if 'SIZE' is below the HASHLIB_GIL_MINSIZE threshold.
76+
*/
6777
#define HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(SIZE, STATEMENTS) \
6878
do { \
6979
if ((SIZE) > HASHLIB_GIL_MINSIZE) { \
@@ -76,6 +86,11 @@
7686
} \
7787
} while (0)
7888

89+
/*
90+
* Lock 'OBJ' and execute a suite of C statements 'STATEMENTS'.
91+
*
92+
* The GIL is held if 'SIZE' is below the HASHLIB_GIL_MINSIZE threshold.
93+
*/
7994
#define HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(OBJ, SIZE, STATEMENTS) \
8095
do { \
8196
if ((SIZE) > HASHLIB_GIL_MINSIZE) { \

Modules/hmacmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ get_hmacmodule_state_by_cls(PyTypeObject *cls)
283283
typedef Hacl_Streaming_HMAC_agile_state HACL_HMAC_state;
284284

285285
typedef struct HMACObject {
286-
PyObject_HASHLIB_HEAD
286+
HASHLIB_OBJECT_HEAD
287287
// Hash function information
288288
PyObject *name; // rendered name (exact unicode object)
289289
HMAC_Hash_Kind kind; // can be used for runtime dispatch (must be known)

Modules/md5module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MD5Type "MD5object *" "&PyType_Type"
3838
#include "_hacl/Hacl_Hash_MD5.h"
3939

4040
typedef struct {
41-
PyObject_HASHLIB_HEAD
41+
HASHLIB_OBJECT_HEAD
4242
Hacl_Hash_MD5_state_t *hash_state;
4343
} MD5object;
4444

Modules/sha1module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SHA1Type "SHA1object *" "&PyType_Type"
3838
#include "_hacl/Hacl_Hash_SHA1.h"
3939

4040
typedef struct {
41-
PyObject_HASHLIB_HEAD
41+
HASHLIB_OBJECT_HEAD
4242
Hacl_Hash_SHA1_state_t *hash_state;
4343
} SHA1object;
4444

Modules/sha2module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ class SHA512Type "SHA512object *" "&PyType_Type"
5050
// TODO: Get rid of int digestsize in favor of Hacl state info?
5151

5252
typedef struct {
53-
PyObject_HASHLIB_HEAD
53+
HASHLIB_OBJECT_HEAD
5454
int digestsize;
5555
Hacl_Hash_SHA2_state_t_256 *state;
5656
} SHA256object;
5757

5858
typedef struct {
59-
PyObject_HASHLIB_HEAD
59+
HASHLIB_OBJECT_HEAD
6060
int digestsize;
6161
Hacl_Hash_SHA2_state_t_512 *state;
6262
} SHA512object;

Modules/sha3module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
5959
#include "_hacl/Hacl_Hash_SHA3.h"
6060

6161
typedef struct {
62-
PyObject_HASHLIB_HEAD
62+
HASHLIB_OBJECT_HEAD
6363
Hacl_Hash_SHA3_state_t *hash_state;
6464
} SHA3object;
6565

0 commit comments

Comments
 (0)