Skip to content

Commit

Permalink
esp32/mpthreadport: Use binary semaphore instead of mutex.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dpgeorge committed May 8, 2021
1 parent 31e0b8c commit 864e4ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ports/esp32/mpthreadport.c
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions 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()

0 comments on commit 864e4ec

Please sign in to comment.