Skip to content

Commit

Permalink
Merge bb0a712 into 3032ae1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleech committed Jan 21, 2020
2 parents 3032ae1 + bb0a712 commit 25da6dd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/library/micropython.rst
Expand Up @@ -82,9 +82,11 @@ Functions

.. function:: heap_lock()
.. function:: heap_unlock()
.. function:: heap_locked()

Lock or unlock the heap. When locked no memory allocation can occur and a
`MemoryError` will be raised if any heap allocation is attempted.
`MemoryError` will be raised if any heap allocation is attempted.
`heap_locked()` will return True if the heap is currently locked.

These functions can be nested, ie `heap_lock()` can be called multiple times
in a row and the lock-depth will increase, and then `heap_unlock()` must be
Expand All @@ -93,6 +95,9 @@ Functions
If the REPL becomes active with the heap locked then it will be forcefully
unlocked.

Note: `heap_locked()` is not enabled on most ports by default,
requires `MICROPY_PY_ENABLE_HEAP_LOCKED`.

.. function:: kbd_intr(chr)

Set the character that will raise a `KeyboardInterrupt` exception. By
Expand Down
1 change: 1 addition & 0 deletions ports/unix/variants/coverage/mpconfigvariant.h
Expand Up @@ -59,6 +59,7 @@
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)
#define MICROPY_PY_UCRYPTOLIB (1)
#define MICROPY_PY_UCRYPTOLIB_CTR (1)
#define MICROPY_PY_ENABLE_HEAP_LOCKED (1)

// TODO these should be generic, not bound to fatfs
#define mp_type_fileio mp_type_vfs_posix_fileio
Expand Down
13 changes: 12 additions & 1 deletion py/modmicropython.c
Expand Up @@ -130,9 +130,17 @@ 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_ENABLE_HEAP_LOCKED
STATIC mp_obj_t mp_micropython_heap_locked(void) {
// check if GC is locked
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)
Expand Down Expand Up @@ -184,6 +192,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_ENABLE_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) },
Expand Down
9 changes: 9 additions & 0 deletions tests/micropython/heap_lock.py
Expand Up @@ -5,6 +5,7 @@
l = []
l2 = list(range(100))

micropython.heap_lock()
micropython.heap_lock()

# general allocation on the heap
Expand All @@ -19,6 +20,14 @@
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
Expand Down
2 changes: 2 additions & 0 deletions tests/micropython/heap_lock.py.exp
@@ -1,3 +1,5 @@
MemoryError
MemoryError
1
MemoryError
[]
8 changes: 8 additions & 0 deletions tests/unix/extra_coverage.py
Expand Up @@ -78,3 +78,11 @@
# test for MP_QSTR_NULL regression
from frzqstr import returns_NULL
print(returns_NULL())

# test micropython.heap_locked()
import micropython
micropython.heap_lock()
print(micropython.heap_locked())
micropython.heap_unlock()
print(micropython.heap_locked())

2 changes: 2 additions & 0 deletions tests/unix/extra_coverage.py.exp
Expand Up @@ -134,3 +134,5 @@ frzmpy_pkg2.mod
ZeroDivisionError
b'# test frozen package'
NULL
1
0

0 comments on commit 25da6dd

Please sign in to comment.