Refactor sleep handling and WASM task termination; consolidate platform-specific sleep functions and improve task cleanup logic

This commit is contained in:
Lukas Höppner
2026-08-12 14:25:21 +02:00
parent b04ba14891
commit bc78ce3534
6 changed files with 35 additions and 37 deletions
+19 -25
View File
@@ -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");