nrf: add support for threading
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
ports/zephyr: Add threading support on the nrf52840DK.
This PR attempts to implement the _thread module on the Zephyr port. Three important remarks about this PR:
-
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.
-
For some reason, threads get blocked forever before calling
mp_thread_start();insidethread_entry()(see modthread.c, line 174), becauseMP_THREAD_GIL_ENTER()never returns. I'm unable to understand why this is happening, since this implementation ofmpthreadportfor 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 reasonMICROPY_PY_THREAD_GILis disabled at the moment. This obviously needs to be fixed before this PR is merged. -
Some of the tests in the
tests/threadfolder run successfully, and some other don't most likely becauseMICROPY_PY_THREAD_GILis 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,))