Skip to content

Commit 864e4ec

Browse files
committed
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 <damien@micropython.org>
1 parent 31e0b8c commit 864e4ec

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

ports/esp32/mpthreadport.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ void vPortCleanUpTCB(void *tcb) {
192192
}
193193

194194
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
195-
mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer);
195+
// Need a binary semaphore so a lock can be acquired on one Python thread
196+
// and then released on another.
197+
mutex->handle = xSemaphoreCreateBinaryStatic(&mutex->buffer);
198+
xSemaphoreGive(mutex->handle);
196199
}
197200

198201
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {

tests/thread/thread_lock5.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# test _thread lock objects where a lock is acquired/released by a different thread
2+
3+
import _thread
4+
5+
6+
def thread_entry():
7+
print("thread about to release lock")
8+
lock.release()
9+
10+
11+
lock = _thread.allocate_lock()
12+
lock.acquire()
13+
_thread.start_new_thread(thread_entry, ())
14+
lock.acquire()
15+
print("main has lock")
16+
lock.release()

0 commit comments

Comments
 (0)