aboutsummaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorAndrew Leech2019-06-28 16:35:51 +1000
committerDamien George2020-03-11 16:54:16 +1100
commit86bfabec11456b892fadd47cecab12157bbd8c0e (patch)
tree3eeb9c66bc8df735d7be67c4e2153bae680482b8 /py
parent7eea0d8b6c8ac97a585ed96ead7e1ffef0687c63 (diff)
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.
Diffstat (limited to 'py')
-rw-r--r--py/modmicropython.c12
-rw-r--r--py/mpconfig.h5
2 files changed, 16 insertions, 1 deletions
diff --git a/py/modmicropython.c b/py/modmicropython.c
index d73d2bd02..ff2faba66 100644
--- a/py/modmicropython.c
+++ b/py/modmicropython.c
@@ -130,9 +130,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_he
STATIC mp_obj_t mp_micropython_heap_unlock(void) {
gc_unlock();
- return mp_const_none;
+ return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock);
+
+#if MICROPY_PY_MICROPYTHON_HEAP_LOCKED
+STATIC mp_obj_t mp_micropython_heap_locked(void) {
+ return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_locked_obj, mp_micropython_heap_locked);
+#endif
#endif
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
@@ -184,6 +191,9 @@ STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
#if MICROPY_ENABLE_GC
{ MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) },
{ MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) },
+ #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED
+ { MP_ROM_QSTR(MP_QSTR_heap_locked), MP_ROM_PTR(&mp_micropython_heap_locked_obj) },
+ #endif
#endif
#if MICROPY_KBD_EXCEPTION
{ MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) },
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 4cefdf31d..33df8f3c5 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -1069,6 +1069,11 @@ typedef double mp_float_t;
#define MICROPY_PY_MICROPYTHON_STACK_USE (MICROPY_PY_MICROPYTHON_MEM_INFO)
#endif
+// Whether to provide the "micropython.heap_locked" function
+#ifndef MICROPY_PY_MICROPYTHON_HEAP_LOCKED
+#define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (0)
+#endif
+
// Whether to provide "array" module. Note that large chunk of the
// underlying code is shared with "bytearray" builtin type, so to
// get real savings, it should be disabled too.