Enhance logging and task management; enable trace facility, add scheduler state logging, and improve shutdown handling

This commit is contained in:
Lukas Höppner
2026-08-12 14:12:43 +02:00
parent b623e5cf0a
commit b04ba14891
11 changed files with 187 additions and 119 deletions
+50 -6
View File
@@ -14,7 +14,7 @@ typedef enum { SLOT_FREE = 0, SLOT_RUNNING, SLOT_STOPPING } slot_state_t;
typedef struct {
int id;
slot_state_t state;
// TaskHandle_t freertos_handle;
TaskHandle_t freertos_handle;
wasm_module_inst_t module_inst;
wasm_exec_env_t exec_env;
} wasm_task_slot_t;
@@ -29,7 +29,7 @@ bool wasm_runner_initialize(void) {
for (int i = 0; i < MAX_WASM_TASKS; i++) {
g_wasm_slots[i].id = i;
g_wasm_slots[i].state = SLOT_FREE;
// g_wasm_slots[i].freertos_handle = NULL;
g_wasm_slots[i].freertos_handle = NULL;
g_wasm_slots[i].module_inst = NULL;
}
@@ -45,6 +45,16 @@ bool wasm_runner_destroy(void) {
return true;
}
uint32_t wasm_runner_get_active_slots() {
uint32_t result = 0;
for (int i = 0; i < MAX_WASM_TASKS; i++) {
if (g_wasm_slots[i].state != SLOT_FREE) {
result++;
}
}
return result;
}
bool wasm_runner_start_program(
const char *module_path,
const wasm_runner_framebuffer_config_t *framebuffer_config) {
@@ -111,6 +121,10 @@ bool wasm_runner_start_program(
slot->state = SLOT_RUNNING;
/* Record the FreeRTOS task handle for this slot so the task can be
* referenced or force-deleted later if necessary. */
slot->freertos_handle = xTaskGetCurrentTaskHandle();
if (wasm_runtime_call_wasm(slot->exec_env, start_func, 0, NULL)) {
nova64_log("WASM module executed successfully.\n");
} else {
@@ -129,6 +143,10 @@ bool wasm_runner_start_program(
slot->module_inst = NULL;
slot->exec_env = NULL;
slot->state = SLOT_FREE;
nova64_log("WASM module execution completed and resources cleaned up.\n");
nova64_log("WASM task slot %d is now free. Deleting task %p...\n", slot->id,
(void *)xTaskGetCurrentTaskHandle());
slot->freertos_handle = NULL;
vTaskDelete(NULL);
return true;
@@ -138,12 +156,17 @@ void wasm_runner_terminate(void) {
nova64_log("Stopping all WASM tasks...\n");
for (int i = 0; i < MAX_WASM_TASKS; i++) {
if (g_wasm_slots[i].state == SLOT_RUNNING && g_wasm_slots[i].module_inst) {
if (g_wasm_slots[i].state == SLOT_RUNNING) {
g_wasm_slots[i].state = SLOT_STOPPING;
wasm_runtime_terminate(g_wasm_slots[i].module_inst);
if (g_wasm_slots[i].module_inst) {
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;
@@ -153,9 +176,30 @@ void wasm_runner_terminate(void) {
break;
}
}
if (any_running) {
vTaskDelay(pdMS_TO_TICKS(10));
if (!any_running) {
break;
}
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;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
nova64_log("All WASM tasks successfully stopped.\n");