diff options
| author | Damien George <damien@micropython.org> | 2025-07-13 22:35:53 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-07-23 11:35:38 +1000 |
| commit | b6460df721893ee89336641afea8e71b4d72f7c5 (patch) | |
| tree | 984aeff0bf787de8c1d61466a9f260e0d9c73b8c | |
| parent | 92193112bf0750615139b2d980667055a64a92a6 (diff) | |
unix: Allow the GIL to be enabled.
The unix port can now be built with the GIL enabled, by passing
MICROPY_PY_THREAD_GIL=1 on the make command line.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | ports/unix/Makefile | 4 | ||||
| -rw-r--r-- | ports/unix/mpconfigport.h | 3 | ||||
| -rw-r--r-- | ports/unix/mpconfigport.mk | 1 | ||||
| -rw-r--r-- | ports/unix/mphalport.h | 7 | ||||
| -rw-r--r-- | ports/unix/variants/mpconfigvariant_common.h | 2 |
5 files changed, 15 insertions, 2 deletions
diff --git a/ports/unix/Makefile b/ports/unix/Makefile index f1ceabb11..4e9a3736a 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -132,7 +132,11 @@ ifeq ($(MICROPY_PY_SOCKET),1) CFLAGS += -DMICROPY_PY_SOCKET=1 endif ifeq ($(MICROPY_PY_THREAD),1) +ifeq ($(MICROPY_PY_THREAD_GIL),1) +CFLAGS += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=1 +else CFLAGS += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0 +endif LDFLAGS += $(LIBPTHREAD) endif diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 973b5e74c..c18859ecb 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -137,6 +137,9 @@ typedef long mp_off_t; #define MICROPY_STACKLESS_STRICT (0) #endif +// Recursive mutex is needed when threading is enabled, regardless of GIL setting. +#define MICROPY_PY_THREAD_RECURSIVE_MUTEX (MICROPY_PY_THREAD) + // Implementation of the machine module. #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/unix/modmachine.c" diff --git a/ports/unix/mpconfigport.mk b/ports/unix/mpconfigport.mk index 1557c5461..f5ad0a143 100644 --- a/ports/unix/mpconfigport.mk +++ b/ports/unix/mpconfigport.mk @@ -13,6 +13,7 @@ MICROPY_PY_BTREE = 1 # _thread module using pthreads MICROPY_PY_THREAD = 1 +MICROPY_PY_THREAD_GIL = 0 # Subset of CPython termios module MICROPY_PY_TERMIOS = 1 diff --git a/ports/unix/mphalport.h b/ports/unix/mphalport.h index 02b60d8a8..0efd6940b 100644 --- a/ports/unix/mphalport.h +++ b/ports/unix/mphalport.h @@ -40,7 +40,12 @@ // // Note that we don't delay for the full TIMEOUT_MS, as execution // can't be woken from the delay. -#define MICROPY_INTERNAL_WFE(TIMEOUT_MS) mp_hal_delay_us(500) +#define MICROPY_INTERNAL_WFE(TIMEOUT_MS) \ + do { \ + MP_THREAD_GIL_EXIT(); \ + mp_hal_delay_us(500); \ + MP_THREAD_GIL_ENTER(); \ + } while (0) void mp_hal_set_interrupt_char(char c); diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h index 9eeed8797..65c874317 100644 --- a/ports/unix/variants/mpconfigvariant_common.h +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -29,7 +29,7 @@ // Send raise KeyboardInterrupt directly from the signal handler rather than // scheduling it into the VM. -#define MICROPY_ASYNC_KBD_INTR (1) +#define MICROPY_ASYNC_KBD_INTR (!MICROPY_PY_THREAD_GIL) // Enable helpers for printing debugging information. #ifndef MICROPY_DEBUG_PRINTERS |
