b6b39bff47
This commit makes gc_lock_depth have one counter per thread, instead of one global counter. This makes threads properly independent with respect to the GC, in particular threads can now independently lock the GC for themselves without locking it for other threads. It also means a given thread can run a hard IRQ without temporarily locking the GC for all other threads and potentially making them have MemoryError exceptions at random locations (this really only occurs on MCUs with multiple cores and no GIL, eg on the rp2 port). The commit also removes protection of the GC lock/unlock functions, which is no longer needed when the counter is per thread (and this also fixes the cas where a hard IRQ calling gc_lock() may stall waiting for the mutex). It also puts the check for `gc_lock_depth > 0` outside the GC mutex in gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from waiting on a mutex if it does attempt to allocate heap memory (and putting the check outside the GC mutex is now safe now that there is a gc_lock_depth per thread). Signed-off-by: Damien George <damien@micropython.org>
27 lines
428 B
Python
27 lines
428 B
Python
# test interaction of micropython.heap_lock with threads
|
|
|
|
import _thread, micropython
|
|
|
|
lock1 = _thread.allocate_lock()
|
|
lock2 = _thread.allocate_lock()
|
|
|
|
|
|
def thread_entry():
|
|
lock1.acquire()
|
|
print([1, 2, 3])
|
|
lock2.release()
|
|
|
|
|
|
lock1.acquire()
|
|
lock2.acquire()
|
|
|
|
_thread.start_new_thread(thread_entry, ())
|
|
|
|
micropython.heap_lock()
|
|
lock1.release()
|
|
lock2.acquire()
|
|
micropython.heap_unlock()
|
|
|
|
lock1.release()
|
|
lock2.release()
|