From 864e4ecc47f204e6ca6fbe8342e268932c89acdf Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 May 2021 18:17:27 +1000 Subject: [PATCH] esp32/mpthreadport: Use binary semaphore instead of mutex. So a lock can be acquired on one Python thread and then released on another. A test for this is added. Signed-off-by: Damien George --- ports/esp32/mpthreadport.c | 5 ++++- tests/thread/thread_lock5.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/thread/thread_lock5.py diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index bbfc53d3f..f575d99e6 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -192,7 +192,10 @@ void vPortCleanUpTCB(void *tcb) { } void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { - mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer); + // Need a binary semaphore so a lock can be acquired on one Python thread + // and then released on another. + mutex->handle = xSemaphoreCreateBinaryStatic(&mutex->buffer); + xSemaphoreGive(mutex->handle); } int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { diff --git a/tests/thread/thread_lock5.py b/tests/thread/thread_lock5.py new file mode 100644 index 000000000..830b5efa8 --- /dev/null +++ b/tests/thread/thread_lock5.py @@ -0,0 +1,16 @@ +# test _thread lock objects where a lock is acquired/released by a different thread + +import _thread + + +def thread_entry(): + print("thread about to release lock") + lock.release() + + +lock = _thread.allocate_lock() +lock.acquire() +_thread.start_new_thread(thread_entry, ()) +lock.acquire() +print("main has lock") +lock.release()