From bc78ce3534af86dde4cff218665fa3d0936ca685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20H=C3=B6ppner?= Date: Wed, 12 Aug 2026 14:25:21 +0200 Subject: [PATCH] Refactor sleep handling and WASM task termination; consolidate platform-specific sleep functions and improve task cleanup logic --- core/include/nova64_internal.h | 10 ++++++++ core/src/nova64_core.c | 10 -------- core/src/wasm_runner.c | 44 ++++++++++++++------------------- simulator/sd/slot0/init.wasm | Bin 139 -> 169 bytes software/build.bat | 6 ++++- software/init.zig | 2 +- 6 files changed, 35 insertions(+), 37 deletions(-) diff --git a/core/include/nova64_internal.h b/core/include/nova64_internal.h index cbdd5b0..a4fdec3 100644 --- a/core/include/nova64_internal.h +++ b/core/include/nova64_internal.h @@ -9,4 +9,14 @@ const nova64_io_interface_t *nova64_get_io_interface(void); void nova64_log_scheduler_state(void); +#if defined(_WIN32) +#include +#define nova64_host_sleep_ms(ms) Sleep(ms) +#elif defined(__unix__) || defined(__APPLE__) +#include +#define nova64_host_sleep_ms(ms) usleep((ms) * 1000) +#else +#define nova64_host_sleep_ms(ms) ((void)0) +#endif + #endif // NOVA64_INTERNAL_H \ No newline at end of file diff --git a/core/src/nova64_core.c b/core/src/nova64_core.c index 1f05efd..9cfc4c4 100644 --- a/core/src/nova64_core.c +++ b/core/src/nova64_core.c @@ -20,16 +20,6 @@ #include #endif -#if defined(_WIN32) -#include -#define nova64_host_sleep_ms(ms) Sleep(ms) -#elif defined(__unix__) || defined(__APPLE__) -#include -#define nova64_host_sleep_ms(ms) usleep((ms) * 1000) -#else -#define nova64_host_sleep_ms(ms) ((void)0) -#endif - volatile bool is_initialized = false; volatile bool shutdown_requested = false; static nova64_io_interface_t g_io_interface_storage; diff --git a/core/src/wasm_runner.c b/core/src/wasm_runner.c index 762952e..9856535 100644 --- a/core/src/wasm_runner.c +++ b/core/src/wasm_runner.c @@ -155,21 +155,21 @@ bool wasm_runner_start_program( void wasm_runner_terminate(void) { nova64_log("Stopping all WASM tasks...\n"); + // 1. Send stop signal to all running WASM instances for (int i = 0; i < MAX_WASM_TASKS; i++) { if (g_wasm_slots[i].state == SLOT_RUNNING) { g_wasm_slots[i].state = SLOT_STOPPING; if (g_wasm_slots[i].module_inst) { + // Thread-safe: Only sets an abort flag internally in WAMR wasm_runtime_terminate(g_wasm_slots[i].module_inst); } } } - /* Wait a short grace period for tasks to exit on their own. */ - const TickType_t grace = pdMS_TO_TICKS(2000); - TickType_t start = xTaskGetTickCount(); - bool any_running = true; - while (any_running) { - any_running = false; + // 2. Wait with HOST-Sleep (NATIVE C / C# thread-safe!) + int timeout_ms = 2000; + while (timeout_ms > 0) { + bool any_running = false; for (int i = 0; i < MAX_WASM_TASKS; i++) { if (g_wasm_slots[i].state != SLOT_FREE) { any_running = true; @@ -178,28 +178,22 @@ void wasm_runner_terminate(void) { } if (!any_running) { - break; + break; // All WASM tasks have terminated and cleaned up themselves! } - if ((xTaskGetTickCount() - start) > grace) { - /* Force-delete any remaining FreeRTOS tasks */ - for (int i = 0; i < MAX_WASM_TASKS; i++) { - if (g_wasm_slots[i].state != SLOT_FREE && - g_wasm_slots[i].freertos_handle != NULL) { - nova64_log("Force-deleting WASM task %d\n", i); - vTaskDelete(g_wasm_slots[i].freertos_handle); - g_wasm_slots[i].freertos_handle = NULL; - } - /* Mark slot free even if we couldn't fully clean up module state to - * avoid blocking shutdown; it's better to free the native thread. */ - g_wasm_slots[i].module_inst = NULL; - g_wasm_slots[i].exec_env = NULL; - g_wasm_slots[i].state = SLOT_FREE; - } - break; - } + // IMPORTANT: Use Host-Sleep, NEVER use vTaskDelay on the C# thread! + nova64_host_sleep_ms(10); + timeout_ms -= 10; + } - vTaskDelay(pdMS_TO_TICKS(10)); + // 3. If after 2s a task is still hanging: ONLY clean up flags, + // NEVER call vTaskDelete from outside! + for (int i = 0; i < MAX_WASM_TASKS; i++) { + if (g_wasm_slots[i].state != SLOT_FREE) { + nova64_log("Warning: Task slot %d failed to exit gracefully.\n", i); + g_wasm_slots[i].state = SLOT_FREE; + g_wasm_slots[i].freertos_handle = NULL; + } } nova64_log("All WASM tasks successfully stopped.\n"); diff --git a/simulator/sd/slot0/init.wasm b/simulator/sd/slot0/init.wasm index aa6716760102ebfe7c3047074d7ec456d6eccd49..f70185274749ffd0ce74997ce7be6e3a30fe0aa7 100644 GIT binary patch delta 97 zcmeBXT*+w5kXW3{$iTqB$ibArSkI8az`&%<#Fm#|mS|?e1)<_|^3xd@pkh2wvC7QU koRo>`h7wF%vW(IU%nptX4F?(+1R8*V!O=*dabkut0A)WJ8vp