From 98047c626db23f3ae4c7f40b916f94a9941e333a Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sun, 21 Feb 2021 14:16:17 -0800 Subject: [PATCH] Adding pthreads + arudino threads. --- ueforth/arduino/arduino.template.ino | 11 +++++++++++ ueforth/common/core.h | 2 ++ ueforth/posix/pthreads.fs | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 ueforth/posix/pthreads.fs diff --git a/ueforth/arduino/arduino.template.ino b/ueforth/arduino/arduino.template.ino index ae31201..585000d 100644 --- a/ueforth/arduino/arduino.template.ino +++ b/ueforth/arduino/arduino.template.ino @@ -14,6 +14,7 @@ #define ENABLE_SDCARD_SUPPORT #define ENABLE_I2C_SUPPORT #define ENABLE_SOCKETS_SUPPORT +#define ENABLE_FREERTOS_SUPPORT // For now assume only boards with PSRAM (ESP32-CAM) // will want SerialBluetooth (very large) and camera support. @@ -117,6 +118,7 @@ OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ OPTIONAL_CAMERA_SUPPORT \ OPTIONAL_SOCKETS_SUPPORT \ + OPTIONAL_FREERTOS_SUPPORT \ #ifndef ENABLE_SPIFFS_SUPPORT // Provide a default failing SPIFFS.begin @@ -133,6 +135,15 @@ X("SPIFFS.usedBytes", SPIFFS_USED_BYTES, PUSH SPIFFS.usedBytes()) #endif +#ifndef ENABLE_FREERTOS_SUPPORT +# define OPTIONAL_FREERTOS_SUPPORT +#else +# define OPTIONAL_FREERTOS_SUPPORT \ + Y(vTaskDelete, vTaskDelete((TaskHandle_t) n0); DROP) \ + Y(xTaskCreatePinnedToCore, n0 = xTaskCreatePinnedToCore((TaskFunction_t) a6, c5, n4, a3, (UBaseType_t) n2, (TaskHandler_t *) a1, (BaseType_t) n0); NIPn(6)) \ + Y(xPortGetCoreID, PUSH xPortGetCoreID()) +#endif + #ifndef ENABLE_CAMERA_SUPPORT # define OPTIONAL_CAMERA_SUPPORT #else diff --git a/ueforth/common/core.h b/ueforth/common/core.h index d08b1cc..db2d1bf 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -17,6 +17,7 @@ static struct { cell_t *heap, **current, ***context, notfound; int argc; char **argv; + cell_t *(*runner)(cell_t *rp); // pointer to forth_run cell_t *rp; // spot to park main thread cell_t DOLIT_XT, DOEXIT_XT, YIELD_XT; } g_sys; @@ -160,4 +161,5 @@ static void forth_init(int argc, char *argv[], void *heap, *++rp = (cell_t) sp; *++rp = (cell_t) start; g_sys.rp = rp; + g_sys.runner = forth_run; } diff --git a/ueforth/posix/pthreads.fs b/ueforth/posix/pthreads.fs new file mode 100644 index 0000000..b5a33c4 --- /dev/null +++ b/ueforth/posix/pthreads.fs @@ -0,0 +1,22 @@ +( pthreads ) +posix definitions also internals + +z" libpthread.so" shared-library pthread + z" pthread_create" 4 pthread pthread_create + z" pthread_join" 2 pthread pthread_join + z" pthread_exit" 1 pthread pthread_exit + +'sys 11 cells + @ constant forth_run + +: ++! ( n a ) cell+ dup >r ! r> ; +: >entry ( xt sp rp -- rp ) here >r rot , ['] yield , ++! r> swap ++! ; + +: thread ( xt dstack rstack -- tid ) + here >r cells allot here cell+ >r cells allot r> r> >entry ( rinit ) + 0 >r rp@ ( rinit tid ) + 0 rot forth_run swap ( tid attr forth_run rinit ) + pthread_create throw r> ; +: join ( tid -- ) 0 pthread_join throw ; + +only forth definitions +