← index #4377PR #15467
Related · high · value 4.371
QUERY · ISSUE

nrf: add support for threading

openby george-xiongopened 2018-12-24updated 2019-05-21
port-nrf

hi Micropython,

could you please add thread to nrf as some cusotmer in china need micropython support multi-thread function. currently, it is not supported on nrf51&52 platform.
my email is george.xiong@nordicsemi.no

thanks.

Best regards
george

CANDIDATE · PULL REQUEST

ports/zephyr: Add threading support on the nrf52840DK.

mergedby danicamporaopened 2024-07-15updated 2024-09-06
port-zephyr

This PR attempts to implement the _thread module on the Zephyr port. Three important remarks about this PR:

  1. Due to the fact that we are still using a rather old version of Zephyr, CONFIG_DYNAMIC_THREAD is not available and therefore the stack for threads cannot be allocated dynamically, only at compile time. So for the time being and for the purpose of these PR, a maximum of 4 Zephyr threads (besides the main thread) can be created. Once we manage to update to the latest version of Zephyr this won't be a problem anymore.

  2. For some reason, threads get blocked forever before calling mp_thread_start(); inside thread_entry() (see modthread.c, line 174), because MP_THREAD_GIL_ENTER() never returns. I'm unable to understand why this is happening, since this implementation of mpthreadport for the Zephyr port is following the same approach used by the ESP32 port. I suspect there's some kind of incompatibility with the rest of the Micropython options enabled for the Zephyr port, and I hope that @dpgeorge can shed some light in here. For this reason MICROPY_PY_THREAD_GIL is disabled at the moment. This obviously needs to be fixed before this PR is merged.

  3. Some of the tests in the tests/thread folder run successfully, and some other don't most likely because MICROPY_PY_THREAD_GIL is not enabled.

A simple test that runs OK:

import _thread
import time

def thread_entry(num):
    for i in range(3):
        time.sleep(1 + (num * 0.5))
        print("Thread {} running...".format(num))

_thread.start_new_thread(thread_entry, (0,))
_thread.start_new_thread(thread_entry, (1,))
_thread.start_new_thread(thread_entry, (2,))
_thread.start_new_thread(thread_entry, (3,))

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied