Skip to content

Commit 7b7b1a3

Browse files
committed
use a macro for the mutex API
1 parent 2bd3895 commit 7b7b1a3

7 files changed

Lines changed: 15 additions & 21 deletions

File tree

Modules/blake2module.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ type_to_impl(PyTypeObject *type)
353353

354354
typedef struct {
355355
PyObject_HEAD
356+
HASHLIB_MUTEX_API
356357
union {
357358
Hacl_Hash_Blake2s_state_t *blake2s_state;
358359
Hacl_Hash_Blake2b_state_t *blake2b_state;
@@ -364,8 +365,6 @@ typedef struct {
364365
#endif
365366
};
366367
blake2_impl impl;
367-
bool use_mutex;
368-
PyMutex mutex;
369368
} Blake2Object;
370369

371370
#define _Blake2Object_CAST(op) ((Blake2Object *)(op))

Modules/hashlib.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
#include "pycore_lock.h" // PyMutex
44

5+
#define HASHLIB_MUTEX_API \
6+
/*
7+
* Attributes to prevent undefined behaviors
8+
* via multiple threads entering the C API.
9+
*/ \
10+
bool use_mutex; \
11+
PyMutex mutex;
12+
513
/*
614
* Given a PyObject* obj, fill in the Py_buffer* viewp with the result
715
* of PyObject_GetBuffer. Sets an exception and issues the erraction

Modules/hmacmodule.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,7 @@ typedef Hacl_Streaming_HMAC_agile_state HACL_HMAC_state;
383383

384384
typedef struct HMACObject {
385385
PyObject_HEAD
386-
387-
bool use_mutex;
388-
PyMutex mutex;
386+
HASHLIB_MUTEX_API
389387

390388
// Hash function information
391389
PyObject *name; // rendered name (exact unicode object)

Modules/md5module.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class MD5Type "MD5object *" "&PyType_Type"
3939

4040
typedef struct {
4141
PyObject_HEAD
42-
// Prevents undefined behavior via multiple threads entering the C API.
43-
bool use_mutex;
44-
PyMutex mutex;
42+
HASHLIB_MUTEX_API
4543
Hacl_Hash_MD5_state_t *hash_state;
4644
} MD5object;
4745

Modules/sha1module.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ class SHA1Type "SHA1object *" "&PyType_Type"
3939

4040
typedef struct {
4141
PyObject_HEAD
42-
// Prevents undefined behavior via multiple threads entering the C API.
43-
bool use_mutex;
44-
PyMutex mutex;
45-
PyThread_type_lock lock;
42+
HASHLIB_MUTEX_API
4643
Hacl_Hash_SHA1_state_t *hash_state;
4744
} SHA1object;
4845

Modules/sha2module.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,14 @@ class SHA512Type "SHA512object *" "&PyType_Type"
5252
typedef struct {
5353
PyObject_HEAD
5454
int digestsize;
55-
// Prevents undefined behavior via multiple threads entering the C API.
56-
bool use_mutex;
57-
PyMutex mutex;
55+
HASHLIB_MUTEX_API
5856
Hacl_Hash_SHA2_state_t_256 *state;
5957
} SHA256object;
6058

6159
typedef struct {
6260
PyObject_HEAD
6361
int digestsize;
64-
// Prevents undefined behavior via multiple threads entering the C API.
65-
bool use_mutex;
66-
PyMutex mutex;
62+
HASHLIB_MUTEX_API
6763
Hacl_Hash_SHA2_state_t_512 *state;
6864
} SHA512object;
6965

Modules/sha3module.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
6060

6161
typedef struct {
6262
PyObject_HEAD
63-
// Prevents undefined behavior via multiple threads entering the C API.
64-
bool use_mutex;
65-
PyMutex mutex;
63+
HASHLIB_MUTEX_API
6664
Hacl_Hash_SHA3_state_t *hash_state;
6765
} SHA3object;
6866

0 commit comments

Comments
 (0)