Skip to content

Commit 7f9f7b7

Browse files
committed
refactor update logic
1 parent 81e3046 commit 7f9f7b7

1 file changed

Lines changed: 34 additions & 17 deletions

File tree

Modules/md5module.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ MD5Type_hexdigest_impl(MD5object *self)
166166
}
167167

168168
static void
169-
update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len)
169+
_hacl_md5_update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len)
170170
{
171171
/*
172172
* Note: we explicitly ignore the error code on the basis that it would
@@ -184,6 +184,36 @@ update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len)
184184
(void)Hacl_Hash_MD5_update(state, buf, (uint32_t)len);
185185
}
186186

187+
static void
188+
md5_update_state_with_lock(MD5object *self, uint8_t *buf, Py_ssize_t len)
189+
{
190+
Py_BEGIN_ALLOW_THREADS
191+
PyMutex_Lock(&self->mutex); // unconditionally acquire a lock
192+
_hacl_md5_update(self->hash_state, buf, len);
193+
PyMutex_Unlock(&self->mutex);
194+
Py_END_ALLOW_THREADS
195+
}
196+
197+
static void
198+
md5_update_state_cond_lock(MD5object *self, uint8_t *buf, Py_ssize_t len)
199+
{
200+
ENTER_HASHLIB(self); // conditionally acquire a lock
201+
_hacl_md5_update(self->hash_state, buf, len);
202+
LEAVE_HASHLIB(self);
203+
}
204+
205+
static inline void
206+
md5_update_state(MD5object *self, uint8_t *buf, Py_ssize_t len)
207+
{
208+
assert(buf != 0);
209+
assert(len >= 0);
210+
if (len != 0) {
211+
len < HASHLIB_GIL_MINSIZE
212+
? md5_update_state_cond_lock(self, buf, len)
213+
: md5_update_state_with_lock(self, buf, len);
214+
}
215+
}
216+
187217
/*[clinic input]
188218
MD5Type.update
189219
@@ -200,20 +230,7 @@ MD5Type_update_impl(MD5object *self, PyObject *obj)
200230
Py_buffer buf;
201231

202232
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
203-
204-
if (!self->use_mutex && buf.len >= HASHLIB_GIL_MINSIZE) {
205-
self->use_mutex = true;
206-
}
207-
if (self->use_mutex) {
208-
Py_BEGIN_ALLOW_THREADS
209-
PyMutex_Lock(&self->mutex);
210-
update(self->hash_state, buf.buf, buf.len);
211-
PyMutex_Unlock(&self->mutex);
212-
Py_END_ALLOW_THREADS
213-
} else {
214-
update(self->hash_state, buf.buf, buf.len);
215-
}
216-
233+
md5_update_state(self, buf.buf, buf.len);
217234
PyBuffer_Release(&buf);
218235
Py_RETURN_NONE;
219236
}
@@ -319,11 +336,11 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity,
319336
/* We do not initialize self->lock here as this is the constructor
320337
* where it is not yet possible to have concurrent access. */
321338
Py_BEGIN_ALLOW_THREADS
322-
update(new->hash_state, buf.buf, buf.len);
339+
_hacl_md5_update(new->hash_state, buf.buf, buf.len);
323340
Py_END_ALLOW_THREADS
324341
}
325342
else {
326-
update(new->hash_state, buf.buf, buf.len);
343+
_hacl_md5_update(new->hash_state, buf.buf, buf.len);
327344
}
328345
PyBuffer_Release(&buf);
329346
}

0 commit comments

Comments
 (0)