From e374cfff80c37b96b03d4438e475e405b03e6d61 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Jun 2017 14:43:50 +1000 Subject: [PATCH] py/modthread: Raise RuntimeError in release() if lock is not acquired. --- py/modthread.c | 4 +++- tests/thread/thread_lock1.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/py/modthread.c b/py/modthread.c index d0e71dad3..1d7602789 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -84,7 +84,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock_acquire_obj, 1, 3, thread STATIC mp_obj_t thread_lock_release(mp_obj_t self_in) { mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in); - // TODO check if already unlocked + if (!self->locked) { + mp_raise_msg(&mp_type_RuntimeError, NULL); + } self->locked = false; MP_THREAD_GIL_EXIT(); mp_thread_mutex_unlock(&self->mutex); diff --git a/tests/thread/thread_lock1.py b/tests/thread/thread_lock1.py index ca585ffbb..ba5c7dff0 100644 --- a/tests/thread/thread_lock1.py +++ b/tests/thread/thread_lock1.py @@ -38,3 +38,9 @@ try: except KeyError: print('KeyError') print(lock.locked()) + +# test that we can't release an unlocked lock +try: + lock.release() +except RuntimeError: + print('RuntimeError')