py/modmicropython: Add heap_locked function to test state of heap.

This commit adds micropython.heap_locked() which returns the current
lock-depth of the heap, and can be used by Python code to check if the heap
is locked or not.  This new function is configured via
MICROPY_PY_MICROPYTHON_HEAP_LOCKED and is disabled by default.

This commit also changes the return value of micropython.heap_unlock() so
it returns the current lock-depth as well.
This commit is contained in:
Andrew Leech
2019-06-28 16:35:51 +10:00
committed by Damien George
parent 7eea0d8b6c
commit 86bfabec11
8 changed files with 51 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import micropython
l = []
l2 = list(range(100))
micropython.heap_lock()
micropython.heap_lock()
# general allocation on the heap
@@ -19,6 +20,14 @@ try:
except MemoryError:
print('MemoryError')
print(micropython.heap_unlock())
# Should still fail
try:
print([])
except MemoryError:
print('MemoryError')
micropython.heap_unlock()
# check that allocation works after an unlock

View File

@@ -1,3 +1,5 @@
MemoryError
MemoryError
1
MemoryError
[]

View File

@@ -0,0 +1,12 @@
# test micropython.heap_locked()
import micropython
if not hasattr(micropython, "heap_locked"):
print("SKIP")
raise SystemExit
micropython.heap_lock()
print(micropython.heap_locked())
micropython.heap_unlock()
print(micropython.heap_locked())

View File

@@ -0,0 +1,2 @@
1
0